Skip to content

Commit

Permalink
fix: handle methodnotallowed with path variables (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyhadden committed May 22, 2020
1 parent ccb4c33 commit 5704d7e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,18 @@ func TestMuxNestedMethodNotAllowed(t *testing.T) {
w.Write([]byte("sub2"))
})

pathVar := NewRouter()
pathVar.Get("/{var}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pv"))
})
pathVar.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(405)
w.Write([]byte("pv 405"))
})

r.Mount("/prefix1", sr1)
r.Mount("/prefix2", sr2)
r.Mount("/pathVar", pathVar)

ts := httptest.NewServer(r)
defer ts.Close()
Expand All @@ -441,6 +451,12 @@ func TestMuxNestedMethodNotAllowed(t *testing.T) {
if _, body := testRequest(t, ts, "PUT", "/prefix2/sub2", nil); body != "root 405" {
t.Fatalf(body)
}
if _, body := testRequest(t, ts, "GET", "/pathVar/myvar", nil); body != "pv" {
t.Fatalf(body)
}
if _, body := testRequest(t, ts, "DELETE", "/pathVar/myvar", nil); body != "pv 405" {
t.Fatalf(body)
}
}

func TestMuxComplicatedNotFound(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
return xn
}

// flag that the routing context found a route, but not a corresponding
// supported method
rctx.methodNotAllowed = true
}
}

Expand Down

0 comments on commit 5704d7e

Please sign in to comment.