@@ -8,10 +8,8 @@ import {
8
8
getQuery ,
9
9
normalizeUrl ,
10
10
} from '../lib/request-utils.js' ;
11
- import { debug } from '../lib/debug.js' ;
12
11
13
12
const debuggableUrlFunc = ( func ) => ( url ) => {
14
- debug ( 'Actual url:' , url ) ;
15
13
return func ( url ) ;
16
14
} ;
17
15
@@ -35,46 +33,32 @@ const stringMatchers = {
35
33
} ;
36
34
37
35
const getHeaderMatcher = ( { headers : expectedHeaders } ) => {
38
- debug ( 'Generating header matcher' ) ;
39
36
if ( ! expectedHeaders ) {
40
- debug ( ' No header expectations defined - skipping' ) ;
41
37
return ;
42
38
}
43
39
const expectation = headerUtils . toLowerCase ( expectedHeaders ) ;
44
- debug ( ' Expected headers:' , expectation ) ;
45
40
return ( url , { headers = { } } ) => {
46
- debug ( 'Attempting to match headers' ) ;
47
41
const lowerCaseHeaders = headerUtils . toLowerCase (
48
42
headerUtils . normalize ( headers ) ,
49
43
) ;
50
- debug ( ' Expected headers:' , expectation ) ;
51
- debug ( ' Actual headers:' , lowerCaseHeaders ) ;
52
44
return Object . keys ( expectation ) . every ( ( headerName ) =>
53
45
headerUtils . equal ( lowerCaseHeaders [ headerName ] , expectation [ headerName ] ) ,
54
46
) ;
55
47
} ;
56
48
} ;
57
49
58
50
const getMethodMatcher = ( { method : expectedMethod } ) => {
59
- debug ( 'Generating method matcher' ) ;
60
51
if ( ! expectedMethod ) {
61
- debug ( ' No method expectations defined - skipping' ) ;
62
52
return ;
63
53
}
64
- debug ( ' Expected method:' , expectedMethod ) ;
65
54
return ( url , { method } ) => {
66
- debug ( 'Attempting to match method' ) ;
67
55
const actualMethod = method ? method . toLowerCase ( ) : 'get' ;
68
- debug ( ' Expected method:' , expectedMethod ) ;
69
- debug ( ' Actual method:' , actualMethod ) ;
70
56
return expectedMethod === actualMethod ;
71
57
} ;
72
58
} ;
73
59
74
60
const getQueryStringMatcher = ( { query : passedQuery } ) => {
75
- debug ( 'Generating query parameters matcher' ) ;
76
61
if ( ! passedQuery ) {
77
- debug ( ' No query parameters expectations defined - skipping' ) ;
78
62
return ;
79
63
}
80
64
@@ -101,14 +85,8 @@ const getQueryStringMatcher = ({ query: passedQuery }) => {
101
85
102
86
const keys = Array . from ( expectedQuery . keys ( ) ) ;
103
87
return ( url ) => {
104
- debug ( 'Attempting to match query parameters' ) ;
105
88
const queryString = getQuery ( url ) ;
106
89
const query = new URLSearchParams ( queryString ) ;
107
- debug (
108
- ' Expected query parameters:' ,
109
- Object . fromEntries ( expectedQuery . entries ( ) ) ,
110
- ) ;
111
- debug ( ' Actual query parameters:' , Object . fromEntries ( query . entries ( ) ) ) ;
112
90
113
91
return keys . every ( ( key ) => {
114
92
const expectedValues = expectedQuery . getAll ( key ) . sort ( ) ;
@@ -130,30 +108,24 @@ const getQueryStringMatcher = ({ query: passedQuery }) => {
130
108
} ;
131
109
132
110
const getParamsMatcher = ( { params : expectedParams , url : matcherUrl } ) => {
133
- debug ( 'Generating path parameters matcher' ) ;
134
111
if ( ! expectedParams ) {
135
- debug ( ' No path parameters expectations defined - skipping' ) ;
136
112
return ;
137
113
}
138
114
if ( ! / e x p r e s s : / . test ( matcherUrl ) ) {
139
115
throw new Error (
140
116
'fetch-mock: matching on params is only possible when using an express: matcher' ,
141
117
) ;
142
118
}
143
- debug ( ' Expected path parameters:' , expectedParams ) ;
144
119
const expectedKeys = Object . keys ( expectedParams ) ;
145
120
const re = regexparam . parse ( matcherUrl . replace ( / ^ e x p r e s s : / , '' ) ) ;
146
121
return ( url ) => {
147
- debug ( 'Attempting to match path parameters' ) ;
148
122
const vals = re . pattern . exec ( getPath ( url ) ) || [ ] ;
149
123
vals . shift ( ) ;
150
124
const params = re . keys . reduce (
151
125
( map , paramName , i ) =>
152
126
vals [ i ] ? Object . assign ( map , { [ paramName ] : vals [ i ] } ) : map ,
153
127
{ } ,
154
128
) ;
155
- debug ( ' Expected path parameters:' , expectedParams ) ;
156
- debug ( ' Actual path parameters:' , params ) ;
157
129
return expectedKeys . every ( ( key ) => params [ key ] === expectedParams [ key ] ) ;
158
130
} ;
159
131
} ;
@@ -162,28 +134,17 @@ const getBodyMatcher = (route, fetchMock) => {
162
134
const matchPartialBody = fetchMock . getOption ( 'matchPartialBody' , route ) ;
163
135
const { body : expectedBody } = route ;
164
136
165
- debug ( 'Generating body matcher' ) ;
166
137
return ( url , { body, method = 'get' } ) => {
167
- debug ( 'Attempting to match body' ) ;
168
138
if ( method . toLowerCase ( ) === 'get' ) {
169
- debug ( ' GET request - skip matching body' ) ;
170
139
// GET requests don’t send a body so the body matcher should be ignored for them
171
140
return true ;
172
141
}
173
142
174
143
let sentBody ;
175
144
176
145
try {
177
- debug ( ' Parsing request body as JSON' ) ;
178
146
sentBody = JSON . parse ( body ) ;
179
- } catch ( err ) {
180
- debug ( ' Failed to parse request body as JSON' , err ) ;
181
- }
182
- debug ( 'Expected body:' , expectedBody ) ;
183
- debug ( 'Actual body:' , sentBody ) ;
184
- if ( matchPartialBody ) {
185
- debug ( 'matchPartialBody is true - checking for partial match only' ) ;
186
- }
147
+ } catch { } // eslint-disable-line no-empty
187
148
188
149
return (
189
150
sentBody &&
@@ -199,55 +160,42 @@ const getFullUrlMatcher = (route, matcherUrl, query) => {
199
160
// but we have to be careful to normalize the url we check and the name
200
161
// of the route to allow for e.g. http://it.at.there being indistinguishable
201
162
// from http://it.at.there/ once we start generating Request/Url objects
202
- debug ( ' Matching using full url' , matcherUrl ) ;
203
163
const expectedUrl = normalizeUrl ( matcherUrl ) ;
204
- debug ( ' Normalised url to:' , matcherUrl ) ;
205
164
if ( route . identifier === matcherUrl ) {
206
- debug ( ' Updating route identifier to match normalized url:' , matcherUrl ) ;
207
165
route . identifier = expectedUrl ;
208
166
}
209
167
210
168
return ( matcherUrl ) => {
211
- debug ( 'Expected url:' , expectedUrl ) ;
212
- debug ( 'Actual url:' , matcherUrl ) ;
213
169
if ( query && expectedUrl . indexOf ( '?' ) ) {
214
- debug ( 'Ignoring query string when matching url' ) ;
215
170
return matcherUrl . indexOf ( expectedUrl ) === 0 ;
216
171
}
217
172
return normalizeUrl ( matcherUrl ) === expectedUrl ;
218
173
} ;
219
174
} ;
220
175
221
176
const getFunctionMatcher = ( { functionMatcher } ) => {
222
- debug ( 'Detected user defined function matcher' , functionMatcher ) ;
223
177
return ( ...args ) => {
224
- debug ( 'Calling function matcher with arguments' , args ) ;
225
178
return functionMatcher ( ...args ) ;
226
179
} ;
227
180
} ;
228
181
229
182
const getUrlMatcher = ( route ) => {
230
- debug ( 'Generating url matcher' ) ;
231
183
const { url : matcherUrl , query } = route ;
232
184
233
185
if ( matcherUrl === '*' ) {
234
- debug ( ' Using universal * rule to match any url' ) ;
235
186
return ( ) => true ;
236
187
}
237
188
238
189
if ( matcherUrl instanceof RegExp ) {
239
- debug ( ' Using regular expression to match url:' , matcherUrl ) ;
240
190
return ( url ) => matcherUrl . test ( url ) ;
241
191
}
242
192
243
193
if ( matcherUrl . href ) {
244
- debug ( ' Using URL object to match url' , matcherUrl ) ;
245
194
return getFullUrlMatcher ( route , matcherUrl . href , query ) ;
246
195
}
247
196
248
197
for ( const shorthand in stringMatchers ) {
249
198
if ( matcherUrl . indexOf ( `${ shorthand } :` ) === 0 ) {
250
- debug ( ` Using ${ shorthand } : pattern to match url` , matcherUrl ) ;
251
199
const urlFragment = matcherUrl . replace ( new RegExp ( `^${ shorthand } :` ) , '' ) ;
252
200
return stringMatchers [ shorthand ] ( urlFragment ) ;
253
201
}
0 commit comments