From 9e97ae256f7a496f90630a7993a28cfb6701e814 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 9 Sep 2020 15:23:48 +0200 Subject: [PATCH 1/9] Overwrite `prefixUrl` if empty string + test --- source/core/index.ts | 4 ++-- test/merge-instances.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/source/core/index.ts b/source/core/index.ts index 7973b1a33..49b101c72 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 (options.prefixUrl && options.prefixUrl !== '') { 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)) { diff --git a/test/merge-instances.ts b/test/merge-instances.ts index 8798fd1df..c4aca0963 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -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/'); +}); From a2016ddcfdd08f7ee7c346d6ac732e414357ca04 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 9 Sep 2020 15:50:07 +0200 Subject: [PATCH 2/9] Fixed `cache` and `stream` tests --- test/cache.ts | 9 ++++----- test/stream.ts | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) 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/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'}); }); From 881d05eeb434488d9cb576116fe3fe368ccf8e74 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 9 Sep 2020 16:36:09 +0200 Subject: [PATCH 3/9] Pagination test rewrite --- test/pagination.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/pagination.ts b/test/pagination.ts index bdb48484c..b6e478bfa 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -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); @@ -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 }, @@ -568,7 +568,6 @@ test('next url in json response', withServer, async (t, server, got) => { return { url: next, - prefixUrl: '', searchParams: undefined }; } From bdee105bedeaa04fd38193bb8c2686bc09b56d18 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 9 Sep 2020 16:54:58 +0200 Subject: [PATCH 4/9] Fixed `arguments` and `timeout` tests --- test/arguments.ts | 12 +++++------- test/timeout.ts | 6 ++---- 2 files changed, 7 insertions(+), 11 deletions(-) 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/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 })); From b3aa79a6a922bad3290e4a6a66daf1cd95aef09d Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 9 Sep 2020 20:21:22 +0200 Subject: [PATCH 5/9] Removed redundant check for empty string --- source/core/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 49b101c72..42bb37455 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1608,7 +1608,7 @@ export default class Request extends Duplex implements RequestEvents { options.password = options.password ?? ''; // `options.prefixUrl` & `options.url` - if (options.prefixUrl && options.prefixUrl !== '') { + if (options.prefixUrl) { options.prefixUrl = options.prefixUrl.toString(); if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) { From 88f407dcf8117377a6413e0acd1f82e7fb67e757 Mon Sep 17 00:00:00 2001 From: Chris DiLorenzo Date: Wed, 9 Sep 2020 09:58:30 -0400 Subject: [PATCH 6/9] Point travis-ci.org badge to travis-ci.com (#1442) Co-authored-by: Sindre Sorhus --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 0a670b502..030acc451 100644 --- a/readme.md +++ b/readme.md @@ -2194,12 +2194,12 @@ The Electron `net` module is not consistent with the Node.js `http` module. See [ab]: https://badgen.net/travis/axios/axios?label [sb]: https://badgen.net/travis/visionmedia/superagent?label -[g5]: https://travis-ci.org/sindresorhus/got -[k5]: https://travis-ci.org/sindresorhus/ky -[r5]: https://travis-ci.org/request/request -[n5]: https://travis-ci.org/bitinn/node-fetch -[a5]: https://travis-ci.org/axios/axios -[s5]: https://travis-ci.org/visionmedia/superagent +[g5]: https://travis-ci.com/github/sindresorhus/got +[k5]: https://travis-ci.com/github/sindresorhus/ky +[r5]: https://travis-ci.org/github/request/request +[n5]: https://travis-ci.org/github/bitinn/node-fetch +[a5]: https://travis-ci.org/github/axios/axios +[s5]: https://travis-ci.org/github/visionmedia/superagent [gbg]: https://badgen.net/github/label-issues/sindresorhus/got/bug/open?label From ccec843c25f52bd4bc06ad05e9f2eeff592a2aaf Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 10 Sep 2020 00:51:32 +0200 Subject: [PATCH 7/9] fixes --- source/core/index.ts | 6 +++--- test/merge-instances.ts | 2 +- test/pagination.ts | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/source/core/index.ts b/source/core/index.ts index 42bb37455..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 = defaults?.prefixUrl ?? ''; } if (is.string(options.url)) { diff --git a/test/merge-instances.ts b/test/merge-instances.ts index c4aca0963..fb72a0140 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -167,6 +167,6 @@ test('merging `prefixUrl`', t => { const mergedAonB = instanceB.extend(instanceA); const mergedBonA = instanceA.extend(instanceB); - t.is(mergedAonB.defaults.options.prefixUrl, 'http://example.com/'); + t.is(mergedAonB.defaults.options.prefixUrl, ''); t.is(mergedBonA.defaults.options.prefixUrl, 'http://example.com/'); }); diff --git a/test/pagination.ts b/test/pagination.ts index b6e478bfa..bdb48484c 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -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) => { +test('next url in json response', withServer, async (t, server, got) => { server.get('/', (request, response) => { const parameters = new URLSearchParams(request.url.slice(2)); const page = Number(parameters.get('page') ?? 0); @@ -550,7 +550,7 @@ test('next url in json response', withServer, async (t, server) => { next?: string; } - const all = await got.paginate.all(server.url, { + const all = await got.paginate.all('', { searchParams: { page: 0 }, @@ -568,6 +568,7 @@ test('next url in json response', withServer, async (t, server) => { return { url: next, + prefixUrl: '', searchParams: undefined }; } From a6cb8877dbb87bec2088436357978e89b29dd972 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 10 Sep 2020 00:59:40 +0200 Subject: [PATCH 8/9] Revert readme.md --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 030acc451..0a670b502 100644 --- a/readme.md +++ b/readme.md @@ -2194,12 +2194,12 @@ The Electron `net` module is not consistent with the Node.js `http` module. See [ab]: https://badgen.net/travis/axios/axios?label [sb]: https://badgen.net/travis/visionmedia/superagent?label -[g5]: https://travis-ci.com/github/sindresorhus/got -[k5]: https://travis-ci.com/github/sindresorhus/ky -[r5]: https://travis-ci.org/github/request/request -[n5]: https://travis-ci.org/github/bitinn/node-fetch -[a5]: https://travis-ci.org/github/axios/axios -[s5]: https://travis-ci.org/github/visionmedia/superagent +[g5]: https://travis-ci.org/sindresorhus/got +[k5]: https://travis-ci.org/sindresorhus/ky +[r5]: https://travis-ci.org/request/request +[n5]: https://travis-ci.org/bitinn/node-fetch +[a5]: https://travis-ci.org/axios/axios +[s5]: https://travis-ci.org/visionmedia/superagent [gbg]: https://badgen.net/github/label-issues/sindresorhus/got/bug/open?label From 575d5912053ee51234bd9e12f8513acfc11e2cb3 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 10 Sep 2020 01:01:45 +0200 Subject: [PATCH 9/9] Update merge-instances.ts --- test/merge-instances.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/merge-instances.ts b/test/merge-instances.ts index fb72a0140..f32141e39 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -162,11 +162,16 @@ test('accepts options for promise API', t => { }); test('merging `prefixUrl`', t => { + const prefixUrl = 'http://example.com/'; + const instanceA = got.extend({headers: {unicorn: 'rainbow'}}); - const instanceB = got.extend({prefixUrl: 'http://example.com'}); + 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, 'http://example.com/'); + 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); });