diff --git a/handler.go b/handler.go index 8ce1bcf..12f9c04 100644 --- a/handler.go +++ b/handler.go @@ -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 diff --git a/handler_test.go b/handler_test.go index 77fe3e7..07ac5f4 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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) { @@ -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 diff --git a/testutils_test.go b/testutils_test.go index 8169677..0c4fc8b 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -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)