fix(router): preserve HTTP method when handling double-slash paths #2649
+23
−179
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2643
This PR addresses a critical routing bug where HTTP POST requests with double-slash paths (
//hello) are incorrectly routed to GET handlers when both methods are registered for the same endpoint.Problem Statement
Current Behavior:
Impact:
Root Cause
The router initialization uses
StrictSlash(false), causing Gorilla Mux to silently normalize paths with double slashes. During this normalization, the HTTP method context is lost, and the router defaults to the first registered handler for the normalized path.Solution
Enable
StrictSlash(true)in router configuration to handle path normalization via HTTP 301 redirects instead of silent normalization. This preserves the HTTP method throughout the redirect flow.After Fix:
Changes
pkg/gofr/http/router.goStrictSlash(false)→StrictSlash(true)pkg/gofr/http/router_test.goTestDoubleSlashRoutingTesting
Unit Tests
/hello) work without redirectsManual Testing
Backward Compatibility
Checklist
Additional Notes
This fix aligns with standard HTTP router behavior (similar to Express.js, Gin, etc.) where path normalization is handled via redirects rather than silent rewriting.