-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #216: Enable to call binding multiple times in some formats #1341
Merged
appleboy
merged 10 commits into
gin-gonic:master
from
delphinus:feature/multiple-binding
May 11, 2018
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ae520b
Add interface to read body bytes in binding
e825c37
Add BindingBody implementation for some binding
1e0e4a8
Merge branch 'master' into feature/multiple-binding
appleboy 2c82901
Fix to use `BindBodyBytesKey` for key
edc5f44
Revert "Fix to use `BindBodyBytesKey` for key"
e68e6d2
Use private-like key for body bytes
09a2ba4
Add tests for BindingBody & ShouldBindBodyWith
1cecbd3
Add note for README
4a60eed
Remove redundant space between sentences
bfb9aab
Merge branch 'master' into feature/multiple-binding
appleboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not need named return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this, I should add an explicit declaration
var err error
because this is needed for reusingbody
in L527.I want to simplify the code and use this named return variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, got it! Thanks~