Skip to content

Commit 6e1d9f9

Browse files
committed
Fix Invalid URL checks on Node.js 16
1 parent a06e758 commit 6e1d9f9

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

source/core/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ An error to be thrown when a request fails.
1717
Contains a `code` property with error class code, like `ECONNREFUSED`.
1818
*/
1919
export class RequestError extends Error {
20+
input?: string;
21+
2022
code?: string;
2123
stack!: string;
2224
declare readonly options: Options;
@@ -30,6 +32,7 @@ export class RequestError extends Error {
3032

3133
this.name = 'RequestError';
3234
this.code = error.code;
35+
this.input = (error as any).input;
3336

3437
if (isRequest(self)) {
3538
Object.defineProperty(this, 'request', {

test/arguments.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Handler} from 'express';
44
import pEvent from 'p-event';
55
import got, {Options, StrictOptions} from '../source/index.js';
66
import withServer, {withBodyParsingServer} from './helpers/with-server.js';
7+
import invalidUrl from './helpers/invalid-url.js';
78

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

21-
await t.throwsAsync(
22-
got(''),
23-
{
24-
message: 'Invalid URL: '
25-
}
26-
);
22+
const firstError = await t.throwsAsync(got(''));
23+
invalidUrl(t, firstError, '');
2724

28-
await t.throwsAsync(
29-
got({
30-
url: ''
31-
}),
32-
{
33-
message: 'Invalid URL: '
34-
}
35-
);
25+
const secondError = await t.throwsAsync(got({url: ''}));
26+
invalidUrl(t, secondError, '');
3627
});
3728

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

6051
test('throws an error if the protocol is not specified', async t => {
61-
await t.throwsAsync(got('example.com'), {
62-
message: 'Invalid URL: example.com'
63-
});
52+
const error = await t.throwsAsync(got('example.com'));
53+
invalidUrl(t, error, 'example.com');
6454
});
6555

6656
test('properly encodes query string', withServer, async (t, server, got) => {

test/error.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import is from '@sindresorhus/is';
99
import got, {RequestError, HTTPError, TimeoutError} from '../source/index.js';
1010
import Request from '../source/core';
1111
import withServer from './helpers/with-server.js';
12+
import invalidUrl from './helpers/invalid-url.js';
1213

1314
const pStreamPipeline = promisify(stream.pipeline);
1415

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

196197
test('normalization errors using convenience methods', async t => {
197198
const url = 'undefined/https://example.com';
198-
await t.throwsAsync(got(url).json(), {message: `Invalid URL: ${url}`});
199-
await t.throwsAsync(got(url).text(), {message: `Invalid URL: ${url}`});
200-
await t.throwsAsync(got(url).buffer(), {message: `Invalid URL: ${url}`});
199+
200+
{
201+
const error = await t.throwsAsync(got(url).json());
202+
invalidUrl(t, error, url);
203+
}
204+
205+
{
206+
const error = await t.throwsAsync(got(url).text());
207+
invalidUrl(t, error, url);
208+
}
209+
210+
{
211+
const error = await t.throwsAsync(got(url).buffer());
212+
invalidUrl(t, error, url);
213+
}
201214
});
202215

203216
test('errors can have request property', withServer, async (t, server, got) => {

test/helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava';
22
import got, {HTTPError} from '../source/index.js';
33
import withServer from './helpers/with-server.js';
4+
import invalidUrl from './helpers/invalid-url.js';
45

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

20-
await t.throwsAsync(got.get('.com', {retry: {limit: 0}}), {message: 'Invalid URL: .com'});
21+
const secondError = await t.throwsAsync(got.get('.com', {retry: {limit: 0}}));
22+
invalidUrl(t, secondError, '.com');
2123
});

test/helpers/invalid-url.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// eslint-disable-next-line ava/use-test
2+
import {ExecutionContext} from 'ava';
3+
4+
export default function invalidUrl(t: ExecutionContext, error: TypeError & NodeJS.ErrnoException, url: string) {
5+
t.is(error.code, 'ERR_INVALID_URL');
6+
7+
if (error.message === 'Invalid URL') {
8+
t.is((error as any).input, url);
9+
} else {
10+
t.is(error.message.slice('Invalid URL: '.length), url);
11+
}
12+
}

0 commit comments

Comments
 (0)