From 79e554ee4b95d3e63561d861614de5d8663bc609 Mon Sep 17 00:00:00 2001 From: Bryce Fisher Date: Mon, 23 Nov 2015 15:58:33 -0500 Subject: [PATCH] Update tests to account for new gzip strategy --- gzip_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/gzip_test.go b/gzip_test.go index 694421f..e77013a 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -1,8 +1,8 @@ package gzip import ( - "github.com/go-martini/martini" "bufio" + "github.com/go-martini/martini" "net" "net/http" "net/http/httptest" @@ -15,13 +15,27 @@ func Test_GzipAll(t *testing.T) { recorder := httptest.NewRecorder() before := false - m := martini.New() - m.Use(All()) - m.Use(func(r http.ResponseWriter) { + bM := martini.New() + bM.Use(All()) + bM.Use(func(r http.ResponseWriter) { r.(martini.ResponseWriter).Before(func(rw martini.ResponseWriter) { before = true }) }) + router := martini.NewRouter() + bM.MapTo(router, (*martini.Routes)(nil)) + bM.Action(router.Handle) + m := &martini.ClassicMartini{ + Martini: bM, + Router: router, + } + //Create tests for + m.Get("/", (func(w http.ResponseWriter) { + w.Write([]byte("data!")) + })) + m.Get("/blank", (func(w http.ResponseWriter) (int, []byte) { + return http.StatusOK, nil + })) r, err := http.NewRequest("GET", "/", nil) if err != nil { @@ -30,6 +44,8 @@ func Test_GzipAll(t *testing.T) { m.ServeHTTP(recorder, r) + t.Logf("recorder: %#v", recorder) + // Make our assertions _, ok := recorder.HeaderMap[HeaderContentEncoding] if ok { @@ -59,6 +75,36 @@ func Test_GzipAll(t *testing.T) { if before == false { t.Error("Before hook was not called") } + + //Setup case where we request gzip but no response body is given + recorder = httptest.NewRecorder() + r, err = http.NewRequest("GET", "/blank", nil) + if err != nil { + t.Errorf("Error creating request", err) + t.FailNow() + } + r.Header.Set(HeaderAcceptEncoding, "gzip") + m.ServeHTTP(recorder, r) + + // Make our assertions + _, ok = recorder.HeaderMap[HeaderContentEncoding] + if ok { + t.Error(HeaderContentEncoding + " is present") + } + + ce = recorder.Header().Get(HeaderContentEncoding) + if strings.EqualFold(ce, "gzip") { + t.Error(HeaderContentEncoding + " is 'gzip'") + } + + data := recorder.Body.Bytes() + if len(data) > 0 { + t.Error("Body has content " + string(data)) + } + + if before == false { + t.Error("Before hook was not called") + } } type hijackableResponse struct {