diff --git a/internal/server/user/mux/gorillize.go b/internal/server/user/mux/gorillize.go new file mode 100644 index 0000000..2d8c62c --- /dev/null +++ b/internal/server/user/mux/gorillize.go @@ -0,0 +1,18 @@ +package mux + +import ( + "net/http" + + "github.com/BBVA/kapow/internal/server/model" + "github.com/gorilla/mux" +) + +func gorillize(rs []model.Route, f func(*model.Handler) http.Handler) *mux.Router { + m := mux.NewRouter() + + for _, r := range rs { + m.Handle(r.Pattern, f(nil)).Methods(r.Method) + } + + return m +} diff --git a/internal/server/user/mux/gorillize_test.go b/internal/server/user/mux/gorillize_test.go new file mode 100644 index 0000000..3ba4a4f --- /dev/null +++ b/internal/server/user/mux/gorillize_test.go @@ -0,0 +1,106 @@ +package mux + +import ( + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/BBVA/kapow/internal/server/model" + "github.com/gorilla/mux" +) + +func handlerStatusOK(h *model.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) +} + +func TestGorillizeReturnsAnEmptyMuxWhenAnEmptyRouteList(t *testing.T) { + m := gorillize([]model.Route{}, handlerStatusOK) + + if !reflect.DeepEqual(*m, *mux.NewRouter()) { + t.Error("Returned mux not empty") + } +} + +func TestGorillizeReturnsAMuxThat404sWhenEmptyRouteList(t *testing.T) { + m := *gorillize([]model.Route{}, handlerStatusOK) + + req := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if res.StatusCode != http.StatusNotFound { + t.Errorf("status mismatch, got %d, want 404", res.StatusCode) + } + +} + +func TestGorillizeReturnsAMuxThatMatchesByRoute(t *testing.T) { + var rs []model.Route + rs = append(rs, model.Route{ + Pattern: "/foo", + Method: "GET", + }) + + m := *gorillize(rs, handlerStatusOK) + + req := httptest.NewRequest("GET", "/foo", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if res.StatusCode != http.StatusOK { + t.Errorf("status mismatch, got %d, want 200", res.StatusCode) + } +} + +func TestGorillizeReturnsAMuxThat405sWhenMethodMismatch(t *testing.T) { + var rs []model.Route + rs = append(rs, model.Route{ + Pattern: "/foo", + Method: "GET", + }) + + m := *gorillize(rs, handlerStatusOK) + + req := httptest.NewRequest("POST", "/foo", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if res.StatusCode != http.StatusMethodNotAllowed { + t.Errorf("status mismatch, got %d, want 405", res.StatusCode) + } +} + +func TestGorillizeReturnsAMuxThatMatchesByMethod(t *testing.T) { + var rs []model.Route + rs = append(rs, model.Route{ + Pattern: "/foo", + Method: "UNORTHODOX", + }) + + m := *gorillize(rs, handlerStatusOK) + + req := httptest.NewRequest("UNORTHODOX", "/foo", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if res.StatusCode != http.StatusOK { + t.Errorf("status mismatch, got %d, want 200", res.StatusCode) + } +} + +// TODO: TestGorillizeReturnsAMuxThatRespectsRouteOrder