trout
3:3df515f0cec5 Browse Files
Fix bug in handling of finding closest match for params. We had a bug that finding the closest match for params would only work if the parent was also a param. This lead to problems where multiple methods declared on a single endpoint as follows were treated as multiple endpoints: router.Endpoint("/{id}").Methods("GET").Handler(handleGet) router.Endpoint("/{id}").Methods("POST").Handler(handlePost) This caused erroneous Method Not Allowed errors, as the POST method would be part of an Endpoint that could never be routed to. We also discovered that Method Not Allowed errors weren't returning the required Allow header with a list of acceptable methods, so this has been fixed.
1.1 --- a/route.go Sun Dec 13 20:48:26 2015 -0800 1.2 +++ b/route.go Sat Jan 02 15:42:57 2016 -0800 1.3 @@ -14,6 +14,7 @@ 1.4 return 1.5 })) 1.6 default405Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1.7 + w.Header().Set("Allow", strings.Join(r.Header[http.CanonicalHeaderKey("Trout-Methods")], ", ")) 1.8 w.WriteHeader(http.StatusMethodNotAllowed) 1.9 w.Write([]byte("405 Method Not Allowed")) 1.10 return 1.11 @@ -217,7 +218,7 @@ 1.12 func pickNextRoute(b *branch, offset int, input string, variable bool) int { 1.13 count := len(b.children) 1.14 for i := offset; i < count; i++ { 1.15 - if b.children[i].key == input && b.isParam == variable { 1.16 + if b.children[i].key == input && b.children[i].isParam == variable { 1.17 return i 1.18 } 1.19 }