File tree Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -37,17 +37,26 @@ export function matchRequest(
37
37
return true ;
38
38
}
39
39
40
+ // eslint-disable-next-line complexity
40
41
export function matchResponse (
41
42
verb : string ,
42
- path : string ,
43
+ path : string | RegExp ,
43
44
response : AxiosResponse ,
44
45
params ?: object ,
45
46
) {
46
- return (
47
- response . config . method === verb &&
48
- response . config . url === path &&
49
- hasSameParams ( response . config . params , params )
50
- ) ;
47
+ const { config } = response ;
48
+ if ( ! config . url ) return false ;
49
+ const samePath = matchPaths ( path , config . url ) ;
50
+ const requestURL = new $URL ( config . url ) ;
51
+
52
+ const sameMethod = config . method === verb ;
53
+
54
+ if ( ! sameMethod ) return false ;
55
+ if ( ! samePath ) return false ;
56
+
57
+ if ( params ) return hasSameParams ( params , config . params || requestURL . query ) ;
58
+
59
+ return true ;
51
60
}
52
61
53
62
export function ejectFromRequest ( axios : AxiosInstance , id : number ) {
Original file line number Diff line number Diff line change @@ -356,6 +356,25 @@ describe('axios-dev-proxy tests', () => {
356
356
expect ( consoleLog ) . toBeCalledTimes ( 2 ) ;
357
357
} ) ;
358
358
359
+ it ( 'should print response once with regex on match' , async ( ) => {
360
+ server
361
+ . get ( '/print-response' )
362
+ . reply ( 200 , { data : 1 } )
363
+ . get ( '/print-response' )
364
+ . reply ( 200 , { data : 2 } ) ;
365
+ const consoleLog = vi . spyOn ( console , 'log' ) ;
366
+ proxy . onGet ( / \/ p r i n t - \w + / ) . printResponseOnce ( ) ;
367
+
368
+ await api . get ( '/print-response' ) ;
369
+ await api . get ( '/print-response' ) ;
370
+
371
+ expect ( consoleLog ) . toHaveBeenNthCalledWith (
372
+ 2 ,
373
+ JSON . stringify ( { data : 1 } , null , 2 ) ,
374
+ ) ;
375
+ expect ( consoleLog ) . toBeCalledTimes ( 2 ) ;
376
+ } ) ;
377
+
359
378
it ( 'should change the request config' , async ( ) => {
360
379
const server2 = nock ( 'https://api-2.com.br' ) ;
361
380
server2 . get ( '/config' ) . reply ( 200 , { data : 2 } ) ;
You can’t perform that action at this time.
0 commit comments