Skip to content

Commit 88b32ea

Browse files
committedSep 18, 2020
Fix a regression where body was sent after redirect
Fixes #1406
1 parent 25ae938 commit 88b32ea

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
 

‎source/core/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
20712071
if ('form' in options) {
20722072
delete options.form;
20732073
}
2074+
2075+
this[kBody] = undefined;
2076+
delete options.headers['content-length'];
20742077
}
20752078

20762079
if (this.redirects.length >= options.maxRedirects) {

‎test/redirects.ts

+15
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ test('redirects on 303 if GET or HEAD', withServer, async (t, server, got) => {
156156
t.is(request.options.method, 'HEAD');
157157
});
158158

159+
test('removes body on GET redirect', withServer, async (t, server, got) => {
160+
server.get('/', (request, response) => request.pipe(response));
161+
162+
server.post('/seeOther', (_request, response) => {
163+
response.writeHead(303, {
164+
location: '/'
165+
});
166+
response.end();
167+
});
168+
169+
const {headers, body} = await got.post('seeOther', {body: 'hello'});
170+
t.is(body, '');
171+
t.is(headers['content-length'], '0');
172+
});
173+
159174
test('redirects on 303 response even on post, put, delete', withServer, async (t, server, got) => {
160175
server.get('/', reachedHandler);
161176

‎test/stream.ts

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ test('piping to got.stream.put()', withServer, async (t, server, got) => {
323323
});
324324

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

0 commit comments

Comments
 (0)
Please sign in to comment.