Skip to content

Commit

Permalink
fix(router): fix method not allowed logic
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Apr 8, 2016
1 parent 5478607 commit 22936d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 1 addition & 3 deletions match.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package router

import (
"net/url"
)
import "net/url"

// Match matches the given path string againts the register pattern.
func Match(pat, path string) (url.Values, bool) {
Expand Down
3 changes: 3 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func (r *Router) match(method, path string) (url.Values, *Route) {
return doMatch(r.Routes["*"], path)
}

// doMatch is used to match a given path againts the registered routes pool.
func doMatch(routes []*Route, path string) (url.Values, *Route) {
if routes == nil || len(routes) == 0 {
return nil, nil
Expand Down Expand Up @@ -283,6 +284,8 @@ func (r *Router) HandleHTTP(w http.ResponseWriter, req *http.Request, h http.Han
h.ServeHTTP(w, req)
}

// isMethodNowAllowed is used to verify if the given route doesn't allows
// the given HTTP request method.
func (r *Router) isMethodNotAllowed(w http.ResponseWriter, req *http.Request) bool {
allowed := make([]string, 0, len(r.Routes))
for meth, handlers := range r.Routes {
Expand Down
14 changes: 12 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestRoutingHit(t *testing.T) {

func TestRoutingMethodNotAllowed(t *testing.T) {
p := New()
p.ForceMethodNotAllowed = true

var ok bool
p.Post("/foo/:name").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -60,9 +61,13 @@ func TestRoutingMethodNotAllowed(t *testing.T) {
}))

r := httptest.NewRecorder()
p.HandleHTTP(r, newRequest("GET", "/foo/keith", nil), nil)
var final bool
p.HandleHTTP(r, newRequest("GET", "/foo/keith", nil), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
final = true
}))

st.Expect(t, ok, false)
st.Expect(t, final, false)
st.Expect(t, r.Code, http.StatusMethodNotAllowed)

got := strings.Split(r.Header().Get("Allow"), ", ")
Expand Down Expand Up @@ -146,7 +151,12 @@ func TestMethodPatch(t *testing.T) {
// issue a GET request to a handler that only supports the PATCH method.
res := httptest.NewRecorder()
res.Code = http.StatusMethodNotAllowed
p.HandleHTTP(res, newRequest("GET", "/foo/bar", nil), nil)
var final bool
p.HandleHTTP(res, newRequest("GET", "/foo/bar", nil), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
final = true
}))

st.Expect(t, final, true)
st.Expect(t, res.Code, http.StatusMethodNotAllowed)

// Now, test to see if we get a 200 OK from issuing a PATCH request to
Expand Down

0 comments on commit 22936d4

Please sign in to comment.