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

Fix incorrect response object being emitted on redirect #1761

Merged
merged 1 commit into from
Jan 25, 2023
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
15 changes: 14 additions & 1 deletion src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,13 @@ Request.prototype._redirect = function (res) {
_initHeaders(this);

// redirect
this.res = res;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@titanism This is the main change I'm not super certain about. Without doing this, a couple of the tests around .pipe() fail because there's no this.res on the response. I don't know if this assignment has the potential to screw anything else up.

this._endCalled = false;
this.url = url;
this.qs = {};
this._query.length = 0;
this.set(headers);
this.emit('redirect', res);
this._emitRedirect();
this._redirectList.push(this.url);
this.end(this._callback);
return this;
Expand Down Expand Up @@ -969,6 +970,18 @@ Request.prototype._emitResponse = function (body, files) {
return response;
};

/**
* Emit `redirect` event, passing an instanceof `Response`.
*
* @api private
*/

Request.prototype._emitRedirect = function () {
const response = new Response(this);
response.redirects = this._redirectList;
this.emit('redirect', response);
};

Request.prototype.end = function (fn) {
this.request();
debug('%s %s', this.method, this.url);
Expand Down
19 changes: 19 additions & 0 deletions test/node/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ describe('request', () => {
});
});

it('should overwrite previously set cookie during a redirect when agent is used', (done) => {
const agent = request.agent();
agent.get(`${base}/set-cookie`).end((error) => {
assert.ifError(error);
agent
.get(`${base}/cookie-redirect`)
.redirects(1)
.end((error, res) => {
try {
assert.ifError(error);
assert(/replaced=yes/.test(res.text), 'replaced=yes');
done();
} catch (err) {
done(err);
}
});
});
})

it('should follow Location', (done) => {
const redirects = [];

Expand Down
1 change: 1 addition & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ app.get('/cookie-redirect', (request, res) => {
});

app.get('/set-cookie', (request, res) => {
res.cookie('replaced', 'no')
res.cookie('persist', '123');
res.send('ok');
});
Expand Down