@@ -87,6 +87,49 @@ func TestPath(t *testing.T) {
87
87
}
88
88
}
89
89
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
+
90
133
func TestMatchRequest (t * testing.T ) {
91
134
t .Parallel ()
92
135
@@ -173,6 +216,40 @@ func TestMatchHeader(t *testing.T) {
173
216
})
174
217
}
175
218
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
+
176
253
func TestMatchNoBody (t * testing.T ) {
177
254
t .Parallel ()
178
255
0 commit comments