Skip to content

Commit 6987327

Browse files
author
Sam A. Horvath-Hunt
authored
fix(typescript): support nock(new URL('https://example.test/'))` (#2526)
1 parent feaa66f commit 6987327

42 files changed

Lines changed: 316 additions & 311 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ nock.enableNetConnect(/(amazon|github)\.com/)
11421142

11431143
// Or a Function
11441144
nock.enableNetConnect(
1145-
host => host.includes('amazon.com') || host.includes('github.com')
1145+
host => host.includes('amazon.com') || host.includes('github.com'),
11461146
)
11471147

11481148
http.get('http://www.amazon.com/')
@@ -1248,7 +1248,7 @@ nocks.forEach(function (nock) {
12481248
/(timestamp):([0-9]+)/g,
12491249
function (match, key, value) {
12501250
return key + ':' + recordedTimestamp
1251-
}
1251+
},
12521252
)
12531253
} else {
12541254
return body
@@ -1467,7 +1467,7 @@ function prepareScope(scope) {
14671467
const recordedTimestamp = recordedBodyResult[1]
14681468
return body.replace(
14691469
/(timestamp):([0-9]+)/g,
1470-
(match, key, value) => `${key}:${recordedTimestamp}`
1470+
(match, key, value) => `${key}:${recordedTimestamp}`,
14711471
)
14721472
} else {
14731473
return body

examples/binary-reply.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const nock = require('../')
66

77
const readFile = function () {
88
return fs.readFileSync(
9-
path.resolve(__dirname, '../tests/assets/reply_file_2.txt.gz')
9+
path.resolve(__dirname, '../tests/assets/reply_file_2.txt.gz'),
1010
)
1111
}
1212

@@ -29,7 +29,7 @@ http.get('http://binary.com/binary', function (res) {
2929
console.log(
3030
Buffer.compare(readFile(), buffer) === 0
3131
? 'Received the file.'
32-
: 'Received something else.'
32+
: 'Received something else.',
3333
)
3434
})
3535
})

lib/back.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function Back(fixtureName, options, nockedFn) {
4949
throw new Error(
5050
'Back requires nock.back.fixtures to be set\n' +
5151
'Ex:\n' +
52-
"\trequire(nock).back.fixtures = '/path/to/fixtures/'"
52+
"\trequire(nock).back.fixtures = '/path/to/fixtures/'",
5353
)
5454
}
5555

@@ -300,8 +300,8 @@ function assertScopes(scopes, fixture) {
300300
format(
301301
'%j was not used, consider removing %s to rerecord fixture',
302302
[].concat(...pending),
303-
fixture
304-
)
303+
fixture,
304+
),
305305
)
306306
}
307307
}

lib/common.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function overrideRequests(newRequest) {
8181

8282
if (requestOverrides[moduleName]) {
8383
throw new Error(
84-
`Module's request already overridden for ${moduleName} protocol.`
84+
`Module's request already overridden for ${moduleName} protocol.`,
8585
)
8686
}
8787

