Skip to content

Commit

Permalink
Fix Invalid URL checks on Node.js 16
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Jun 2, 2021
1 parent a06e758 commit 6e1d9f9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions source/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ An error to be thrown when a request fails.
Contains a `code` property with error class code, like `ECONNREFUSED`.
*/
export class RequestError extends Error {
input?: string;

code?: string;
stack!: string;
declare readonly options: Options;
Expand All @@ -30,6 +32,7 @@ export class RequestError extends Error {

this.name = 'RequestError';
this.code = error.code;
this.input = (error as any).input;

if (isRequest(self)) {
Object.defineProperty(this, 'request', {
Expand Down
24 changes: 7 additions & 17 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Handler} from 'express';
import pEvent from 'p-event';
import got, {Options, StrictOptions} from '../source/index.js';
import withServer, {withBodyParsingServer} from './helpers/with-server.js';
import invalidUrl from './helpers/invalid-url.js';

const echoUrl: Handler = (request, response) => {
response.end(request.url);
Expand All @@ -18,21 +19,11 @@ test('`url` is required', async t => {
}
);

await t.throwsAsync(
got(''),
{
message: 'Invalid URL: '
}
);
const firstError = await t.throwsAsync(got(''));
invalidUrl(t, firstError, '');

await t.throwsAsync(
got({
url: ''
}),
{
message: 'Invalid URL: '
}
);
const secondError = await t.throwsAsync(got({url: ''}));
invalidUrl(t, secondError, '');
});

test('`url` should be utf-8 encoded', async t => {
Expand All @@ -58,9 +49,8 @@ test('throws if the url option is missing', async t => {
});

test('throws an error if the protocol is not specified', async t => {
await t.throwsAsync(got('example.com'), {
message: 'Invalid URL: example.com'
});
const error = await t.throwsAsync(got('example.com'));
invalidUrl(t, error, 'example.com');
});

test('properly encodes query string', withServer, async (t, server, got) => {
Expand Down
19 changes: 16 additions & 3 deletions test/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import is from '@sindresorhus/is';
import got, {RequestError, HTTPError, TimeoutError} from '../source/index.js';
import Request from '../source/core';
import withServer from './helpers/with-server.js';
import invalidUrl from './helpers/invalid-url.js';

const pStreamPipeline = promisify(stream.pipeline);

Expand Down Expand Up @@ -195,9 +196,21 @@ test('returns a stream even if normalization fails', async t => {

test('normalization errors using convenience methods', async t => {
const url = 'undefined/https://example.com';
await t.throwsAsync(got(url).json(), {message: `Invalid URL: ${url}`});
await t.throwsAsync(got(url).text(), {message: `Invalid URL: ${url}`});
await t.throwsAsync(got(url).buffer(), {message: `Invalid URL: ${url}`});

{
const error = await t.throwsAsync(got(url).json());
invalidUrl(t, error, url);
}

{
const error = await t.throwsAsync(got(url).text());
invalidUrl(t, error, url);
}

{
const error = await t.throwsAsync(got(url).buffer());
invalidUrl(t, error, url);
}
});

test('errors can have request property', withServer, async (t, server, got) => {
Expand Down
4 changes: 3 additions & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava';
import got, {HTTPError} from '../source/index.js';
import withServer from './helpers/with-server.js';
import invalidUrl from './helpers/invalid-url.js';

test('works', withServer, async (t, server) => {
server.get('/', (_request, response) => {
Expand All @@ -17,5 +18,6 @@ test('works', withServer, async (t, server) => {
const error = await t.throwsAsync<HTTPError>(got.get(`${server.url}/404`), {instanceOf: HTTPError});
t.is(error.response.body, 'not found');

await t.throwsAsync(got.get('.com', {retry: {limit: 0}}), {message: 'Invalid URL: .com'});
const secondError = await t.throwsAsync(got.get('.com', {retry: {limit: 0}}));
invalidUrl(t, secondError, '.com');
});
12 changes: 12 additions & 0 deletions test/helpers/invalid-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line ava/use-test
import {ExecutionContext} from 'ava';

export default function invalidUrl(t: ExecutionContext, error: TypeError & NodeJS.ErrnoException, url: string) {
t.is(error.code, 'ERR_INVALID_URL');

if (error.message === 'Invalid URL') {
t.is((error as any).input, url);
} else {
t.is(error.message.slice('Invalid URL: '.length), url);
}
}

0 comments on commit 6e1d9f9

Please sign in to comment.