feat(binding): add BindPlain (#3904)

* add BindPlain

* fix ci/cd error
This commit is contained in:
guonaihong
2024-05-13 11:11:56 +08:00
committed by GitHub
parent 40131af124
commit 6ca8ddb1ae
6 changed files with 238 additions and 1 deletions
+40
View File
@@ -1342,6 +1342,46 @@ func (h hook) Read([]byte) (int, error) {
return 0, errors.New("error")
}
type failRead struct{}
func (f *failRead) Read(b []byte) (n int, err error) {
return 0, errors.New("my fail")
}
func (f *failRead) Close() error {
return nil
}
func TestPlainBinding(t *testing.T) {
p := Plain
assert.Equal(t, "plain", p.Name())
var s string
req := requestWithBody("POST", "/", "test string")
assert.NoError(t, p.Bind(req, &s))
assert.Equal(t, s, "test string")
var bs []byte
req = requestWithBody("POST", "/", "test []byte")
assert.NoError(t, p.Bind(req, &bs))
assert.Equal(t, bs, []byte("test []byte"))
var i int
req = requestWithBody("POST", "/", "test fail")
assert.Error(t, p.Bind(req, &i))
req = requestWithBody("POST", "/", "")
req.Body = &failRead{}
assert.Error(t, p.Bind(req, &s))
req = requestWithBody("POST", "/", "")
assert.Nil(t, p.Bind(req, nil))
var ptr *string
req = requestWithBody("POST", "/", "")
assert.Nil(t, p.Bind(req, ptr))
}
func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())