nock 13.2.9 → 13.3.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/README.md CHANGED
@@ -159,7 +159,7 @@ If you don’t want interceptors to be removed as they are used, you can use the
159
159
 
160
160
  ### Specifying hostname
161
161
 
162
- The request hostname can be a string or a RegExp.
162
+ The request hostname can be a string, URL, or a RegExp.
163
163
 
164
164
  ```js
165
165
  const scope = nock('http://www.example.com')
@@ -167,6 +167,12 @@ const scope = nock('http://www.example.com')
167
167
  .reply(200, 'domain matched')
168
168
  ```
169
169
 
170
+ ```js
171
+ const scope = nock(new URL('http://www.example.com'))
172
+ .get('/resource')
173
+ .reply(200, 'domain matched')
174
+ ```
175
+
170
176
  ```js
171
177
  const scope = nock(/example\.com/)
172
178
  .get('/resource')
@@ -1533,9 +1539,9 @@ import test from 'ava' // You can use any test framework.
1533
1539
  // can't be intercepted by nock. So, configure axios to use the node adapter.
1534
1540
  //
1535
1541
  // References:
1536
- // https://github.com/nock/nock/issues/699#issuecomment-272708264
1537
- // https://github.com/axios/axios/issues/305
1538
- axios.defaults.adapter = require('axios/lib/adapters/http')
1542
+ // https://github.com/axios/axios/pull/5277
1543
+
1544
+ axios.defaults.adapter = 'http'
1539
1545
 
1540
1546
  test('can fetch test response', async t => {
1541
1547
  // Set up the mock request.
@@ -1553,6 +1559,18 @@ test('can fetch test response', async t => {
1553
1559
  })
1554
1560
  ```
1555
1561
 
1562
+ For Nock + Axios + Jest to work, you'll have to also adapt your jest.config.js, like so:
1563
+
1564
+ ```js
1565
+ const config = {
1566
+ moduleNameMapper: {
1567
+ // Force CommonJS build for http adapter to be available.
1568
+ // via https://github.com/axios/axios/issues/5101#issuecomment-1276572468
1569
+ '^axios$': require.resolve('axios'),
1570
+ },
1571
+ }
1572
+ ```
1573
+
1556
1574
  [axios]: https://github.com/axios/axios
1557
1575
 
1558
1576
  ### Memory issues with Jest
package/lib/scope.js CHANGED
@@ -11,6 +11,7 @@ const debug = require('debug')('nock.scope')
11
11
  const { EventEmitter } = require('events')
12
12
  const Interceptor = require('./interceptor')
13
13
 
14
+ const { URL, Url: LegacyUrl } = url
14
15
  let fs
15
16
 
16
17
  try {
@@ -20,7 +21,46 @@ try {
20
21
  }
21
22
 
22
23
  /**
23
- * @param {string|RegExp|url.url} basePath
24
+ * Normalizes the passed url for consistent internal processing
25
+ * @param {string|LegacyUrl|URL} u
26
+ */
27
+ function normalizeUrl(u) {
28
+ if (!(u instanceof URL)) {
29
+ if (u instanceof LegacyUrl) {
30
+ return normalizeUrl(new URL(url.format(u)))
31
+ }
32
+ // If the url is invalid, let the URL library report it
33
+ return normalizeUrl(new URL(u))
34
+ }
35
+
36
+ if (!/https?:/.test(u.protocol)) {
37
+ throw new TypeError(
38
+ `Protocol '${u.protocol}' not recognized. This commonly occurs when a hostname and port are included without a protocol, producing a URL that is valid but confusing, and probably not what you want.`
39
+ )
40
+ }
41
+
42
+ return {
43
+ href: u.href,
44
+ origin: u.origin,
45
+ protocol: u.protocol,
46
+ username: u.username,
47
+ password: u.password,
48
+ host: u.host,
49
+ hostname:
50
+ // strip brackets from IPv6
51
+ typeof u.hostname === 'string' && u.hostname.startsWith('[')
52
+ ? u.hostname.slice(1, -1)
53
+ : u.hostname,
54
+ port: u.port || (u.protocol === 'http:' ? 80 : 443),
55
+ pathname: u.pathname,
56
+ search: u.search,
57
+ searchParams: u.searchParams,
58
+ hash: u.hash,
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @param {string|RegExp|LegacyUrl|URL} basePath
24
64
  * @param {Object} options
25
65
  * @param {boolean} options.allowUnmocked
26
66
  * @param {string[]} options.badheaders
@@ -52,9 +92,8 @@ class Scope extends EventEmitter {
52
92
  let logNamespace = String(basePath)
53
93
 
54
94
  if (!(basePath instanceof RegExp)) {
55
- this.urlParts = url.parse(basePath)
56
- this.port =
57
- this.urlParts.port || (this.urlParts.protocol === 'http:' ? 80 : 443)
95
+ this.urlParts = normalizeUrl(basePath)
96
+ this.port = this.urlParts.port
58
97
  this.basePathname = this.urlParts.pathname.replace(/\/$/, '')
59
98
  this.basePath = `${this.urlParts.protocol}//${this.urlParts.hostname}:${this.port}`
60
99
  logNamespace = this.urlParts.host
package/lib/socket.js CHANGED
@@ -40,6 +40,7 @@ module.exports = class Socket extends EventEmitter {
40
40
  resume() {}
41
41
  ref() {}
42
42
  unref() {}
43
+ write() {}
43
44
 
44
45
  address() {
45
46
  return {
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.2.9",
10
+ "version": "13.3.1",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -28,8 +28,8 @@
28
28
  "propagate": "^2.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@definitelytyped/dtslint": "^0.0.112",
32
- "@sinonjs/fake-timers": "^9.0.0",
31
+ "@definitelytyped/dtslint": "^0.0.159",
32
+ "@sinonjs/fake-timers": "^10.0.0",
33
33
  "assert-rejects": "^1.0.0",
34
34
  "chai": "^4.1.2",
35
35
  "dirty-chai": "^2.0.1",
@@ -49,7 +49,7 @@
49
49
  "proxyquire": "^2.1.0",
50
50
  "rimraf": "^3.0.0",
51
51
  "semantic-release": "^19.0.2",
52
- "sinon": "^14.0.0",
52
+ "sinon": "^15.0.1",
53
53
  "sinon-chai": "^3.7.0",
54
54
  "typescript": "^4.2.2"
55
55
  },
@@ -59,7 +59,7 @@
59
59
  "lint": "run-p lint:js lint:ts",
60
60
  "lint:js": "eslint --cache --cache-location './.cache/eslint' '**/*.js'",
61
61
  "lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
62
- "lint:ts": "dtslint types",
62
+ "lint:ts": "dtslint --expectOnly types",
63
63
  "test": "nyc --reporter=lcov --reporter=text mocha tests",
64
64
  "test:coverage": "open coverage/lcov-report/index.html"
65
65
  },