Skip to content

Commit

Permalink
feat: update HTTP method parsing in patterns for Handle and `Handle…
Browse files Browse the repository at this point in the history
…Func` (#900)
  • Loading branch information
angelofallars committed Feb 17, 2024
1 parent 9436cc8 commit 60b4f5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
16 changes: 7 additions & 9 deletions mux.go
Expand Up @@ -109,15 +109,7 @@ func (mx *Mux) Use(middlewares ...func(http.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)
mx.Method(parts[0], parts[1], handler)
return
}

Expand All @@ -127,6 +119,12 @@ func (mx *Mux) Handle(pattern string, handler http.Handler) {
// HandleFunc adds the route `pattern` that matches any http method to
// execute the `handlerFn` http.HandlerFunc.
func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
parts := strings.SplitN(pattern, " ", 2)
if len(parts) == 2 {
mx.Method(parts[0], parts[1], handlerFn)
return
}

mx.handle(mALL, pattern, handlerFn)
}

Expand Down
30 changes: 19 additions & 11 deletions mux_test.go
Expand Up @@ -691,24 +691,32 @@ func TestMuxHandlePatternValidation(t *testing.T) {
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)
t.Errorf("Unexpected panic for pattern %s:\n%v", tc.pattern, r)
}
}()

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

// Test that HandleFunc also handles method patterns
r2 := NewRouter()
r2.HandleFunc(tc.pattern, 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)
for _, r := range []Router{r1, r2} {
// 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)
}
}
}
})
Expand Down

0 comments on commit 60b4f5f

Please sign in to comment.