Skip to content

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

Merged
ReneWerner87 merged 7 commits intogofiber:mainfrom
andradei:main
May 12, 2025
Merged

♻️ refactor: Change c.Redirect() default status#3415
ReneWerner87 merged 7 commits intogofiber:mainfrom
andradei:main

Conversation

@andradei
Copy link
Contributor

@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
@welcome
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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 19, 2025

Walkthrough

The changes update the default HTTP status code for redirects from 302 (Found) to 303 (See Other) throughout the codebase and documentation. This includes updating comments, documentation, tests, and example usages to reflect the new default. No changes were made to method signatures or core redirect logic.

Changes

File(s) Change Summary
docs/api/redirect.md, docs/extra/internal.md, docs/whats_new.md Updated documentation to reflect the new default redirect status code (303 See Other).
redirect.go, ctx.go, ctx_interface_gen.go Changed default status code in comments and code from 302 to 303 for redirects.
redirect_test.go Updated all test assertions and references from 302 to 303; added context release in a test.
middleware/expvar/expvar_test.go, middleware/pprof/pprof_test.go Updated test paths and assertions to use 303 instead of 302 for redirect tests.
docs/middleware/redirect.md Replaced hardcoded status code 301 with constant fiber.StatusMovedPermanently in examples.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FiberApp
    participant RedirectHandler

    Client->>FiberApp: HTTP Request (any method)
    FiberApp->>RedirectHandler: Route match
    RedirectHandler->>FiberApp: c.Redirect("/some/path")
    FiberApp-->>Client: HTTP 303 See Other with Location header
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure c.Redirect respects c.Method override (Issue #3405) No logic changes were made to address method override; only the default status code was updated.

Poem

A hop, a skip, a redirect anew—
From 302 to 303, we bid adieu!
Docs and tests now all agree,
"See Other" is the way to be.
The code is neat, the status clear,
A rabbit’s cheer for changes here! 🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


🪧 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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
Contributor

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
Contributor 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
Contributor 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.

@codecov
Copy link

codecov bot commented Apr 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.07%. Comparing base (057647a) to head (b629fe9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3415      +/-   ##
==========================================
+ Coverage   83.97%   84.07%   +0.09%     
==========================================
  Files         119      119              
  Lines       11897    11897              
==========================================
+ Hits         9991    10002      +11     
+ Misses       1479     1469      -10     
+ Partials      427      426       -1     
Flag Coverage Δ
unittests 84.07% <100.00%> (+0.09%) ⬆️

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"

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/debug/vars/303", nil))
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

@ReneWerner87
Copy link
Member

@andradei can you fix the review hints

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

🧹 Nitpick comments (1)
docs/whats_new.md (1)

837-838: Correct grammar in caching description

The phrase “define a custom conditions” is grammatically incorrect. Consider one of the following revisions:

- ... allowing you to define a custom conditions for invalidating cache entries.
+ ... allowing you to define custom conditions for invalidating cache entries.

or

- ... allowing you to define a custom conditions for invalidating cache entries.
+ ... allowing you to define a custom condition for invalidating cache entries.
🧰 Tools
🪛 LanguageTool

[grammar] ~837-~837: 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)

📜 Review details

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

📥 Commits

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

📒 Files selected for processing (4)
  • docs/middleware/redirect.md (1 hunks)
  • docs/whats_new.md (5 hunks)
  • middleware/expvar/expvar_test.go (1 hunks)
  • redirect_test.go (17 hunks)
✅ Files skipped from review due to trivial changes (1)
  • docs/middleware/redirect.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • middleware/expvar/expvar_test.go
  • redirect_test.go
🧰 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`.
🪛 LanguageTool
docs/whats_new.md

[grammar] ~837-~837: 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)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: unit (1.23.x, macos-latest)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: Compare
🔇 Additional comments (3)
docs/whats_new.md (3)

601-608: Approve updated redirect behavior documentation

The new "Changed behavior" section clearly communicates the switch from 302 Found to 303 See Other and aligns with the code changes and cross-browser expectations.


300-303: Skip formatting-only change

This removal of trailing whitespace in the app.Use example does not affect the content or functionality.


957-959: Skip formatting-only change

This whitespace cleanup in the logger middleware example has no impact on the documentation’s meaning.

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

♻️ Duplicate comments (1)
docs/whats_new.md (1)

259-259: Removed extraneous '+' in the route signature example.
The stray plus sign in this code snippet has been removed, improving consistency with the surrounding diff blocks.

🧹 Nitpick comments (1)
docs/whats_new.md (1)

849-849: Grammar: remove extraneous 'a' before plural noun.
In the caching middleware description, update “define a custom conditions” to “define custom conditions” (or “define a custom condition”).

🧰 Tools
🪛 LanguageTool

[grammar] ~849-~849: 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)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2d29229 and b629fe9.

📒 Files selected for processing (1)
  • docs/whats_new.md (5 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/whats_new.md

[grammar] ~849-~849: 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)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Compare
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: repeated
🔇 Additional comments (1)
docs/whats_new.md (1)

601-608: Validate the new “Changed behavior” section for redirects.
This section succinctly explains the default status change from 302 Found to 303 See Other. Please verify that all related documentation—particularly docs/api/redirect.md and any code examples—are updated to reflect this new default.

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.

👍 LGTM

@ReneWerner87 ReneWerner87 merged commit 4321dfe into gofiber:main May 12, 2025
17 of 18 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 May 12, 2025
efectn pushed a commit to ckoch786/fiber that referenced this pull request May 15, 2025
* Set default redirect response status to 303 SeeOther

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.

* Add tests to Redirect default status change.

* Update docs.

* Fix remaining tests to reflect redirect 303 status as the new default. Reflect that in docs/whats_new.md

* Update redirect_test.go

* Fix code review hints

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
Co-authored-by: René <rene@gofiber.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

4 participants