Skip to content

Commit 7446950

Browse files
authored
Extend Handle method to parse HTTP method in pattern (#897)
1 parent f6b97ed commit 7446950

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

mux.go

+14
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler) {
107107
// Handle adds the route `pattern` that matches any http method to
108108
// execute the `handler` http.Handler.
109109
func (mx *Mux) Handle(pattern string, handler http.Handler) {
110+
parts := strings.SplitN(pattern, " ", 2)
111+
if len(parts) == 2 {
112+
methodStr := strings.ToUpper(parts[0])
113+
path := parts[1]
114+
115+
method, ok := methodMap[methodStr]
116+
if !ok {
117+
panic("chi: invalid HTTP method specified in pattern: " + methodStr)
118+
}
119+
120+
mx.handle(method, path, handler)
121+
return
122+
}
123+
110124
mx.handle(mALL, pattern, handler)
111125
}
112126

mux_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,81 @@ func TestMuxWith(t *testing.T) {
640640
}
641641
}
642642

643+
func TestMuxHandlePatternValidation(t *testing.T) {
644+
testCases := []struct {
645+
name string
646+
pattern string
647+
shouldPanic bool
648+
method string // Method to be used for the test request
649+
path string // Path to be used for the test request
650+
expectedBody string // Expected response body
651+
expectedStatus int // Expected HTTP status code
652+
}{
653+
// Valid patterns
654+
{
655+
name: "Valid pattern without HTTP GET",
656+
pattern: "/user/{id}",
657+
shouldPanic: false,
658+
method: "GET",
659+
path: "/user/123",
660+
expectedBody: "without-prefix GET",
661+
expectedStatus: http.StatusOK,
662+
},
663+
{
664+
name: "Valid pattern with HTTP POST",
665+
pattern: "POST /products/{id}",
666+
shouldPanic: false,
667+
method: "POST",
668+
path: "/products/456",
669+
expectedBody: "with-prefix POST",
670+
expectedStatus: http.StatusOK,
671+
},
672+
// Invalid patterns
673+
{
674+
name: "Invalid pattern with no method",
675+
pattern: "INVALID/user/{id}",
676+
shouldPanic: true,
677+
},
678+
{
679+
name: "Invalid pattern with supported method",
680+
pattern: "GET/user/{id}",
681+
shouldPanic: true,
682+
},
683+
{
684+
name: "Invalid pattern with unsupported method",
685+
pattern: "UNSUPPORTED /unsupported-method",
686+
shouldPanic: true,
687+
},
688+
}
689+
690+
for _, tc := range testCases {
691+
t.Run(tc.name, func(t *testing.T) {
692+
defer func() {
693+
if r := recover(); r != nil && !tc.shouldPanic {
694+
t.Errorf("Unexpected panic for pattern %s", tc.pattern)
695+
}
696+
}()
697+
698+
r := NewRouter()
699+
r.Handle(tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
700+
w.Write([]byte(tc.expectedBody))
701+
}))
702+
703+
if !tc.shouldPanic {
704+
// Use testRequest for valid patterns
705+
ts := httptest.NewServer(r)
706+
defer ts.Close()
707+
708+
resp, body := testRequest(t, ts, tc.method, tc.path, nil)
709+
if body != tc.expectedBody || resp.StatusCode != tc.expectedStatus {
710+
t.Errorf("Expected status %d and body %s; got status %d and body %s for pattern %s",
711+
tc.expectedStatus, tc.expectedBody, resp.StatusCode, body, tc.pattern)
712+
}
713+
}
714+
})
715+
}
716+
}
717+
643718
func TestRouterFromMuxWith(t *testing.T) {
644719
t.Parallel()
645720

0 commit comments

Comments
 (0)