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

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