From 43952efd77ca726358b42423452911840ad001eb Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 24 Jan 2020 12:04:12 +0100 Subject: [PATCH] Fixing tests --- handlers/handlers.go | 7 ++++++- handlers/handlers_test.go | 11 +++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 56ea9055..b7d3150f 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -377,7 +377,12 @@ func respondWithError(h *Handlers, w http.ResponseWriter, r *http.Request, err e func (h *Handlers) Create(model interface{}) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { - h.post(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern()) + switch r.Method { + case "GET": + h.get(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern()) + case "POST": + h.post(w, r, reflect.ModelNameLowerPlural(model), h.Config.CreatePattern()) + } } return http.HandlerFunc(fn) diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 01c5b139..d113903f 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -263,7 +263,7 @@ func (t *testSuite) TestReadContest() { } func (t *testSuite) TestSchoolSubscriptionForm() { - req, err := handlers.NewCreateRequest(&orm.School{}, "/create/", "html", "GET", nil) + req, err := handlers.NewCreateRequest(&orm.School{}, "/schools/create/", "html", "GET", nil) t.Nil(err) req, err = login(req, handlers, "subscriber", "subscribe") @@ -272,7 +272,7 @@ func (t *testSuite) TestSchoolSubscriptionForm() { rr := httptest.NewRecorder() router := mux.NewRouter() - router.Handle("/schools/create", handlers.Create(&orm.School{})) + router.Handle("/schools/create/", handlers.Create(&orm.School{})) router.ServeHTTP(rr, req) t.Equal(http.StatusOK, rr.Code) @@ -311,7 +311,7 @@ func (t *testSuite) TestSchoolSubscription() { form.Add("Code", "123") form.Add("Email", "foo@school.org") - req, err := handlers.NewCreateRequest(&orm.School{}, "/create/", "html", "POST", form) + req, err := handlers.NewCreateRequest(&orm.School{}, "/schools/create/", "html", "POST", form) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") t.Nil(err) @@ -338,7 +338,6 @@ func (t *testSuite) TestSchoolSubscription() { } ok := true doc.Find("dd").Each(func(i int, s *goquery.Selection) { - log.Print(s) t.Equal(expected[i], s.Text()) if t.Failed() { ok = false @@ -367,7 +366,7 @@ func (t *testSuite) TestSchoolSubscription() { form.Add("category_id", "1") form.Add("school_id", strconv.Itoa(int(school.ID))) - req, err := handlers.NewCreateRequest(&orm.Participant{}, "/create/", "html", "POST", form) + req, err := handlers.NewCreateRequest(&orm.Participant{}, "/participants/create/", "html", "POST", form) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") t.Nil(err) @@ -377,7 +376,7 @@ func (t *testSuite) TestSchoolSubscription() { rr := httptest.NewRecorder() router := mux.NewRouter() - router.Handle("/create/", handlers.Create(&orm.Participant{})) + router.Handle("/participants/create/", handlers.Create(&orm.Participant{})) router.ServeHTTP(rr, req) t.Equal(http.StatusSeeOther, rr.Code)