diff --git a/source/core/index.ts b/source/core/index.ts index 7973b1a33..4b11fdec2 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1608,14 +1608,14 @@ export default class Request extends Duplex implements RequestEvents { 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)) { diff --git a/test/arguments.ts b/test/arguments.ts index 17ff04450..5088b898a 100644 --- a/test/arguments.ts +++ b/test/arguments.ts @@ -97,12 +97,12 @@ 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.'} ); @@ -110,22 +110,20 @@ test.failing('throws an error when legacy URL is passed', withServer, async (t, 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'); diff --git a/test/cache.ts b/test/cache.ts index b31eea088..e9532211e 100644 --- a/test/cache.ts +++ b/test/cache.ts @@ -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')); @@ -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); }); diff --git a/test/merge-instances.ts b/test/merge-instances.ts index 8798fd1df..f32141e39 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -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); +}); diff --git a/test/stream.ts b/test/stream.ts index 508098e5b..61e5f2528 100644 --- a/test/stream.ts +++ b/test/stream.ts @@ -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'}); }); diff --git a/test/timeout.ts b/test/timeout.ts index 1d07bd9e7..dc09328b0 100644 --- a/test/timeout.ts +++ b/test/timeout.ts @@ -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 as net.SocketConstructorOpts); // @ts-expect-error We know that it is readonly, but we have to test it @@ -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 }));