Skip to content

Commit

Permalink
Remove WHATWG URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sholladay committed Jul 15, 2020
1 parent 0ee1d3f commit a71750d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ class Ky {
this.request = new globals.Request(this._input, this._options);

if (this._options.searchParams) {
const url = new URL(this.request.url);
url.search = new URLSearchParams(this._options.searchParams);
const searchParams = '?' + new URLSearchParams(this._options.searchParams).toString();
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
if (((supportsFormData && this._options.body instanceof globals.FormData) || this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers['content-type'])) {
Expand Down
14 changes: 14 additions & 0 deletions test/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import test from 'ava';
import ky from '..';

test.serial('relative URLs are passed to fetch unresolved', async (t) => {
const originalFetch = global.fetch;
global.fetch = async (input) => {
t.true(input.url.startsWith('/'));
return new Response('zebra');
};
t.is(await ky('/unicorn').text(), 'zebra');
t.is(await ky('/unicorn', {searchParams: {foo: 'bar'}}).text(), 'zebra');
t.is(await ky('unicorn', {prefixUrl: '/api/'}).text(), 'zebra');
global.fetch = originalFetch;
});

0 comments on commit a71750d

Please sign in to comment.