-
Notifications
You must be signed in to change notification settings - Fork 582
/
negroni_test.go
207 lines (167 loc) · 5.27 KB
/
negroni_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package negroni
import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
)
/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func refute(t *testing.T, a interface{}, b interface{}) {
if a == b {
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func TestNegroniRun(t *testing.T) {
// just test that Run doesn't bomb
go New().Run(":3000")
}
func TestNegroniWith(t *testing.T) {
result := ""
response := httptest.NewRecorder()
n1 := New()
n1.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result = "one"
next(rw, r)
}))
n1.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "two"
next(rw, r)
}))
n1.ServeHTTP(response, (*http.Request)(nil))
expect(t, 2, len(n1.Handlers()))
expect(t, result, "onetwo")
n2 := n1.With(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "three"
next(rw, r)
}))
// Verify that n1 was left intact and not modified.
n1.ServeHTTP(response, (*http.Request)(nil))
expect(t, 2, len(n1.Handlers()))
expect(t, result, "onetwo")
n2.ServeHTTP(response, (*http.Request)(nil))
expect(t, 3, len(n2.Handlers()))
expect(t, result, "onetwothree")
}
func TestNegroniWith_doNotModifyOriginal(t *testing.T) {
result := ""
response := httptest.NewRecorder()
n1 := New()
n1.handlers = make([]Handler, 0, 10) // enforce initial capacity
n1.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result = "one"
next(rw, r)
}))
n1.ServeHTTP(response, (*http.Request)(nil))
expect(t, 1, len(n1.Handlers()))
n2 := n1.With(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "two"
next(rw, r)
}))
n3 := n1.With(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "three"
next(rw, r)
}))
// rebuilds middleware
n2.UseHandlerFunc(func(rw http.ResponseWriter, r *http.Request) {})
n3.UseHandlerFunc(func(rw http.ResponseWriter, r *http.Request) {})
n1.ServeHTTP(response, (*http.Request)(nil))
expect(t, 1, len(n1.Handlers()))
expect(t, result, "one")
n2.ServeHTTP(response, (*http.Request)(nil))
expect(t, 3, len(n2.Handlers()))
expect(t, result, "onetwo")
n3.ServeHTTP(response, (*http.Request)(nil))
expect(t, 3, len(n3.Handlers()))
expect(t, result, "onethree")
}
func TestNegroniServeHTTP(t *testing.T) {
result := ""
response := httptest.NewRecorder()
n := New()
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "foo"
next(rw, r)
result += "ban"
}))
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "bar"
next(rw, r)
result += "baz"
}))
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
result += "bat"
rw.WriteHeader(http.StatusBadRequest)
}))
n.ServeHTTP(response, (*http.Request)(nil))
expect(t, result, "foobarbatbazban")
expect(t, response.Code, http.StatusBadRequest)
}
// Ensures that a Negroni middleware chain
// can correctly return all of its handlers.
func TestHandlers(t *testing.T) {
response := httptest.NewRecorder()
n := New()
handlers := n.Handlers()
expect(t, 0, len(handlers))
n.Use(HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
rw.WriteHeader(http.StatusOK)
}))
// Expects the length of handlers to be exactly 1
// after adding exactly one handler to the middleware chain
handlers = n.Handlers()
expect(t, 1, len(handlers))
// Ensures that the first handler that is in sequence behaves
// exactly the same as the one that was registered earlier
handlers[0].ServeHTTP(response, (*http.Request)(nil), nil)
expect(t, response.Code, http.StatusOK)
}
func TestNegroni_Use_Nil(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Errorf("Expected negroni.Use(nil) to panic, but it did not")
}
}()
n := New()
n.Use(nil)
}
func TestDetectAddress(t *testing.T) {
if detectAddress() != DefaultAddress {
t.Error("Expected the DefaultAddress")
}
if detectAddress(":6060") != ":6060" {
t.Error("Expected the provided address")
}
os.Setenv("PORT", "8080")
if detectAddress() != ":8080" {
t.Error("Expected the PORT env var with a prefixed colon")
}
}
func voidHTTPHandlerFunc(rw http.ResponseWriter, r *http.Request) {
// Do nothing
}
// Test for function Wrap
func TestWrap(t *testing.T) {
response := httptest.NewRecorder()
handler := Wrap(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(response, (*http.Request)(nil), voidHTTPHandlerFunc)
expect(t, response.Code, http.StatusOK)
}
// Test for function WrapFunc
func TestWrapFunc(t *testing.T) {
response := httptest.NewRecorder()
// WrapFunc(f) equals Wrap(http.HandlerFunc(f)), it's simpler and useful.
handler := WrapFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})
handler.ServeHTTP(response, (*http.Request)(nil), voidHTTPHandlerFunc)
expect(t, response.Code, http.StatusOK)
}