Skip to content

Commit

Permalink
Follow redirects with encoded URI (#564)
Browse files Browse the repository at this point in the history
Fixes #563
  • Loading branch information
jstewmon authored and sindresorhus committed Aug 23, 2018
1 parent 45d3a60 commit 3d98b9b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"decompress-response": "^3.3.0",
"duplexer3": "^0.1.4",
"get-stream": "^4.0.0",
"mimic-response": "^1.0.0",
"mimic-response": "^1.0.1",
"p-cancelable": "^0.5.0",
"to-readable-stream": "^1.0.0",
"url-parse-lax": "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion source/request-as-event-emitter.js
Expand Up @@ -74,7 +74,7 @@ module.exports = options => {
redirectUrl = (new URLGlobal(bufferString, urlLib.format(options))).toString();

try {
redirectUrl = decodeURI(redirectUrl);
decodeURI(redirectUrl);
} catch (error) {
emitter.emit('error', error);
return;
Expand Down
13 changes: 13 additions & 0 deletions test/arguments.js
Expand Up @@ -22,6 +22,10 @@ test.before('setup', async () => {
response.end(request.url);
});

s.on('/?test=it鈥檚+ok', (request, response) => {
response.end(request.url);
});

s.on('/stream', (request, response) => {
response.end('ok');
});
Expand Down Expand Up @@ -99,6 +103,15 @@ test('overrides querystring from opts', async t => {
t.is(response.body, '/?test=wow');
});

test('escapes query parameter values', async t => {
const response = await got(`${s.url}`, {
query: {
test: `it鈥檚 ok`
}
});
t.is(response.body, '/?test=it%E2%80%99s+ok');
});

test('the `query` option can be a URLSearchParams', async t => {
const query = new URLSearchParams({test: 'wow'});
const {body} = await got(s.url, {query});
Expand Down
8 changes: 7 additions & 1 deletion test/helpers/server.js
Expand Up @@ -14,7 +14,13 @@ exports.createServer = async () => {
const port = await getPort();

const s = http.createServer((request, response) => {
s.emit(request.url, request, response);
const event = decodeURI(request.url);
if (s.listeners(event).length === 0) {
response.writeHead(404, 'Not Found');
response.end(`No listener for ${event}`);
} else {
s.emit(event, request, response);
}
});

s.host = host;
Expand Down
25 changes: 18 additions & 7 deletions test/redirects.js
Expand Up @@ -7,6 +7,9 @@ let http;
let https;

test.before('setup', async () => {
const reached = (request, response) => {
response.end('reached');
};
https = await createSSLServer();
http = await createServer();

Expand All @@ -25,9 +28,7 @@ test.before('setup', async () => {

// HTTP Handlers

http.on('/', (request, response) => {
response.end('reached');
});
http.on('/', reached);

http.on('/finite', (request, response) => {
response.writeHead(302, {
Expand All @@ -36,9 +37,8 @@ test.before('setup', async () => {
response.end();
});

http.on('/utf8-url-谩茅', (request, response) => {
response.end('reached');
});
http.on('/utf8-url-谩茅', reached);
http.on('/?test=it鈥檚+ok', reached);

http.on('/redirect-with-utf8-binary', (request, response) => {
response.writeHead(302, {
Expand All @@ -47,6 +47,13 @@ test.before('setup', async () => {
response.end();
});

http.on('/redirect-with-uri-encoded-location', (request, response) => {
response.writeHead(302, {
location: new URL('/?test=it鈥檚+ok', http.url).toString()
});
response.end();
});

http.on('/endless', (request, response) => {
response.writeHead(302, {
location: `${http.url}/endless`
Expand Down Expand Up @@ -187,10 +194,14 @@ test('redirect response contains old url', async t => {
t.is(requestUrl, `${http.url}/finite`);
});

test('redirect response contains utf8 with binary encoding', async t => {
test('redirect response contains UTF-8 with binary encoding', async t => {
t.is((await got(`${http.url}/redirect-with-utf8-binary`)).body, 'reached');
});

test('redirect response contains UTF-8 with URI encoding', async t => {
t.is((await got(`${http.url}/redirect-with-uri-encoded-location`)).body, 'reached');
});

test('throws on malformed redirect URI', async t => {
const error = await t.throwsAsync(got(`${http.url}/malformedRedirect`));
t.is(error.name, 'URIError');
Expand Down

0 comments on commit 3d98b9b

Please sign in to comment.