Skip to content

Commit

Permalink
refactor: rename proto to protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 28, 2021
1 parent 44283ca commit 0756a34
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
24 changes: 12 additions & 12 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const timers = require('timers')
* @param {Object} options - a parsed options object of the request
*/
function normalizeRequestOptions(options) {
options.proto = options.proto || 'http'
options.port = options.port || (options.proto === 'http' ? 80 : 443)
options.protocol = options.protocol || 'http:'
options.port = options.port || (options.protocol === 'http:' ? 80 : 443)
if (options.host) {
debug('options.host:', options.host)
if (!options.hostname) {
Expand Down Expand Up @@ -135,22 +135,22 @@ function restoreOverriddenRequests() {
* In WHATWG URL vernacular, this returns the origin portion of a URL.
* However, the port is not included if it's standard and not already present on the host.
*/
function normalizeOrigin(proto, host, port) {
function normalizeOrigin(protocol, host, port) {
const hostHasPort = host.includes(':')
const portIsStandard =
(proto === 'http' && (port === 80 || port === '80')) ||
(proto === 'https' && (port === 443 || port === '443'))
(protocol === 'http:' && (port === 80 || port === '80')) ||
(protocol === 'https:' && (port === 443 || port === '443'))
const portStr = hostHasPort || portIsStandard ? '' : `:${port}`

return `${proto}://${host}${portStr}`
return `${protocol}//${host}${portStr}`
}

/**
* Get high level information about request as string
* @param {Object} options
* @param {string} options.method
* @param {number|string} options.port
* @param {string} options.proto Set internally. always http or https
* @param {string} options.protocol Set internally. always http: or https:
* @param {string} options.hostname
* @param {string} options.path
* @param {Object} options.headers
Expand All @@ -159,7 +159,7 @@ function normalizeOrigin(proto, host, port) {
*/
function stringifyRequest(options, body) {
const { method = 'GET', path = '', port } = options
const origin = normalizeOrigin(options.proto, options.hostname, port)
const origin = normalizeOrigin(options.protocol, options.hostname, port)

const log = {
method,
Expand Down Expand Up @@ -620,11 +620,11 @@ function isPlainObject(value) {
if (Object.getPrototypeOf(value) === null) {
return true
}
let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
let prototype = value
while (Object.getPrototypeOf(prototype) !== null) {
prototype = Object.getPrototypeOf(prototype)
}
return Object.getPrototypeOf(value) === proto
return Object.getPrototypeOf(value) === prototype
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function interceptorsFor(options) {

debug('interceptors for %j', options.host)

const basePath = `${options.proto}://${options.host}`
const basePath = `${options.protocol}//${options.host}`

debug('filtering interceptors for basepath', basePath)

Expand Down Expand Up @@ -194,22 +194,22 @@ function interceptorsFor(options) {
}
}

return undefined
return
}

function removeInterceptor(options) {
// Lazily import to avoid circular imports.
const Interceptor = require('./interceptor')

let baseUrl, key, method, proto
let baseUrl, key, method, protocol
if (options instanceof Interceptor) {
baseUrl = options.basePath
key = options._key
} else {
proto = options.proto ? options.proto : 'http'
protocol = options.protocol ? options.protocol : 'http:'

common.normalizeRequestOptions(options)
baseUrl = `${proto}://${options.host}`
baseUrl = `${protocol}//${options.host}`
method = (options.method && options.method.toUpperCase()) || 'GET'
key = `${method} ${baseUrl}${options.path || '/'}`
}
Expand Down
11 changes: 6 additions & 5 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ module.exports = class Interceptor {
let { path = '/' } = options
let matches
let matchKey
const { proto } = options
const { protocol } = options

if (this.method !== method) {
this.scope.logger(
Expand Down Expand Up @@ -330,13 +330,13 @@ module.exports = class Interceptor {
}

// If we have a filtered scope then we use it instead reconstructing the
// scope from the request options (proto, host and port) as these two won't
// scope from the request options (protocol, host and port) as these two won't
// necessarily match and we have to remove the scope that was matched (vs.
// that was defined).
if (this.__nock_filteredScope) {
matchKey = this.__nock_filteredScope
} else {
matchKey = common.normalizeOrigin(proto, options.host, options.port)
matchKey = common.normalizeOrigin(protocol, options.host, options.port)
}

if (typeof this.uri === 'function') {
Expand All @@ -359,6 +359,7 @@ module.exports = class Interceptor {
}

matches = matchBody(options, this._requestBody, body)

if (!matches) {
this.scope.logger(
"bodies don't match: \n",
Expand All @@ -383,7 +384,7 @@ module.exports = class Interceptor {

const method = (options.method || 'GET').toUpperCase()
let { path } = options
const { proto } = options
const { protocol } = options

// NOTE: Do not split off the query params as the regex could use them
if (!isRegex) {
Expand All @@ -394,7 +395,7 @@ module.exports = class Interceptor {
path = this.scope.transformPathFunction(path)
}
const comparisonKey = isPathFn || isRegex ? this.__nock_scopeKey : this._key
const matchKey = `${method} ${proto}://${options.host}${path}`
const matchKey = `${method} ${protocol}//${options.host}${path}`

if (isPathFn) {
return !!(matchKey.match(comparisonKey) && this.path(path))
Expand Down
2 changes: 1 addition & 1 deletion modules/intercept-node-http/lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Socket extends EventEmitter {
super()

// Pretend this is a TLSSocket
if (options.proto === 'https') {
if (options.protocol === 'https:') {
// https://github.com/nock/nock/issues/158
this.authorized = true
// https://github.com/nock/nock/issues/2147
Expand Down
8 changes: 4 additions & 4 deletions modules/intercept-node-http/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ function isPlainObject(value) {
if (Object.getPrototypeOf(value) === null) {
return true
}
let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
let prototype = value
while (Object.getPrototypeOf(prototype) !== null) {
prototype = Object.getPrototypeOf(prototype)
}
return Object.getPrototypeOf(value) === proto
return Object.getPrototypeOf(value) === prototype
}

0 comments on commit 0756a34

Please sign in to comment.