nock 13.2.9 → 13.3.0

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')
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/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.2.9",
10
+ "version": "13.3.0",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@definitelytyped/dtslint": "^0.0.112",
32
- "@sinonjs/fake-timers": "^9.0.0",
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
  },