2015-04-09 13:15:02 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-26 06:46:11 +03:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-03-18 05:16:34 +03:00
|
|
|
"io"
|
2018-01-26 06:46:11 +03:00
|
|
|
"io/ioutil"
|
2015-07-03 05:20:00 +03:00
|
|
|
"mime/multipart"
|
2015-04-09 13:15:02 +03:00
|
|
|
"net/http"
|
2019-03-18 05:16:34 +03:00
|
|
|
"os"
|
2020-10-31 02:20:47 +03:00
|
|
|
"reflect"
|
2018-12-28 04:57:09 +03:00
|
|
|
"strconv"
|
2019-03-03 09:39:43 +03:00
|
|
|
"strings"
|
2015-04-09 13:15:02 +03:00
|
|
|
"testing"
|
2018-01-26 06:46:11 +03:00
|
|
|
"time"
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2018-08-12 18:38:31 +03:00
|
|
|
"github.com/gin-gonic/gin/testdata/protoexample"
|
2015-04-09 13:15:02 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-19 10:46:31 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2015-04-09 13:15:02 +03:00
|
|
|
)
|
|
|
|
|
2019-05-13 05:17:31 +03:00
|
|
|
type appkey struct {
|
|
|
|
Appkey string `json:"appkey" form:"appkey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryTest struct {
|
|
|
|
Page int `json:"page" form:"page"`
|
|
|
|
Size int `json:"size" form:"size"`
|
|
|
|
appkey
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
type FooStruct struct {
|
2021-01-03 16:43:34 +03:00
|
|
|
Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required,max=32"`
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
|
2015-05-24 04:33:21 +03:00
|
|
|
type FooBarStruct struct {
|
|
|
|
FooStruct
|
2017-02-23 17:08:37 +03:00
|
|
|
Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
|
2015-05-24 04:33:21 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 05:16:34 +03:00
|
|
|
type FooBarFileStruct struct {
|
|
|
|
FooBarStruct
|
|
|
|
File *multipart.FileHeader `form:"file" binding:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooBarFileFailStruct struct {
|
|
|
|
FooBarStruct
|
|
|
|
File *multipart.FileHeader `invalid_name:"file" binding:"required"`
|
|
|
|
// for unexport test
|
|
|
|
data *multipart.FileHeader `form:"data" binding:"required"`
|
|
|
|
}
|
|
|
|
|
2018-04-25 11:24:03 +03:00
|
|
|
type FooDefaultBarStruct struct {
|
|
|
|
FooStruct
|
|
|
|
Bar string `msgpack:"bar" json:"bar" form:"bar,default=hello" xml:"bar" binding:"required"`
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
type FooStructUseNumber struct {
|
|
|
|
Foo interface{} `json:"foo" binding:"required"`
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:56:59 +03:00
|
|
|
type FooStructDisallowUnknownFields struct {
|
|
|
|
Foo interface{} `json:"foo" binding:"required"`
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
type FooBarStructForTimeType struct {
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
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"`
|
|
|
|
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
|
|
|
|
UnixTime time.Time `form:"unixTime" time_format:"unix"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForTimeTypeNotUnixFormat struct {
|
|
|
|
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
|
|
|
|
UnixTime time.Time `form:"unixTime" time_format:"unix"`
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForTimeTypeNotFormat struct {
|
|
|
|
TimeFoo time.Time `form:"time_foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForTimeTypeFailFormat struct {
|
|
|
|
TimeFoo time.Time `form:"time_foo" time_format:"2017-11-15"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForTimeTypeFailLocation struct {
|
|
|
|
TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_location:"/asia/chongqing"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForMapType struct {
|
|
|
|
MapFoo map[string]interface{} `form:"map_foo"`
|
|
|
|
}
|
|
|
|
|
2019-02-22 07:23:52 +03:00
|
|
|
type FooStructForIgnoreFormTag struct {
|
|
|
|
Foo *string `form:"-"`
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
type InvalidNameType struct {
|
|
|
|
TestName string `invalid_name:"test_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type InvalidNameMapType struct {
|
|
|
|
TestName struct {
|
|
|
|
MapFoo map[string]interface{} `form:"map_foo"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForSliceType struct {
|
|
|
|
SliceFoo []int `form:"slice_foo"`
|
|
|
|
}
|
|
|
|
|
2018-05-01 09:24:18 +03:00
|
|
|
type FooStructForStructType struct {
|
|
|
|
StructFoo struct {
|
|
|
|
Idx int `form:"idx"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForStructPointerType struct {
|
|
|
|
StructPointerFoo *struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
type FooStructForSliceMapType struct {
|
|
|
|
// Unknown type: not support map
|
|
|
|
SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:40:33 +03:00
|
|
|
type FooStructForBoolType struct {
|
|
|
|
BoolFoo bool `form:"bool_foo"`
|
|
|
|
}
|
|
|
|
|
2018-04-26 17:09:34 +03:00
|
|
|
type FooStructForStringPtrType struct {
|
|
|
|
PtrFoo *string `form:"ptr_foo"`
|
|
|
|
PtrBar *string `form:"ptr_bar" binding:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FooStructForMapPtrType struct {
|
|
|
|
PtrBar *map[string]interface{} `form:"ptr_bar"`
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
func TestBindingDefault(t *testing.T) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, Form, Default("GET", ""))
|
|
|
|
assert.Equal(t, Form, Default("GET", MIMEJSON))
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, JSON, Default("POST", MIMEJSON))
|
|
|
|
assert.Equal(t, JSON, Default("PUT", MIMEJSON))
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, XML, Default("POST", MIMEXML))
|
|
|
|
assert.Equal(t, XML, Default("PUT", MIMEXML2))
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, Form, Default("POST", MIMEPOSTForm))
|
|
|
|
assert.Equal(t, Form, Default("PUT", MIMEPOSTForm))
|
2015-05-26 15:21:35 +03:00
|
|
|
|
2019-03-18 05:16:34 +03:00
|
|
|
assert.Equal(t, FormMultipart, Default("POST", MIMEMultipartPOSTForm))
|
|
|
|
assert.Equal(t, FormMultipart, Default("PUT", MIMEMultipartPOSTForm))
|
2015-07-12 12:42:39 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, ProtoBuf, Default("POST", MIMEPROTOBUF))
|
|
|
|
assert.Equal(t, ProtoBuf, Default("PUT", MIMEPROTOBUF))
|
2017-02-23 17:08:37 +03:00
|
|
|
|
2018-11-06 04:49:45 +03:00
|
|
|
assert.Equal(t, YAML, Default("POST", MIMEYAML))
|
|
|
|
assert.Equal(t, YAML, Default("PUT", MIMEYAML))
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
|
2018-11-22 04:55:51 +03:00
|
|
|
func TestBindingJSONNilBody(t *testing.T) {
|
|
|
|
var obj FooStruct
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/", nil)
|
|
|
|
err := JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
func TestBindingJSON(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
testBodyBinding(t,
|
2015-04-09 13:15:02 +03:00
|
|
|
JSON, "json",
|
|
|
|
"/", "/",
|
|
|
|
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:43:34 +03:00
|
|
|
func TestBindingJSONSlice(t *testing.T) {
|
|
|
|
EnableDecoderDisallowUnknownFields = true
|
|
|
|
defer func() {
|
|
|
|
EnableDecoderDisallowUnknownFields = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[]`, ``)
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{}]`)
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": ""}]`)
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": 123}]`)
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"bar": 123}]`)
|
|
|
|
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": "123456789012345678901234567890123"}]`)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
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"}`)
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:56:59 +03:00
|
|
|
func TestBindingJSONDisallowUnknownFields(t *testing.T) {
|
|
|
|
testBodyBindingDisallowUnknownFields(t, JSON,
|
|
|
|
"/", "/",
|
|
|
|
`{"foo": "bar"}`, `{"foo": "bar", "what": "this"}`)
|
|
|
|
}
|
|
|
|
|
2020-10-31 02:20:47 +03:00
|
|
|
func TestBindingJSONStringMap(t *testing.T) {
|
|
|
|
testBodyBindingStringMap(t, JSON,
|
|
|
|
"/", "/",
|
|
|
|
`{"foo": "bar", "hello": "world"}`, `{"num": 2}`)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func TestBindingForm(t *testing.T) {
|
|
|
|
testFormBinding(t, "POST",
|
2015-04-09 13:15:02 +03:00
|
|
|
"/", "/",
|
2015-05-24 04:33:21 +03:00
|
|
|
"foo=bar&bar=foo", "bar2=foo")
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func TestBindingForm2(t *testing.T) {
|
|
|
|
testFormBinding(t, "GET",
|
2015-05-24 04:33:21 +03:00
|
|
|
"/?foo=bar&bar=foo", "/?bar2=foo",
|
2015-04-09 13:15:02 +03:00
|
|
|
"", "")
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:17:31 +03:00
|
|
|
func TestBindingFormEmbeddedStruct(t *testing.T) {
|
|
|
|
testFormBindingEmbeddedStruct(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"page=1&size=2&appkey=test-appkey", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormEmbeddedStruct2(t *testing.T) {
|
|
|
|
testFormBindingEmbeddedStruct(t, "GET",
|
|
|
|
"/?page=1&size=2&appkey=test-appkey", "/?bar2=foo",
|
|
|
|
"", "")
|
|
|
|
}
|
|
|
|
|
2018-04-25 11:24:03 +03:00
|
|
|
func TestBindingFormDefaultValue(t *testing.T) {
|
|
|
|
testFormBindingDefaultValue(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"foo=bar", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormDefaultValue2(t *testing.T) {
|
|
|
|
testFormBindingDefaultValue(t, "GET",
|
|
|
|
"/?foo=bar", "/?bar2=foo",
|
|
|
|
"", "")
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestBindingFormForTime(t *testing.T) {
|
|
|
|
testFormBindingForTime(t, "POST",
|
|
|
|
"/", "/",
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
"time_foo=2017-11-15&time_bar=&createTime=1562400033000000123&unixTime=1562400033", "bar2=foo")
|
|
|
|
testFormBindingForTimeNotUnixFormat(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"time_foo=2017-11-15&createTime=bad&unixTime=bad", "bar2=foo")
|
2018-01-26 06:46:11 +03:00
|
|
|
testFormBindingForTimeNotFormat(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"time_foo=2017-11-15", "bar2=foo")
|
|
|
|
testFormBindingForTimeFailFormat(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"time_foo=2017-11-15", "bar2=foo")
|
|
|
|
testFormBindingForTimeFailLocation(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"time_foo=2017-11-15", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormForTime2(t *testing.T) {
|
|
|
|
testFormBindingForTime(t, "GET",
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
"/?time_foo=2017-11-15&time_bar=&createTime=1562400033000000123&unixTime=1562400033", "/?bar2=foo",
|
2018-01-26 06:46:11 +03:00
|
|
|
"", "")
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
testFormBindingForTimeNotUnixFormat(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"time_foo=2017-11-15&createTime=bad&unixTime=bad", "bar2=foo")
|
2018-01-26 06:46:11 +03:00
|
|
|
testFormBindingForTimeNotFormat(t, "GET",
|
|
|
|
"/?time_foo=2017-11-15", "/?bar2=foo",
|
|
|
|
"", "")
|
|
|
|
testFormBindingForTimeFailFormat(t, "GET",
|
|
|
|
"/?time_foo=2017-11-15", "/?bar2=foo",
|
|
|
|
"", "")
|
|
|
|
testFormBindingForTimeFailLocation(t, "GET",
|
|
|
|
"/?time_foo=2017-11-15", "/?bar2=foo",
|
|
|
|
"", "")
|
|
|
|
}
|
|
|
|
|
2019-02-22 07:23:52 +03:00
|
|
|
func TestFormBindingIgnoreField(t *testing.T) {
|
|
|
|
testFormBindingIgnoreField(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"-=bar", "")
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestBindingFormInvalidName(t *testing.T) {
|
|
|
|
testFormBindingInvalidName(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"test_name=bar", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormInvalidName2(t *testing.T) {
|
|
|
|
testFormBindingInvalidName2(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"map_foo=bar", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormForType(t *testing.T) {
|
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
2019-03-03 09:39:43 +03:00
|
|
|
"map_foo={\"bar\":123}", "map_foo=1", "Map")
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"slice_foo=1&slice_foo=2", "bar2=1&bar2=2", "Slice")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "GET",
|
|
|
|
"/?slice_foo=1&slice_foo=2", "/?bar2=1&bar2=2",
|
|
|
|
"", "", "Slice")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"slice_map_foo=1&slice_map_foo=2", "bar2=1&bar2=2", "SliceMap")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "GET",
|
|
|
|
"/?slice_map_foo=1&slice_map_foo=2", "/?bar2=1&bar2=2",
|
|
|
|
"", "", "SliceMap")
|
|
|
|
|
2018-04-26 17:09:34 +03:00
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"ptr_bar=test", "bar2=test", "Ptr")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "GET",
|
|
|
|
"/?ptr_bar=test", "/?bar2=test",
|
|
|
|
"", "", "Ptr")
|
2018-05-01 09:24:18 +03:00
|
|
|
|
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"idx=123", "id1=1", "Struct")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "GET",
|
|
|
|
"/?idx=123", "/?id1=1",
|
|
|
|
"", "", "Struct")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "POST",
|
|
|
|
"/", "/",
|
|
|
|
"name=thinkerou", "name1=ou", "StructPointer")
|
|
|
|
|
|
|
|
testFormBindingForType(t, "GET",
|
|
|
|
"/?name=thinkerou", "/?name1=ou",
|
|
|
|
"", "", "StructPointer")
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
2020-10-31 02:20:47 +03:00
|
|
|
func TestBindingFormStringMap(t *testing.T) {
|
|
|
|
testBodyBindingStringMap(t, Form,
|
|
|
|
"/", "",
|
|
|
|
`foo=bar&hello=world`, "")
|
|
|
|
// Should pick the last value
|
|
|
|
testBodyBindingStringMap(t, Form,
|
|
|
|
"/", "",
|
|
|
|
`foo=something&foo=bar&hello=world`, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormStringSliceMap(t *testing.T) {
|
|
|
|
obj := make(map[string][]string)
|
|
|
|
req := requestWithBody("POST", "/", "foo=something&foo=bar&hello=world")
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
err := Form.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, obj)
|
|
|
|
assert.Len(t, obj, 2)
|
|
|
|
target := map[string][]string{
|
|
|
|
"foo": {"something", "bar"},
|
|
|
|
"hello": {"world"},
|
|
|
|
}
|
|
|
|
assert.True(t, reflect.DeepEqual(obj, target))
|
|
|
|
|
|
|
|
objInvalid := make(map[string][]int)
|
|
|
|
req = requestWithBody("POST", "/", "foo=something&foo=bar&hello=world")
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
err = Form.Bind(req, &objInvalid)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:50:05 +03:00
|
|
|
func TestBindingQuery(t *testing.T) {
|
|
|
|
testQueryBinding(t, "POST",
|
|
|
|
"/?foo=bar&bar=foo", "/",
|
|
|
|
"foo=unused", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingQuery2(t *testing.T) {
|
|
|
|
testQueryBinding(t, "GET",
|
|
|
|
"/?foo=bar&bar=foo", "/?bar2=foo",
|
|
|
|
"foo=unused", "")
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestBindingQueryFail(t *testing.T) {
|
|
|
|
testQueryBindingFail(t, "POST",
|
|
|
|
"/?map_foo=", "/",
|
|
|
|
"map_foo=unused", "bar2=foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingQueryFail2(t *testing.T) {
|
|
|
|
testQueryBindingFail(t, "GET",
|
|
|
|
"/?map_foo=", "/?bar2=foo",
|
|
|
|
"map_foo=unused", "")
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:40:33 +03:00
|
|
|
func TestBindingQueryBoolFail(t *testing.T) {
|
|
|
|
testQueryBindingBoolFail(t, "GET",
|
|
|
|
"/?bool_foo=fasl", "/?bar2=foo",
|
|
|
|
"bool_foo=unused", "")
|
|
|
|
}
|
|
|
|
|
2020-10-31 02:20:47 +03:00
|
|
|
func TestBindingQueryStringMap(t *testing.T) {
|
|
|
|
b := Query
|
|
|
|
|
|
|
|
obj := make(map[string]string)
|
|
|
|
req := requestWithBody("GET", "/?foo=bar&hello=world", "")
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, obj)
|
|
|
|
assert.Len(t, obj, 2)
|
|
|
|
assert.Equal(t, "bar", obj["foo"])
|
|
|
|
assert.Equal(t, "world", obj["hello"])
|
|
|
|
|
|
|
|
obj = make(map[string]string)
|
|
|
|
req = requestWithBody("GET", "/?foo=bar&foo=2&hello=world", "") // should pick last
|
|
|
|
err = b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, obj)
|
|
|
|
assert.Len(t, obj, 2)
|
|
|
|
assert.Equal(t, "2", obj["foo"])
|
|
|
|
assert.Equal(t, "world", obj["hello"])
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
func TestBindingXML(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
testBodyBinding(t,
|
2015-04-09 13:15:02 +03:00
|
|
|
XML, "xml",
|
|
|
|
"/", "/",
|
|
|
|
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestBindingXMLFail(t *testing.T) {
|
|
|
|
testBodyBindingFail(t,
|
|
|
|
XML, "xml",
|
|
|
|
"/", "/",
|
|
|
|
"<map><foo>bar<foo></map>", "<map><bar>foo</bar></map>")
|
|
|
|
}
|
|
|
|
|
2018-11-06 04:49:45 +03:00
|
|
|
func TestBindingYAML(t *testing.T) {
|
|
|
|
testBodyBinding(t,
|
|
|
|
YAML, "yaml",
|
|
|
|
"/", "/",
|
|
|
|
`foo: bar`, `bar: foo`)
|
|
|
|
}
|
|
|
|
|
2020-10-31 02:20:47 +03:00
|
|
|
func TestBindingYAMLStringMap(t *testing.T) {
|
|
|
|
// YAML is a superset of JSON, so the test below is JSON (to avoid newlines)
|
|
|
|
testBodyBindingStringMap(t, YAML,
|
|
|
|
"/", "/",
|
|
|
|
`{"foo": "bar", "hello": "world"}`, `{"nested": {"foo": "bar"}}`)
|
|
|
|
}
|
|
|
|
|
2018-11-06 04:49:45 +03:00
|
|
|
func TestBindingYAMLFail(t *testing.T) {
|
|
|
|
testBodyBindingFail(t,
|
|
|
|
YAML, "yaml",
|
|
|
|
"/", "/",
|
|
|
|
`foo:\nbar`, `bar: foo`)
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func createFormPostRequest(t *testing.T) *http.Request {
|
|
|
|
req, err := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar&bar=foo"))
|
|
|
|
assert.NoError(t, err)
|
2015-07-03 05:20:00 +03:00
|
|
|
req.Header.Set("Content-Type", MIMEPOSTForm)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func createDefaultFormPostRequest(t *testing.T) *http.Request {
|
|
|
|
req, err := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar"))
|
|
|
|
assert.NoError(t, err)
|
2018-04-25 11:24:03 +03:00
|
|
|
req.Header.Set("Content-Type", MIMEPOSTForm)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func createFormPostRequestForMap(t *testing.T) *http.Request {
|
|
|
|
req, err := http.NewRequest("POST", "/?map_foo=getfoo", bytes.NewBufferString("map_foo={\"bar\":123}"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", MIMEPOSTForm)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFormPostRequestForMapFail(t *testing.T) *http.Request {
|
|
|
|
req, err := http.NewRequest("POST", "/?map_foo=getfoo", bytes.NewBufferString("map_foo=hello"))
|
|
|
|
assert.NoError(t, err)
|
2018-01-26 06:46:11 +03:00
|
|
|
req.Header.Set("Content-Type", MIMEPOSTForm)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-03-18 05:16:34 +03:00
|
|
|
func createFormFilesMultipartRequest(t *testing.T) *http.Request {
|
|
|
|
boundary := "--testboundary"
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
|
|
|
assert.NoError(t, mw.SetBoundary(boundary))
|
|
|
|
assert.NoError(t, mw.WriteField("foo", "bar"))
|
|
|
|
assert.NoError(t, mw.WriteField("bar", "foo"))
|
|
|
|
|
|
|
|
f, err := os.Open("form.go")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
fw, err1 := mw.CreateFormFile("file", "form.go")
|
|
|
|
assert.NoError(t, err1)
|
2019-10-27 08:58:59 +03:00
|
|
|
_, err = io.Copy(fw, f)
|
|
|
|
assert.NoError(t, err)
|
2019-03-18 05:16:34 +03:00
|
|
|
|
|
|
|
req, err2 := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFormFilesMultipartRequestFail(t *testing.T) *http.Request {
|
|
|
|
boundary := "--testboundary"
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
|
|
|
assert.NoError(t, mw.SetBoundary(boundary))
|
|
|
|
assert.NoError(t, mw.WriteField("foo", "bar"))
|
|
|
|
assert.NoError(t, mw.WriteField("bar", "foo"))
|
|
|
|
|
|
|
|
f, err := os.Open("form.go")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
fw, err1 := mw.CreateFormFile("file_foo", "form_foo.go")
|
|
|
|
assert.NoError(t, err1)
|
2019-10-27 08:58:59 +03:00
|
|
|
_, err = io.Copy(fw, f)
|
|
|
|
assert.NoError(t, err)
|
2019-03-18 05:16:34 +03:00
|
|
|
|
|
|
|
req, err2 := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-01-18 04:32:53 +03:00
|
|
|
func createFormMultipartRequest(t *testing.T) *http.Request {
|
2015-07-03 05:20:00 +03:00
|
|
|
boundary := "--testboundary"
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
2019-01-18 04:32:53 +03:00
|
|
|
assert.NoError(t, mw.SetBoundary(boundary))
|
|
|
|
assert.NoError(t, mw.WriteField("foo", "bar"))
|
|
|
|
assert.NoError(t, mw.WriteField("bar", "foo"))
|
2019-03-03 09:39:43 +03:00
|
|
|
req, err := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
|
|
|
|
assert.NoError(t, err)
|
2015-07-03 05:20:00 +03:00
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func createFormMultipartRequestForMap(t *testing.T) *http.Request {
|
2018-01-26 06:46:11 +03:00
|
|
|
boundary := "--testboundary"
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
2019-01-18 04:32:53 +03:00
|
|
|
assert.NoError(t, mw.SetBoundary(boundary))
|
2019-03-03 09:39:43 +03:00
|
|
|
assert.NoError(t, mw.WriteField("map_foo", "{\"bar\":123, \"name\":\"thinkerou\", \"pai\": 3.14}"))
|
|
|
|
req, err := http.NewRequest("POST", "/?map_foo=getfoo", body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFormMultipartRequestForMapFail(t *testing.T) *http.Request {
|
|
|
|
boundary := "--testboundary"
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
|
|
|
assert.NoError(t, mw.SetBoundary(boundary))
|
|
|
|
assert.NoError(t, mw.WriteField("map_foo", "3.14"))
|
|
|
|
req, err := http.NewRequest("POST", "/?map_foo=getfoo", body)
|
|
|
|
assert.NoError(t, err)
|
2018-01-26 06:46:11 +03:00
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:20:00 +03:00
|
|
|
func TestBindingFormPost(t *testing.T) {
|
2019-03-03 09:39:43 +03:00
|
|
|
req := createFormPostRequest(t)
|
2015-07-03 05:20:00 +03:00
|
|
|
var obj FooBarStruct
|
2019-01-18 04:32:53 +03:00
|
|
|
assert.NoError(t, FormPost.Bind(req, &obj))
|
2015-07-03 05:20:00 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form-urlencoded", FormPost.Name())
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "foo", obj.Bar)
|
2015-07-03 05:20:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 11:24:03 +03:00
|
|
|
func TestBindingDefaultValueFormPost(t *testing.T) {
|
2019-03-03 09:39:43 +03:00
|
|
|
req := createDefaultFormPostRequest(t)
|
2018-04-25 11:24:03 +03:00
|
|
|
var obj FooDefaultBarStruct
|
2019-01-18 04:32:53 +03:00
|
|
|
assert.NoError(t, FormPost.Bind(req, &obj))
|
2018-04-25 11:24:03 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "hello", obj.Bar)
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func TestBindingFormPostForMap(t *testing.T) {
|
|
|
|
req := createFormPostRequestForMap(t)
|
|
|
|
var obj FooStructForMapType
|
|
|
|
err := FormPost.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, float64(123), obj.MapFoo["bar"].(float64))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormPostForMapFail(t *testing.T) {
|
|
|
|
req := createFormPostRequestForMapFail(t)
|
2018-01-26 06:46:11 +03:00
|
|
|
var obj FooStructForMapType
|
|
|
|
err := FormPost.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-03-18 05:16:34 +03:00
|
|
|
func TestBindingFormFilesMultipart(t *testing.T) {
|
|
|
|
req := createFormFilesMultipartRequest(t)
|
|
|
|
var obj FooBarFileStruct
|
2019-10-27 08:58:59 +03:00
|
|
|
err := FormMultipart.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2019-03-18 05:16:34 +03:00
|
|
|
|
|
|
|
// file from os
|
|
|
|
f, _ := os.Open("form.go")
|
|
|
|
defer f.Close()
|
|
|
|
fileActual, _ := ioutil.ReadAll(f)
|
|
|
|
|
|
|
|
// file from multipart
|
|
|
|
mf, _ := obj.File.Open()
|
|
|
|
defer mf.Close()
|
|
|
|
fileExpect, _ := ioutil.ReadAll(mf)
|
|
|
|
|
|
|
|
assert.Equal(t, FormMultipart.Name(), "multipart/form-data")
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
|
|
|
assert.Equal(t, obj.Bar, "foo")
|
|
|
|
assert.Equal(t, fileExpect, fileActual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormFilesMultipartFail(t *testing.T) {
|
|
|
|
req := createFormFilesMultipartRequestFail(t)
|
|
|
|
var obj FooBarFileFailStruct
|
|
|
|
err := FormMultipart.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:20:00 +03:00
|
|
|
func TestBindingFormMultipart(t *testing.T) {
|
2019-01-18 04:32:53 +03:00
|
|
|
req := createFormMultipartRequest(t)
|
2015-07-03 05:20:00 +03:00
|
|
|
var obj FooBarStruct
|
2019-01-18 04:32:53 +03:00
|
|
|
assert.NoError(t, FormMultipart.Bind(req, &obj))
|
2015-07-03 05:20:00 +03:00
|
|
|
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "multipart/form-data", FormMultipart.Name())
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "foo", obj.Bar)
|
2015-07-03 05:20:00 +03:00
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func TestBindingFormMultipartForMap(t *testing.T) {
|
|
|
|
req := createFormMultipartRequestForMap(t)
|
|
|
|
var obj FooStructForMapType
|
|
|
|
err := FormMultipart.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, float64(123), obj.MapFoo["bar"].(float64))
|
|
|
|
assert.Equal(t, "thinkerou", obj.MapFoo["name"].(string))
|
|
|
|
assert.Equal(t, float64(3.14), obj.MapFoo["pai"].(float64))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingFormMultipartForMapFail(t *testing.T) {
|
|
|
|
req := createFormMultipartRequestForMapFail(t)
|
2018-01-26 06:46:11 +03:00
|
|
|
var obj FooStructForMapType
|
|
|
|
err := FormMultipart.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:42:39 +03:00
|
|
|
func TestBindingProtoBuf(t *testing.T) {
|
2018-08-12 18:38:31 +03:00
|
|
|
test := &protoexample.Test{
|
2015-07-12 12:42:39 +03:00
|
|
|
Label: proto.String("yes"),
|
|
|
|
}
|
|
|
|
data, _ := proto.Marshal(test)
|
|
|
|
|
|
|
|
testProtoBodyBinding(t,
|
|
|
|
ProtoBuf, "protobuf",
|
|
|
|
"/", "/",
|
|
|
|
string(data), string(data[1:]))
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestBindingProtoBufFail(t *testing.T) {
|
2018-08-12 18:38:31 +03:00
|
|
|
test := &protoexample.Test{
|
2018-01-26 06:46:11 +03:00
|
|
|
Label: proto.String("yes"),
|
|
|
|
}
|
|
|
|
data, _ := proto.Marshal(test)
|
|
|
|
|
|
|
|
testProtoBodyBindingFail(t,
|
|
|
|
ProtoBuf, "protobuf",
|
|
|
|
"/", "/",
|
|
|
|
string(data), string(data[1:]))
|
|
|
|
}
|
|
|
|
|
2015-05-31 17:18:50 +03:00
|
|
|
func TestValidationFails(t *testing.T) {
|
|
|
|
var obj FooStruct
|
|
|
|
req := requestWithBody("POST", "/", `{"bar": "foo"}`)
|
|
|
|
err := JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidationDisabled(t *testing.T) {
|
|
|
|
backup := Validator
|
|
|
|
Validator = nil
|
|
|
|
defer func() { Validator = backup }()
|
|
|
|
|
|
|
|
var obj FooStruct
|
|
|
|
req := requestWithBody("POST", "/", `{"bar": "foo"}`)
|
|
|
|
err := JSON.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:39:56 +03:00
|
|
|
func TestRequiredSucceeds(t *testing.T) {
|
2015-12-31 09:49:38 +03:00
|
|
|
type HogeStruct struct {
|
2019-09-05 16:39:56 +03:00
|
|
|
Hoge *int `json:"hoge" binding:"required"`
|
2015-12-31 09:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var obj HogeStruct
|
|
|
|
req := requestWithBody("POST", "/", `{"hoge": 0}`)
|
|
|
|
err := JSON.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:39:56 +03:00
|
|
|
func TestRequiredFails(t *testing.T) {
|
2015-12-31 09:49:38 +03:00
|
|
|
type HogeStruct struct {
|
2019-09-05 16:39:56 +03:00
|
|
|
Hoge *int `json:"foo" binding:"required"`
|
2015-12-31 09:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var obj HogeStruct
|
|
|
|
req := requestWithBody("POST", "/", `{"boen": 0}`)
|
|
|
|
err := JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:47:45 +03:00
|
|
|
func TestHeaderBinding(t *testing.T) {
|
|
|
|
h := Header
|
|
|
|
assert.Equal(t, "header", h.Name())
|
|
|
|
|
|
|
|
type tHeader struct {
|
|
|
|
Limit int `header:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var theader tHeader
|
|
|
|
req := requestWithBody("GET", "/", "")
|
|
|
|
req.Header.Add("limit", "1000")
|
|
|
|
assert.NoError(t, h.Bind(req, &theader))
|
|
|
|
assert.Equal(t, 1000, theader.Limit)
|
|
|
|
|
|
|
|
req = requestWithBody("GET", "/", "")
|
|
|
|
req.Header.Add("fail", `{fail:fail}`)
|
|
|
|
|
|
|
|
type failStruct struct {
|
|
|
|
Fail map[string]interface{} `header:"fail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.Bind(req, &failStruct{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-11-22 04:29:48 +03:00
|
|
|
func TestUriBinding(t *testing.T) {
|
|
|
|
b := Uri
|
|
|
|
assert.Equal(t, "uri", b.Name())
|
|
|
|
|
|
|
|
type Tag struct {
|
|
|
|
Name string `uri:"name"`
|
|
|
|
}
|
|
|
|
var tag Tag
|
|
|
|
m := make(map[string][]string)
|
|
|
|
m["name"] = []string{"thinkerou"}
|
|
|
|
assert.NoError(t, b.BindUri(m, &tag))
|
|
|
|
assert.Equal(t, "thinkerou", tag.Name)
|
|
|
|
|
|
|
|
type NotSupportStruct struct {
|
|
|
|
Name map[string]interface{} `uri:"name"`
|
|
|
|
}
|
|
|
|
var not NotSupportStruct
|
|
|
|
assert.Error(t, b.BindUri(m, ¬))
|
2018-12-05 00:58:35 +03:00
|
|
|
assert.Equal(t, map[string]interface{}(nil), not.Name)
|
2018-11-22 04:29:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-28 04:57:09 +03:00
|
|
|
func TestUriInnerBinding(t *testing.T) {
|
|
|
|
type Tag struct {
|
|
|
|
Name string `uri:"name"`
|
|
|
|
S struct {
|
|
|
|
Age int `uri:"age"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedName := "mike"
|
|
|
|
expectedAge := 25
|
|
|
|
|
|
|
|
m := map[string][]string{
|
|
|
|
"name": {expectedName},
|
|
|
|
"age": {strconv.Itoa(expectedAge)},
|
|
|
|
}
|
|
|
|
|
|
|
|
var tag Tag
|
|
|
|
assert.NoError(t, Uri.BindUri(m, &tag))
|
|
|
|
assert.Equal(t, tag.Name, expectedName)
|
|
|
|
assert.Equal(t, tag.S.Age, expectedAge)
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:17:31 +03:00
|
|
|
func testFormBindingEmbeddedStruct(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
|
|
|
assert.Equal(t, "form", b.Name())
|
|
|
|
|
|
|
|
obj := QueryTest{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, obj.Page)
|
|
|
|
assert.Equal(t, 2, obj.Size)
|
|
|
|
assert.Equal(t, "test-appkey", obj.Appkey)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2015-05-24 04:33:21 +03:00
|
|
|
obj := FooBarStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "foo", obj.Bar)
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2015-05-24 04:33:21 +03:00
|
|
|
obj = FooBarStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req = requestWithBody(method, badPath, badBody)
|
2018-04-25 11:24:03 +03:00
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingDefaultValue(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
|
|
|
assert.Equal(t, "form", b.Name())
|
|
|
|
|
|
|
|
obj := FooDefaultBarStruct{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "hello", obj.Bar)
|
|
|
|
|
|
|
|
obj = FooDefaultBarStruct{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
2015-05-05 16:06:38 +03:00
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestFormBindingFail(t *testing.T) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooBarStruct{}
|
|
|
|
req, _ := http.NewRequest("POST", "/", nil)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:39:43 +03:00
|
|
|
func TestFormBindingMultipartFail(t *testing.T) {
|
|
|
|
obj := FooBarStruct{}
|
|
|
|
req, err := http.NewRequest("POST", "/", strings.NewReader("foo=bar"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+";boundary=testboundary")
|
|
|
|
_, err = req.MultipartReader()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = Form.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestFormPostBindingFail(t *testing.T) {
|
|
|
|
b := FormPost
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form-urlencoded", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooBarStruct{}
|
|
|
|
req, _ := http.NewRequest("POST", "/", nil)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormMultipartBindingFail(t *testing.T) {
|
|
|
|
b := FormMultipart
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "multipart/form-data", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooBarStruct{}
|
|
|
|
req, _ := http.NewRequest("POST", "/", nil)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingForTime(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooBarStructForTimeType{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, int64(1510675200), obj.TimeFoo.Unix())
|
|
|
|
assert.Equal(t, "Asia/Chongqing", obj.TimeFoo.Location().String())
|
|
|
|
assert.Equal(t, int64(-62135596800), obj.TimeBar.Unix())
|
|
|
|
assert.Equal(t, "UTC", obj.TimeBar.Location().String())
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
assert.Equal(t, int64(1562400033000000123), obj.CreateTime.UnixNano())
|
|
|
|
assert.Equal(t, int64(1562400033), obj.UnixTime.Unix())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj = FooBarStructForTimeType{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
binding: support unix time (#1980)
* binding: support unix time
ref:#1979
* binding: support unix time
add test file
modify readme
```golang
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type shareTime struct {
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"`
}
func main() {
r := gin.Default()
unix := r.Group("/unix")
testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
fmt.Printf("%d\n", testCT.UnixNano())
testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
fmt.Printf("%d\n", testUT.Unix())
unix.GET("/nano", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testCT.Equal(s.CreateTime) {
c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
return
}
c.JSON(200, s)
})
unix.GET("/sec", func(c *gin.Context) {
s := shareTime{}
c.ShouldBindQuery(&s)
if !testUT.Equal(s.UnixTime) {
c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
return
}
c.JSON(200, s)
})
r.Run()
}
```
* Contraction variable scope
2019-07-10 08:02:40 +03:00
|
|
|
func testFormBindingForTimeNotUnixFormat(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
|
|
|
assert.Equal(t, "form", b.Name())
|
|
|
|
|
|
|
|
obj := FooStructForTimeTypeNotUnixFormat{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = FooStructForTimeTypeNotUnixFormat{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func testFormBindingForTimeNotFormat(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooStructForTimeTypeNotFormat{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = FooStructForTimeTypeNotFormat{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingForTimeFailFormat(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooStructForTimeTypeFailFormat{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = FooStructForTimeTypeFailFormat{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingForTimeFailLocation(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooStructForTimeTypeFailLocation{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = FooStructForTimeTypeFailLocation{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-02-22 07:23:52 +03:00
|
|
|
func testFormBindingIgnoreField(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
|
|
|
assert.Equal(t, "form", b.Name())
|
|
|
|
|
|
|
|
obj := FooStructForIgnoreFormTag{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Nil(t, obj.Foo)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := InvalidNameType{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "", obj.TestName)
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj = InvalidNameType{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingInvalidName2(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := InvalidNameMapType{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = InvalidNameMapType{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody string, typ string) {
|
|
|
|
b := Form
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "form", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case "Slice":
|
|
|
|
obj := FooStructForSliceType{}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, []int{1, 2}, obj.SliceFoo)
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj = FooStructForSliceType{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
2018-05-01 09:24:18 +03:00
|
|
|
case "Struct":
|
|
|
|
obj := FooStructForStructType{}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t,
|
|
|
|
struct {
|
|
|
|
Idx int "form:\"idx\""
|
|
|
|
}(struct {
|
|
|
|
Idx int "form:\"idx\""
|
|
|
|
}{Idx: 123}),
|
|
|
|
obj.StructFoo)
|
|
|
|
case "StructPointer":
|
|
|
|
obj := FooStructForStructPointerType{}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t,
|
|
|
|
struct {
|
|
|
|
Name string "form:\"name\""
|
|
|
|
}(struct {
|
|
|
|
Name string "form:\"name\""
|
|
|
|
}{Name: "thinkerou"}),
|
|
|
|
*obj.StructPointerFoo)
|
2018-01-26 06:46:11 +03:00
|
|
|
case "Map":
|
|
|
|
obj := FooStructForMapType{}
|
|
|
|
err := b.Bind(req, &obj)
|
2019-03-03 09:39:43 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, float64(123), obj.MapFoo["bar"].(float64))
|
2018-01-26 06:46:11 +03:00
|
|
|
case "SliceMap":
|
|
|
|
obj := FooStructForSliceMapType{}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
2018-04-26 17:09:34 +03:00
|
|
|
case "Ptr":
|
|
|
|
obj := FooStructForStringPtrType{}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, obj.PtrFoo)
|
|
|
|
assert.Equal(t, "test", *obj.PtrBar)
|
|
|
|
|
|
|
|
obj = FooStructForStringPtrType{}
|
|
|
|
obj.PtrBar = new(string)
|
|
|
|
err = b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test", *obj.PtrBar)
|
|
|
|
|
|
|
|
objErr := FooStructForMapPtrType{}
|
|
|
|
err = b.Bind(req, &objErr)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
obj = FooStructForStringPtrType{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:50:05 +03:00
|
|
|
func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Query
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "query", b.Name())
|
2017-07-19 10:50:05 +03:00
|
|
|
|
|
|
|
obj := FooBarStruct{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, "foo", obj.Bar)
|
2017-07-19 10:50:05 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func testQueryBindingFail(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Query
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "query", b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooStructForMapType{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:40:33 +03:00
|
|
|
func testQueryBindingBoolFail(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Query
|
|
|
|
assert.Equal(t, "query", b.Name())
|
|
|
|
|
|
|
|
obj := FooStructForBoolType{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2015-04-09 13:15:02 +03:00
|
|
|
|
|
|
|
obj := FooStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req := requestWithBody("POST", path, body)
|
2015-04-09 13:15:02 +03:00
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
2015-04-09 13:15:02 +03:00
|
|
|
|
|
|
|
obj = FooStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
2015-04-09 13:15:02 +03:00
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:43:34 +03:00
|
|
|
func testBodyBindingSlice(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
|
|
|
assert.Equal(t, name, b.Name())
|
|
|
|
|
|
|
|
var obj1 []FooStruct
|
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
err := b.Bind(req, &obj1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var obj2 []FooStruct
|
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj2)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-10-31 02:20:47 +03:00
|
|
|
func testBodyBindingStringMap(t *testing.T, b Binding, path, badPath, body, badBody string) {
|
|
|
|
obj := make(map[string]string)
|
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
if b.Name() == "form" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, obj)
|
|
|
|
assert.Len(t, obj, 2)
|
|
|
|
assert.Equal(t, "bar", obj["foo"])
|
|
|
|
assert.Equal(t, "world", obj["hello"])
|
|
|
|
|
|
|
|
if badPath != "" && badBody != "" {
|
|
|
|
obj = make(map[string]string)
|
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
err = b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
objInt := make(map[string]int)
|
|
|
|
req = requestWithBody("POST", path, body)
|
|
|
|
err = b.Bind(req, &objInt)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
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)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, int64(123), v)
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
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) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
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
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, float64(123), obj.Foo)
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj = FooStructUseNumber{}
|
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:56:59 +03:00
|
|
|
func testBodyBindingDisallowUnknownFields(t *testing.T, b Binding, path, badPath, body, badBody string) {
|
|
|
|
EnableDecoderDisallowUnknownFields = true
|
|
|
|
defer func() {
|
|
|
|
EnableDecoderDisallowUnknownFields = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
obj := FooStructDisallowUnknownFields{}
|
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
|
|
|
|
obj = FooStructDisallowUnknownFields{}
|
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "what")
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj := FooStruct{}
|
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "", obj.Foo)
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
obj = FooStruct{}
|
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:42:39 +03:00
|
|
|
func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2015-07-12 12:42:39 +03:00
|
|
|
|
2018-08-12 18:38:31 +03:00
|
|
|
obj := protoexample.Test{}
|
2015-07-12 12:42:39 +03:00
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
req.Header.Add("Content-Type", MIMEPROTOBUF)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "yes", *obj.Label)
|
2015-07-12 12:42:39 +03:00
|
|
|
|
2018-08-12 18:38:31 +03:00
|
|
|
obj = protoexample.Test{}
|
2015-07-12 12:42:39 +03:00
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
req.Header.Add("Content-Type", MIMEPROTOBUF)
|
|
|
|
err = ProtoBuf.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
type hook struct{}
|
|
|
|
|
|
|
|
func (h hook) Read([]byte) (int, error) {
|
|
|
|
return 0, errors.New("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, name, b.Name())
|
2018-01-26 06:46:11 +03:00
|
|
|
|
2018-08-12 18:38:31 +03:00
|
|
|
obj := protoexample.Test{}
|
2018-01-26 06:46:11 +03:00
|
|
|
req := requestWithBody("POST", path, body)
|
|
|
|
|
|
|
|
req.Body = ioutil.NopCloser(&hook{})
|
|
|
|
req.Header.Add("Content-Type", MIMEPROTOBUF)
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-09-07 05:10:32 +03:00
|
|
|
invalid_obj := FooStruct{}
|
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(`{"msg":"hello"}`))
|
|
|
|
req.Header.Add("Content-Type", MIMEPROTOBUF)
|
|
|
|
err = b.Bind(req, &invalid_obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err.Error(), "obj is not ProtoMessage")
|
|
|
|
|
2018-08-12 18:38:31 +03:00
|
|
|
obj = protoexample.Test{}
|
2018-01-26 06:46:11 +03:00
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
|
|
|
req.Header.Add("Content-Type", MIMEPROTOBUF)
|
|
|
|
err = ProtoBuf.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func requestWithBody(method, path, body string) (req *http.Request) {
|
|
|
|
req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
|
2015-04-09 13:15:02 +03:00
|
|
|
return
|
|
|
|
}
|