@@ -126,7 +126,7 @@ function restoreOverriddenRequests() {
126126
module.request = request
127127
module.get = get
128128
debug('- restored request for', proto)
129-
}
129+
},
130130
)
131131
requestOverrides = {}
132132
}
@@ -206,11 +206,11 @@ function headersFieldNamesToLowerCase(headers, throwOnDuplicate) {
206206
if (lowerCaseHeaders[key] !== undefined) {
207207
if (throwOnDuplicate) {
208208
throw Error(
209-
`Failed to convert header keys to lower case due to field name conflict: ${key}`
209+
`Failed to convert header keys to lower case due to field name conflict: ${key}`,
210210
)
211211
} else {
212212
debug(
213-
`Duplicate header provided in request: ${key}. Only the last value can be matched.`
213+
`Duplicate header provided in request: ${key}. Only the last value can be matched.`,
214214
)
215215
}
216216
}
@@ -244,7 +244,7 @@ function headersInputToRawArray(headers) {
244244
// but throw an error if there aren't an even number of items in the array
245245
if (headers.length % 2) {
246246
throw new Error(
247-
`Raw headers must be provided as an array with an even number of items. [fieldName, value, ...]`
247+
`Raw headers must be provided as an array with an even number of items. [fieldName, value, ...]`,
248248
)
249249
}
250250
return [...headers]
@@ -260,7 +260,7 @@ function headersInputToRawArray(headers) {
260260
}
261261

262262
throw new Error(
263-
`Headers must be provided as an array of raw values, a Map, or a plain Object. ${headers}`
263+
`Headers must be provided as an array of raw values, a Map, or a plain Object. ${headers}`,
264264
)
265265
}
266266

@@ -600,7 +600,7 @@ function deepEqual(expected, actual) {
600600

601601
if (isPlainObject(expected) && isPlainObject(actual)) {
602602
const allKeys = Array.from(
603-
new Set(Object.keys(expected).concat(Object.keys(actual)))
603+
new Set(Object.keys(expected).concat(Object.keys(actual))),
604604
)
605605

606606
return allKeys.every(key => deepEqual(expected[key], actual[key]))

lib/intercept.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function interceptorsFor(options) {
151151

152152
// First try to use filteringScope if any of the interceptors has it defined.
153153
for (const { key, interceptors, allowUnmocked } of Object.values(
154-
allInterceptors
154+
allInterceptors,
155155
)) {
156156
for (const interceptor of interceptors) {
157157
const { filteringScope } = interceptor.__nock_scopeOptions
@@ -185,7 +185,7 @@ function interceptorsFor(options) {
185185
debug(
186186
`matched base path (${interceptors.length} interceptor${
187187
interceptors.length > 1 ? 's' : ''
188-
})`
188+
})`,
189189
)
190190
return interceptors
191191
}
@@ -243,7 +243,7 @@ function ErroringClientRequest(error) {
243243
process.nextTick(
244244
function () {
245245
this.emit('error', error)
246-
}.bind(this)
246+
}.bind(this),
247247
)
248248
}
249249

@@ -272,7 +272,7 @@ function overrideClientRequest() {
272272
// https://github.com/nock/nock/pull/1386
273273
// https://github.com/nock/nock/pull/1440
274274
throw Error(
275-
'Creating a ClientRequest with empty `options` is not supported in Nock'
275+
'Creating a ClientRequest with empty `options` is not supported in Nock',
276276
)
277277
}
278278

@@ -308,10 +308,10 @@ function overrideClientRequest() {
308308
function () {
309309
const error = new NetConnectNotAllowedError(
310310
options.host,
311-
options.path
311+
options.path,
312312
)
313313
this.emit('error', error)
314-
}.bind(this)
314+
}.bind(this),
315315
)
316316
}
317317
}
@@ -348,7 +348,7 @@ function isActive() {
348348

349349
function interceptorScopes() {
350350
const nestedInterceptors = Object.values(allInterceptors).map(
351-
i => i.interceptors
351+
i => i.interceptors,
352352
)
353353
return [].concat(...nestedInterceptors).map(i => i.scope)
354354
}
@@ -389,7 +389,7 @@ function activate() {
389389
// https://github.com/nock/nock/pull/1386
390390
// https://github.com/nock/nock/pull/1440
391391
throw Error(
392-
'Making a request with empty `options` is not supported in Nock'
392+
'Making a request with empty `options` is not supported in Nock',
393393
)
394394
}
395395

@@ -402,10 +402,10 @@ function activate() {
402402

403403
if (isOn() && interceptors) {
404404
const matches = interceptors.some(interceptor =>
405-
interceptor.matchOrigin(options)
405+
interceptor.matchOrigin(options),
406406
)
407407
const allowUnmocked = interceptors.some(
408-
interceptor => interceptor.options.allowUnmocked
408+
interceptor => interceptor.options.allowUnmocked,
409409
)
410410

411411
if (!matches && allowUnmocked) {

lib/intercepted_request_router.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class InterceptedRequestRouter {
4242
// We use lower-case header field names throughout Nock.
4343
headers: common.headersFieldNamesToLowerCase(
4444
options.headers || {},
45-
false
45+
false,
4646
),
4747
}
4848
this.interceptors = interceptors
@@ -89,7 +89,7 @@ class InterceptedRequestRouter {
8989
req.setHeader(
9090
// We use lower-case header field names throughout Nock.
9191
'authorization',
92-
`Basic ${Buffer.from(options.auth).toString('base64')}`
92+
`Basic ${Buffer.from(options.auth).toString('base64')}`,
9393
)
9494
}
9595

@@ -297,16 +297,16 @@ class InterceptedRequestRouter {
297297
const requestBodyIsUtf8Representable =
298298
common.isUtf8Representable(requestBodyBuffer)
299299
const requestBodyString = requestBodyBuffer.toString(
300-
requestBodyIsUtf8Representable ? 'utf8' : 'hex'
300+
requestBodyIsUtf8Representable ? 'utf8' : 'hex',
301301
)
302302

303303
const matchedInterceptor = interceptors.find(i =>
304-
i.match(req, options, requestBodyString)
304+
i.match(req, options, requestBodyString),
305305
)
306306

307307
if (matchedInterceptor) {
308308
matchedInterceptor.scope.logger(
309-
'interceptor identified, starting mocking'
309+
'interceptor identified, starting mocking',
310310
)
311311

312312
matchedInterceptor.markConsumed()
@@ -329,7 +329,7 @@ class InterceptedRequestRouter {
329329

330330
// Try to find a hostname match that allows unmocked.
331331
const allowUnmocked = interceptors.some(
332-
i => i.matchHostName(options) && i.options.allowUnmocked
332+
i => i.matchHostName(options) && i.options.allowUnmocked,
333333
)
334334

335335
if (allowUnmocked && req instanceof ClientRequest) {

lib/interceptor.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ module.exports = class Interceptor {
3939
!uri.startsWith('*')
4040
) {
4141
throw Error(
42-
`Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: ${uri})`
42+
`Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: ${uri})`,
4343
)
4444
}
4545

4646
if (!method) {
4747
throw new Error(
48-
'The "method" parameter is required for an intercept call.'
48+
'The "method" parameter is required for an intercept call.',
4949
)
5050
}
5151

@@ -67,10 +67,10 @@ module.exports = class Interceptor {
6767
// We use lower-case header field names throughout Nock.
6868
this.reqheaders = common.headersFieldNamesToLowerCase(
6969
scope.scopeOptions.reqheaders || {},
70-
true
70+
true,
7171
)
7272
this.badheaders = common.headersFieldsArrayToLowerCase(
73-
scope.scopeOptions.badheaders || []
73+
scope.scopeOptions.badheaders || [],
7474
)
7575

7676
this.delayBodyInMs = 0
@@ -118,7 +118,7 @@ module.exports = class Interceptor {
118118
// It's not very Javascript-y to throw an error for extra args to a function, but because
119119
// of legacy behavior, this error was added to reduce confusion for those migrating.
120120
throw Error(
121-
'Invalid arguments. When providing a function for the first argument, .reply does not accept other arguments.'
121+
'Invalid arguments. When providing a function for the first argument, .reply does not accept other arguments.',
122122
)
123123
}
124124
this.statusCode = null
@@ -152,7 +152,7 @@ module.exports = class Interceptor {
152152
// Including all the default headers is safe for our purposes because of the specific headers we introspect.
153153
// A more thoughtful process is used to merge the default headers when the response headers are finally computed.
154154
this.headers = common.headersArrayToObject(
155-
this.rawHeaders.concat(this.scope._defaultReplyHeaders)
155+
this.rawHeaders.concat(this.scope._defaultReplyHeaders),
156156
)
157157

158158
// If the content is not encoded we may need to transform the response body.
@@ -236,7 +236,7 @@ module.exports = class Interceptor {
236236
"request header field doesn't match:",
237237
key,
238238
header,
239-
reqHeader
239+
reqHeader,
240240
)
241241
return false
242242
}
@@ -247,7 +247,7 @@ module.exports = class Interceptor {
247247
this.scope.logger(
248248
'attempting match %s, body = %s',
249249
stringify(options),
250-
stringify(body)
250+
stringify(body),
251251
)
252252
}
253253

@@ -259,7 +259,7 @@ module.exports = class Interceptor {
259259

260260
if (this.method !== method) {
261261
this.scope.logger(
262-
`Method did not match. Request ${method} Interceptor ${this.method}`
262+
`Method did not match. Request ${method} Interceptor ${this.method}`,
263263
)
264264
return false
265265
}
@@ -286,7 +286,7 @@ module.exports = class Interceptor {
286286
}
287287

288288
const reqHeadersMatch = Object.keys(this.reqheaders).every(key =>
289-
this.reqheaderMatches(options, key)
289+
this.reqheaderMatches(options, key),
290290
)
291291

292292
if (!reqHeadersMatch) {
@@ -299,13 +299,13 @@ module.exports = class Interceptor {
299299
!this.scope.scopeOptions.conditionally()
300300
) {
301301
this.scope.logger(
302-
'matching failed because Scope.conditionally() did not validate'
302+
'matching failed because Scope.conditionally() did not validate',
303303
)
304304
return false
305305
}
306306

307307
const badHeaders = this.badheaders.filter(
308-
header => header in options.headers
308+
header => header in options.headers,
309309
)
310310

311311
if (badHeaders.length) {
@@ -322,7 +322,7 @@ module.exports = class Interceptor {
322322
const matchQueries = this.matchQuery({ search })
323323

324324
this.scope.logger(
325-
matchQueries ? 'query matching succeeded' : 'query matching failed'
325+
matchQueries ? 'query matching succeeded' : 'query matching failed',
326326
)
327327

328328
if (!matchQueries) {
@@ -369,7 +369,7 @@ module.exports = class Interceptor {
369369
"bodies don't match: \n",
370370
this._requestBody,
371371
'\n',
372-
body
372+
body,
373373
)
374374
}
375375
}

lib/playback_interceptor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function parseFullReplyResult(response, fullReplyResult) {
2929

3030
if (fullReplyResult.length > 3) {
3131
throw Error(
32-
'The array returned from the .reply callback contains too many values'
32+
'The array returned from the .reply callback contains too many values',
3333
)
3434
}
3535

@@ -257,7 +257,7 @@ function playbackInterceptor({
257257
}
258258

259259
response.rawHeaders.push(
260-
...selectDefaultHeaders(response.rawHeaders, defaultHeaders)
260+
...selectDefaultHeaders(response.rawHeaders, defaultHeaders),
261261
)
262262

263263
// Evaluate functional headers.

0 commit comments

Comments
 (0)