-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlerlist_test.go
99 lines (85 loc) · 2.38 KB
/
handlerlist_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
package httptools
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestHandlerList_IsModifiedResponseWriter(t *testing.T) {
h := List{
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := w.(CheckResponseWriter)
w.Header().Add("WasCRW", fmt.Sprintf("%v", ok))
}),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, nil)
expected := "true"
got := rr.HeaderMap.Get("WasCRW")
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func TestHandlerList_IsSilentModifiedResponseWriter(t *testing.T) {
h := List{
SilentHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := w.(CheckResponseWriter)
w.Header().Add("WasCRW", fmt.Sprintf("%v", ok))
})),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, nil)
expected := "true"
got := rr.HeaderMap.Get("WasCRW")
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func TestHandlerList_Order(t *testing.T) {
h := List{
SilentHandler(http.HandlerFunc(handlerA)),
SilentHandler(http.HandlerFunc(handlerB)),
SilentHandler(http.HandlerFunc(handlerC)),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, nil)
expected := []string{"a", "b", "c"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func TestHandlerList_Fail(t *testing.T) {
h := List{
SilentHandler(http.HandlerFunc(handlerA)),
SilentHandler(http.HandlerFunc(handlerB)),
SilentHandler(http.HandlerFunc(failHandler)),
SilentHandler(http.HandlerFunc(handlerC)),
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, nil)
expected := []string{"a", "b"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func handlerA(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Handler", "a")
}
func handlerB(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Handler", "b")
}
func handlerC(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Handler", "c")
}
func failHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error", http.StatusInternalServerError)
}
func MustRequest(r *http.Request, err error) *http.Request {
if err != nil {
panic(err)
}
return r
}