Merge pull request #54 from rogpeppe/001-custom-method-not-allowed
add support for custom MethodNotAllowed function
diff --git a/router_test.go b/router_test.go
index e5f3e09..75d2dfc 100644
--- a/router_test.go
+++ b/router_test.go
@@ -176,7 +176,7 @@
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if !(w.Code == http.StatusMethodNotAllowed) {
- t.Errorf("NotAllowed handling route %s failed: Code=%d, Header=%v", w.Code, w.Header())
+ t.Errorf("NotAllowed handling failed: Code=%d, Header=%v", w.Code, w.Header())
}
w = httptest.NewRecorder()
diff --git a/tree.go b/tree.go
index 121d0c3..c84a489 100644
--- a/tree.go
+++ b/tree.go
@@ -445,53 +445,52 @@
// without a trailing slash if a leaf exists for that path
found = (fixTrailingSlash && path == "/" && n.handle != nil)
return
+ }
- } else {
- n = n.children[0]
+ n = n.children[0]
+ switch n.nType {
+ case param:
+ // find param end (either '/' or path end)
+ k := 0
+ for k < len(path) && path[k] != '/' {
+ k++
+ }
- switch n.nType {
- case param:
- // find param end (either '/' or path end)
- k := 0
- for k < len(path) && path[k] != '/' {
- k++
- }
+ // add param value to case insensitive path
+ ciPath = append(ciPath, path[:k]...)
- // add param value to case insensitive path
- ciPath = append(ciPath, path[:k]...)
-
- // we need to go deeper!
- if k < len(path) {
- if len(n.children) > 0 {
- path = path[k:]
- n = n.children[0]
- continue
- } else { // ... but we can't
- if fixTrailingSlash && len(path) == k+1 {
- return ciPath, true
- }
- return
- }
- }
-
- if n.handle != nil {
- return ciPath, true
- } else if fixTrailingSlash && len(n.children) == 1 {
- // No handle found. Check if a handle for this path + a
- // trailing slash exists
+ // we need to go deeper!
+ if k < len(path) {
+ if len(n.children) > 0 {
+ path = path[k:]
n = n.children[0]
- if n.path == "/" && n.handle != nil {
- return append(ciPath, '/'), true
- }
+ continue
+ }
+
+ // ... but we can't
+ if fixTrailingSlash && len(path) == k+1 {
+ return ciPath, true
}
return
-
- case catchAll:
- return append(ciPath, path...), true
-
- default:
- panic("Invalid node type")
}
+
+ if n.handle != nil {
+ return ciPath, true
+ } else if fixTrailingSlash && len(n.children) == 1 {
+ // No handle found. Check if a handle for this path + a
+ // trailing slash exists
+ n = n.children[0]
+ if n.path == "/" && n.handle != nil {
+ return append(ciPath, '/'), true
+ }
+ }
+ return
+
+ case catchAll:
+ return append(ciPath, path...), true
+
+ default:
+ panic("Invalid node type")
}
} else {
// We should have reached the node containing the handle.