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

Extend Handle method to parse HTTP method in pattern #897

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler) {
// Handle adds the route `pattern` that matches any http method to
// execute the `handler` http.Handler.
func (mx *Mux) Handle(pattern string, handler http.Handler) {
parts := strings.SplitN(pattern, " ", 2)
if len(parts) == 2 {
methodStr := strings.ToUpper(parts[0])
path := parts[1]

method, ok := methodMap[methodStr]
if !ok {
panic("chi: invalid HTTP method specified in pattern: " + methodStr)
Spartan09 marked this conversation as resolved.
Show resolved Hide resolved
}

mx.handle(method, path, handler)
return
}

mx.handle(mALL, pattern, handler)
}

Expand Down
75 changes: 75 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,81 @@ func TestMuxWith(t *testing.T) {
}
}

func TestMuxHandlePatternValidation(t *testing.T) {
testCases := []struct {
name string
pattern string
shouldPanic bool
method string // Method to be used for the test request
path string // Path to be used for the test request
expectedBody string // Expected response body
expectedStatus int // Expected HTTP status code
}{
// Valid patterns
{
name: "Valid pattern without HTTP GET",
pattern: "/user/{id}",
shouldPanic: false,
method: "GET",
path: "/user/123",
expectedBody: "without-prefix GET",
expectedStatus: http.StatusOK,
},
{
name: "Valid pattern with HTTP POST",
pattern: "POST /products/{id}",
shouldPanic: false,
method: "POST",
path: "/products/456",
expectedBody: "with-prefix POST",
expectedStatus: http.StatusOK,
},
// Invalid patterns
{
name: "Invalid pattern with no method",
pattern: "INVALID/user/{id}",
shouldPanic: true,
},
{
name: "Invalid pattern with supported method",
pattern: "GET/user/{id}",
shouldPanic: true,
},
{
name: "Invalid pattern with unsupported method",
pattern: "UNSUPPORTED /unsupported-method",
shouldPanic: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && !tc.shouldPanic {
t.Errorf("Unexpected panic for pattern %s", tc.pattern)
}
}()

r := NewRouter()
r.Handle(tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(tc.expectedBody))
}))

if !tc.shouldPanic {
// Use testRequest for valid patterns
ts := httptest.NewServer(r)
defer ts.Close()

resp, body := testRequest(t, ts, tc.method, tc.path, nil)
if body != tc.expectedBody || resp.StatusCode != tc.expectedStatus {
t.Errorf("Expected status %d and body %s; got status %d and body %s for pattern %s",
tc.expectedStatus, tc.expectedBody, resp.StatusCode, body, tc.pattern)
}
}
})
}
}

func TestRouterFromMuxWith(t *testing.T) {
t.Parallel()

Expand Down