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

Invalid redirect in pipe #1471

Merged
merged 2 commits into from
Apr 28, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ coverage
.nyc_output
lib
dist
*.swp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added *.swp to the gitignore for vim files

10 changes: 7 additions & 3 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,13 @@ Request.prototype.pipe = function(stream, options) {
Request.prototype._pipeContinue = function(stream, options) {
this.req.once('response', res => {
// redirect
const redirect = isRedirect(res.statusCode);
if (redirect && this._redirects++ !== this._maxRedirects) {
return this._redirect(res)._pipeContinue(stream, options);
if (
isRedirect(res.statusCode) &&
this._redirects++ !== this._maxRedirects
) {
return this._redirect(res) === this
? this._pipeContinue(stream, options)
: undefined;
}

this.res = res;
Expand Down
53 changes: 53 additions & 0 deletions test/node/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ app.get('/', (req, res) => {
fs.createReadStream('test/node/fixtures/user.json').pipe(res);
});

app.get('/redirect', (req, res) => {
res.set('Location', '/').sendStatus(302);
});

app.get('/badRedirectNoLocation', (req, res) => {
res.set('Location', '').sendStatus(302);
});

app.post('/', (req, res) => {
if (process.env.HTTP2_TEST) {
// body-parser does not support http2 yet.
Expand Down Expand Up @@ -93,4 +101,49 @@ describe('request pipe', () => {
});
req.pipe(stream);
});

it('should follow redirects', done => {
const stream = fs.createWriteStream(destPath);

let responseCalled = false;
const req = request.get(base + '/redirect');
req.type('json');

req.on('response', res => {
res.status.should.eql(200);
responseCalled = true;
});
stream.on('finish', () => {
JSON.parse(fs.readFileSync(destPath, 'utf8')).should.eql({
name: 'tobi'
});
responseCalled.should.be.true();
done();
});
req.pipe(stream);
});

it('should not throw on bad redirects', done => {
const stream = fs.createWriteStream(destPath);

let responseCalled = false;
let errorCalled = false;
const req = request.get(base + '/badRedirectNoLocation');
req.type('json');

req.on('response', res => {
responseCalled = true;
});
req.on('error', err => {
err.message.should.eql('No location header for redirect');
errorCalled = true;
stream.end();
});
stream.on('finish', () => {
responseCalled.should.be.false();
errorCalled.should.be.true();
done();
});
req.pipe(stream);
});
});