Skip to content

Commit

Permalink
Fix prefixUrl not working when the url argument is empty
Browse files Browse the repository at this point in the history
Fixes #1322
  • Loading branch information
szmarczak committed Jul 3, 2020
1 parent d325d35 commit 8d3412a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,13 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if (is.object(url) && !is.urlInstance(url)) {
options = {...defaults, ...(url as Options), ...options};
} else {
if (url && options && options.url) {
if (url && options && options.url !== undefined) {
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
}

options = {...defaults, ...options};

if (url) {
if (url !== undefined) {
options.url = url;
}

Expand Down
25 changes: 24 additions & 1 deletion test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ const echoUrl: Handler = (request, response) => {

test('`url` is required', async t => {
await t.throwsAsync(
got(''),
// @ts-ignore No argument on purpose.
got(),
{
message: 'Missing `url` property'
}
);

await t.throwsAsync(
got(''),
{
message: 'No URL protocol specified'
}
);

await t.throwsAsync(
got({
url: ''
Expand Down Expand Up @@ -541,3 +549,18 @@ test('no URL pollution', withServer, async (t, server) => {
t.is(url.pathname, '/');
t.is(body, '/ok');
});

test('prefixUrl is properly replaced when extending', withServer, async (t, server) => {
server.get('/', (request, response) => {
response.end(request.url);
});

server.get('/other/path/', (request, response) => {
response.end(request.url);
});

const parent = got.extend({ prefixUrl: server.url });
const child = parent.extend({ prefixUrl: `${server.url}/other/path/` });

t.is(await child.get('').text(), '/other/path/');
});

0 comments on commit 8d3412a

Please sign in to comment.