From 401644b65efc15d1cc31e2571e346c5ff8c3cd5f Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Wed, 9 Oct 2019 13:06:08 +0200 Subject: [PATCH] Finish gorillize() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Roberto Abdelkader Martínez Pérez --- internal/server/user/mux/gorillize.go | 4 +- internal/server/user/mux/gorillize_test.go | 66 +++++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/internal/server/user/mux/gorillize.go b/internal/server/user/mux/gorillize.go index 3b88aff..3ce34b7 100644 --- a/internal/server/user/mux/gorillize.go +++ b/internal/server/user/mux/gorillize.go @@ -7,11 +7,11 @@ import ( "github.com/gorilla/mux" ) -func gorillize(rs []model.Route, buildHandler func(*model.Route) http.Handler) *mux.Router { +func gorillize(rs []model.Route, buildHandler func(model.Route) http.Handler) *mux.Router { m := mux.NewRouter() for _, r := range rs { - m.Handle(r.Pattern, buildHandler(nil)).Methods(r.Method) + m.Handle(r.Pattern, buildHandler(r)).Methods(r.Method) } return m diff --git a/internal/server/user/mux/gorillize_test.go b/internal/server/user/mux/gorillize_test.go index 84d2620..516e324 100644 --- a/internal/server/user/mux/gorillize_test.go +++ b/internal/server/user/mux/gorillize_test.go @@ -1,6 +1,8 @@ package mux import ( + "io" + "io/ioutil" "net/http" "net/http/httptest" "reflect" @@ -10,12 +12,18 @@ import ( "github.com/gorilla/mux" ) -func handlerStatusOK(h *model.Route) http.Handler { +func handlerStatusOK(route model.Route) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) } +func handleRouteIDToBody(route model.Route) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.WriteString(w, route.ID) + }) +} + func TestGorillizeReturnsAnEmptyMuxWhenAnEmptyRouteList(t *testing.T) { m := gorillize([]model.Route{}, handlerStatusOK) @@ -103,4 +111,58 @@ func TestGorillizeReturnsAMuxThatMatchesByMethod(t *testing.T) { } } -// TODO: TestGorillizeReturnsAMuxThatRespectsRouteOrder +func TestGorillizeReturnsAMuxThatRespectsRouteOrderAB(t *testing.T) { + var rs []model.Route + rs = append(rs, + model.Route{ + ID: "routeA", + Pattern: "/foo", + Method: "GET", + }, + model.Route{ + ID: "routeB", + Pattern: "/foo", + Method: "GET", + }, + ) + m := *gorillize(rs, handleRouteIDToBody) + + req := httptest.NewRequest("GET", "/foo", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if body, _ := ioutil.ReadAll(res.Body); string(body) != "routeA" { + t.Errorf("Mux did not respect route order %q", body) + } +} + +func TestGorillizeReturnsAMuxThatRespectsRouteOrderBA(t *testing.T) { + var rs []model.Route + rs = append(rs, + model.Route{ + ID: "routeB", + Pattern: "/foo", + Method: "GET", + }, + model.Route{ + ID: "routeA", + Pattern: "/foo", + Method: "GET", + }, + ) + m := *gorillize(rs, handleRouteIDToBody) + + req := httptest.NewRequest("GET", "/foo", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, req) + + res := w.Result() + + if body, _ := ioutil.ReadAll(res.Body); string(body) != "routeB" { + t.Errorf("Mux did not respect route order %q", body) + } +}