Skip to content

Commit

Permalink
Add test case and proposed fix for path component with trailing colon…
Browse files Browse the repository at this point in the history
… (and string) (grpc-ecosystem#708)

* If a pattern has no verb, match with a verb arg appended to last component

Parsing out a "verb" from the input path is a convenience for
parsing. Whether the colon and string are _actually_ a verb depends on
the matching HTTP rule. This change gives a verb-less pattern a chance
to match a path by assuming the colon+string suffix are a part of the
final component.

Fixes grpc-ecosystem#224

Signed-off-by: James Hamlin <[email protected]>
  • Loading branch information
jfhamlin authored and achew22 committed Aug 1, 2018
1 parent 209d7ce commit 7226a0d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"context"

"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down
32 changes: 32 additions & 0 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ func TestMuxServeHTTP(t *testing.T) {
respStatus: http.StatusOK,
respContent: "POST /foo:bar",
},
{
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"},
},
},
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"},
},
},
reqMethod: "GET",
reqPath: "/foo/bar:123",
headers: map[string]string{
"Content-Type": "application/json",
},
respStatus: http.StatusOK,
respContent: "GET /foo/{id=*}",
},
} {
mux := runtime.NewServeMux()
for _, p := range spec.patterns {
Expand Down
11 changes: 10 additions & 1 deletion runtime/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7226a0d

Please sign in to comment.