Skip to content

♻️ refactor: Change c.Redirect() default status #3415

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

andradei
Copy link

@andradei andradei commented Apr 19, 2025

Fixes #3405

In some browsers, redirect status 302 Found sometimes is used to change the HTTP verb of the response from what the user set to what was used in the request. Changing to 303 SeeOther in the default works more like expected: it defaults to GET and can be overriden by the user.

Closes gofiber#3405

In some browsers, redirect status 302 Found sometimes is used to change
the HTTP verb of the response from what the user set to what was used in
the request. Changing to 303 SeeOther in the default works more like
expected: it defaults to GET and can be overriden by the user.
@andradei andradei requested a review from a team as a code owner April 19, 2025 00:12
Copy link

welcome bot commented Apr 19, 2025

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Copy link
Contributor

coderabbitai bot commented Apr 19, 2025

Walkthrough

The changes update the default HTTP status code for redirects in the Fiber framework from 302 (Found) to 303 (See Other). This update is reflected in the documentation, code comments, struct field defaults, and all related unit and benchmark tests. The method signatures and core logic remain unchanged, with only the default status code and associated documentation/comments being modified to use 303 instead of 302.

Changes

File(s) Change Summary
docs/api/redirect.md, docs/extra/internal.md, docs/whats_new.md Updated documentation to state the default redirect status code is now 303 (See Other) instead of 302 (Found); updated example comments and notes accordingly.
redirect.go, ctx.go, ctx_interface_gen.go Changed the default redirect status code in the implementation and comments from 302 to 303.
redirect_test.go, middleware/expvar/expvar_test.go, middleware/pprof/pprof_test.go Updated all test and benchmark assertions to expect 303 status code instead of 302 for redirects and related paths.
middleware/redirect/config.go Updated comment for default redirect middleware status code from 302 to 303; actual default value remains 302.

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Respect c.Method override in c.Redirect (Issue #3405) The changes only update the default status code; method override is not addressed.

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • gaby
  • efectn

Poem

In the land of HTTP, a new code takes the lead,
303 See Other, the rabbits decreed!
Docs and tests now hop in line,
Redirects shine with a status so fine.
Though the method bug remains to mend,
This patch brings clarity—onward, friend!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b17b5ad and cf68f93.

📒 Files selected for processing (1)
  • redirect_test.go (17 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • redirect_test.go
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: Analyse
  • GitHub Check: unit (1.23.x, macos-13)
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.24.x, ubuntu-latest)
  • GitHub Check: unit (1.24.x, macos-latest)
  • GitHub Check: unit (1.23.x, macos-latest)
  • GitHub Check: lint
  • GitHub Check: repeated
  • GitHub Check: Compare

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request updates the default redirection status code from 302 Found to 303 See Other to prevent potential HTTP verb change issues in some browsers. Key changes include:

  • Updating all redirect tests to expect 303 instead of 302.
  • Changing the default status value in redirect.go and ensuring it is reset accordingly.
  • Revising documentation in docs/api/redirect.md to reflect the default change.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
redirect_test.go Updated test assertions to check for 303 status code.
redirect.go Changed default status to StatusSeeOther and updated reset.
docs/api/redirect.md Updated documentation to reference 303 See Other.
Comments suppressed due to low confidence (1)

redirect_test.go:25

  • [nitpick] Consider using the defined StatusSeeOther constant instead of the literal 303 in the test assertions to improve consistency with the rest of the codebase.
require.Equal(t, 303, c.Response().StatusCode())

@gaby gaby changed the title ♻️ Improve default: Change c.Redirect default status to 303 See Other ♻️ refactor: Change c.Redirect() default status Apr 19, 2025
@gaby gaby added this to v3 Apr 19, 2025
@gaby gaby added this to the v3 milestone Apr 19, 2025
@gaby gaby moved this to In Progress in v3 Apr 19, 2025
@andradei
Copy link
Author

Tests passed on my linux host with no issues, not sure what caused the error in ubuntu-latest except it looks like it ran an older commit for its tests.

@gaby
Copy link
Member

gaby commented Apr 19, 2025

@andradei How are you running the tests? What's your command?

@andradei
Copy link
Author

andradei commented Apr 19, 2025

@gaby just go test. I ran go test ./... and fixed the remaining failing errors.
I also added the change to docs/whats_new.md.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
middleware/redirect/config.go (1)

28-34: ⚠️ Potential issue

Inconsistent default status code: comment and code mismatch.

The comment specifies "Default: 303 See Other", but ConfigDefault.StatusCode remains set to fiber.StatusFound (302). This discrepancy needs to be resolved by updating the default value to fiber.StatusSeeOther (303).

Suggested diff:

 var ConfigDefault = Config{
-   StatusCode: fiber.StatusFound,
+   StatusCode: fiber.StatusSeeOther,
 }
🧹 Nitpick comments (2)
docs/whats_new.md (1)

832-834: Grammar refinement: remove stray article
In “allowing you to define a custom conditions”, drop the “a” before the plural noun “conditions” to read “…define custom conditions…”.

🧰 Tools
🪛 LanguageTool

[grammar] ~832-~832: Do not use the singular ‘a’ before the plural noun ‘conditions’.
Context: ...ache management, allowing you to define a custom conditions for invalidating cache entries. Additio...

(VB_A_JJ_NNS)

redirect_test.go (1)

25-27: Tests correctly expect 303 See Other but use magic number
All require.Equal(..., 303, ...) assertions align with the new default redirect code. For clarity and maintainability, consider replacing the literal 303 with the fiber.StatusSeeOther constant.

Also applies to: 42-44, 71-73, 91-93, 115-117, 130-132, 149-151, 164-166, 185-187, 214-217, 236-238, 274-276, 309-311, 353-355, 484-486, 511-514, 539-541

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 85cce3c and b17b5ad.

📒 Files selected for processing (10)
  • ctx.go (1 hunks)
  • ctx_interface_gen.go (1 hunks)
  • docs/api/redirect.md (5 hunks)
  • docs/extra/internal.md (1 hunks)
  • docs/whats_new.md (5 hunks)
  • middleware/expvar/expvar_test.go (1 hunks)
  • middleware/pprof/pprof_test.go (2 hunks)
  • middleware/redirect/config.go (1 hunks)
  • redirect.go (3 hunks)
  • redirect_test.go (17 hunks)
🧰 Additional context used
🧠 Learnings (1)
docs/whats_new.md (1)
Learnt from: ckoch786
PR: gofiber/fiber#3230
File: docs/whats_new.md:944-951
Timestamp: 2024-12-15T19:56:45.935Z
Learning: Detailed usage examples and explanations for new methods like `RemoveRoute` and `RemoveRouteByName` are documented in `docs/api/app.md`, so it's unnecessary to duplicate them in `docs/whats_new.md`.
🧬 Code Graph Analysis (3)
middleware/expvar/expvar_test.go (1)
constants.go (1)
  • MethodGet (5-5)
middleware/pprof/pprof_test.go (1)
constants.go (2)
  • MethodGet (5-5)
  • StatusSeeOther (68-68)
redirect.go (1)
constants.go (1)
  • StatusSeeOther (68-68)
🪛 LanguageTool
docs/whats_new.md

[grammar] ~832-~832: Do not use the singular ‘a’ before the plural noun ‘conditions’.
Context: ...ache management, allowing you to define a custom conditions for invalidating cache entries. Additio...

(VB_A_JJ_NNS)

🔇 Additional comments (13)
middleware/expvar/expvar_test.go (1)

84-84: Test expectations updated to match new default redirect status.

The test for /debug/vars/303 now correctly asserts resp.StatusCode == 303, aligning with the change from 302 to 303.

Also applies to: 86-86

docs/extra/internal.md (1)

346-346: Documentation updated to reflect new default redirect status.

The description now mentions "defaulting to 303 See Other", which is consistent with the framework-wide update.

ctx.go (1)

1316-1317: Verify implementation aligns with updated comment.

The comment for DefaultCtx.Redirect() now states the default status is 303 See Other. Please confirm that AcquireRedirect() (in redirect.go) initializes Redirect.StatusCode to fiber.StatusSeeOther rather than the previous 302.

ctx_interface_gen.go (1)

254-255: Interface comment updated: validate underlying behavior.

The generated Ctx.Redirect() interface documentation now states the default status is 303 See Other. Ensure that the concrete implementation honors this new default status code.

middleware/pprof/pprof_test.go (1)

156-159: Tests updated to 303 See Other as expected default redirect status
Testing for /debug/pprof/303 now correctly asserts fiber.StatusSeeOther. Matches the framework change.

docs/api/redirect.md (3)

17-19: Documentation default status updated to 303 See Other
The info block now correctly indicates that when no status is specified, redirects default to 303 See Other.


37-44: Example comments reflect new default redirect code
The inline examples under “More examples” have been updated from 302 to 303, aligning sample output with the new default.


94-96: Back method documentation default adjusted
The Back section’s info box now correctly shows 303 See Other as the default status.

docs/whats_new.md (1)

580-582: Release notes updated for default redirect status change
The “Redirect” section now mentions the default status change from 302 Found to 303 See Other for more predictable behavior.

redirect.go (4)

19-23: Default redirect status updated in pool initializer
The sync.Pool now instantiates Redirect with StatusSeeOther (303) instead of 302, aligning with the new default.


93-97: Reset behavior updated to new default status
The release() method correctly resets r.status to StatusSeeOther (303), ensuring reused instances start with the updated default.


62-65: Struct comment reflects new default status
The comment on the status field now accurately documents the default as 303 StatusSeeOther.


99-102: Status() comment updated to reflect new default
The doc comment for Status() now states defaulting to 303 See Other when unset.

Copy link

codecov bot commented Apr 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.93%. Comparing base (85cce3c) to head (cf68f93).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3415      +/-   ##
==========================================
+ Coverage   83.87%   83.93%   +0.06%     
==========================================
  Files         119      119              
  Lines       11892    11892              
==========================================
+ Hits         9974     9982       +8     
+ Misses       1488     1480       -8     
  Partials      430      430              
Flag Coverage Δ
unittests 83.93% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby gaby removed the ☢️ Bug label Apr 22, 2025
Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

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

Use fiber.StatusSeeOther instead of "302"

@@ -577,6 +577,9 @@ In this example, the `Bind` method is used to bind the request body to the `User

Fiber v3 enhances the redirect functionality by introducing new methods and improving existing ones. The new redirect methods provide more flexibility and control over the redirection process.

The default response status is changes from 302 Found to 303 See Other for more
Copy link
Member

Choose a reason for hiding this comment

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

This statement needs work. It should be a separate section.

@@ -256,7 +256,7 @@ The route method is now like [`Express`](https://expressjs.com/de/api.html#app.r

```diff
- Route(prefix string, fn func(router Router), name ...string) Router
+ Route(path string) Register
Copy link
Member

Choose a reason for hiding this comment

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

Why was this changed?

require.NoError(t, err)
require.Equal(t, 302, resp.StatusCode)
require.Equal(t, 303, resp.StatusCode)
Copy link
Member

Choose a reason for hiding this comment

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

Use fiber.StatusSeeOther


err := c.Redirect().To("http://default.com")
require.NoError(t, err)
require.Equal(t, 302, c.Response().StatusCode())
require.Equal(t, 303, c.Response().StatusCode())
Copy link
Member

Choose a reason for hiding this comment

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

Use fiber.StatusSeeOther

@@ -39,7 +40,7 @@ func Test_Redirect_To_WithFlashMessages(t *testing.T) {

err := c.Redirect().With("success", "2").With("success", "1").With("message", "test", 2).To("http://example.com")
require.NoError(t, err)
require.Equal(t, 302, c.Response().StatusCode())
require.Equal(t, 303, c.Response().StatusCode())
Copy link
Member

Choose a reason for hiding this comment

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

Use fiber.StatusSeeOther

@@ -68,7 +69,7 @@ func Test_Redirect_Route_WithParams(t *testing.T) {
},
})
require.NoError(t, err)
require.Equal(t, 302, c.Response().StatusCode())
require.Equal(t, 303, c.Response().StatusCode())
Copy link
Member

Choose a reason for hiding this comment

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

Use fiber.StatusSeeOther

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: c.Redirect doesn't respect c.Method
2 participants