Skip to content

Commit

Permalink
Update tests to account for new gzip strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
bfisher-novetta committed Nov 23, 2015
1 parent 064528d commit 79e554e
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions gzip_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gzip

import (
"github.com/go-martini/martini"
"bufio"
"github.com/go-martini/martini"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 79e554e

Please sign in to comment.