remove unused Name method from Binding interface

This commit is contained in:
Dmitry Kutakov 2019-10-11 08:23:44 +02:00
parent f7becac7bc
commit 8145652a8f
2 changed files with 38 additions and 32 deletions

View File

@ -25,7 +25,6 @@ const (
// data present in the request such as JSON request body, query parameters or // data present in the request such as JSON request body, query parameters or
// the form POST. // the form POST.
type Binding interface { type Binding interface {
Name() string
Bind(*http.Request, interface{}) error Bind(*http.Request, interface{}) error
} }

View File

@ -24,6 +24,27 @@ import (
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
) )
func TestBindingName(t *testing.T) {
for _, test := range []struct {
name string
namer interface{ Name() string }
}{
{"json", JSON},
{"xml", XML},
{"form", Form},
{"query", Query},
{"form-urlencoded", FormPost},
{"multipart/form-data", FormMultipart},
{"protobuf", ProtoBuf},
{"msgpack", MsgPack},
{"yaml", YAML},
{"uri", Uri},
{"header", Header},
} {
assert.Equal(t, test.name, test.namer.Name())
}
}
type appkey struct { type appkey struct {
Appkey string `json:"appkey" form:"appkey"` Appkey string `json:"appkey" form:"appkey"`
} }
@ -179,21 +200,21 @@ func TestBindingJSONNilBody(t *testing.T) {
func TestBindingJSON(t *testing.T) { func TestBindingJSON(t *testing.T) {
testBodyBinding(t, testBodyBinding(t,
JSON, "json", JSON,
"/", "/", "/", "/",
`{"foo": "bar"}`, `{"bar": "foo"}`) `{"foo": "bar"}`, `{"bar": "foo"}`)
} }
func TestBindingJSONUseNumber(t *testing.T) { func TestBindingJSONUseNumber(t *testing.T) {
testBodyBindingUseNumber(t, testBodyBindingUseNumber(t,
JSON, "json", JSON,
"/", "/", "/", "/",
`{"foo": 123}`, `{"bar": "foo"}`) `{"foo": 123}`, `{"bar": "foo"}`)
} }
func TestBindingJSONUseNumber2(t *testing.T) { func TestBindingJSONUseNumber2(t *testing.T) {
testBodyBindingUseNumber2(t, testBodyBindingUseNumber2(t,
JSON, "json", JSON,
"/", "/", "/", "/",
`{"foo": 123}`, `{"bar": "foo"}`) `{"foo": 123}`, `{"bar": "foo"}`)
} }
@ -372,28 +393,28 @@ func TestBindingQueryBoolFail(t *testing.T) {
func TestBindingXML(t *testing.T) { func TestBindingXML(t *testing.T) {
testBodyBinding(t, testBodyBinding(t,
XML, "xml", XML,
"/", "/", "/", "/",
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>") "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
} }
func TestBindingXMLFail(t *testing.T) { func TestBindingXMLFail(t *testing.T) {
testBodyBindingFail(t, testBodyBindingFail(t,
XML, "xml", XML,
"/", "/", "/", "/",
"<map><foo>bar<foo></map>", "<map><bar>foo</bar></map>") "<map><foo>bar<foo></map>", "<map><bar>foo</bar></map>")
} }
func TestBindingYAML(t *testing.T) { func TestBindingYAML(t *testing.T) {
testBodyBinding(t, testBodyBinding(t,
YAML, "yaml", YAML,
"/", "/", "/", "/",
`foo: bar`, `bar: foo`) `foo: bar`, `bar: foo`)
} }
func TestBindingYAMLFail(t *testing.T) { func TestBindingYAMLFail(t *testing.T) {
testBodyBindingFail(t, testBodyBindingFail(t,
YAML, "yaml", YAML,
"/", "/", "/", "/",
`foo:\nbar`, `bar: foo`) `foo:\nbar`, `bar: foo`)
} }
@ -613,7 +634,7 @@ func TestBindingProtoBuf(t *testing.T) {
data, _ := proto.Marshal(test) data, _ := proto.Marshal(test)
testProtoBodyBinding(t, testProtoBodyBinding(t,
ProtoBuf, "protobuf", ProtoBuf,
"/", "/", "/", "/",
string(data), string(data[1:])) string(data), string(data[1:]))
} }
@ -625,7 +646,7 @@ func TestBindingProtoBufFail(t *testing.T) {
data, _ := proto.Marshal(test) data, _ := proto.Marshal(test)
testProtoBodyBindingFail(t, testProtoBodyBindingFail(t,
ProtoBuf, "protobuf", ProtoBuf,
"/", "/", "/", "/",
string(data), string(data[1:])) string(data), string(data[1:]))
} }
@ -645,7 +666,7 @@ func TestBindingMsgPack(t *testing.T) {
data := buf.Bytes() data := buf.Bytes()
testMsgPackBodyBinding(t, testMsgPackBodyBinding(t,
MsgPack, "msgpack", MsgPack,
"/", "/", "/", "/",
string(data), string(data[1:])) string(data), string(data[1:]))
} }
@ -1120,9 +1141,7 @@ func testQueryBindingBoolFail(t *testing.T, method, path, badPath, body, badBody
assert.Error(t, err) assert.Error(t, err)
} }
func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testBodyBinding(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := FooStruct{} obj := FooStruct{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
err := b.Bind(req, &obj) err := b.Bind(req, &obj)
@ -1135,9 +1154,7 @@ func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody
assert.Error(t, err) assert.Error(t, err)
} }
func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testBodyBindingUseNumber(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := FooStructUseNumber{} obj := FooStructUseNumber{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
EnableDecoderUseNumber = true EnableDecoderUseNumber = true
@ -1154,9 +1171,7 @@ func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body
assert.Error(t, err) assert.Error(t, err)
} }
func testBodyBindingUseNumber2(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testBodyBindingUseNumber2(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := FooStructUseNumber{} obj := FooStructUseNumber{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
EnableDecoderUseNumber = false EnableDecoderUseNumber = false
@ -1191,9 +1206,7 @@ func testBodyBindingDisallowUnknownFields(t *testing.T, b Binding, path, badPath
assert.Contains(t, err.Error(), "what") assert.Contains(t, err.Error(), "what")
} }
func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testBodyBindingFail(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := FooStruct{} obj := FooStruct{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
err := b.Bind(req, &obj) err := b.Bind(req, &obj)
@ -1206,9 +1219,7 @@ func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, bad
assert.Error(t, err) assert.Error(t, err)
} }
func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testProtoBodyBinding(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := protoexample.Test{} obj := protoexample.Test{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
req.Header.Add("Content-Type", MIMEPROTOBUF) req.Header.Add("Content-Type", MIMEPROTOBUF)
@ -1229,9 +1240,7 @@ func (h hook) Read([]byte) (int, error) {
return 0, errors.New("error") return 0, errors.New("error")
} }
func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testProtoBodyBindingFail(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := protoexample.Test{} obj := protoexample.Test{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
@ -1247,9 +1256,7 @@ func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body
assert.Error(t, err) assert.Error(t, err)
} }
func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testMsgPackBodyBinding(t *testing.T, b Binding, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())
obj := FooStruct{} obj := FooStruct{}
req := requestWithBody("POST", path, body) req := requestWithBody("POST", path, body)
req.Header.Add("Content-Type", MIMEMSGPACK) req.Header.Add("Content-Type", MIMEMSGPACK)