Skip to content

Commit

Permalink
Fix a regression where body was sent after redirect
Browse files Browse the repository at this point in the history
Fixes #1406
  • Loading branch information
szmarczak committed Sep 18, 2020
1 parent 25ae938 commit 88b32ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/core/index.ts
Expand Up @@ -2071,6 +2071,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if ('form' in options) {
delete options.form;
}

this[kBody] = undefined;
delete options.headers['content-length'];
}

if (this.redirects.length >= options.maxRedirects) {
Expand Down
15 changes: 15 additions & 0 deletions test/redirects.ts
Expand Up @@ -156,6 +156,21 @@ test('redirects on 303 if GET or HEAD', withServer, async (t, server, got) => {
t.is(request.options.method, 'HEAD');
});

test('removes body on GET redirect', withServer, async (t, server, got) => {
server.get('/', (request, response) => request.pipe(response));

server.post('/seeOther', (_request, response) => {
response.writeHead(303, {
location: '/'
});
response.end();
});

const {headers, body} = await got.post('seeOther', {body: 'hello'});
t.is(body, '');
t.is(headers['content-length'], '0');
});

test('redirects on 303 response even on post, put, delete', withServer, async (t, server, got) => {
server.get('/', reachedHandler);

Expand Down
1 change: 1 addition & 0 deletions test/stream.ts
Expand Up @@ -323,6 +323,7 @@ test('piping to got.stream.put()', withServer, async (t, server, got) => {
});

// See https://github.com/nodejs/node/issues/35237
// eslint-disable-next-line ava/no-skip-test
test.skip('no unhandled body stream errors', async t => {
const body = new FormData();
body.append('upload', fs.createReadStream('/bin/sh'));
Expand Down

0 comments on commit 88b32ea

Please sign in to comment.