Skip to content

Commit 30b99ae

Browse files
Merge pull request #13 from suzuki-shunsuke/feat/add-query-equal
feat: add the condition about query and header
2 parents 69ea92a + 326d2f3 commit 30b99ae

File tree

5 files changed

+98
-34
lines changed

5 files changed

+98
-34
lines changed

fagott/matcher.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,28 @@ func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
4747
return f, err
4848
}
4949
}
50-
if matcher.Header != nil {
51-
f, err := isMatchHeader(req, matcher)
50+
if matcher.PartOfHeader != nil {
51+
f, err := isMatchPartOfHeader(req, matcher)
5252
if err != nil || !f {
5353
return f, err
5454
}
5555
}
56-
if matcher.Query != nil {
57-
f, err := isMatchQuery(req, matcher)
56+
if matcher.Header != nil {
57+
if !reflect.DeepEqual(matcher.Header, req.Header) {
58+
return false, nil
59+
}
60+
}
61+
if matcher.PartOfQuery != nil {
62+
f, err := isMatchPartOfQuery(req, matcher)
5863
if err != nil || !f {
5964
return f, err
6065
}
6166
}
67+
if matcher.Query != nil {
68+
if !reflect.DeepEqual(matcher.Query, req.URL.Query()) {
69+
return false, nil
70+
}
71+
}
6272
if matcher.Match != nil {
6373
f, err := matcher.Match(req)
6474
if err != nil || !f {
@@ -68,8 +78,8 @@ func isMatch(req *http.Request, matcher *Matcher) (bool, error) {
6878
return true, nil
6979
}
7080

71-
func isMatchHeader(req *http.Request, matcher *Matcher) (bool, error) {
72-
for k, v := range matcher.Header {
81+
func isMatchPartOfHeader(req *http.Request, matcher *Matcher) (bool, error) {
82+
for k, v := range matcher.PartOfHeader {
7383
a, ok := req.Header[k]
7484
if !ok {
7585
return false, nil
@@ -83,9 +93,9 @@ func isMatchHeader(req *http.Request, matcher *Matcher) (bool, error) {
8393
return true, nil
8494
}
8595

86-
func isMatchQuery(req *http.Request, matcher *Matcher) (bool, error) {
96+
func isMatchPartOfQuery(req *http.Request, matcher *Matcher) (bool, error) {
8797
query := req.URL.Query()
88-
for k, v := range matcher.Query {
98+
for k, v := range matcher.PartOfQuery {
8999
a, ok := query[k]
90100
if !ok {
91101
return false, nil

fagott/matcher_test.go

+39-12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ func Test_isMatch(t *testing.T) {
114114
},
115115
},
116116
},
117+
{
118+
title: "header isn't equal",
119+
req: &http.Request{
120+
Header: http.Header{
121+
"FOO": []string{"foo"},
122+
"BAR": []string{"bar"},
123+
},
124+
},
125+
matcher: &Matcher{
126+
Header: http.Header{
127+
"FOO": []string{"foo"},
128+
},
129+
},
130+
},
117131
{
118132
title: "query doesn't match",
119133
req: &http.Request{
@@ -122,11 +136,24 @@ func Test_isMatch(t *testing.T) {
122136
},
123137
},
124138
matcher: &Matcher{
125-
Query: url.Values{
139+
PartOfQuery: url.Values{
126140
"name": []string{"bar"},
127141
},
128142
},
129143
},
144+
{
145+
title: "query isn't equal",
146+
req: &http.Request{
147+
URL: &url.URL{
148+
RawQuery: "name=foo&age=10",
149+
},
150+
},
151+
matcher: &Matcher{
152+
Query: url.Values{
153+
"name": []string{"foo"},
154+
},
155+
},
156+
},
130157
{
131158
title: "match function doesn't match",
132159
matcher: &Matcher{
@@ -154,7 +181,7 @@ func Test_isMatch(t *testing.T) {
154181
}
155182
}
156183

157-
func Test_isMatchQuery(t *testing.T) {
184+
func Test_isMatchPartOfQuery(t *testing.T) {
158185
data := []struct {
159186
title string
160187
req *http.Request
@@ -163,14 +190,14 @@ func Test_isMatchQuery(t *testing.T) {
163190
exp bool
164191
}{
165192
{
166-
title: "header value doesn't match",
193+
title: "query value doesn't match",
167194
req: &http.Request{
168195
URL: &url.URL{
169196
RawQuery: "name=foo",
170197
},
171198
},
172199
matcher: &Matcher{
173-
Query: url.Values{
200+
PartOfQuery: url.Values{
174201
"name": []string{"bar"},
175202
},
176203
},
@@ -181,7 +208,7 @@ func Test_isMatchQuery(t *testing.T) {
181208
URL: &url.URL{},
182209
},
183210
matcher: &Matcher{
184-
Query: url.Values{
211+
PartOfQuery: url.Values{
185212
"name": nil,
186213
},
187214
},
@@ -194,7 +221,7 @@ func Test_isMatchQuery(t *testing.T) {
194221
},
195222
},
196223
matcher: &Matcher{
197-
Query: url.Values{
224+
PartOfQuery: url.Values{
198225
"name": []string{"foo"},
199226
},
200227
},
@@ -204,7 +231,7 @@ func Test_isMatchQuery(t *testing.T) {
204231

205232
for _, d := range data {
206233
t.Run(d.title, func(t *testing.T) {
207-
b, err := isMatchQuery(d.req, d.matcher)
234+
b, err := isMatchPartOfQuery(d.req, d.matcher)
208235
if d.isErr {
209236
require.NotNil(t, err)
210237
return
@@ -219,7 +246,7 @@ func Test_isMatchQuery(t *testing.T) {
219246
}
220247
}
221248

222-
func Test_isMatchHeader(t *testing.T) {
249+
func Test_isMatchPartOfHeader(t *testing.T) {
223250
data := []struct {
224251
title string
225252
req *http.Request
@@ -235,7 +262,7 @@ func Test_isMatchHeader(t *testing.T) {
235262
},
236263
},
237264
matcher: &Matcher{
238-
Header: http.Header{
265+
PartOfHeader: http.Header{
239266
"FOO": []string{"bar"},
240267
},
241268
},
@@ -246,7 +273,7 @@ func Test_isMatchHeader(t *testing.T) {
246273
Header: http.Header{},
247274
},
248275
matcher: &Matcher{
249-
Header: http.Header{
276+
PartOfHeader: http.Header{
250277
"FOO": nil,
251278
},
252279
},
@@ -259,7 +286,7 @@ func Test_isMatchHeader(t *testing.T) {
259286
},
260287
},
261288
matcher: &Matcher{
262-
Header: http.Header{
289+
PartOfHeader: http.Header{
263290
"FOO": []string{"foo"},
264291
},
265292
},
@@ -269,7 +296,7 @@ func Test_isMatchHeader(t *testing.T) {
269296

270297
for _, d := range data {
271298
t.Run(d.title, func(t *testing.T) {
272-
b, err := isMatchHeader(d.req, d.matcher)
299+
b, err := isMatchPartOfHeader(d.req, d.matcher)
273300
if d.isErr {
274301
require.NotNil(t, err)
275302
return

fagott/struct.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type (
4444
Method string
4545
// Path is the request path.
4646
Path string
47+
// PartOfQuery is the request query parameters.
48+
PartOfQuery url.Values
4749
// Query is the request query parameters.
4850
Query url.Values
4951
// BodyString is the request body.
@@ -52,9 +54,11 @@ type (
5254
BodyJSON interface{}
5355
// BodyJSONString is a JSON string and compared to the request body as JSON.
5456
BodyJSONString string
55-
// Header is the request header's conditions.
57+
// PartOfHeader is the request header's conditions.
5658
// If the header value is nil, RoundTrip checks whether the key is included in the request header.
5759
// Otherwise, RoundTrip also checks whether the value is equal.
60+
PartOfHeader http.Header
61+
// Header is the request header's conditions.
5862
Header http.Header
5963
}
6064

@@ -71,13 +75,17 @@ type (
7175
BodyJSON interface{}
7276
// BodyJSONString is a JSON string and compared to the request body as JSON.
7377
BodyJSONString string
74-
// Header is the request header's conditions.
78+
// PartOfHeader is the request header's conditions.
7579
// If the header value is nil, RoundTrip checks whether the key is included in the request header.
7680
// Otherwise, RoundTrip also checks whether the value is equal.
81+
PartOfHeader http.Header
82+
// Header is the request header's conditions.
7783
Header http.Header
78-
// Query is the request query parameters.
84+
// PartOfQuery is the request query parameters.
7985
// If the query value is nil, RoundTrip checks whether the key is included in the request query.
8086
// Otherwise, RoundTrip also checks whether the value is equal.
87+
PartOfQuery url.Values
88+
// Query is the request query parameters.
8189
Query url.Values
8290
}
8391

fagott/tester.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ func testRequest(t *testing.T, req *http.Request, service *Service, route *Route
2828
if tester.BodyJSONString != "" {
2929
testBodyJSONString(t, req, service, route)
3030
}
31+
if tester.PartOfHeader != nil {
32+
testPartOfHeader(t, req, service, route)
33+
}
3134
if tester.Header != nil {
32-
testHeader(t, req, service, route)
35+
assert.Equal(
36+
t, tester.Header, req.Header,
37+
makeMsg("request header should match", service.Endpoint, route.Name))
38+
}
39+
if tester.PartOfQuery != nil {
40+
testPartOfQuery(t, req, service, route)
3341
}
3442
if tester.Query != nil {
35-
testQuery(t, req, service, route)
43+
assert.Equal(
44+
t, tester.Query, req.URL.Query(),
45+
makeMsg("request query parameter should match", service.Endpoint, route.Name))
3646
}
3747
if tester.Test != nil {
3848
tester.Test(t, req, service, route)
@@ -148,11 +158,11 @@ func testBodyJSONString(
148158
makeMsg("request body should match", srv, reqName))
149159
}
150160

151-
func testHeader(t *testing.T, req *http.Request, service *Service, route *Route) {
161+
func testPartOfHeader(t *testing.T, req *http.Request, service *Service, route *Route) {
152162
reqName := route.Name
153163
srv := service.Endpoint
154164

155-
for k, v := range route.Tester.Header {
165+
for k, v := range route.Tester.PartOfHeader {
156166
a, ok := req.Header[k]
157167
if !ok {
158168
assert.Fail(
@@ -168,12 +178,12 @@ func testHeader(t *testing.T, req *http.Request, service *Service, route *Route)
168178
}
169179
}
170180

171-
func testQuery(t *testing.T, req *http.Request, service *Service, route *Route) {
181+
func testPartOfQuery(t *testing.T, req *http.Request, service *Service, route *Route) {
172182
reqName := route.Name
173183
srv := service.Endpoint
174184

175185
query := req.URL.Query()
176-
for k, v := range route.Tester.Query {
186+
for k, v := range route.Tester.PartOfQuery {
177187
a, ok := query[k]
178188
if !ok {
179189
assert.Fail(

fagott/tester_test.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func Test_testRequest(t *testing.T) {
2323
Method: "POST",
2424
URL: &url.URL{
2525
Path: "/users",
26-
RawQuery: "name=foo",
26+
RawQuery: "name=foo&age=10",
2727
},
2828
Body: ioutil.NopCloser(strings.NewReader(`{
2929
"name": "foo",
3030
"email": "[email protected]"
3131
}`)),
3232
Header: http.Header{
3333
"Authorization": []string{"token XXXXX"},
34+
"Content-Type": []string{"application/json"},
3435
},
3536
},
3637
service: &Service{},
@@ -42,11 +43,19 @@ func Test_testRequest(t *testing.T) {
4243
"name": "foo",
4344
"email": "[email protected]"
4445
}`,
46+
PartOfHeader: http.Header{
47+
"Authorization": []string{"token XXXXX"},
48+
},
4549
Header: http.Header{
4650
"Authorization": []string{"token XXXXX"},
51+
"Content-Type": []string{"application/json"},
52+
},
53+
PartOfQuery: url.Values{
54+
"name": []string{"foo"},
4755
},
4856
Query: url.Values{
4957
"name": []string{"foo"},
58+
"age": []string{"10"},
5059
},
5160
Test: func(t *testing.T, req *http.Request, service *Service, route *Route) {},
5261
},
@@ -263,7 +272,7 @@ func Test_testBodyJSONString(t *testing.T) {
263272
}
264273
}
265274

266-
func Test_testHeader(t *testing.T) {
275+
func Test_testPartOfHeader(t *testing.T) {
267276
data := []struct {
268277
title string
269278
req *http.Request
@@ -292,12 +301,12 @@ func Test_testHeader(t *testing.T) {
292301

293302
for _, d := range data {
294303
t.Run(d.title, func(t *testing.T) {
295-
testHeader(t, d.req, d.service, d.route)
304+
testPartOfHeader(t, d.req, d.service, d.route)
296305
})
297306
}
298307
}
299308

300-
func Test_testQuery(t *testing.T) {
309+
func Test_testPartOfQuery(t *testing.T) {
301310
data := []struct {
302311
title string
303312
req *http.Request
@@ -324,7 +333,7 @@ func Test_testQuery(t *testing.T) {
324333

325334
for _, d := range data {
326335
t.Run(d.title, func(t *testing.T) {
327-
testQuery(t, d.req, d.service, d.route)
336+
testPartOfQuery(t, d.req, d.service, d.route)
328337
})
329338
}
330339
}

0 commit comments

Comments
 (0)