Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throws on unsupported option path #1282

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ This section documents our most commonly used API methods. Additional APIs are d
Arguments:

* **url** `string | URL | UrlObject`
* **options** [`RequestOptions`](./docs/api/Dispatcher.md#parameter-requestoptions)
* **options** [`RequestOptions`](./docs/api/Dispatcher.md#parameter-requestoptions)(`path` and `origin` are omitted)
* **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher)
* **method** `String` - Default: `PUT` if `options.body`, otherwise `GET`
* **maxRedirections** `Integer` - Default: `0`
Expand Down
23 changes: 7 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,15 @@ function makeDispatcher (fn) {
}

if (opts && opts.path != null) {
if (typeof opts.path !== 'string') {
throw new InvalidArgumentError('invalid opts.path')
}

let path = opts.path
if (!opts.path.startsWith('/')) {
path = `/${path}`
}

url = new URL(util.parseOrigin(url).origin + path)
} else {
if (!opts) {
opts = typeof url === 'object' ? url : {}
}

url = util.parseURL(url)
throw new InvalidArgumentError('unsupported option `path`')
}

if (!opts) {
opts = typeof url === 'object' ? url : {}
}

url = util.parseURL(url)

const { agent, dispatcher = getGlobalDispatcher() } = opts

if (agent) {
Expand Down
14 changes: 6 additions & 8 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ test('fails with invalid args', t => {
t.throws(() => request(''), errors.InvalidArgumentError, 'throws on invalid url')
t.throws(() => request({}), errors.InvalidArgumentError, 'throws on missing url.origin argument')
t.throws(() => request({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument')
t.throws(() => request('https://example.com', { path: 0 }), errors.InvalidArgumentError, 'throws on opts.path argument')
t.throws(() => request('https://example.com', { path: '/hello' }), errors.InvalidArgumentError, 'throws on opts.path argument')
t.throws(() => request('https://example.com', { agent: new Agent() }), errors.InvalidArgumentError, 'throws on opts.path argument')
t.throws(() => request('https://example.com', 'asd'), errors.InvalidArgumentError, 'throws on non object opts argument')
t.end()
Expand Down Expand Up @@ -648,7 +648,7 @@ test('drain', t => {
})

test('global api', t => {
t.plan(6 * 2)
t.plan(4 * 2)

const server = http.createServer((req, res) => {
if (req.url === '/bar') {
Expand All @@ -665,10 +665,8 @@ test('global api', t => {

server.listen(0, async () => {
const origin = `http://localhost:${server.address().port}`
await request(origin, { path: '/foo' })
await request(`${origin}/foo`)
await request({ origin, path: '/foo' })
await stream({ origin, path: '/foo' }, () => new PassThrough())
await request(`${origin}/foo`, {}, () => new PassThrough())
await request({ protocol: 'http:', hostname: 'localhost', port: server.address().port, path: '/foo' })
await request(`${origin}/bar`, { body: 'asd' })
})
Expand All @@ -677,9 +675,9 @@ test('global api', t => {
test('global api throws', t => {
const origin = 'http://asd'
t.throws(() => request(`${origin}/foo`, { path: '/foo' }), errors.InvalidArgumentError)
t.throws(() => request({ origin, path: 0 }, { path: '/foo' }), errors.InvalidArgumentError)
t.throws(() => request({ origin, pathname: 0 }, { path: '/foo' }), errors.InvalidArgumentError)
t.throws(() => request({ origin: 0 }, { path: '/foo' }), errors.InvalidArgumentError)
t.throws(() => request({ origin, path: 0 }, { path: 0 }), errors.InvalidArgumentError)
t.throws(() => request({ origin, pathname: 0 }, { path: 0 }), errors.InvalidArgumentError)
t.throws(() => request({ origin: 0 }, { path: 0 }), errors.InvalidArgumentError)
t.throws(() => request(0), errors.InvalidArgumentError)
t.throws(() => request(1), errors.InvalidArgumentError)
t.end()
Expand Down