Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle methodnotallowed with path variables #512

Merged
merged 1 commit into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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