From 3185ecdda1894867ba796e1b6a1274f7ab18f128 Mon Sep 17 00:00:00 2001 From: Vincent Vanackere Date: Tue, 7 Jan 2014 09:17:11 +0100 Subject: [PATCH] Static : fix and improve tests. - The tests were previously always passing (response.Code is initialised to http.StatusOK, without a router it won't be modified even if the file does not exists). - Also check response.Body for HEAD and GET requests. --- static_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/static_test.go b/static_test.go index 243a558..b92526d 100644 --- a/static_test.go +++ b/static_test.go @@ -1,6 +1,7 @@ package martini import ( + "bytes" "net/http" "net/http/httptest" "testing" @@ -8,26 +9,34 @@ import ( func Test_Static(t *testing.T) { response := httptest.NewRecorder() + response.Body = new(bytes.Buffer) m := New() + r := NewRouter() m.Use(Static(".")) + m.Action(r.Handle) req, err := http.NewRequest("GET", "http://localhost:3000/martini.go", nil) if err != nil { t.Error(err) } - m.ServeHTTP(response, req) expect(t, response.Code, http.StatusOK) + if response.Body.Len() == 0 { + t.Errorf("Got empty body for GET request") + } } func Test_Static_Head(t *testing.T) { response := httptest.NewRecorder() + response.Body = new(bytes.Buffer) m := New() + r := NewRouter() m.Use(Static(".")) + m.Action(r.Handle) req, err := http.NewRequest("HEAD", "http://localhost:3000/martini.go", nil) if err != nil { @@ -36,6 +45,9 @@ func Test_Static_Head(t *testing.T) { m.ServeHTTP(response, req) expect(t, response.Code, http.StatusOK) + if response.Body.Len() != 0 { + t.Errorf("Got non-empty body for HEAD request") + } } func Test_Static_As_Post(t *testing.T) {