forked from seborama/govcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
170 lines (153 loc) · 4.03 KB
/
request_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
package govcr
import (
"net/http"
"net/textproto"
"net/url"
"testing"
)
func mustParseURL(s string) url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return *u
}
func failIfCalledRequestFilter(t *testing.T) RequestFilter {
return func(req Request) Request {
t.Fatal("Filter was called unexpectedly")
return req
}
}
// mustCallRequestFilterOnce will return a request filter that will record
// how many times it was called.
// The returned function will test if the filter was called once and fail otherwise.
func mustCallRequestFilterOnce(t *testing.T) (RequestFilter, func()) {
var n int
return func(req Request) Request {
n++
return req
}, func() {
if n != 1 {
t.Fatalf("Filter was called %d times, should be called once", n)
}
}
}
func requestTestBase() Request {
return Request{
Header: map[string][]string{textproto.CanonicalMIMEHeaderKey("a-header"): {"a-value"}},
Body: nil,
Method: http.MethodGet,
URL: mustParseURL("https://127.0.0.1/example-url/id/42"),
}
}
func TestRequestFilter_OnMethod(t *testing.T) {
f := failIfCalledRequestFilter(t).OnMethod(http.MethodPost)
f(requestTestBase())
f, ok := mustCallRequestFilterOnce(t)
f = f.OnMethod(http.MethodGet)
f(requestTestBase())
ok()
}
func TestRequestFilter_OnPath(t *testing.T) {
f := failIfCalledRequestFilter(t).OnPath("non-existing-path")
f(requestTestBase())
f, ok := mustCallRequestFilterOnce(t)
f = f.OnPath(`/example-url/id/`)
f(requestTestBase())
ok()
// Empty matches everything
f, ok = mustCallRequestFilterOnce(t)
f = f.OnPath("")
f(requestTestBase())
ok()
}
func TestRequestAddHeaderValue(t *testing.T) {
r := RequestAddHeaderValue("new-header", "new-value")(requestTestBase())
if r.Header.Get("new-header") != "new-value" {
t.Error("did not get expected new header")
}
// Check if existing is still untouched.
if r.Header.Get("a-header") != "a-value" {
t.Error("did not get expected old header")
}
}
func TestRequestDeleteHeaderKeys(t *testing.T) {
r := RequestDeleteHeaderKeys("non-existing", "a-header")(requestTestBase())
if r.Header.Get("a-header") != "" {
t.Error("'a-header' not removed")
}
if len(r.Header) != 0 {
t.Errorf("want no headers, got %d (%+v)", len(r.Header), r.Header)
}
}
func TestRequestExcludeHeaderFunc(t *testing.T) {
req := requestTestBase()
req.Header.Add("another-header", "yeah")
header1, header2 := textproto.CanonicalMIMEHeaderKey("a-header"), textproto.CanonicalMIMEHeaderKey("another-header")
// We expect both headers to be checked.
want := map[string]struct{}{header1: {}, header2: {}}
r := RequestExcludeHeaderFunc(func(key string) bool {
_, ok := want[key]
if !ok {
t.Errorf("got unexpected key %q", key)
}
// Delete so we check we only get key once.
delete(want, key)
// Delete 'a-header'
return header1 == key
})
req = r(req)
if len(want) > 0 {
t.Errorf("header was not checked: %v", want)
}
if len(req.Header) != 1 {
t.Fatalf("unexpected header count, want one: %v", req.Header)
}
if req.Header.Get("another-header") != "yeah" {
t.Errorf("unexpected header value: %s", req.Header.Get("another-header"))
}
}
func TestRequestFilters_Add(t *testing.T) {
var f RequestFilters
f1, ok1 := mustCallRequestFilterOnce(t)
f2, ok2 := mustCallRequestFilterOnce(t)
f.Add(f1, f2)
f.combined()(requestTestBase())
ok1()
ok2()
}
func TestRequestFilters_Prepend(t *testing.T) {
var f RequestFilters
var firstRan bool
first := func(req Request) Request {
firstRan = true
return req
}
second := func(req Request) Request {
if !firstRan {
t.Fatal("second ran before first")
}
return req
}
third := func(req Request) Request {
if !firstRan {
t.Fatal("third ran before first")
}
return req
}
f.Add(second)
f.Prepend(first)
f.Add(third)
f.combined()(requestTestBase())
}
func TestRequestFilters_combined(t *testing.T) {
var f RequestFilters
f1, ok1 := mustCallRequestFilterOnce(t)
f2, ok2 := mustCallRequestFilterOnce(t)
f3, ok3 := mustCallRequestFilterOnce(t)
f.Add(f1, f2, f3)
f.combined()(requestTestBase())
ok1()
ok2()
ok3()
}