Skip to content

Commit

Permalink
Use http.Error to also set text/plain content type
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
wader committed Dec 8, 2015
1 parent 49e8f43 commit 122db5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion handler.go
Expand Up @@ -60,7 +60,7 @@ type CSRFHandler struct {
}

func defaultFailureHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(FailureCode)
http.Error(w, "", FailureCode)
}

// Extracts the "sent" token from the request
Expand Down
14 changes: 14 additions & 0 deletions handler_test.go
Expand Up @@ -249,6 +249,13 @@ func TestNoTokenFails(t *testing.T) {
t.Errorf("The check should've failed with the code %d, but instead, it"+
" returned code %d", FailureCode, writer.Code)
}

expectedContentType := "text/plain; charset=utf-8"
actualContentType := writer.Header().Get("Content-Type")
if actualContentType != expectedContentType {
t.Errorf("The check should've failed with content type %s, but instead, it"+
" returned content type %s", expectedContentType, actualContentType)
}
}

func TestWrongTokenFails(t *testing.T) {
Expand All @@ -274,6 +281,13 @@ func TestWrongTokenFails(t *testing.T) {
t.Errorf("The check should've failed with the code %d, but instead, it"+
" returned code %d", FailureCode, writer.Code)
}

expectedContentType := "text/plain; charset=utf-8"
actualContentType := writer.Header().Get("Content-Type")
if actualContentType != expectedContentType {
t.Errorf("The check should've failed with content type %s, but instead, it"+
" returned content type %s", expectedContentType, actualContentType)
}
}

// For this and similar tests we start a test server
Expand Down
2 changes: 1 addition & 1 deletion testutils_test.go
Expand Up @@ -40,7 +40,7 @@ func correctReason(t *testing.T, reason error) http.Handler {
" but it failed with the reason %#v", reason, got)
}
// Writes the default failure code
w.WriteHeader(FailureCode)
http.Error(w, "", FailureCode)
}

return http.HandlerFunc(fn)
Expand Down

0 comments on commit 122db5e

Please sign in to comment.