Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't force query string normalization #1246

Merged
merged 7 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
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
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,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 @@ -1068,8 +1060,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
Original file line number Diff line number Diff line change
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