Skip to content

Commit

Permalink
Throw if no protocol specified
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Jan 18, 2019
1 parent 8fa18f4 commit 92bc808
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ yarn.lock
coverage
.nyc_output
dist
.vscode
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -108,7 +108,7 @@ The URL to request, as a string, a [`https.request` options object](https://node

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

If no protocol is specified, it will default to `https`.
If no protocol is specified, it will throw a `TypeError`.

##### options

Expand Down
8 changes: 2 additions & 6 deletions source/normalize-arguments.js
Expand Up @@ -2,7 +2,6 @@
const {URL, URLSearchParams} = require('url'); // TODO: Use the `URL` global when targeting Node.js 10
const urlLib = require('url');
const is = require('@sindresorhus/is');
const urlParseLax = require('url-parse-lax');
const lowercaseKeys = require('lowercase-keys');
const urlToOptions = require('./utils/url-to-options').default;
const validateSearchParams = require('./utils/validate-search-params');
Expand Down Expand Up @@ -118,14 +117,11 @@ const normalize = (url, options, defaults) => {
if (url.toString().startsWith('/')) {
url = url.toString().slice(1);
}

url = urlToOptions(new URL(url, options.baseUrl));
} else {
url = url.replace(/^unix:/, 'http://$&');
url = urlParseLax(url);
url.searchParams = url.query;
delete url.query;
}

url = urlToOptions(new URL(url, options.baseUrl));
} else if (is(url) === 'URL') {
url = urlToOptions(url);
}
Expand Down
2 changes: 2 additions & 0 deletions source/utils/url-to-options.ts
Expand Up @@ -4,6 +4,7 @@ import { URL } from 'url';
export interface URLOptions {
protocol: string,
hostname: string,
host: string,
hash: string,
search: string,
pathname: string,
Expand All @@ -17,6 +18,7 @@ export default (url: URL): URLOptions => {
const options: URLOptions = {
protocol: url.protocol,
hostname: url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
host: url.host,
hash: url.hash,
search: url.search,
pathname: url.pathname,
Expand Down
4 changes: 4 additions & 0 deletions test/arguments.js
Expand Up @@ -54,6 +54,10 @@ test('url should be utf-8 encoded', async t => {
);
});

test('throws an error if the protocol is not specified', async t => {
await t.throwsAsync(got('example.com'), TypeError);
});

test('string url with searchParams is preserved', async t => {
const path = '/?test=http://example.com?foo=bar';
const {body} = await got(`${s.url}${path}`);
Expand Down
4 changes: 2 additions & 2 deletions test/error.js
Expand Up @@ -59,10 +59,10 @@ test('properties', async t => {
});

test('dns message', async t => {
const error = await t.throwsAsync(got('.com', {retry: 0}));
const error = await t.throwsAsync(got('http://doesntexist', {retry: 0}));
t.truthy(error);
t.regex(error.message, /getaddrinfo ENOTFOUND/);
t.is(error.host, '.com');
t.is(error.host, 'doesntexist');
t.is(error.method, 'GET');
});

Expand Down
9 changes: 0 additions & 9 deletions test/https.js
Expand Up @@ -27,12 +27,3 @@ test('make request to https server with ca', async t => {
});
t.is(body, 'ok');
});

test('protocol-less URLs default to HTTPS', async t => {
const {body, requestUrl} = await got(s.url.replace(/^https:\/\//, ''), {
ca: s.caRootCert,
headers: {host: 'sindresorhus.com'}
});
t.is(body, 'ok');
t.true(requestUrl.startsWith('https://'));
});
2 changes: 1 addition & 1 deletion test/stream.js
Expand Up @@ -87,7 +87,7 @@ test('have error event', async t => {
});

test('have error event #2', async t => {
const stream = got.stream('.com', {retry: 0});
const stream = got.stream('http://doesntexist', {retry: 0});
await t.throwsAsync(pEvent(stream, 'response'), /getaddrinfo ENOTFOUND/);
});

Expand Down
3 changes: 3 additions & 0 deletions test/url-to-options.js
Expand Up @@ -10,6 +10,7 @@ test('converts URL to options', t => {
const expected = {
auth: 'user:password',
hash: '#bang',
host: 'github.com',
hostname: 'github.com',
href: 'https://user:password@github.com/say?hello=world#bang',
path: '/say?hello=world',
Expand All @@ -28,6 +29,7 @@ test('converts IPv6 URL to options', t => {
const options = urlToOptions(parsedURL);
const expected = {
hash: '',
host: '[2001:cdba::3257:9652]',
hostname: '2001:cdba::3257:9652',
href: 'https://[2001:cdba::3257:9652]/',
path: '/',
Expand All @@ -45,6 +47,7 @@ test('only adds port to options for URLs with ports', t => {
const options = urlToOptions(parsedURL);
const expected = {
hash: '',
host: 'github.com',
hostname: 'github.com',
href: 'https://github.com/',
path: '/',
Expand Down

0 comments on commit 92bc808

Please sign in to comment.