Skip to content

Commit

Permalink
remove no longer used normalizeClientRequestArgs method
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Dec 26, 2021
1 parent 9355665 commit 70480a3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 91 deletions.
59 changes: 0 additions & 59 deletions lib/common.js
Expand Up @@ -410,65 +410,6 @@ function isStream(obj) {
)
}

/**
* Converts the arguments from the various signatures of http[s].request into a standard
* options object and an optional callback function.
*
* https://nodejs.org/api/http.html#http_http_request_url_options_callback
*
* Taken from the beginning of the native `ClientRequest`.
* https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/_http_client.js#L68
*/
function normalizeClientRequestArgs(input, options, cb) {
if (typeof input === 'string') {
input = urlToOptions(new url.URL(input))
} else if (input instanceof url.URL) {
input = urlToOptions(input)
} else {
cb = options
options = input
input = null
}

if (typeof options === 'function') {
cb = options
options = input || {}
} else {
options = Object.assign(input || {}, options)
}

return { options, callback: cb }
}

/**
* Utility function that converts a URL object into an ordinary
* options object as expected by the http.request and https.request APIs.
*
* This was copied from Node's source
* https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257
*/
function urlToOptions(url) {
const options = {
protocol: url.protocol,
hostname:
typeof url.hostname === 'string' && url.hostname.startsWith('[')
? url.hostname.slice(1, -1)
: url.hostname,
hash: url.hash,
search: url.search,
pathname: url.pathname,
path: `${url.pathname}${url.search || ''}`,
href: url.href,
}
if (url.port !== '') {
options.port = Number(url.port)
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`
}
return options
}

/**
* Determines if request data matches the expected schema.
*
Expand Down
32 changes: 0 additions & 32 deletions tests/test_common.js
Expand Up @@ -454,38 +454,6 @@ it('`percentEncode()` encodes extra reserved characters', () => {
expect(common.percentEncode('foo+(*)!')).to.equal('foo%2B%28%2A%29%21')
})

describe('`normalizeClientRequestArgs()`', () => {
it('should throw for invalid URL', () => {
// See https://github.com/nodejs/node/pull/38614 release in node v16.2.0
const isNewErrorText = semver.gte(process.versions.node, '16.2.0')
const errorText = isNewErrorText ? 'Invalid URL' : 'example.test'

// no schema
expect(() => http.get('example.test')).to.throw(TypeError, errorText)
})

it('can include auth info', async () => {
const scope = nock('http://example.test')
.get('/')
.basicAuth({ user: 'user', pass: 'pw' })
.reply()

http.get('http://user:pw@example.test')
scope.isDone()
})

it('should handle a single callback', async () => {
// TODO: Only passing a callback isn't currently supported by Nock,
// but should be in the future as Node allows it.
const cb = () => {}

const { options, callback } = common.normalizeClientRequestArgs(cb)

expect(options).to.deep.equal({})
expect(callback).to.equal(cb)
})
})

describe('`dataEqual()`', () => {
it('treats explicit and implicit undefined object values as equal', () => {
const result = common.dataEqual({ a: 'a', b: undefined }, { a: 'a' })
Expand Down

0 comments on commit 70480a3

Please sign in to comment.