-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectation_test.go
178 lines (146 loc) · 4.58 KB
/
expectation_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
package hex
import (
"fmt"
"net/http/httptest"
"testing"
)
func ExampleExpectation_Never() {
e := Expecter{}
e.ExpectReq("GET", "/users").Never()
e.LogReq(httptest.NewRequest("GET", "/users", nil))
fmt.Println(e.Summary())
// Output:
// Expectations
// GET /users - failed, expected 0 matches, got 1
}
func ExampleExpectation_Once() {
e := Expecter{}
e.ExpectReq("GET", "/status").Once()
e.LogReq(httptest.NewRequest("GET", "/status", nil))
e.LogReq(httptest.NewRequest("GET", "/status", nil))
fmt.Println(e.Summary())
// Output:
// Expectations
// GET /status - failed, expected 1 matches, got 2
}
func TestExpectation(t *testing.T) {
testCases := []struct {
expMethod string
expPath string
reqMethod string
reqPath string
shouldPass bool
}{
// Cases that should match
{"GET", "/foobar", "GET", "/foobar", true},
{"POST", "/foo/baz", "POST", "/foo/baz", true},
{"PATCH", "/", "PATCH", "/", true},
// Cases that should not match
{"GET", "/foobar", "POST", "/foobar", false},
{"GET", "/foobar", "GET", "/foobar/1", false},
{"PATCH", "/", "PATCH", "/foobar", false},
}
for _, tc := range testCases {
e := Expecter{}
e.ExpectReq(tc.expMethod, tc.expPath).Do(func() {
e.LogReq(httptest.NewRequest(tc.reqMethod, tc.reqPath, nil))
})
if tc.shouldPass {
if !e.Pass() {
t.Errorf("Expected request %s %s to match expectation %s %s, but it didn't",
tc.reqMethod, tc.reqPath, tc.expMethod, tc.expPath)
}
} else {
if e.Pass() {
t.Errorf("Expected request %s %s to not match expectation %s %s, but it did",
tc.reqMethod, tc.reqPath, tc.expMethod, tc.expPath)
}
}
}
}
func TestNever(t *testing.T) {
t.Run("Never() passes when a expectation does not match any requests", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").Never().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/bar", nil))
})
if !e.Pass() {
t.Errorf("Never() failed when it was expected to pass")
}
})
t.Run("Never() fails when a expectation matches a request", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").Never().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo", nil))
})
if e.Pass() {
t.Errorf("Never() passed when it was expected to fail")
}
})
t.Run("Never() causes a failure when a request with sub-conditions matches", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").WithQuery("name", "bob").Never().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo?name=bob&foo=bar", nil))
})
if e.Pass() {
t.Errorf("Never() passed when it was expected to fail")
}
})
t.Run("Never() fails when an expectation matches in method/path but differs in sub-conditions", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").WithQuery("name", "bob").Never().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo?name=sam", nil))
})
if !e.Pass() {
t.Errorf("Never() failed when it was expected to pass")
}
})
}
func TestOnce(t *testing.T) {
t.Run("Once() passes when a expectation matches one request", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").Once().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo", nil))
})
if !e.Pass() {
t.Errorf("Once() failed when it was expected to pass")
}
})
t.Run("Once() fails when a expectation does not match a request", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").Once().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/bar", nil))
})
if e.Pass() {
t.Errorf("Once() passed when it was expected to fail")
}
})
t.Run("Once() fails when a expectation matches more than one request", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").Once().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo", nil))
e.LogReq(httptest.NewRequest("GET", "/foo", nil))
})
if e.Pass() {
t.Errorf("Once() passed when it was expected to fail")
}
})
t.Run("Once() passes when a request with sub-conditions matches", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").WithQuery("name", "bob").Once().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo?name=bob&foo=bar", nil))
})
if !e.Pass() {
t.Errorf("Once() failed when it was expected to pass")
}
})
t.Run("Once() fails when an expectation matches in method/path but differs in sub-conditions", func(t *testing.T) {
e := Expecter{}
e.ExpectReq("GET", "/foo").WithQuery("name", "bob").Once().Do(func() {
e.LogReq(httptest.NewRequest("GET", "/foo?name=sam", nil))
})
if e.Pass() {
t.Errorf("Once() passed when it was expected to fail")
}
})
}