Skip to content

Commit

Permalink
Extend Handle method to parse HTTP method in pattern (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spartan09 authored Jan 18, 2024
1 parent f6b97ed commit 7446950
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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)
}

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

0 comments on commit 7446950

Please sign in to comment.