Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 7359c15

Browse files
Attempt to match mock raw query string before complex match (#11)
The complex parsing and matching method sometimes fails if the regex isn't quite right, and some users just want to perform an exact string match. So we attempt an exact match before parsing the query string
1 parent 2a71311 commit 7359c15

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

domain/mocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ var matchesQuery matcher = func(r *http.Request, mock Mock) bool {
8989
return true
9090
}
9191

92+
if mock.MatchRequest.Query == r.URL.RawQuery {
93+
return true
94+
}
95+
9296
receivedQuery := r.URL.Query()
9397
mockQuery, err := url.ParseQuery(mock.MatchRequest.Query)
9498
if err != nil {

domain/mocks_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package domain
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"net/http"
65
"net/http/httptest"
76
"strings"
87
"testing"
8+
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestMatcher_Matches(t *testing.T) {
@@ -39,6 +40,38 @@ func TestMatcher_Matches(t *testing.T) {
3940
expectedResponse: Response{},
4041
expectedOK: false,
4142
},
43+
"with query params - page 1": {
44+
mock: Mock{
45+
MatchRequest: MatchRequest{
46+
Method: "GET",
47+
Path: "/user",
48+
Query: "page=1",
49+
},
50+
Response: Response{
51+
Status: 200,
52+
Body: `{"field": "page1"}`,
53+
},
54+
},
55+
request: httptest.NewRequest(http.MethodGet, "/user?page=1", nil),
56+
expectedResponse: Response{Body: `{"field": "page1"}`, Status: 200},
57+
expectedOK: true,
58+
},
59+
"with query params - page 2": {
60+
mock: Mock{
61+
MatchRequest: MatchRequest{
62+
Method: "GET",
63+
Path: "/user",
64+
Query: "page=2",
65+
},
66+
Response: Response{
67+
Status: 200,
68+
Body: `{"field": "page1"}`,
69+
},
70+
},
71+
request: httptest.NewRequest(http.MethodGet, "/user?page=2", nil),
72+
expectedResponse: Response{Body: `{"field": "page2"}`, Status: 200},
73+
expectedOK: true,
74+
},
4275
}
4376
for name, test := range tests {
4477
t.Run(name, func(t *testing.T) {

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
99
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1010
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
1111
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
12-
github.com/steinfletcher/apitest v1.3.13 h1:E0BAXde9dke8jEjK1hqTGvmI20vyyfC+xSdE9nmTc84=
13-
github.com/steinfletcher/apitest v1.3.13/go.mod h1:pCHKMM2TcH1pezw/xbmilaCdK9/dGsoCZBafwaqJ2sY=
1412
github.com/steinfletcher/apitest v1.4.4 h1:ZT/Wa3J615x7mBuarTf92o43AseYfHEFsLhnl1dBkl4=
1513
github.com/steinfletcher/apitest v1.4.4/go.mod h1:yaYc9GDlj4fa0qUUDywqAmrELlNbDrBwd36Zgo5hMZo=
1614
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
18-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
1915
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
2016
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
2117
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=

proxy/proxy_test.go

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,72 @@ func TestProxy_MocksEnabled_ProxyBackend_Success(t *testing.T) {
204204
}
205205

206206
func TestProxy_MocksEnabled_MockBackend_Success(t *testing.T) {
207-
newApiTest(config(), "http://test-backend", true).
207+
conf := config()
208+
conf.Routes = []domain.Route{
209+
{
210+
Type: "mock",
211+
Mock: &domain.Mock{
212+
MatchRequest: domain.MatchRequest{
213+
Method: "GET",
214+
Path: "^/api/users/.*",
215+
Query: "c=3",
216+
},
217+
Response: domain.Response{
218+
Status: 200,
219+
Body: `{"name": "bob"}`,
220+
},
221+
},
222+
},
223+
{
224+
Type: "mock",
225+
Mock: &domain.Mock{
226+
MatchRequest: domain.MatchRequest{
227+
Method: "GET",
228+
Path: "^/api/users/.*",
229+
Query: "a=1&b=2",
230+
},
231+
Response: domain.Response{
232+
Status: 200,
233+
Body: `{"name": "jon"}`,
234+
},
235+
},
236+
},
237+
}
238+
239+
newApiTest(conf, "http://test-backend", true).
240+
Intercept(func(request *http.Request) {
241+
request.URL.RawQuery = "a=1&b=2"
242+
}).
243+
Get("/api/users/info").
244+
Expect(t).
245+
Status(http.StatusOK).
246+
Body(`{"name": "jon"}`).
247+
End()
248+
249+
newApiTest(conf, "http://test-backend", true).
250+
Intercept(func(request *http.Request) {
251+
request.URL.RawQuery = "b=2&a=1"
252+
}).
253+
Get("/api/users/info").
254+
Expect(t).
255+
Status(http.StatusOK).
256+
Body(`{"name": "jon"}`).
257+
End()
258+
259+
newApiTest(conf, "http://test-backend", true).
260+
Intercept(func(request *http.Request) {
261+
request.URL.RawQuery = "c=3"
262+
}).
208263
Get("/api/users/info").
209-
Query("include", "user_id").
210264
Expect(t).
211265
Status(http.StatusOK).
212-
Body(`{"user_id": "123456"}`).
266+
Body(`{"name": "bob"}`).
267+
End()
268+
269+
newApiTest(conf, "http://test-backend", true).
270+
Get("/api/users/info").
271+
Expect(t).
272+
Status(http.StatusBadGateway).
213273
End()
214274
}
215275

0 commit comments

Comments
 (0)