Skip to content

Commit

Permalink
Inherit the prefixUrl option from parent if it's undefined (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino committed Sep 9, 2020
1 parent f248618 commit a3da70a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
6 changes: 3 additions & 3 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 (is.undefined(options.prefixUrl)) {
options.prefixUrl = defaults?.prefixUrl ?? '';
} else {
options.prefixUrl = options.prefixUrl.toString();

if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) {
options.prefixUrl += '/';
}
} else {
options.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
15 changes: 15 additions & 0 deletions test/merge-instances.ts
Expand Up @@ -160,3 +160,18 @@ test('accepts options for promise API', t => {

t.pass();
});

test('merging `prefixUrl`', t => {
const prefixUrl = 'http://example.com/';

const instanceA = got.extend({headers: {unicorn: 'rainbow'}});
const instanceB = got.extend({prefixUrl});
const mergedAonB = instanceB.extend(instanceA);
const mergedBonA = instanceA.extend(instanceB);

t.is(mergedAonB.defaults.options.prefixUrl, '');
t.is(mergedBonA.defaults.options.prefixUrl, prefixUrl);

t.is(instanceB.extend({}).defaults.options.prefixUrl, prefixUrl);
t.is(instanceB.extend({prefixUrl: undefined}).defaults.options.prefixUrl, prefixUrl);
});
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

0 comments on commit a3da70a

Please sign in to comment.