1 | 'use strict'
|
2 |
|
3 | const _ = require('lodash')
|
4 | const debug = require('debug')('nock.common')
|
5 | const url = require('url')
|
6 | const timers = require('timers')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function normalizeRequestOptions(options) {
|
14 | options.proto = options.proto || 'http'
|
15 | options.port = options.port || (options.proto === 'http' ? 80 : 443)
|
16 | if (options.host) {
|
17 | debug('options.host:', options.host)
|
18 | if (!options.hostname) {
|
19 | if (options.host.split(':').length === 2) {
|
20 | options.hostname = options.host.split(':')[0]
|
21 | } else {
|
22 | options.hostname = options.host
|
23 | }
|
24 | }
|
25 | }
|
26 | debug('options.hostname in the end: %j', options.hostname)
|
27 | options.host = `${options.hostname || 'localhost'}:${options.port}`
|
28 | debug('options.host in the end: %j', options.host)
|
29 |
|
30 |
|
31 | ;['hostname', 'host'].forEach(function(attr) {
|
32 | if (options[attr]) {
|
33 | options[attr] = options[attr].toLowerCase()
|
34 | }
|
35 | })
|
36 |
|
37 | return options
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function isUtf8Representable(buffer) {
|
48 | const utfEncodedBuffer = buffer.toString('utf8')
|
49 | const reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8')
|
50 | return reconstructedBuffer.equals(buffer)
|
51 | }
|
52 |
|
53 |
|
54 | let requestOverrides = {}
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function overrideRequests(newRequest) {
|
68 | debug('overriding requests')
|
69 | ;['http', 'https'].forEach(function(proto) {
|
70 | debug('- overriding request for', proto)
|
71 |
|
72 | const moduleName = proto
|
73 | const module = {
|
74 | http: require('http'),
|
75 | https: require('https'),
|
76 | }[moduleName]
|
77 | const overriddenRequest = module.request
|
78 | const overriddenGet = module.get
|
79 |
|
80 | if (requestOverrides[moduleName]) {
|
81 | throw new Error(
|
82 | `Module's request already overridden for ${moduleName} protocol.`
|
83 | )
|
84 | }
|
85 |
|
86 |
|
87 | requestOverrides[moduleName] = {
|
88 | module,
|
89 | request: overriddenRequest,
|
90 | get: overriddenGet,
|
91 | }
|
92 | // https://nodejs.org/api/http.html#http_http_request_url_options_callback
|
93 | module.request = function(input, options, callback) {
|
94 | return newRequest(proto, overriddenRequest.bind(module), [
|
95 | input,
|
96 | options,
|
97 | callback,
|
98 | ])
|
99 | }
|
100 |
|
101 | module.get = function(input, options, callback) {
|
102 | const req = newRequest(proto, overriddenGet.bind(module), [
|
103 | input,
|
104 | options,
|
105 | callback,
|
106 | ])
|
107 | req.end()
|
108 | return req
|
109 | }
|
110 |
|
111 | debug('- overridden request for', proto)
|
112 | })
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function restoreOverriddenRequests() {
|
120 | debug('restoring requests')
|
121 | Object.entries(requestOverrides).forEach(
|
122 | ([proto, { module, request, get }]) => {
|
123 | debug('- restoring request for', proto)
|
124 | module.request = request
|
125 | module.get = get
|
126 | debug('- restored request for', proto)
|
127 | }
|
128 | )
|
129 | requestOverrides = {}
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function normalizeOrigin(proto, host, port) {
|
137 | const hostHasPort = host.includes(':')
|
138 | const portIsStandard =
|
139 | (proto === 'http' && (port === 80 || port === '80')) ||
|
140 | (proto === 'https' && (port === 443 || port === '443'))
|
141 | const portStr = hostHasPort || portIsStandard ? '' : `:${port}`
|
142 |
|
143 | return `${proto}://${host}${portStr}`
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function stringifyRequest(options, body) {
|
159 | const { method = 'GET', path = '', port } = options
|
160 | const origin = normalizeOrigin(options.proto, options.hostname, port)
|
161 |
|
162 | const log = {
|
163 | method,
|
164 | url: `${origin}${path}`,
|
165 | headers: options.headers,
|
166 | }
|
167 |
|
168 | if (body) {
|
169 | log.body = body
|
170 | }
|
171 |
|
172 | return JSON.stringify(log, null, 2)
|
173 | }
|
174 |
|
175 | function isContentEncoded(headers) {
|
176 | const contentEncoding = headers['content-encoding']
|
177 |
|
178 | return _.isString(contentEncoding) && contentEncoding !== ''
|
179 | }
|
180 |
|
181 | function contentEncoding(headers, encoder) {
|
182 | const contentEncoding = headers['content-encoding']
|
183 | return contentEncoding === encoder
|
184 | }
|
185 |
|
186 | function isJSONContent(headers) {
|
187 |
|
188 | const contentType = String(headers['content-type'] || '').toLowerCase()
|
189 | return contentType.startsWith('application/json')
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function headersFieldNamesToLowerCase(headers) {
|
198 | if (!_.isPlainObject(headers)) {
|
199 | throw Error('Headers must be provided as an object')
|
200 | }
|
201 |
|
202 | const lowerCaseHeaders = {}
|
203 | Object.entries(headers).forEach(([fieldName, fieldValue]) => {
|
204 | const key = fieldName.toLowerCase()
|
205 | if (lowerCaseHeaders[key] !== undefined) {
|
206 | throw Error(
|
207 | `Failed to convert header keys to lower case due to field name conflict: ${key}`
|
208 | )
|
209 | }
|
210 | lowerCaseHeaders[key] = fieldValue
|
211 | })
|
212 |
|
213 | return lowerCaseHeaders
|
214 | }
|
215 |
|
216 | const headersFieldsArrayToLowerCase = headers => [
|
217 | ...new Set(headers.map(fieldName => fieldName.toLowerCase())),
|
218 | ]
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function headersInputToRawArray(headers) {
|
231 | if (headers === undefined) {
|
232 | return []
|
233 | }
|
234 |
|
235 | if (Array.isArray(headers)) {
|
236 |
|
237 |
|
238 | if (headers.length % 2) {
|
239 | throw new Error(
|
240 | `Raw headers must be provided as an array with an even number of items. [fieldName, value, ...]`
|
241 | )
|
242 | }
|
243 | return [...headers]
|
244 | }
|
245 |
|
246 |
|
247 | if (_.isMap(headers)) {
|
248 | return [].concat(...Array.from(headers, ([k, v]) => [k.toString(), v]))
|
249 | }
|
250 |
|
251 | if (_.isPlainObject(headers)) {
|
252 | return [].concat(...Object.entries(headers))
|
253 | }
|
254 |
|
255 | throw new Error(
|
256 | `Headers must be provided as an array of raw values, a Map, or a plain Object. ${headers}`
|
257 | )
|
258 | }
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function headersArrayToObject(rawHeaders) {
|
266 | if (!Array.isArray(rawHeaders)) {
|
267 | throw Error('Expected a header array')
|
268 | }
|
269 |
|
270 | const accumulator = {}
|
271 |
|
272 | forEachHeader(rawHeaders, (value, fieldName) => {
|
273 | addHeaderLine(accumulator, fieldName, value)
|
274 | })
|
275 |
|
276 | return accumulator
|
277 | }
|
278 |
|
279 | const noDuplicatesHeaders = new Set([
|
280 | 'age',
|
281 | 'authorization',
|
282 | 'content-length',
|
283 | 'content-type',
|
284 | 'etag',
|
285 | 'expires',
|
286 | 'from',
|
287 | 'host',
|
288 | 'if-modified-since',
|
289 | 'if-unmodified-since',
|
290 | 'last-modified',
|
291 | 'location',
|
292 | 'max-forwards',
|
293 | 'proxy-authorization',
|
294 | 'referer',
|
295 | 'retry-after',
|
296 | 'user-agent',
|
297 | ])
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | function addHeaderLine(headers, name, value) {
|
321 | let values
|
322 | if (typeof value === 'function') {
|
323 |
|
324 |
|
325 | values = [value.name]
|
326 | } else if (Array.isArray(value)) {
|
327 | values = value.map(String)
|
328 | } else {
|
329 | values = [String(value)]
|
330 | }
|
331 |
|
332 | const key = name.toLowerCase()
|
333 | if (key === 'set-cookie') {
|
334 |
|
335 | if (headers['set-cookie'] === undefined) {
|
336 | headers['set-cookie'] = values
|
337 | } else {
|
338 | headers['set-cookie'].push(...values)
|
339 | }
|
340 | } else if (noDuplicatesHeaders.has(key)) {
|
341 | if (headers[key] === undefined) {
|
342 |
|
343 | headers[key] = values[0]
|
344 | }
|
345 | } else {
|
346 | if (headers[key] !== undefined) {
|
347 | values = [headers[key], ...values]
|
348 | }
|
349 |
|
350 | const separator = key === 'cookie' ? '; ' : ', '
|
351 | headers[key] = values.join(separator)
|
352 | }
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | function deleteHeadersField(headers, fieldNameToDelete) {
|
363 | if (!_.isPlainObject(headers)) {
|
364 | throw Error('headers must be an object')
|
365 | }
|
366 |
|
367 |
|
368 | if (!_.isString(fieldNameToDelete)) {
|
369 | throw Error('field name must be a string')
|
370 | }
|
371 |
|
372 | const lowerCaseFieldNameToDelete = fieldNameToDelete.toLowerCase()
|
373 |
|
374 |
|
375 | Object.keys(headers)
|
376 | .filter(fieldName => fieldName.toLowerCase() === lowerCaseFieldNameToDelete)
|
377 | .forEach(fieldName => delete headers[fieldName])
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | function forEachHeader(rawHeaders, callback) {
|
389 | for (let i = 0; i < rawHeaders.length; i += 2) {
|
390 | callback(rawHeaders[i + 1], rawHeaders[i], i)
|
391 | }
|
392 | }
|
393 |
|
394 | function percentDecode(str) {
|
395 | try {
|
396 | return decodeURIComponent(str.replace(/\+/g, ' '))
|
397 | } catch (e) {
|
398 | return str
|
399 | }
|
400 | }
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 | function percentEncode(str) {
|
411 | return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
|
412 | return `%${c
|
413 | .charCodeAt(0)
|
414 | .toString(16)
|
415 | .toUpperCase()}`
|
416 | })
|
417 | }
|
418 |
|
419 | function matchStringOrRegexp(target, pattern) {
|
420 | const targetStr =
|
421 | target === undefined || target === null ? '' : String(target)
|
422 |
|
423 | return pattern instanceof RegExp
|
424 | ? pattern.test(targetStr)
|
425 | : targetStr === String(pattern)
|
426 | }
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 | function formatQueryValue(key, value, stringFormattingFn) {
|
439 |
|
440 | switch (true) {
|
441 | case _.isNumber(value):
|
442 | case _.isBoolean(value):
|
443 | value = value.toString()
|
444 | break
|
445 | case value === null:
|
446 | case value === undefined:
|
447 | value = ''
|
448 | break
|
449 |
|
450 | case _.isString(value):
|
451 | if (stringFormattingFn) {
|
452 | value = stringFormattingFn(value)
|
453 | }
|
454 | break
|
455 | case value instanceof RegExp:
|
456 | break
|
457 | case Array.isArray(value): {
|
458 | value = value.map(function(val, idx) {
|
459 | return formatQueryValue(idx, val, stringFormattingFn)[1]
|
460 | })
|
461 | break
|
462 | }
|
463 | case typeof value === 'object': {
|
464 | value = Object.entries(value).reduce(function(acc, [subKey, subVal]) {
|
465 | const subPair = formatQueryValue(subKey, subVal, stringFormattingFn)
|
466 | acc[subPair[0]] = subPair[1]
|
467 |
|
468 | return acc
|
469 | }, {})
|
470 | break
|
471 | }
|
472 | }
|
473 |
|
474 | if (stringFormattingFn) key = stringFormattingFn(key)
|
475 | return [key, value]
|
476 | }
|
477 |
|
478 | function isStream(obj) {
|
479 | return (
|
480 | obj &&
|
481 | typeof obj !== 'string' &&
|
482 | !Buffer.isBuffer(obj) &&
|
483 | typeof obj.setEncoding === 'function'
|
484 | )
|
485 | }
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | function normalizeClientRequestArgs(input, options, cb) {
|
497 | if (typeof input === 'string') {
|
498 | input = urlToOptions(new url.URL(input))
|
499 | } else if (input instanceof url.URL) {
|
500 | input = urlToOptions(input)
|
501 | } else {
|
502 | cb = options
|
503 | options = input
|
504 | input = null
|
505 | }
|
506 |
|
507 | if (typeof options === 'function') {
|
508 | cb = options
|
509 | options = input || {}
|
510 | } else {
|
511 | options = Object.assign(input || {}, options)
|
512 | }
|
513 |
|
514 | return { options, callback: cb }
|
515 | }
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 | function urlToOptions(url) {
|
525 | const options = {
|
526 | protocol: url.protocol,
|
527 | hostname:
|
528 | typeof url.hostname === 'string' && url.hostname.startsWith('[')
|
529 | ? url.hostname.slice(1, -1)
|
530 | : url.hostname,
|
531 | hash: url.hash,
|
532 | search: url.search,
|
533 | pathname: url.pathname,
|
534 | path: `${url.pathname}${url.search || ''}`,
|
535 | href: url.href,
|
536 | }
|
537 | if (url.port !== '') {
|
538 | options.port = Number(url.port)
|
539 | }
|
540 | if (url.username || url.password) {
|
541 | options.auth = `${url.username}:${url.password}`
|
542 | }
|
543 | return options
|
544 | }
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 | const dataEqual = (expected, actual) =>
|
557 | deepEqual(expand(expected), expand(actual))
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 | const expand = input =>
|
568 | Object.entries(input).reduce((acc, [k, v]) => _.set(acc, k, v), {})
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 | function deepEqual(expected, actual) {
|
576 | debug('deepEqual comparing', typeof expected, expected, typeof actual, actual)
|
577 | if (expected instanceof RegExp) {
|
578 | return expected.test(actual)
|
579 | }
|
580 |
|
581 | if (Array.isArray(expected) || _.isPlainObject(expected)) {
|
582 | if (actual === undefined) {
|
583 | return false
|
584 | }
|
585 |
|
586 | const expKeys = Object.keys(expected)
|
587 | if (expKeys.length !== Object.keys(actual).length) {
|
588 | return false
|
589 | }
|
590 |
|
591 | return expKeys.every(key => deepEqual(expected[key], actual[key]))
|
592 | }
|
593 |
|
594 | return expected === actual
|
595 | }
|
596 |
|
597 | const timeouts = []
|
598 | const intervals = []
|
599 | const immediates = []
|
600 |
|
601 | const wrapTimer = (timer, ids) => (...args) => {
|
602 | const id = timer(...args)
|
603 | ids.push(id)
|
604 | return id
|
605 | }
|
606 |
|
607 | const setTimeout = wrapTimer(timers.setTimeout, timeouts)
|
608 | const setInterval = wrapTimer(timers.setInterval, intervals)
|
609 | const setImmediate = wrapTimer(timers.setImmediate, immediates)
|
610 |
|
611 | function clearTimer(clear, ids) {
|
612 | while (ids.length) {
|
613 | clear(ids.shift())
|
614 | }
|
615 | }
|
616 |
|
617 | function removeAllTimers() {
|
618 | clearTimer(clearTimeout, timeouts)
|
619 | clearTimer(clearInterval, intervals)
|
620 | clearTimer(clearImmediate, immediates)
|
621 | }
|
622 |
|
623 | exports.normalizeClientRequestArgs = normalizeClientRequestArgs
|
624 | exports.normalizeRequestOptions = normalizeRequestOptions
|
625 | exports.normalizeOrigin = normalizeOrigin
|
626 | exports.isUtf8Representable = isUtf8Representable
|
627 | exports.overrideRequests = overrideRequests
|
628 | exports.restoreOverriddenRequests = restoreOverriddenRequests
|
629 | exports.stringifyRequest = stringifyRequest
|
630 | exports.isContentEncoded = isContentEncoded
|
631 | exports.contentEncoding = contentEncoding
|
632 | exports.isJSONContent = isJSONContent
|
633 | exports.headersFieldNamesToLowerCase = headersFieldNamesToLowerCase
|
634 | exports.headersFieldsArrayToLowerCase = headersFieldsArrayToLowerCase
|
635 | exports.headersArrayToObject = headersArrayToObject
|
636 | exports.headersInputToRawArray = headersInputToRawArray
|
637 | exports.deleteHeadersField = deleteHeadersField
|
638 | exports.forEachHeader = forEachHeader
|
639 | exports.percentEncode = percentEncode
|
640 | exports.percentDecode = percentDecode
|
641 | exports.matchStringOrRegexp = matchStringOrRegexp
|
642 | exports.formatQueryValue = formatQueryValue
|
643 | exports.isStream = isStream
|
644 | exports.dataEqual = dataEqual
|
645 | exports.setTimeout = setTimeout
|
646 | exports.setInterval = setInterval
|
647 | exports.setImmediate = setImmediate
|
648 | exports.removeAllTimers = removeAllTimers
|