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

Fixed the bug in redirect exposed in #1049 #1051

Merged
merged 7 commits into from
Jul 24, 2018
Merged

Conversation

ShraddheyaS
Copy link
Contributor

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

Copy link
Member

@mcollina mcollina left a 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
Copy link
Member

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']
}
Copy link
Member

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
Copy link
Member

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 :)

Copy link
Member

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:

fastify/lib/reply.js

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 = {}
}
The default value should be false.

@delvedor delvedor added the bugfix Issue or PR that should land as semver patch label Jul 23, 2018
Copy link
Member

@delvedor delvedor left a 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.

@ShraddheyaS
Copy link
Contributor Author

While writing the unit tests, does the case like res.code(302).redirect(200, '/') need to be handled?

@mcollina
Copy link
Member

Yes I think so

lib/reply.js Outdated
finalCode = code
}

this.header('location', finalUrl).code(finalCode).send()
Copy link
Member

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?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bugfix Issue or PR that should land as semver patch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants