-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add interface to read body bytes in binding * Add BindingBody implementation for some binding * Fix to use `BindBodyBytesKey` for key * Revert "Fix to use `BindBodyBytesKey` for key" This reverts commit 2c82901. * Use private-like key for body bytes * Add tests for BindingBody & ShouldBindBodyWith * Add note for README * Remove redundant space between sentences
- Loading branch information
Showing
9 changed files
with
279 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package binding | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin/binding/example" | ||
"github.com/golang/protobuf/proto" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
func TestBindingBody(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
binding BindingBody | ||
body string | ||
want string | ||
}{ | ||
{ | ||
name: "JSON bidning", | ||
binding: JSON, | ||
body: `{"foo":"FOO"}`, | ||
}, | ||
{ | ||
name: "XML bidning", | ||
binding: XML, | ||
body: `<?xml version="1.0" encoding="UTF-8"?> | ||
<root> | ||
<foo>FOO</foo> | ||
</root>`, | ||
}, | ||
{ | ||
name: "MsgPack binding", | ||
binding: MsgPack, | ||
body: msgPackBody(t), | ||
}, | ||
} { | ||
t.Logf("testing: %s", tt.name) | ||
req := requestWithBody("POST", "/", tt.body) | ||
form := FooStruct{} | ||
body, _ := ioutil.ReadAll(req.Body) | ||
assert.NoError(t, tt.binding.BindBody(body, &form)) | ||
assert.Equal(t, FooStruct{"FOO"}, form) | ||
} | ||
} | ||
|
||
func msgPackBody(t *testing.T) string { | ||
test := FooStruct{"FOO"} | ||
h := new(codec.MsgpackHandle) | ||
buf := bytes.NewBuffer(nil) | ||
assert.NoError(t, codec.NewEncoder(buf, h).Encode(test)) | ||
return buf.String() | ||
} | ||
|
||
func TestBindingBodyProto(t *testing.T) { | ||
test := example.Test{ | ||
Label: proto.String("FOO"), | ||
} | ||
data, _ := proto.Marshal(&test) | ||
req := requestWithBody("POST", "/", string(data)) | ||
form := example.Test{} | ||
body, _ := ioutil.ReadAll(req.Body) | ||
assert.NoError(t, ProtoBuf.BindBody(body, &form)) | ||
assert.Equal(t, test, form) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters