Browse Source

Added missing content-type checking to handleCreateJSON test

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
fbcdad4c59
  1. 7
      handlers/handlers.go
  2. 9
      handlers/handlers_test.go
  3. 2
      store/store.go

7
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)

9
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)

2
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")

Loading…
Cancel
Save