-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fixed the bug in redirect exposed in #1049 #1051
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Would you mind adding some unit tests as well?
lib/reply.js
Outdated
@@ -122,6 +122,7 @@ Reply.prototype.headers = function (headers) { | |||
|
|||
Reply.prototype.code = function (code) { | |||
this.res.statusCode = code | |||
this._headers['statusCode'] = code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please use a _hasStatusCode = true
property instead?
lib/reply.js
Outdated
@@ -147,6 +148,10 @@ Reply.prototype.redirect = function (code, url) { | |||
code = 302 | |||
} | |||
|
|||
if (this._headers.hasOwnProperty('statusCode')) { | |||
code = this._headers['statusCode'] | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid this block, and avoid calling code(code)
.
lib/reply.js
Outdated
@@ -122,6 +122,7 @@ Reply.prototype.headers = function (headers) { | |||
|
|||
Reply.prototype.code = function (code) { | |||
this.res.statusCode = code | |||
this._headers['_hasStatusCode'] = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can directly use this._hasStatusCode
, without changing the headers object :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must initialize it here:
Lines 24 to 33 in 3696096
function Reply (res, context, request) { | |
this.res = res | |
this.context = context | |
this.sent = false | |
this._serializer = null | |
this._customError = false | |
this._isError = false | |
this.request = request | |
this._headers = {} | |
} |
false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also add
this._hasStatusCode = false
to the Reply constructor.
While writing the unit tests, does the case like |
Yes I think so |
…atus passed in redirect call)
lib/reply.js
Outdated
finalCode = code | ||
} | ||
|
||
this.header('location', finalUrl).code(finalCode).send() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can refactor the function in this way:
Reply.prototype.redirect = function (code, url) {
if (typeof code === 'string') {
url = code
code = this._hasStatusCode ? this.res.statusCode : 302
}
this.header('location', url).code(code).send()
}
Does it work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Checklist
npm run test
andnpm run benchmark