Skip to content

Commit

Permalink
Don't mangle user-provided searchParams string (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra committed Feb 20, 2021
1 parent 42424d4 commit 5815518
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ class Ky {
this.request = new globalThis.Request(this._input, this._options);

if (this._options.searchParams) {
const searchParams = '?' + new URLSearchParams(this._options.searchParams).toString();
const textSearchParams = typeof this._options.searchParams === 'string' ?
this._options.searchParams.replace(/^\?/, '') :
new URLSearchParams(this._options.searchParams).toString();
const searchParams = '?' + textSearchParams;
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);

// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
Expand Down
12 changes: 6 additions & 6 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test.serial('relative URLs are passed to fetch unresolved', async t => {

t.is(await ky('/unicorn').text(), '/unicorn');
t.is(await ky('/unicorn', {searchParams: {foo: 'bar'}}).text(), '/unicorn?foo=bar');
t.is(await ky('/unicorn#hash', {searchParams: 'foo'}).text(), '/unicorn?foo=#hash');
t.is(await ky('/unicorn?old', {searchParams: 'new'}).text(), '/unicorn?new=');
t.is(await ky('/unicorn?old#hash', {searchParams: 'new'}).text(), '/unicorn?new=#hash');
t.is(await ky('/unicorn#hash', {searchParams: 'foo'}).text(), '/unicorn?foo#hash');
t.is(await ky('/unicorn?old', {searchParams: 'new'}).text(), '/unicorn?new');
t.is(await ky('/unicorn?old#hash', {searchParams: 'new'}).text(), '/unicorn?new#hash');
t.is(await ky('unicorn', {prefixUrl: '/api/'}).text(), '/api/unicorn');
globalThis.fetch = originalFetch;
});
Expand All @@ -27,8 +27,8 @@ test('fetch option takes a custom fetch function', async t => {

t.is(await ky('/unicorn', {fetch: customFetch}).text(), '/unicorn');
t.is(await ky('/unicorn', {fetch: customFetch, searchParams: {foo: 'bar'}}).text(), '/unicorn?foo=bar');
t.is(await ky('/unicorn#hash', {fetch: customFetch, searchParams: 'foo'}).text(), '/unicorn?foo=#hash');
t.is(await ky('/unicorn?old', {fetch: customFetch, searchParams: 'new'}).text(), '/unicorn?new=');
t.is(await ky('/unicorn?old#hash', {fetch: customFetch, searchParams: 'new'}).text(), '/unicorn?new=#hash');
t.is(await ky('/unicorn#hash', {fetch: customFetch, searchParams: 'foo'}).text(), '/unicorn?foo#hash');
t.is(await ky('/unicorn?old', {fetch: customFetch, searchParams: 'new'}).text(), '/unicorn?new');
t.is(await ky('/unicorn?old#hash', {fetch: customFetch, searchParams: 'new'}).text(), '/unicorn?new#hash');
t.is(await ky('unicorn', {fetch: customFetch, prefixUrl: '/api/'}).text(), '/api/unicorn');
});
2 changes: 2 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,13 @@ test('searchParams option', async t => {
};
const searchParameters = new URLSearchParams(arrayParameters);
const stringParameters = '?cats=meow&dogs=true&opossums=false';
const customStringParameters = '?cats&dogs[0]=true&dogs[1]=false';

t.is(await ky(server.url, {searchParams: arrayParameters}).text(), stringParameters);
t.is(await ky(server.url, {searchParams: objectParameters}).text(), stringParameters);
t.is(await ky(server.url, {searchParams: searchParameters}).text(), stringParameters);
t.is(await ky(server.url, {searchParams: stringParameters}).text(), stringParameters);
t.is(await ky(server.url, {searchParams: customStringParameters}).text(), customStringParameters);

await server.close();
});
Expand Down

0 comments on commit 5815518

Please sign in to comment.