Writing tests for POST handlers
This commit is contained in:
parent
21478b20ae
commit
70996c6f60
@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -25,6 +26,20 @@ var theTests = []struct {
|
||||
{"sa", "/availability", "GET", []postData{}, http.StatusOK},
|
||||
{"contact", "/contact", "GET", []postData{}, http.StatusOK},
|
||||
{"ma", "/make-reservation", "GET", []postData{}, http.StatusOK},
|
||||
{"post-search-avail", "/availability", "POST", []postData{
|
||||
{key: "start", value: "2020-01-01"},
|
||||
{key: "end", value: "2020-01-02"},
|
||||
}, http.StatusOK},
|
||||
{"post-search-avail-json", "/availability-json", "POST", []postData{
|
||||
{key: "start", value: "2020-01-01"},
|
||||
{key: "end", value: "2020-01-02"},
|
||||
}, http.StatusOK},
|
||||
{"make-reservation", "/make-reservation", "POST", []postData{
|
||||
{key: "first_name", value: "John"},
|
||||
{key: "last_name", value: "Smith"},
|
||||
{key: "email", value: "me@here.com"},
|
||||
{key: "phone", value: "12345"},
|
||||
}, http.StatusOK},
|
||||
}
|
||||
|
||||
func TestHandlers(t *testing.T) {
|
||||
@ -43,6 +58,18 @@ func TestHandlers(t *testing.T) {
|
||||
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
||||
}
|
||||
} else {
|
||||
values := url.Values{}
|
||||
for _, x := range e.params {
|
||||
values.Add(x.key, x.value)
|
||||
}
|
||||
resp, err := ts.Client().PostForm(ts.URL+e.url, values)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != e.expectedStatusCode {
|
||||
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func getRoutes() http.Handler {
|
||||
mux := chi.NewMux()
|
||||
|
||||
mux.Use(middleware.Recoverer)
|
||||
mux.Use(NoSurf)
|
||||
// mux.Use(NoSurf) // No need to test
|
||||
mux.Use(SessionLoad)
|
||||
|
||||
mux.Get("/", Repo.Home)
|
||||
|
Loading…
Reference in New Issue
Block a user