mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
add test cases about EnableDecoderUseNumber
This commit is contained in:
parent
8caca24a22
commit
a94da2a7b7
@ -6,6 +6,7 @@ package binding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
@ -26,6 +27,10 @@ type FooBarStruct struct {
|
|||||||
Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
|
Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FooStructUseNumber struct {
|
||||||
|
Foo interface{} `json:"foo" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
type FooBarStructForTimeType struct {
|
type FooBarStructForTimeType struct {
|
||||||
TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"asia/chongqing"`
|
TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"asia/chongqing"`
|
||||||
TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`
|
TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`
|
||||||
@ -156,6 +161,20 @@ func TestBindingJSON(t *testing.T) {
|
|||||||
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBindingJSONUseNumber(t *testing.T) {
|
||||||
|
testBodyBindingUseNumber(t,
|
||||||
|
JSON, "json",
|
||||||
|
"/", "/",
|
||||||
|
`{"foo": 123}`, `{"bar": "foo"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBindingJSONUseNumber2(t *testing.T) {
|
||||||
|
testBodyBindingUseNumber2(t,
|
||||||
|
JSON, "json",
|
||||||
|
"/", "/",
|
||||||
|
`{"foo": 123}`, `{"bar": "foo"}`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBindingForm(t *testing.T) {
|
func TestBindingForm(t *testing.T) {
|
||||||
testFormBinding(t, "POST",
|
testFormBinding(t, "POST",
|
||||||
"/", "/",
|
"/", "/",
|
||||||
@ -846,6 +865,43 @@ func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||||
|
assert.Equal(t, b.Name(), name)
|
||||||
|
|
||||||
|
obj := FooStructUseNumber{}
|
||||||
|
req := requestWithBody("POST", path, body)
|
||||||
|
EnableDecoderUseNumber = true
|
||||||
|
err := b.Bind(req, &obj)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
// we hope it is int64(123)
|
||||||
|
v, e := obj.Foo.(json.Number).Int64()
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, v, int64(123))
|
||||||
|
|
||||||
|
obj = FooStructUseNumber{}
|
||||||
|
req = requestWithBody("POST", badPath, badBody)
|
||||||
|
err = JSON.Bind(req, &obj)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBodyBindingUseNumber2(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||||
|
assert.Equal(t, b.Name(), name)
|
||||||
|
|
||||||
|
obj := FooStructUseNumber{}
|
||||||
|
req := requestWithBody("POST", path, body)
|
||||||
|
EnableDecoderUseNumber = false
|
||||||
|
err := b.Bind(req, &obj)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
// it will return float64(123) if not use EnableDecoderUseNumber
|
||||||
|
// maybe it is not hoped
|
||||||
|
assert.Equal(t, obj.Foo, float64(123))
|
||||||
|
|
||||||
|
obj = FooStructUseNumber{}
|
||||||
|
req = requestWithBody("POST", badPath, badBody)
|
||||||
|
err = JSON.Bind(req, &obj)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||||
assert.Equal(t, b.Name(), name)
|
assert.Equal(t, b.Name(), name)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user