mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 04:57:07 +08:00
Merge 1b24276250657dbb4c7323d7d5c1e325c0572ef2 into 0a192fb0fa0127eac08cf24c624b92048ed823f6
This commit is contained in:
commit
e7c47d40a4
@ -32,48 +32,49 @@ func TestBindingDefault(t *testing.T) {
|
||||
|
||||
func TestBindingJSON(t *testing.T) {
|
||||
testBinding(t,
|
||||
JSON, "json",
|
||||
JSON, MIMEJSON, "json",
|
||||
"/", "/",
|
||||
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
||||
}
|
||||
|
||||
func TestBindingPOSTForm(t *testing.T) {
|
||||
testBinding(t,
|
||||
POSTForm, "post_form",
|
||||
POSTForm, MIMEPOSTForm, "post_form",
|
||||
"/", "/",
|
||||
"foo=bar", "bar=foo")
|
||||
}
|
||||
|
||||
func TestBindingGETForm(t *testing.T) {
|
||||
testBinding(t,
|
||||
GETForm, "get_form",
|
||||
GETForm, MIMEPOSTForm, "get_form",
|
||||
"/?foo=bar", "/?bar=foo",
|
||||
"", "")
|
||||
}
|
||||
|
||||
func TestBindingXML(t *testing.T) {
|
||||
testBinding(t,
|
||||
XML, "xml",
|
||||
XML, MIMEXML, "xml",
|
||||
"/", "/",
|
||||
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
|
||||
}
|
||||
|
||||
func testBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||
func testBinding(t *testing.T, b Binding, contentType, name, path, badPath, body, badBody string) {
|
||||
assert.Equal(t, b.Name(), name)
|
||||
|
||||
obj := FooStruct{}
|
||||
req := requestWithBody(path, body)
|
||||
req := requestWithBody(path, body, contentType)
|
||||
err := b.Bind(req, &obj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, obj.Foo, "bar")
|
||||
|
||||
obj = FooStruct{}
|
||||
req = requestWithBody(badPath, badBody)
|
||||
req = requestWithBody(badPath, badBody, contentType)
|
||||
err = JSON.Bind(req, &obj)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func requestWithBody(path, body string) (req *http.Request) {
|
||||
func requestWithBody(path, body, contentType string) (req *http.Request) {
|
||||
req, _ = http.NewRequest("POST", path, bytes.NewBufferString(body))
|
||||
return
|
||||
req.Header.Add("Content-Type", contentType)
|
||||
return req
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user