Browse Source

Add WHATWG URL support (#289)

remove-strictssl-option
Alexander Tesfamichael 8 years ago
committed by Sindre Sorhus
parent
commit
f501fc2c7c
  1. 9
      index.js
  2. 3
      package.json
  3. 2
      readme.md
  4. 13
      test/arguments.js

9
index.js

@ -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 PCancelable = require('p-cancelable');
const pkg = require('./package');
@ -235,6 +236,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: '',

3
package.json

@ -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",
"p-cancelable": "^0.2.0",
"safe-buffer": "^5.0.1",
@ -70,7 +71,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": {

2
readme.md

@ -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`.

13
test/arguments.js

@ -1,3 +1,4 @@
import {URL} from 'universal-url';
import test from 'ava';
import got from '..';
import {createServer} from './helpers/server';
@ -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();
});

Loading…
Cancel
Save