Skip to content

Commit

Permalink
Don't force query string normalization (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed May 12, 2020
1 parent 5480b31 commit 761b8e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
10 changes: 10 additions & 0 deletions readme.md
Expand Up @@ -139,6 +139,16 @@ Properties from `options` will override properties in the parsed `url`.

If no protocol is specified, it will throw a `TypeError`.

**Note:** The query string is **not** parsed as search params. Example:

```
got('https://example.com/?query=a b'); //=> https://example.com/?query=a%20b
got('https://example.com/', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
// The query string is overridden by `searchParams`
got('https://example.com/?query=a b', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
```

##### options

Type: `object`
Expand Down
12 changes: 3 additions & 9 deletions source/core/index.ts
Expand Up @@ -727,14 +727,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
options.url.search = options.searchParams.toString();
}

// Trigger search params normalization
if (options.url.search) {
const triggerSearchParameters = '_GOT_INTERNAL_TRIGGER_NORMALIZATION';

options.url.searchParams.append(triggerSearchParameters, '');
options.url.searchParams.delete(triggerSearchParameters);
}

// Protocol check
if (protocol !== 'http:' && protocol !== 'https:') {
throw new UnsupportedProtocolError(options as NormalizedOptions);
Expand Down Expand Up @@ -1069,8 +1061,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

try {
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
// Do not remove. See https://github.com/sindresorhus/got/pull/214
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();

// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
const redirectUrl = new URL(redirectBuffer, url);
const redirectString = redirectUrl.toString();
decodeURI(redirectString);
Expand Down
8 changes: 4 additions & 4 deletions test/arguments.ts
Expand Up @@ -55,12 +55,12 @@ test('throws an error if the protocol is not specified', async t => {
});
});

test('string url with searchParams is preserved', withServer, async (t, server, got) => {
test('properly encodes query string', withServer, async (t, server, got) => {
server.get('/', echoUrl);

const path = '?test=http://example.com?foo=bar';
const {body} = await got(path);
t.is(body, '/?test=http%3A%2F%2Fexample.com%3Ffoo%3Dbar');
t.is(body, '/?test=http://example.com?foo=bar');
});

test('options are optional', withServer, async (t, server, got) => {
Expand Down Expand Up @@ -469,12 +469,12 @@ test('does not throw on frozen options', withServer, async (t, server, got) => {
t.is(body, '/');
});

test('normalizes search params included in input', t => {
test('encodes query string included in input', t => {
const {url} = got.mergeOptions({
url: new URL('https://example.com/?a=b c')
});

t.is(url.search, '?a=b+c');
t.is(url.search, '?a=b%20c');
});

test('normalizes search params included in options', t => {
Expand Down

0 comments on commit 761b8e0

Please sign in to comment.