From 5704d7ee98edd3fe55169b506531bdd061667c70 Mon Sep 17 00:00:00 2001 From: Casey Hadden Date: Fri, 22 May 2020 09:51:03 -0400 Subject: [PATCH] fix: handle methodnotallowed with path variables (#512) --- mux_test.go | 16 ++++++++++++++++ tree.go | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/mux_test.go b/mux_test.go index 4cf96a2f..e5bff29b 100644 --- a/mux_test.go +++ b/mux_test.go @@ -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() @@ -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) { diff --git a/tree.go b/tree.go index a7e29c5b..59b5b5f7 100644 --- a/tree.go +++ b/tree.go @@ -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 } }