Skip to content

Commit 60b4f5f

Browse files
feat: update HTTP method parsing in patterns for Handle and HandleFunc (#900)
1 parent 9436cc8 commit 60b4f5f

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

mux.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler) {
109109
func (mx *Mux) Handle(pattern string, handler http.Handler) {
110110
parts := strings.SplitN(pattern, " ", 2)
111111
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)
112+
mx.Method(parts[0], parts[1], handler)
121113
return
122114
}
123115

@@ -127,6 +119,12 @@ func (mx *Mux) Handle(pattern string, handler http.Handler) {
127119
// HandleFunc adds the route `pattern` that matches any http method to
128120
// execute the `handlerFn` http.HandlerFunc.
129121
func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
122+
parts := strings.SplitN(pattern, " ", 2)
123+
if len(parts) == 2 {
124+
mx.Method(parts[0], parts[1], handlerFn)
125+
return
126+
}
127+
130128
mx.handle(mALL, pattern, handlerFn)
131129
}
132130

mux_test.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -691,24 +691,32 @@ func TestMuxHandlePatternValidation(t *testing.T) {
691691
t.Run(tc.name, func(t *testing.T) {
692692
defer func() {
693693
if r := recover(); r != nil && !tc.shouldPanic {
694-
t.Errorf("Unexpected panic for pattern %s", tc.pattern)
694+
t.Errorf("Unexpected panic for pattern %s:\n%v", tc.pattern, r)
695695
}
696696
}()
697697

698-
r := NewRouter()
699-
r.Handle(tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
698+
r1 := NewRouter()
699+
r1.Handle(tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
700700
w.Write([]byte(tc.expectedBody))
701701
}))
702702

703+
// Test that HandleFunc also handles method patterns
704+
r2 := NewRouter()
705+
r2.HandleFunc(tc.pattern, func(w http.ResponseWriter, r *http.Request) {
706+
w.Write([]byte(tc.expectedBody))
707+
})
708+
703709
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)
710+
for _, r := range []Router{r1, r2} {
711+
// Use testRequest for valid patterns
712+
ts := httptest.NewServer(r)
713+
defer ts.Close()
714+
715+
resp, body := testRequest(t, ts, tc.method, tc.path, nil)
716+
if body != tc.expectedBody || resp.StatusCode != tc.expectedStatus {
717+
t.Errorf("Expected status %d and body %s; got status %d and body %s for pattern %s",
718+
tc.expectedStatus, tc.expectedBody, resp.StatusCode, body, tc.pattern)
719+
}
712720
}
713721
}
714722
})

0 commit comments

Comments
 (0)