-
Notifications
You must be signed in to change notification settings - Fork 18
/
gzip_test.go
106 lines (87 loc) · 2.26 KB
/
gzip_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gzip
import (
"github.com/go-martini/martini"
"bufio"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test_GzipAll(t *testing.T) {
// Set up
recorder := httptest.NewRecorder()
before := false
m := martini.New()
m.Use(All())
m.Use(func(r http.ResponseWriter) {
r.(martini.ResponseWriter).Before(func(rw martini.ResponseWriter) {
before = true
})
})
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, r)
// Make our assertions
_, ok := recorder.HeaderMap[HeaderContentEncoding]
if ok {
t.Error(HeaderContentEncoding + " present")
}
ce := recorder.Header().Get(HeaderContentEncoding)
if strings.EqualFold(ce, "gzip") {
t.Error(HeaderContentEncoding + " is 'gzip'")
}
recorder = httptest.NewRecorder()
r.Header.Set(HeaderAcceptEncoding, "gzip")
m.ServeHTTP(recorder, r)
// Make our assertions
_, ok = recorder.HeaderMap[HeaderContentEncoding]
if !ok {
t.Error(HeaderContentEncoding + " not present")
}
ce = recorder.Header().Get(HeaderContentEncoding)
if !strings.EqualFold(ce, "gzip") {
t.Error(HeaderContentEncoding + " is not 'gzip'")
}
if before == false {
t.Error("Before hook was not called")
}
}
type hijackableResponse struct {
Hijacked bool
header http.Header
}
func newHijackableResponse() *hijackableResponse {
return &hijackableResponse{header: make(http.Header)}
}
func (h *hijackableResponse) Header() http.Header { return h.header }
func (h *hijackableResponse) Write(buf []byte) (int, error) { return 0, nil }
func (h *hijackableResponse) WriteHeader(code int) {}
func (h *hijackableResponse) Flush() {}
func (h *hijackableResponse) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h.Hijacked = true
return nil, nil, nil
}
func Test_ResponseWriter_Hijack(t *testing.T) {
hijackable := newHijackableResponse()
m := martini.New()
m.Use(All())
m.Use(func(rw http.ResponseWriter) {
if hj, ok := rw.(http.Hijacker); !ok {
t.Error("Unable to hijack")
} else {
hj.Hijack()
}
})
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}
r.Header.Set(HeaderAcceptEncoding, "gzip")
m.ServeHTTP(hijackable, r)
if !hijackable.Hijacked {
t.Error("Hijack was not called")
}
}