Skip to content

Commit

Permalink
StatusNoContent does not allow body
Browse files Browse the repository at this point in the history
Fix http: request method or response status code does not allow body
  • Loading branch information
shogo82148 committed Dec 3, 2018
1 parent a0216ac commit bf49c34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func WithConfig(service *goa.Service, conf *GoaCORSConfig) goa.Middleware {
if conf.MaxAge > 0 {
rw.Header().Set(HeaderAccessControlMaxAge, maxAge)
}
return service.Send(c, http.StatusNoContent, http.StatusText(http.StatusNoContent))
rw.WriteHeader(http.StatusNoContent)
return nil
}
}

Expand Down
17 changes: 17 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func TestPreflightRequet(t *testing.T) {
t.Error("access control allow headers should be 'X-OriginalRequest' but ", rw.Header().Get(goacors.HeaderAccessControlAllowHeaders))
t.Fail()
}

// StatusNoContent does not allow body
if rw.Status != http.StatusNoContent {
t.Errorf("the status should be %d, got %d", http.StatusNoContent, rw.Status)
}
if len(rw.Body) != 0 {
t.Errorf("the length of the body should be 0, got %d", len(rw.Body))
}
}

func TestNotGivenAllowHeaderOnRequest(t *testing.T) {
Expand Down Expand Up @@ -325,4 +333,13 @@ func TestAddedAllowOrigHeader(t *testing.T) {
t.Error("allow origin should be empty")
t.Fail()
}

// StatusNoContent does not allow body
if rw.Status != http.StatusNoContent {
t.Errorf("the status should be %d, got %d", http.StatusNoContent, rw.Status)
}
if len(rw.Body) != 0 {
t.Errorf("the length of the body should be 0, got %d", len(rw.Body))
t.Log(string(rw.Body))
}
}

0 comments on commit bf49c34

Please sign in to comment.