@mswjs/interceptors 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/interceptors/ClientRequest/http.request.js +1 -1
- package/lib/interceptors/ClientRequest/http.request.js.map +1 -1
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js +14 -0
- package/lib/interceptors/ClientRequest/utils/normalizeClientRequestArgs.js.map +1 -1
- package/package.json +1 -1
- package/src/interceptors/ClientRequest/http.request.ts +1 -1
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +43 -7
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +29 -2
|
@@ -32,7 +32,7 @@ function request(protocol, resolver, observer) {
|
|
|
32
32
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
33
33
|
args[_i] = arguments[_i];
|
|
34
34
|
}
|
|
35
|
-
log('
|
|
35
|
+
log('request call (protocol "%s"):', protocol, args);
|
|
36
36
|
var clientRequestArgs = normalizeClientRequestArgs_1.normalizeClientRequestArgs.apply(void 0, __spreadArray([protocol + ":"], __read(args)));
|
|
37
37
|
return new NodeClientRequest_1.NodeClientRequest(clientRequestArgs, {
|
|
38
38
|
observer: observer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.request.js","sourceRoot":"","sources":["../../../src/interceptors/ClientRequest/http.request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA6B;AAG7B,yDAAiE;AACjE,iFAG2C;AAE3C,IAAM,GAAG,GAAG,aAAK,CAAC,cAAc,CAAC,CAAA;AAEjC,SAAgB,OAAO,CACrB,QAAkB,EAClB,QAAkB,EAClB,QAAkB;IAElB,OAAO;QAAC,cAA0B;aAA1B,UAA0B,EAA1B,qBAA0B,EAA1B,IAA0B;YAA1B,yBAA0B;;QAChC,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"http.request.js","sourceRoot":"","sources":["../../../src/interceptors/ClientRequest/http.request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA6B;AAG7B,yDAAiE;AACjE,iFAG2C;AAE3C,IAAM,GAAG,GAAG,aAAK,CAAC,cAAc,CAAC,CAAA;AAEjC,SAAgB,OAAO,CACrB,QAAkB,EAClB,QAAkB,EAClB,QAAkB;IAElB,OAAO;QAAC,cAA0B;aAA1B,UAA0B,EAA1B,qBAA0B,EAA1B,IAA0B;YAA1B,yBAA0B;;QAChC,GAAG,CAAC,+BAA+B,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAA;QAEpD,IAAM,iBAAiB,GAAG,uDAA0B,8BAC/C,QAAQ,MAAG,UACX,IAAI,GACR,CAAA;QACD,OAAO,IAAI,qCAAiB,CAAC,iBAAiB,EAAE;YAC9C,QAAQ,UAAA;YACR,QAAQ,UAAA;SACT,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC;AAjBD,0BAiBC"}
|
|
@@ -160,8 +160,22 @@ function normalizeClientRequestArgs(defaultProtocol) {
|
|
|
160
160
|
options.agent = agent;
|
|
161
161
|
log('resolved fallback agent:', agent);
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Ensure that the default Agent is always set.
|
|
165
|
+
* This prevents the protocol mismatch for requests with { agent: false },
|
|
166
|
+
* where the global Agent is inferred.
|
|
167
|
+
* @see https://github.com/mswjs/msw/issues/1150
|
|
168
|
+
* @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L130
|
|
169
|
+
* @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L157-L159
|
|
170
|
+
*/
|
|
171
|
+
if (!options._defaultAgent) {
|
|
172
|
+
log('has no default agent, setting the default agent for "%s"', options.protocol);
|
|
173
|
+
options._defaultAgent =
|
|
174
|
+
options.protocol === 'https:' ? https_1.globalAgent : http_1.globalAgent;
|
|
175
|
+
}
|
|
163
176
|
log('successfully resolved url:', url.href);
|
|
164
177
|
log('successfully resolved options:', options);
|
|
178
|
+
log('successfully resolved callback:', callback);
|
|
165
179
|
return [url, options, callback];
|
|
166
180
|
}
|
|
167
181
|
exports.normalizeClientRequestArgs = normalizeClientRequestArgs;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeClientRequestArgs.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA6B;AAC7B,
|
|
1
|
+
{"version":3,"file":"normalizeClientRequestArgs.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA6B;AAC7B,6BAIa;AACb,+BAIc;AAEd,gFAA8E;AAC9E,gFAG8C;AAC9C,0DAAwD;AACxD,oDAAkD;AAElD,IAAM,GAAG,GAAG,aAAK,CAAC,iCAAiC,CAAC,CAAA;AASpD,SAAS,qBAAqB,CAC5B,IAAuB,EACvB,GAAQ;IAER,uDAAuD;IACvD,4CAA4C;IAC5C,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,WAAW,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE;QACnE,GAAG,CAAC,qDAAqD,EAAE,GAAG,CAAC,CAAA;QAC/D,OAAO,+CAAsB,CAAC,GAAG,CAAC,CAAA;KACnC;IAED,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE;QACX,GAAG,CAAC,4BAA4B,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;QAC1C,IAAM,qBAAqB,GAAG,+CAAsB,CAAC,GAAG,CAAC,CAAA;QAEzD,GAAG,CAAC,sCAAsC,EAAE,qBAAqB,CAAC,CAAA;QAElE;;;;WAIG;QACH,GAAG,CAAC,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KAAK,GACT,OAAO,CAAC,QAAQ,KAAK,QAAQ;YAC3B,CAAC,CAAC,IAAI,aAAU,CAAC;gBACb,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;aAC/C,CAAC;YACJ,CAAC,CAAC,IAAI,YAAS,EAAE,CAAA;QAErB,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;QACrB,GAAG,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAA;KACvC;IAED;;;;;;;OAOG;IACH,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE;QAC1B,GAAG,CACD,0DAA0D,EAC1D,OAAO,CAAC,QAAQ,CACjB,CAAA;QAED,OAAO,CAAC,aAAa;YACnB,OAAO,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,mBAAgB,CAAC,CAAC,CAAC,kBAAe,CAAA;KACrE;IAED,GAAG,CAAC,4BAA4B,EAAE,GAAG,CAAC,IAAI,CAAC,CAAA;IAC3C,GAAG,CAAC,gCAAgC,EAAE,OAAO,CAAC,CAAA;IAC9C,GAAG,CAAC,iCAAiC,EAAE,QAAQ,CAAC,CAAA;IAEhD,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAA;AACjC,CAAC;AAtJD,gEAsJC"}
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@ export function request(
|
|
|
15
15
|
observer: Observer
|
|
16
16
|
) {
|
|
17
17
|
return (...args: ClientRequestArgs): ClientRequest => {
|
|
18
|
-
log('
|
|
18
|
+
log('request call (protocol "%s"):', protocol, args)
|
|
19
19
|
|
|
20
20
|
const clientRequestArgs = normalizeClientRequestArgs(
|
|
21
21
|
`${protocol}:`,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parse } from 'url'
|
|
2
|
-
import {
|
|
2
|
+
import { globalAgent as httpGlobalAgent, RequestOptions } from 'http'
|
|
3
|
+
import { Agent as HttpsAgent, globalAgent as httpsGlobalAgent } from 'https'
|
|
3
4
|
import { getUrlByRequestOptions } from '../../../utils/getUrlByRequestOptions'
|
|
4
5
|
import { normalizeClientRequestArgs } from './normalizeClientRequestArgs'
|
|
5
6
|
|
|
@@ -192,8 +193,9 @@ test('handles [URL, RequestOptions, callback] input', () => {
|
|
|
192
193
|
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
193
194
|
|
|
194
195
|
// Options must be preserved.
|
|
195
|
-
expect(options).toEqual({
|
|
196
|
+
expect(options).toEqual<RequestOptions>({
|
|
196
197
|
agent: false,
|
|
198
|
+
_defaultAgent: httpsGlobalAgent,
|
|
197
199
|
protocol: url.protocol,
|
|
198
200
|
method: 'GET',
|
|
199
201
|
headers: {
|
|
@@ -294,12 +296,11 @@ test('sets fallback Agent based on the URL protocol', () => {
|
|
|
294
296
|
'https:',
|
|
295
297
|
'https://github.com'
|
|
296
298
|
)
|
|
299
|
+
const agent = options.agent as HttpsAgent
|
|
297
300
|
|
|
298
|
-
expect(
|
|
299
|
-
expect(
|
|
300
|
-
|
|
301
|
-
options.agent.protocol
|
|
302
|
-
).toEqual(url.protocol)
|
|
301
|
+
expect(agent).toBeInstanceOf(HttpsAgent)
|
|
302
|
+
expect(agent).toHaveProperty('defaultPort', 443)
|
|
303
|
+
expect(agent).toHaveProperty('protocol', url.protocol)
|
|
303
304
|
})
|
|
304
305
|
|
|
305
306
|
test('does not set any fallback Agent given "agent: false" option', () => {
|
|
@@ -312,6 +313,41 @@ test('does not set any fallback Agent given "agent: false" option', () => {
|
|
|
312
313
|
expect(options.agent).toEqual(false)
|
|
313
314
|
})
|
|
314
315
|
|
|
316
|
+
test('sets the default Agent for HTTP request', () => {
|
|
317
|
+
const [, options] = normalizeClientRequestArgs(
|
|
318
|
+
'http:',
|
|
319
|
+
'http://github.com',
|
|
320
|
+
{}
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
expect(options._defaultAgent).toEqual(httpGlobalAgent)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
test('sets the default Agent for HTTPS request', () => {
|
|
327
|
+
const [, options] = normalizeClientRequestArgs(
|
|
328
|
+
'https:',
|
|
329
|
+
'https://github.com',
|
|
330
|
+
{}
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
expect(options._defaultAgent).toEqual(httpsGlobalAgent)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('preserves a custom default Agent when set', () => {
|
|
337
|
+
const [, options] = normalizeClientRequestArgs(
|
|
338
|
+
'https:',
|
|
339
|
+
'https://github.com',
|
|
340
|
+
{
|
|
341
|
+
/**
|
|
342
|
+
* @note Intentionally incorrect Agent for HTTPS request.
|
|
343
|
+
*/
|
|
344
|
+
_defaultAgent: httpGlobalAgent,
|
|
345
|
+
}
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
expect(options._defaultAgent).toEqual(httpGlobalAgent)
|
|
349
|
+
})
|
|
350
|
+
|
|
315
351
|
test('merges URL-based RequestOptions with the custom RequestOptions', () => {
|
|
316
352
|
const [url, options] = normalizeClientRequestArgs(
|
|
317
353
|
'https:',
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { debug } from 'debug'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
Agent as HttpAgent,
|
|
4
|
+
globalAgent as httpGlobalAgent,
|
|
5
|
+
IncomingMessage,
|
|
6
|
+
} from 'http'
|
|
7
|
+
import {
|
|
8
|
+
RequestOptions,
|
|
9
|
+
Agent as HttpsAgent,
|
|
10
|
+
globalAgent as httpsGlobalAgent,
|
|
11
|
+
} from 'https'
|
|
4
12
|
import { Url as LegacyURL } from 'url'
|
|
5
13
|
import { getRequestOptionsByUrl } from '../../../utils/getRequestOptionsByUrl'
|
|
6
14
|
import {
|
|
@@ -198,8 +206,27 @@ export function normalizeClientRequestArgs(
|
|
|
198
206
|
log('resolved fallback agent:', agent)
|
|
199
207
|
}
|
|
200
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Ensure that the default Agent is always set.
|
|
211
|
+
* This prevents the protocol mismatch for requests with { agent: false },
|
|
212
|
+
* where the global Agent is inferred.
|
|
213
|
+
* @see https://github.com/mswjs/msw/issues/1150
|
|
214
|
+
* @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L130
|
|
215
|
+
* @see https://github.com/nodejs/node/blob/418ff70b810f0e7112d48baaa72932a56cfa213b/lib/_http_client.js#L157-L159
|
|
216
|
+
*/
|
|
217
|
+
if (!options._defaultAgent) {
|
|
218
|
+
log(
|
|
219
|
+
'has no default agent, setting the default agent for "%s"',
|
|
220
|
+
options.protocol
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
options._defaultAgent =
|
|
224
|
+
options.protocol === 'https:' ? httpsGlobalAgent : httpGlobalAgent
|
|
225
|
+
}
|
|
226
|
+
|
|
201
227
|
log('successfully resolved url:', url.href)
|
|
202
228
|
log('successfully resolved options:', options)
|
|
229
|
+
log('successfully resolved callback:', callback)
|
|
203
230
|
|
|
204
231
|
return [url, options, callback]
|
|
205
232
|
}
|