From 73c8d4edf9e4c00995315c2dd9ff9cba90f13df2 Mon Sep 17 00:00:00 2001 From: Haw-Bin Chai Date: Mon, 14 Sep 2015 23:17:29 -0400 Subject: [PATCH] Fix parsing of verb and final path component. --- runtime/mux.go | 2 +- runtime/mux_test.go | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/runtime/mux.go b/runtime/mux.go index 3538fc3820f..a05c25234a0 100644 --- a/runtime/mux.go +++ b/runtime/mux.go @@ -45,7 +45,7 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } else if idx > 0 { c := components[l-1] - verb, components[l-1] = c[:idx], c[idx+1:] + components[l-1], verb = c[:idx], c[idx+1:] } if override := r.Header.Get("X-HTTP-Method-Override"); override != "" && isPathLengthFallback(r) { diff --git a/runtime/mux_test.go b/runtime/mux_test.go index d86eba23153..fcea9867874 100644 --- a/runtime/mux_test.go +++ b/runtime/mux_test.go @@ -7,8 +7,8 @@ import ( "net/http/httptest" "testing" - "github.com/gengo/grpc-gateway/utilities" "github.com/gengo/grpc-gateway/runtime" + "github.com/gengo/grpc-gateway/utilities" ) func TestMuxServeHTTP(t *testing.T) { @@ -16,6 +16,7 @@ func TestMuxServeHTTP(t *testing.T) { method string ops []int pool []string + verb string } for _, spec := range []struct { patterns []stubPattern @@ -158,13 +159,30 @@ func TestMuxServeHTTP(t *testing.T) { }, respStatus: http.StatusMethodNotAllowed, }, + { + patterns: []stubPattern{ + { + method: "POST", + ops: []int{int(utilities.OpLitPush), 0}, + pool: []string{"foo"}, + verb: "bar", + }, + }, + reqMethod: "POST", + reqPath: "/foo:bar", + headers: map[string]string{ + "Content-Type": "application/json", + }, + respStatus: http.StatusOK, + respContent: "POST /foo:bar", + }, } { mux := runtime.NewServeMux() for _, p := range spec.patterns { func(p stubPattern) { - pat, err := runtime.NewPattern(1, p.ops, p.pool, "") + pat, err := runtime.NewPattern(1, p.ops, p.pool, p.verb) if err != nil { - t.Fatalf("runtime.NewPattern(1, %#v, %#v, %q) failed with %v; want success", p.ops, p.pool, "", err) + t.Fatalf("runtime.NewPattern(1, %#v, %#v, %q) failed with %v; want success", p.ops, p.pool, p.verb, err) } mux.Handle(p.method, pat, func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { fmt.Fprintf(w, "%s %s", p.method, pat.String())