From fbcdad4c59f95c4a95e4c707f591e3f81b2438fd Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 30 Oct 2017 11:16:52 +0100 Subject: [PATCH] Added missing content-type checking to handleCreateJSON test --- handlers/handlers.go | 7 +++++-- handlers/handlers_test.go | 9 ++++++--- store/store.go | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 8fbcc21..17f4ad4 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -32,8 +32,10 @@ func New(addr string, store store.Store) *Handler { addr: addr, store: store, } - router := h.handlers() - h.server = &http.Server{Addr: h.addr, Handler: router} + h.server = &http.Server{ + Addr: h.addr, + Handler: h.handlers(), + } return h } @@ -76,6 +78,7 @@ func (h *Handler) handleCreateJSON(w http.ResponseWriter, r *http.Request) { return } req.URL = h.generateURL(r, id) + w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(req) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 27ef056..8160d6e 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -36,7 +36,7 @@ func TestCreateEntryJSON(t *testing.T) { name: "body is nil", response: "could not decode JSON: EOF", statusCode: http.StatusBadRequest, - contentType: "appication/json", + contentType: "text/plain; charset=utf-8", ignoreResponse: true, }, { @@ -45,7 +45,7 @@ func TestCreateEntryJSON(t *testing.T) { URL: "https://www.google.de/", }, statusCode: http.StatusOK, - contentType: "appication/json", + contentType: "application/json", }, { name: "no valid URL", @@ -53,7 +53,7 @@ func TestCreateEntryJSON(t *testing.T) { URL: "this is really not a URL", }, statusCode: http.StatusBadRequest, - contentType: "appication/json", + contentType: "text/plain; charset=utf-8", response: store.ErrNoValidURL.Error(), ignoreResponse: true, }, @@ -80,6 +80,9 @@ func TestCreateEntryJSON(t *testing.T) { if err != nil { t.Fatalf("could not create post request: %v", err) } + if resp.Header.Get("Content-Type") != tc.contentType { + t.Fatalf("content-type is not the expected one: %s; got: %s", tc.contentType, resp.Header.Get("Content-Type")) + } body, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatalf("could not read response: %v", err) diff --git a/store/store.go b/store/store.go index 66201a9..5e2de6e 100644 --- a/store/store.go +++ b/store/store.go @@ -34,7 +34,7 @@ var ErrNoEntryFound = errors.New("no entry found") var ErrNoValidURL = errors.New("no valid URL") // ErrGeneratingTriesFailed is returned when the 10 tries to generate an id failed -var ErrGeneratingTriesFailed = errors.New("could not generate unique id which doesn't exist in the db") +var ErrGeneratingTriesFailed = errors.New("could not generate unique id") // ErrIDIsEmpty is returned when the given ID is empty var ErrIDIsEmpty = errors.New("id is empty")