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

Inherit the prefixUrl option from parent if it's undefined #1448

Merged
merged 9 commits into from Sep 9, 2020
Merged
4 changes: 2 additions & 2 deletions source/core/index.ts
Expand Up @@ -1608,14 +1608,14 @@ export default class Request extends Duplex implements RequestEvents<Request> {
options.password = options.password ?? '';

// `options.prefixUrl` & `options.url`
if (options.prefixUrl) {
if (options.prefixUrl && options.prefixUrl !== '') {
Giotino marked this conversation as resolved.
Show resolved Hide resolved
options.prefixUrl = options.prefixUrl.toString();

if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) {
options.prefixUrl += '/';
}
} else {
options.prefixUrl = '';
options.prefixUrl = defaults?.prefixUrl ?? '';
}

if (is.string(options.url)) {
Expand Down
12 changes: 5 additions & 7 deletions test/arguments.ts
Expand Up @@ -97,35 +97,33 @@ test('methods are normalized', withServer, async (t, server, got) => {
await instance('test', {method: 'post'});
});

test.failing('throws an error when legacy URL is passed', withServer, async (t, server, got) => {
test.failing('throws an error when legacy URL is passed', withServer, async (t, server) => {
server.get('/test', echoUrl);

await t.throwsAsync(
// @ts-expect-error Error tests
got(parse(`${server.url}/test`), {prefixUrl: ''}),
got(parse(`${server.url}/test`)),
{message: 'The legacy `url.Url` has been deprecated. Use `URL` instead.'}
);

await t.throwsAsync(
got({
protocol: 'http:',
hostname: 'localhost',
port: server.port,
prefixUrl: ''
port: server.port
}),
{message: 'The legacy `url.Url` has been deprecated. Use `URL` instead.'}
);
});

test('accepts legacy URL options', withServer, async (t, server, got) => {
test('accepts legacy URL options', withServer, async (t, server) => {
server.get('/test', echoUrl);

const {body: secondBody} = await got({
protocol: 'http:',
hostname: 'localhost',
port: server.port,
pathname: '/test',
prefixUrl: ''
pathname: '/test'
});

t.is(secondBody, '/test');
Expand Down
9 changes: 4 additions & 5 deletions test/cache.ts
Expand Up @@ -143,10 +143,9 @@ test('doesn\'t cache response when received HTTP error', withServer, async (t, s
t.is(body, 'ok');
});

test('DNS cache works', withServer, async (t, _server, got) => {
test('DNS cache works', async t => {
const instance = got.extend({
dnsCache: true,
prefixUrl: ''
dnsCache: true
});

await t.notThrowsAsync(instance('https://example.com'));
Expand All @@ -155,9 +154,9 @@ test('DNS cache works', withServer, async (t, _server, got) => {
t.is(instance.defaults.options.dnsCache!._cache.size, 1);
});

test('DNS cache works - CacheableLookup instance', withServer, async (t, _server, got) => {
test('DNS cache works - CacheableLookup instance', async t => {
const cache = new CacheableLookup();
await t.notThrowsAsync(got('https://example.com', {dnsCache: cache, prefixUrl: ''}));
await t.notThrowsAsync(got('https://example.com', {dnsCache: cache}));

t.is((cache as any)._cache.size, 1);
});
Expand Down
10 changes: 10 additions & 0 deletions test/merge-instances.ts
Expand Up @@ -160,3 +160,13 @@ test('accepts options for promise API', t => {

t.pass();
});

test('merging `prefixUrl`', t => {
const instanceA = got.extend({headers: {unicorn: 'rainbow'}});
const instanceB = got.extend({prefixUrl: 'http://example.com'});
const mergedAonB = instanceB.extend(instanceA);
const mergedBonA = instanceA.extend(instanceB);

t.is(mergedAonB.defaults.options.prefixUrl, 'http://example.com/');
t.is(mergedBonA.defaults.options.prefixUrl, 'http://example.com/');
});
5 changes: 2 additions & 3 deletions test/pagination.ts
Expand Up @@ -534,7 +534,7 @@ test('`stackAllItems` set to false', withServer, async (t, server, got) => {
t.deepEqual(result, [1, 2, 3]);
});

test('next url in json response', withServer, async (t, server, got) => {
test('next url in json response', withServer, async (t, server) => {
server.get('/', (request, response) => {
const parameters = new URLSearchParams(request.url.slice(2));
const page = Number(parameters.get('page') ?? 0);
Expand All @@ -550,7 +550,7 @@ test('next url in json response', withServer, async (t, server, got) => {
next?: string;
}

const all = await got.paginate.all('', {
const all = await got.paginate.all(server.url, {
searchParams: {
page: 0
},
Expand All @@ -568,7 +568,6 @@ test('next url in json response', withServer, async (t, server, got) => {

return {
url: next,
prefixUrl: '',
searchParams: undefined
};
}
Expand Down
4 changes: 2 additions & 2 deletions test/stream.ts
Expand Up @@ -154,8 +154,8 @@ test('has error event', withServer, async (t, server, got) => {
});
});

test('has error event #2', withServer, async (t, _server, got) => {
const stream = got.stream('http://doesntexist', {prefixUrl: ''});
test('has error event #2', async t => {
const stream = got.stream('http://doesntexist');
await t.throwsAsync(pEvent(stream, 'response'), {code: 'ENOTFOUND'});
});

Expand Down
6 changes: 2 additions & 4 deletions test/timeout.ts
Expand Up @@ -249,11 +249,10 @@ test.serial('connect timeout', withServerAndFakeTimers, async (t, _server, got,
);
});

test.serial('connect timeout (ip address)', withServerAndFakeTimers, async (t, _server, got, clock) => {
test.serial('connect timeout (ip address)', withServerAndFakeTimers, async (t, _server, _got, clock) => {
await t.throwsAsync(
got({
url: 'http://127.0.0.1',
prefixUrl: '',
createConnection: options => {
const socket = new net.Socket(options as Record<string, unknown> as net.SocketConstructorOpts);
// @ts-expect-error We know that it is readonly, but we have to test it
Expand Down Expand Up @@ -338,12 +337,11 @@ test.serial('lookup timeout', withServerAndFakeTimers, async (t, server, got, cl
);
});

test.serial('lookup timeout no error (ip address)', withServerAndFakeTimers, async (t, server, got, clock) => {
test.serial('lookup timeout no error (ip address)', withServerAndFakeTimers, async (t, server, _got, clock) => {
server.get('/', defaultHandler(clock));

await t.notThrowsAsync(got({
url: `http://127.0.0.1:${server.port}`,
prefixUrl: '',
timeout: {lookup: 1},
retry: 0
}));
Expand Down