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
// the form POST.
type Binding interface {
Name() string
Bind(*http.Request, interface{}) error
}

View File

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