Need to review tests in data api
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -14,21 +14,34 @@ import (
|
||||
func configRouter() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.HandleFunc("/handlers/{handler_id}/{root}/{resource:.*$}", readResource).Methods("GET")
|
||||
r.HandleFunc("/handlers/{handler_id}/{root}/{resource:.*$}", updateResource).Methods("PUT")
|
||||
r.HandleFunc("/handlers/{handler_id}/response/headers/", updateResource).Methods("PUT")
|
||||
r.HandleFunc("/handlers/{handler_id}/response/headers/{key}", updateResource).Methods("PUT")
|
||||
return r
|
||||
}
|
||||
|
||||
func Run(bindAddr string) {
|
||||
r := configRouter()
|
||||
var getHandlerId func(string) (*model.Handler, bool) = Handlers.Get
|
||||
|
||||
log.Fatal(http.ListenAndServe(bindAddr, r))
|
||||
}
|
||||
//func Run(bindAddr string) {
|
||||
// r := configRouter()
|
||||
//
|
||||
// log.Fatal(http.ListenAndServe(bindAddr, r))
|
||||
//}
|
||||
|
||||
func readResource(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
}
|
||||
//func readResource(res http.ResponseWriter, req *http.Request) {
|
||||
//
|
||||
//}
|
||||
|
||||
func updateResource(res http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
hID := vars["handler_id"]
|
||||
|
||||
if _, ok := getHandlerId(hID); !ok {
|
||||
res.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := vars["key"]; !ok {
|
||||
res.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package data
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -15,17 +18,20 @@ func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
|
||||
mustMatch bool
|
||||
vars []struct{ k, v string }
|
||||
}{
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "response"}, {"resource", "cookies/name"}}},
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "response"}, {"resource", "cookies/name"}}},
|
||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/request/params/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "request"}, {"resource", "params/name"}}},
|
||||
// {"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/cookies/name", http.MethodGet, reflect.ValueOf(readResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"root", "response"}, {"resource", "cookies/name"}}},
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/headers/", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}}},
|
||||
{"/handlers/HANDLER_ZZZZZZZZZZZZZZZZ/response/headers/name", http.MethodPut, reflect.ValueOf(updateResource).Pointer(), true, []struct{ k, v string }{{"handler_id", "HANDLER_ZZZZZZZZZZZZZZZZ"}, {"key", "name"}}},
|
||||
}
|
||||
r := configRouter()
|
||||
|
||||
for _, tc := range testCases {
|
||||
rm := mux.RouteMatch{}
|
||||
rq, _ := http.NewRequest(tc.method, tc.pattern, nil)
|
||||
if matched := r.Match(rq, &rm); tc.mustMatch == matched {
|
||||
if matched := r.Match(rq, &rm); tc.mustMatch != matched {
|
||||
t.Errorf("Route mismatch: Expected: %+v\n\t\t\t\t\t\t got: %+v", tc, rm)
|
||||
} else {
|
||||
if tc.mustMatch {
|
||||
// Check for Handler match.
|
||||
realHandler := reflect.ValueOf(rm.Handler).Pointer()
|
||||
@@ -42,8 +48,58 @@ func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Route mismatch: %+v", tc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Fails because URL doesn't match
|
||||
func TestUpdateResourceNotFoundWhenInvalidHandlerID(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPut, "/handlers/response/headers/language", strings.NewReader("ES"))
|
||||
response := httptest.NewRecorder()
|
||||
handler := configRouter()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusNotFound, response.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateResourceBadRequestWhenIncompletedResourceURL(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPut, "/handlers/xxxxxxxxx/response/headers/", strings.NewReader("ES"))
|
||||
response := httptest.NewRecorder()
|
||||
handler := configRouter()
|
||||
|
||||
getHandlerId = func(id string) (*model.Handler, bool) {
|
||||
if id == "xxxxxxxxx" {
|
||||
return nil, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
// TODO: We need to assure that an invalid resource path returns 400 (Bad Request)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusBadRequest, response.Code)
|
||||
}
|
||||
}
|
||||
|
||||
//func TestUpdateResourceSetHeaderWhenPutReceived(t *testing.T) {
|
||||
// request := httptest.NewRequest(http.MethodPut, "/handlers/xxxxxxxxxx/response/headers/language", strings.NewReader("ES"))
|
||||
// response := httptest.NewRecorder()
|
||||
// handler := configRouter()
|
||||
//
|
||||
// getHandlerId = func(id string) (*model.Handler, bool) {
|
||||
// if id == "xxxxxxxxxx" {
|
||||
// return nil, true
|
||||
// }
|
||||
// return nil, false
|
||||
// }
|
||||
//
|
||||
// handler.ServeHTTP(response, request)
|
||||
//
|
||||
// if response.Code != http.StatusOK {
|
||||
// t.Errorf("HTTP Status mismatch. Expected: %d, got: %d", http.StatusOK, response.Code)
|
||||
// }
|
||||
//}
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user