From 4d1b66dd4ab5f7174a63b13540e27237163dc3e8 Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Thu, 5 Nov 2015 17:59:24 -0700 Subject: [PATCH] Correct error response content length --- handler.go | 4 ++-- head_test.go | 23 +++++++++++++++++++++-- routed_handler_test.go | 4 +++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/handler.go b/handler.go index 7ebf7d2..107c6e8 100644 --- a/handler.go +++ b/handler.go @@ -459,11 +459,11 @@ func (handler *Handler) sendError(w http.ResponseWriter, r *http.Request, err er if r.Method == "HEAD" { reason = "" } - + reason += "\n" w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Length", strconv.Itoa(len(reason))) w.WriteHeader(status) - w.Write([]byte(err.Error())) + w.Write([]byte(reason)) } // Make an absolute URLs to the given upload id. If the base path is absolute diff --git a/head_test.go b/head_test.go index f1d3fc0..71097ff 100644 --- a/head_test.go +++ b/head_test.go @@ -3,6 +3,7 @@ package tusd import ( "net/http" "os" + "strconv" "testing" ) @@ -46,14 +47,32 @@ func TestHead(t *testing.T) { "Cache-Control": "no-store", }, }).Run(handler, t) +} - (&httpTest{ +func TestHead404(t *testing.T) { + handler, _ := NewRoutedHandler(Config{ + BasePath: "https://buy.art/", + DataStore: headStore{}, + }) + + resp := (&httpTest{ Name: "Non-existing file", Method: "HEAD", URL: "no", ReqHeader: map[string]string{ "Tus-Resumable": "1.0.0", }, - Code: http.StatusNotFound, + Code: http.StatusNotFound, + ResBody: "", }).Run(handler, t) + + body := string(resp.Body.Bytes()) + if body != "\n" { + t.Errorf("Expected body to be empty. Got: %v", body) + } + + contentLength := resp.Header().Get("Content-Length") + if contentLength != strconv.Itoa(len(body)) { + t.Errorf("Expected content length header to match body length. Got: %v", contentLength) + } } diff --git a/routed_handler_test.go b/routed_handler_test.go index 95be344..93965d7 100644 --- a/routed_handler_test.go +++ b/routed_handler_test.go @@ -44,7 +44,7 @@ type httpTest struct { ResHeader map[string]string } -func (test *httpTest) Run(handler http.Handler, t *testing.T) { +func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder { t.Log(test.Name) req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody) @@ -77,6 +77,8 @@ func (test *httpTest) Run(handler http.Handler, t *testing.T) { if test.ResBody != "" && string(w.Body.Bytes()) != test.ResBody { t.Errorf("Expected '%s' as body (got '%s'", test.ResBody, string(w.Body.Bytes())) } + + return w } type methodOverrideStore struct {