fix bind vars

pull/715/head
chenzhihui 4 years ago
parent f83c9ad053
commit 01d93ec61a
  1. 10
      cmd/protoc-gen-go-http/template.go
  2. 6
      transport/http/default.go
  3. 23
      transport/http/default_test.go

@ -18,11 +18,6 @@ func Register{{.ServiceType}}HTTPServer(s http1.ServiceRegistrar, srv {{.Service
{{range .Methods}}
func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (interface{}, error) {
var in {{.Request}}
{{if ne (len .Vars) 0}}
if err := http1.BindVars(req, &in); err != nil {
return nil, err
}
{{end}}
{{if eq .Body ""}}
if err := http1.BindForm(req, &in); err != nil {
return nil, err
@ -35,6 +30,11 @@ func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Con
if err := dec(in{{.Body}}); err != nil {
return nil, err
}
{{end}}
{{if ne (len .Vars) 0}}
if err := http1.BindVars(req, &in); err != nil {
return nil, err
}
{{end}}
out, err := srv.({{$.ServiceType}}Server).{{.Name}}(ctx, &in)
if err != nil {

@ -25,9 +25,9 @@ func contentSubtype(contentType string) string {
// guaranteed since != baseContentType and has baseContentType prefix
switch contentType[len(baseContentType)] {
case '/', ';':
// this will return true for "application/grpc+" or "application/grpc;"
// which the previous validContentType function tested to be valid, so we
// just say that no content-subtype is specified in this case
if i := strings.Index(contentType, ";"); i != -1 {
return contentType[len(baseContentType)+1 : i]
}
return contentType[len(baseContentType)+1:]
default:
return ""

@ -0,0 +1,23 @@
package http
import "testing"
func TestSubtype(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"application/json", "json"},
{"application/json;", "json"},
{"application/json; charset=utf-8", "json"},
{"application/", ""},
{"application", ""},
{"foo", ""},
{"", ""},
}
for _, test := range tests {
if contentSubtype(test.input) != test.expected {
t.Errorf("expected %s got %s", test.expected, test.input)
}
}
}
Loading…
Cancel
Save