Skip to content

Commit

Permalink
Ignore empty query objects (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon93s authored and sindresorhus committed Aug 19, 2018
1 parent ca2675e commit eb652f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions source/normalize-arguments.js
Expand Up @@ -68,15 +68,16 @@ module.exports = (url, options, defaults) => {
}

const {query} = options;
if (query) {
if (!is.empty(query)) {
if (!is.string(query)) {
options.query = (new URLSearchParamsGlobal(query)).toString();
}

options.path = `${options.path.split('?')[0]}?${options.query}`;
delete options.query;
}

delete options.query;

if (options.json && is.undefined(options.headers.accept)) {
options.headers.accept = 'application/json';
}
Expand Down
4 changes: 4 additions & 0 deletions test/arguments.js
Expand Up @@ -99,6 +99,10 @@ test('overrides querystring from opts', async t => {
t.is(response.body, '/?test=wow');
});

test('should ignore empty query object', async t => {
t.is((await got(`${s.url}/test`, {query: {}})).requestUrl, `${s.url}/test`);
});

test('should throw with auth in url string', async t => {
const error = await t.throwsAsync(got('https://test:45d3ps453@account.myservice.com/api/token'));
t.regex(error.message, /Basic authentication must be done with the `auth` option/);
Expand Down

0 comments on commit eb652f8

Please sign in to comment.