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

Add WHATWG URL support #289

Merged
merged 9 commits into from
May 15, 2017
Merged
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
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const decompressResponse = require('decompress-response');
const createErrorClass = require('create-error-class');
const isRetryAllowed = require('is-retry-allowed');
const Buffer = require('safe-buffer').Buffer;
const isURL = require('isurl');
const isPlainObj = require('is-plain-obj');
const pkg = require('./package');

Expand Down Expand Up @@ -221,6 +222,14 @@ function normalizeArguments(url, opts) {
}
}

if (isURL.lenient(url)) {
url = urlParseLax(url.href);

if (url.auth) {
throw new Error('Basic authentication must be done with auth option');
}
}

opts = Object.assign(
{
path: '',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"is-redirect": "^1.0.0",
"is-retry-allowed": "^1.0.0",
"is-stream": "^1.0.0",
"isurl": "^1.0.0-alpha5",
"lowercase-keys": "^1.0.0",
"safe-buffer": "^5.0.1",
"timed-out": "^4.0.0",
Expand All @@ -69,7 +70,9 @@
"nyc": "^10.0.0",
"pem": "^1.4.4",
"pify": "^2.3.0",
"tempfile": "^1.1.1",
"tempy": "^0.1.0",
"universal-url": "^1.0.0-alpha",
"xo": "^0.18.0"
},
"ava": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Returns a Promise for a `response` object with a `body` property, a `url` proper

Type: `string`, `object`

The URL to request or a [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback) object.
The URL to request as simple string, a [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).

Properties from `options` will override properties in the parsed `url`.

Expand Down
13 changes: 13 additions & 0 deletions test/arguments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {URL} from 'universal-url';
import test from 'ava';
import got from '../';
import {createServer} from './helpers/server';
Expand Down Expand Up @@ -65,6 +66,18 @@ test('should throw when body is set to object', async t => {
await t.throws(got(`${s.url}/`, {body: {}}), TypeError);
});

test('WHATWG URL support', async t => {
const wURL = new URL(`${s.url}/test`);
await t.notThrows(got(wURL));
});

test('throws on WHATWG URL with auth', async t => {
const wURL = new URL(`${s.url}/test`);
wURL.username = 'alex';
wURL.password = 'secret';
await t.throws(got(wURL));
});

test.after('cleanup', async () => {
await s.close();
});