diff --git a/runtime/mux.go b/runtime/mux.go index 093373a204a..59204f98cf4 100644 --- a/runtime/mux.go +++ b/runtime/mux.go @@ -1,11 +1,11 @@ package runtime import ( - "context" "fmt" "net/http" "net/textproto" "strings" + "context" "github.com/golang/protobuf/proto" "google.golang.org/grpc/codes" diff --git a/runtime/mux_test.go b/runtime/mux_test.go index e033091a885..bddb73b5cee 100644 --- a/runtime/mux_test.go +++ b/runtime/mux_test.go @@ -228,20 +228,30 @@ func TestMuxServeHTTP(t *testing.T) { ops: []int{int(utilities.OpLitPush), 0, int(utilities.OpPush), 0, int(utilities.OpConcatN), 1, int(utilities.OpCapture), 1}, pool: []string{"foo", "id"}, }, + }, + reqMethod: "GET", + reqPath: "/foo/bar", + headers: map[string]string{ + "Content-Type": "application/json", + }, + respStatus: http.StatusOK, + respContent: "GET /foo/{id=*}", + }, + { + patterns: []stubPattern{ { method: "GET", ops: []int{int(utilities.OpLitPush), 0, int(utilities.OpPush), 0, int(utilities.OpConcatN), 1, int(utilities.OpCapture), 1}, pool: []string{"foo", "id"}, - verb: "verb", }, }, reqMethod: "GET", - reqPath: "/foo/bar:verb", + reqPath: "/foo/bar:123", headers: map[string]string{ "Content-Type": "application/json", }, respStatus: http.StatusOK, - respContent: "GET /foo/{id=*}:verb", + respContent: "GET /foo/{id=*}", }, { // mux identifying invalid path results in 'Not Found' status diff --git a/runtime/pattern.go b/runtime/pattern.go index f16a84ad389..223a12ecc43 100644 --- a/runtime/pattern.go +++ b/runtime/pattern.go @@ -144,7 +144,16 @@ func MustPattern(p Pattern, err error) Pattern { // If otherwise, the function returns an error. func (p Pattern) Match(components []string, verb string) (map[string]string, error) { if p.verb != verb { - return nil, ErrNotMatch + if p.verb != "" { + return nil, ErrNotMatch + } + if len(components) == 0 { + components = []string{":" + verb} + } else { + components = append([]string{}, components...) + components[len(components)-1] += ":" + verb + } + verb = "" } var pos int