Skip to content

Commit 7c7b875

Browse files
authored
Added match query string and url/path regex (#9)
* Added MatchQuery * Added match url and path with regex
1 parent b875674 commit 7c7b875

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

match.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"reflect"
11+
"regexp"
1112
"strings"
1213
)
1314

@@ -29,6 +30,18 @@ func Path(path string) URLMatcher {
2930
}
3031
}
3132

33+
// URLRegex will match http request when the regex pattern specified match to the request URL.
34+
func URLRegex(pattern string) URLMatcher {
35+
regex := regexp.MustCompile(pattern)
36+
return func(url *url.URL) bool { return regex.MatchString(url.String()) }
37+
}
38+
39+
// PathRegex will match http request when the regex pattern specified match to the request URL path part.
40+
func PathRegex(pattern string) URLMatcher {
41+
regex := regexp.MustCompile(pattern)
42+
return func(url *url.URL) bool { return regex.MatchString(url.Path) }
43+
}
44+
3245
func defaultMatchers(method string, url URLMatcher) []requestMatcherFunc {
3346
return []requestMatcherFunc{
3447
methodMatcher(method),
@@ -61,6 +74,15 @@ func MatchHeader(key, value string) StubMatcherRule {
6174
return MatchRequest(matcher)
6275
}
6376

77+
// MatchQuery sets a rule to match the http request with the given query string value.
78+
func MatchQuery(key, value string) StubMatcherRule {
79+
matcher := RequestMatcherFunc(func(r *http.Request) bool {
80+
return r.URL.Query().Get(key) == value
81+
})
82+
83+
return MatchRequest(matcher)
84+
}
85+
6486
// MatchNoBody sets a rule to match the http request with empty body.
6587
func MatchNoBody() StubMatcherRule {
6688
matcher := RequestMatcherFunc(func(r *http.Request) bool {

match_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,49 @@ func TestPath(t *testing.T) {
8787
}
8888
}
8989

90+
func TestURLRegex(t *testing.T) {
91+
t.Parallel()
92+
93+
reqURL := "/api/users?page=1&size=20"
94+
httpReq := httptest.NewRequest(http.MethodGet, reqURL, http.NoBody)
95+
96+
regexValues := []string{
97+
`\/api\/users\?page=1\&size=20`,
98+
`^\/api\/users\?page=1\&size=20$`,
99+
`^\/api\/users\?page=\d+\&size=\d+$`,
100+
`^\/api\/[a-zA-Z]+\?page=\d+\&size=\d+$`,
101+
}
102+
103+
for _, r := range regexValues {
104+
t.Run(r, func(t *testing.T) {
105+
t.Parallel()
106+
matcher := mockaso.URLRegex(r)
107+
assert.True(t, matcher(httpReq.URL))
108+
})
109+
}
110+
}
111+
112+
func TestPathRegex(t *testing.T) {
113+
t.Parallel()
114+
115+
reqURL := "/api/users?page=1&size=20"
116+
httpReq := httptest.NewRequest(http.MethodGet, reqURL, http.NoBody)
117+
118+
regexValues := []string{
119+
`\/api\/users`,
120+
`^\/api\/users$`,
121+
`^\/api\/[a-zA-Z]+`,
122+
}
123+
124+
for _, r := range regexValues {
125+
t.Run(r, func(t *testing.T) {
126+
t.Parallel()
127+
matcher := mockaso.PathRegex(r)
128+
assert.True(t, matcher(httpReq.URL))
129+
})
130+
}
131+
}
132+
90133
func TestMatchRequest(t *testing.T) {
91134
t.Parallel()
92135

@@ -173,6 +216,40 @@ func TestMatchHeader(t *testing.T) {
173216
})
174217
}
175218

219+
func TestMatchQuery(t *testing.T) {
220+
t.Parallel()
221+
222+
server := mockaso.MustStartNewServer(mockaso.WithLogger(t))
223+
t.Cleanup(server.MustShutdown)
224+
225+
const path = "/test/match-query"
226+
227+
server.Stub(http.MethodGet, mockaso.Path(path)).
228+
Match(mockaso.MatchQuery("name", "john")).
229+
Respond(matchedRequestRules()...)
230+
231+
t.Run("should return the specified stub when query match", func(t *testing.T) {
232+
t.Parallel()
233+
234+
httpReq, _ := http.NewRequest(http.MethodGet, path+"?name=john", http.NoBody)
235+
httpResp, err := server.Client().Do(httpReq)
236+
require.NoError(t, err)
237+
238+
assert.Equal(t, http.StatusOK, httpResp.StatusCode)
239+
assertBodyString(t, "matched request", httpResp)
240+
})
241+
242+
t.Run("should return no match response when query does not match", func(t *testing.T) {
243+
t.Parallel()
244+
245+
httpReq, _ := http.NewRequest(http.MethodGet, path+"?name=rick", http.NoBody)
246+
httpResp, err := server.Client().Do(httpReq)
247+
require.NoError(t, err)
248+
249+
assertNotMatchedResponse(t, httpReq, httpResp)
250+
})
251+
}
252+
176253
func TestMatchNoBody(t *testing.T) {
177254
t.Parallel()
178255

0 commit comments

Comments
 (0)