From b82b0d2ef1e17392a208d9e882382752ddea30ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Gallego=20Rodr=C3=ADguez?= Date: Thu, 10 Oct 2019 09:44:41 +0200 Subject: [PATCH] validation of routes --- internal/server/control/control.go | 14 ++++- internal/server/control/control_test.go | 76 ++++++++++++------------- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/internal/server/control/control.go b/internal/server/control/control.go index 9082e8e..602e379 100644 --- a/internal/server/control/control.go +++ b/internal/server/control/control.go @@ -12,18 +12,22 @@ import ( "github.com/BBVA/kapow/internal/server/user" ) +// Run must start the control server in a specific address func Run(bindAddr string) { + r := configRouter() + log.Fatal(http.ListenAndServe(bindAddr, r)) +} + +func configRouter() *mux.Router { r := mux.NewRouter() - r.HandleFunc("/routes/{id}", removeRoute). Methods("DELETE") r.HandleFunc("/routes", listRoutes). Methods("GET") r.HandleFunc("/routes", addRoute). Methods("POST") - - log.Fatal(http.ListenAndServe(bindAddr, r)) + return r } // user.Routes.Remove() []model.Route @@ -67,6 +71,10 @@ func addRoute(res http.ResponseWriter, req *http.Request) { res.WriteHeader(http.StatusUnprocessableEntity) return } + if route.Pattern == "" { + res.WriteHeader(http.StatusUnprocessableEntity) + return + } created := funcAdd(route) createdBytes, _ := json.Marshal(created) diff --git a/internal/server/control/control_test.go b/internal/server/control/control_test.go index 5e860e4..e7efa6c 100644 --- a/internal/server/control/control_test.go +++ b/internal/server/control/control_test.go @@ -15,46 +15,45 @@ import ( ) func TestConfigRouterHasRoutesWellConfigured(t *testing.T) { - t.Skip("****** WIP ******") - //testCases := []struct { - // pattern, method string - // handler uintptr - // mustMatch bool - // vars []string - //}{ - // {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodGet, 0, false, []string{}}, - // {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodPut, 0, false, []string{}}, - // {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodPost, 0, false, []string{}}, - // {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodDelete, reflect.ValueOf(removeRoute).Pointer(), true, []string{"id"}}, - // {"/routes", http.MethodGet, reflect.ValueOf(listRoutes).Pointer(), true, []string{}}, - // {"/routes", http.MethodPut, 0, false, []string{}}, - // {"/routes", http.MethodPost, reflect.ValueOf(addRoute).Pointer(), true, []string{}}, - // {"/routes", http.MethodDelete, 0, false, []string{}}, - //} - //r := configRouter() + testCases := []struct { + pattern, method string + handler uintptr + mustMatch bool + vars []string + }{ + {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodGet, 0, false, []string{}}, + {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodPut, 0, false, []string{}}, + {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodPost, 0, false, []string{}}, + {"/routes/ROUTE_YYYYYYYYYYYYYYY", http.MethodDelete, reflect.ValueOf(removeRoute).Pointer(), true, []string{"id"}}, + {"/routes", http.MethodGet, reflect.ValueOf(listRoutes).Pointer(), true, []string{}}, + {"/routes", http.MethodPut, 0, false, []string{}}, + {"/routes", http.MethodPost, reflect.ValueOf(addRoute).Pointer(), true, []string{}}, + {"/routes", http.MethodDelete, 0, false, []string{}}, + } + 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 tc.mustMatch { - // // Check for Handler match. - // realHandler := reflect.ValueOf(rm.Handler).Pointer() - // if realHandler != tc.handler { - // t.Errorf("Handler mismatch. Expected: %X, got: %X", tc.handler, realHandler) - // } + 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 tc.mustMatch { + // Check for Handler match. + realHandler := reflect.ValueOf(rm.Handler).Pointer() + if realHandler != tc.handler { + t.Errorf("Handler mismatch. Expected: %X, got: %X", tc.handler, realHandler) + } - // // Check for variables - // for _, vn := range tc.vars { - // if _, exists := rm.Vars[vn]; !exists { - // t.Errorf("Variable not present: %s", vn) - // } - // } - // } - // } else { - // t.Errorf("Route mismatch: %+v", tc) - // } - //} + // Check for variables + for _, vn := range tc.vars { + if _, exists := rm.Vars[vn]; !exists { + t.Errorf("Variable not present: %s", vn) + } + } + } + } else { + t.Errorf("Route mismatch: %+v", tc) + } + } } func TestAddRouteReturnsBadRequestWhenMalformedJSONBody(t *testing.T) { @@ -77,7 +76,6 @@ func TestAddRouteReturnsBadRequestWhenMalformedJSONBody(t *testing.T) { } func TestAddRouteReturns422ErrorWhenMandatoryFieldsMissing(t *testing.T) { - t.Skip("****** WIP ******") handler := http.HandlerFunc(addRoute) tc := []struct { payload, testCase string