Skip to content

Commit

Permalink
Merge pull request #55 from hbchai/verbfix
Browse files Browse the repository at this point in the history
Fix parsing of verb and final path component.
  • Loading branch information
yugui committed Sep 15, 2015
2 parents 3507e29 + 73c8d4e commit d5405e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 21 additions & 3 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ 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) {
type stubPattern struct {
method string
ops []int
pool []string
verb string
}
for _, spec := range []struct {
patterns []stubPattern
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit d5405e7

Please sign in to comment.