| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550 |
- // 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"
- "encoding/json"
- "errors"
- "io"
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "os"
- "strconv"
- "strings"
- "testing"
- "time"
- "github.com/gin-gonic/gin/testdata/protoexample"
- "github.com/golang/protobuf/proto"
- "github.com/stretchr/testify/assert"
- "github.com/ugorji/go/codec"
- )
- type FooStruct struct {
- Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required"`
- }
- type FooBarStruct struct {
- FooStruct
- Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
- }
- 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"`
- }
- type FooDefaultBarStruct struct {
- FooStruct
- Bar string `msgpack:"bar" json:"bar" form:"bar,default=hello" xml:"bar" binding:"required"`
- }
- type FooStructUseNumber struct {
- Foo interface{} `json:"foo" binding:"required"`
- }
- type FooBarStructForTimeType struct {
- 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"`
- }
- 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"`
- }
- type FooStructForIgnoreFormTag struct {
- Foo *string `form:"-"`
- }
- 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"`
- }
- type FooStructForStructType struct {
- StructFoo struct {
- Idx int `form:"idx"`
- }
- }
- type FooStructForStructPointerType struct {
- StructPointerFoo *struct {
- Name string `form:"name"`
- }
- }
- type FooStructForSliceMapType struct {
- // Unknown type: not support map
- SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
- }
- type FooStructForBoolType struct {
- BoolFoo bool `form:"bool_foo"`
- }
- type FooBarStructForIntType struct {
- IntFoo int `form:"int_foo"`
- IntBar int `form:"int_bar" binding:"required"`
- }
- type FooBarStructForInt8Type struct {
- Int8Foo int8 `form:"int8_foo"`
- Int8Bar int8 `form:"int8_bar" binding:"required"`
- }
- type FooBarStructForInt16Type struct {
- Int16Foo int16 `form:"int16_foo"`
- Int16Bar int16 `form:"int16_bar" binding:"required"`
- }
- type FooBarStructForInt32Type struct {
- Int32Foo int32 `form:"int32_foo"`
- Int32Bar int32 `form:"int32_bar" binding:"required"`
- }
- type FooBarStructForInt64Type struct {
- Int64Foo int64 `form:"int64_foo"`
- Int64Bar int64 `form:"int64_bar" binding:"required"`
- }
- type FooBarStructForUintType struct {
- UintFoo uint `form:"uint_foo"`
- UintBar uint `form:"uint_bar" binding:"required"`
- }
- type FooBarStructForUint8Type struct {
- Uint8Foo uint8 `form:"uint8_foo"`
- Uint8Bar uint8 `form:"uint8_bar" binding:"required"`
- }
- type FooBarStructForUint16Type struct {
- Uint16Foo uint16 `form:"uint16_foo"`
- Uint16Bar uint16 `form:"uint16_bar" binding:"required"`
- }
- type FooBarStructForUint32Type struct {
- Uint32Foo uint32 `form:"uint32_foo"`
- Uint32Bar uint32 `form:"uint32_bar" binding:"required"`
- }
- type FooBarStructForUint64Type struct {
- Uint64Foo uint64 `form:"uint64_foo"`
- Uint64Bar uint64 `form:"uint64_bar" binding:"required"`
- }
- type FooBarStructForBoolType struct {
- BoolFoo bool `form:"bool_foo"`
- BoolBar bool `form:"bool_bar" binding:"required"`
- }
- type FooBarStructForFloat32Type struct {
- Float32Foo float32 `form:"float32_foo"`
- Float32Bar float32 `form:"float32_bar" binding:"required"`
- }
- type FooBarStructForFloat64Type struct {
- Float64Foo float64 `form:"float64_foo"`
- Float64Bar float64 `form:"float64_bar" binding:"required"`
- }
- 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"`
- }
- func TestBindingDefault(t *testing.T) {
- assert.Equal(t, Form, Default("GET", ""))
- assert.Equal(t, Form, Default("GET", MIMEJSON))
- assert.Equal(t, JSON, Default("POST", MIMEJSON))
- assert.Equal(t, JSON, Default("PUT", MIMEJSON))
- assert.Equal(t, XML, Default("POST", MIMEXML))
- assert.Equal(t, XML, Default("PUT", MIMEXML2))
- assert.Equal(t, Form, Default("POST", MIMEPOSTForm))
- assert.Equal(t, Form, Default("PUT", MIMEPOSTForm))
- assert.Equal(t, FormMultipart, Default("POST", MIMEMultipartPOSTForm))
- assert.Equal(t, FormMultipart, Default("PUT", MIMEMultipartPOSTForm))
- assert.Equal(t, ProtoBuf, Default("POST", MIMEPROTOBUF))
- assert.Equal(t, ProtoBuf, Default("PUT", MIMEPROTOBUF))
- assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK))
- assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2))
- assert.Equal(t, YAML, Default("POST", MIMEYAML))
- assert.Equal(t, YAML, Default("PUT", MIMEYAML))
- }
- func TestBindingJSONNilBody(t *testing.T) {
- var obj FooStruct
- req, _ := http.NewRequest(http.MethodPost, "/", nil)
- err := JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- func TestBindingJSON(t *testing.T) {
- testBodyBinding(t,
- JSON, "json",
- "/", "/",
- `{"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) {
- testFormBinding(t, "POST",
- "/", "/",
- "foo=bar&bar=foo", "bar2=foo")
- }
- func TestBindingForm2(t *testing.T) {
- testFormBinding(t, "GET",
- "/?foo=bar&bar=foo", "/?bar2=foo",
- "", "")
- }
- func TestBindingFormDefaultValue(t *testing.T) {
- testFormBindingDefaultValue(t, "POST",
- "/", "/",
- "foo=bar", "bar2=foo")
- }
- func TestBindingFormDefaultValue2(t *testing.T) {
- testFormBindingDefaultValue(t, "GET",
- "/?foo=bar", "/?bar2=foo",
- "", "")
- }
- func TestBindingFormForTime(t *testing.T) {
- testFormBindingForTime(t, "POST",
- "/", "/",
- "time_foo=2017-11-15&time_bar=", "bar2=foo")
- 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",
- "/?time_foo=2017-11-15&time_bar=", "/?bar2=foo",
- "", "")
- 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",
- "", "")
- }
- func TestFormBindingIgnoreField(t *testing.T) {
- testFormBindingIgnoreField(t, "POST",
- "/", "/",
- "-=bar", "")
- }
- 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",
- "/", "/",
- "map_foo={\"bar\":123}", "map_foo=1", "Map")
- 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")
- testFormBindingForType(t, "POST",
- "/", "/",
- "int_foo=&int_bar=-12", "bar2=-123", "Int")
- testFormBindingForType(t, "GET",
- "/?int_foo=&int_bar=-12", "/?bar2=-123",
- "", "", "Int")
- testFormBindingForType(t, "POST",
- "/", "/",
- "int8_foo=&int8_bar=-12", "bar2=-123", "Int8")
- testFormBindingForType(t, "GET",
- "/?int8_foo=&int8_bar=-12", "/?bar2=-123",
- "", "", "Int8")
- testFormBindingForType(t, "POST",
- "/", "/",
- "int16_foo=&int16_bar=-12", "bar2=-123", "Int16")
- testFormBindingForType(t, "GET",
- "/?int16_foo=&int16_bar=-12", "/?bar2=-123",
- "", "", "Int16")
- testFormBindingForType(t, "POST",
- "/", "/",
- "int32_foo=&int32_bar=-12", "bar2=-123", "Int32")
- testFormBindingForType(t, "GET",
- "/?int32_foo=&int32_bar=-12", "/?bar2=-123",
- "", "", "Int32")
- testFormBindingForType(t, "POST",
- "/", "/",
- "int64_foo=&int64_bar=-12", "bar2=-123", "Int64")
- testFormBindingForType(t, "GET",
- "/?int64_foo=&int64_bar=-12", "/?bar2=-123",
- "", "", "Int64")
- testFormBindingForType(t, "POST",
- "/", "/",
- "uint_foo=&uint_bar=12", "bar2=123", "Uint")
- testFormBindingForType(t, "GET",
- "/?uint_foo=&uint_bar=12", "/?bar2=123",
- "", "", "Uint")
- testFormBindingForType(t, "POST",
- "/", "/",
- "uint8_foo=&uint8_bar=12", "bar2=123", "Uint8")
- testFormBindingForType(t, "GET",
- "/?uint8_foo=&uint8_bar=12", "/?bar2=123",
- "", "", "Uint8")
- testFormBindingForType(t, "POST",
- "/", "/",
- "uint16_foo=&uint16_bar=12", "bar2=123", "Uint16")
- testFormBindingForType(t, "GET",
- "/?uint16_foo=&uint16_bar=12", "/?bar2=123",
- "", "", "Uint16")
- testFormBindingForType(t, "POST",
- "/", "/",
- "uint32_foo=&uint32_bar=12", "bar2=123", "Uint32")
- testFormBindingForType(t, "GET",
- "/?uint32_foo=&uint32_bar=12", "/?bar2=123",
- "", "", "Uint32")
- testFormBindingForType(t, "POST",
- "/", "/",
- "uint64_foo=&uint64_bar=12", "bar2=123", "Uint64")
- testFormBindingForType(t, "GET",
- "/?uint64_foo=&uint64_bar=12", "/?bar2=123",
- "", "", "Uint64")
- testFormBindingForType(t, "POST",
- "/", "/",
- "bool_foo=&bool_bar=true", "bar2=true", "Bool")
- testFormBindingForType(t, "GET",
- "/?bool_foo=&bool_bar=true", "/?bar2=true",
- "", "", "Bool")
- testFormBindingForType(t, "POST",
- "/", "/",
- "float32_foo=&float32_bar=-12.34", "bar2=12.3", "Float32")
- testFormBindingForType(t, "GET",
- "/?float32_foo=&float32_bar=-12.34", "/?bar2=12.3",
- "", "", "Float32")
- testFormBindingForType(t, "POST",
- "/", "/",
- "float64_foo=&float64_bar=-12.34", "bar2=12.3", "Float64")
- testFormBindingForType(t, "GET",
- "/?float64_foo=&float64_bar=-12.34", "/?bar2=12.3",
- "", "", "Float64")
- testFormBindingForType(t, "POST",
- "/", "/",
- "ptr_bar=test", "bar2=test", "Ptr")
- testFormBindingForType(t, "GET",
- "/?ptr_bar=test", "/?bar2=test",
- "", "", "Ptr")
- 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")
- }
- 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", "")
- }
- 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", "")
- }
- func TestBindingQueryBoolFail(t *testing.T) {
- testQueryBindingBoolFail(t, "GET",
- "/?bool_foo=fasl", "/?bar2=foo",
- "bool_foo=unused", "")
- }
- func TestBindingXML(t *testing.T) {
- testBodyBinding(t,
- XML, "xml",
- "/", "/",
- "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
- }
- func TestBindingXMLFail(t *testing.T) {
- testBodyBindingFail(t,
- XML, "xml",
- "/", "/",
- "<map><foo>bar<foo></map>", "<map><bar>foo</bar></map>")
- }
- func TestBindingYAML(t *testing.T) {
- testBodyBinding(t,
- YAML, "yaml",
- "/", "/",
- `foo: bar`, `bar: foo`)
- }
- func TestBindingYAMLFail(t *testing.T) {
- testBodyBindingFail(t,
- YAML, "yaml",
- "/", "/",
- `foo:\nbar`, `bar: foo`)
- }
- 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)
- req.Header.Set("Content-Type", MIMEPOSTForm)
- return req
- }
- func createDefaultFormPostRequest(t *testing.T) *http.Request {
- req, err := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar"))
- assert.NoError(t, err)
- req.Header.Set("Content-Type", MIMEPOSTForm)
- return req
- }
- 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)
- req.Header.Set("Content-Type", MIMEPOSTForm)
- return req
- }
- 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)
- io.Copy(fw, f)
- 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)
- io.Copy(fw, f)
- 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 createFormMultipartRequest(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"))
- req, err := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
- assert.NoError(t, err)
- req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
- return req
- }
- func createFormMultipartRequestForMap(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", "{\"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)
- req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
- return req
- }
- func TestBindingFormPost(t *testing.T) {
- req := createFormPostRequest(t)
- var obj FooBarStruct
- assert.NoError(t, FormPost.Bind(req, &obj))
- assert.Equal(t, "form-urlencoded", FormPost.Name())
- assert.Equal(t, "bar", obj.Foo)
- assert.Equal(t, "foo", obj.Bar)
- }
- func TestBindingDefaultValueFormPost(t *testing.T) {
- req := createDefaultFormPostRequest(t)
- var obj FooDefaultBarStruct
- assert.NoError(t, FormPost.Bind(req, &obj))
- assert.Equal(t, "bar", obj.Foo)
- assert.Equal(t, "hello", obj.Bar)
- }
- 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)
- var obj FooStructForMapType
- err := FormPost.Bind(req, &obj)
- assert.Error(t, err)
- }
- func TestBindingFormFilesMultipart(t *testing.T) {
- req := createFormFilesMultipartRequest(t)
- var obj FooBarFileStruct
- FormMultipart.Bind(req, &obj)
- // 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)
- }
- func TestBindingFormMultipart(t *testing.T) {
- req := createFormMultipartRequest(t)
- var obj FooBarStruct
- assert.NoError(t, FormMultipart.Bind(req, &obj))
- assert.Equal(t, "multipart/form-data", FormMultipart.Name())
- assert.Equal(t, "bar", obj.Foo)
- assert.Equal(t, "foo", obj.Bar)
- }
- 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)
- var obj FooStructForMapType
- err := FormMultipart.Bind(req, &obj)
- assert.Error(t, err)
- }
- func TestBindingProtoBuf(t *testing.T) {
- test := &protoexample.Test{
- Label: proto.String("yes"),
- }
- data, _ := proto.Marshal(test)
- testProtoBodyBinding(t,
- ProtoBuf, "protobuf",
- "/", "/",
- string(data), string(data[1:]))
- }
- func TestBindingProtoBufFail(t *testing.T) {
- test := &protoexample.Test{
- Label: proto.String("yes"),
- }
- data, _ := proto.Marshal(test)
- testProtoBodyBindingFail(t,
- ProtoBuf, "protobuf",
- "/", "/",
- string(data), string(data[1:]))
- }
- func TestBindingMsgPack(t *testing.T) {
- test := FooStruct{
- Foo: "bar",
- }
- h := new(codec.MsgpackHandle)
- assert.NotNil(t, h)
- buf := bytes.NewBuffer([]byte{})
- assert.NotNil(t, buf)
- err := codec.NewEncoder(buf, h).Encode(test)
- assert.NoError(t, err)
- data := buf.Bytes()
- testMsgPackBodyBinding(t,
- MsgPack, "msgpack",
- "/", "/",
- string(data), string(data[1:]))
- }
- 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)
- }
- func TestExistsSucceeds(t *testing.T) {
- type HogeStruct struct {
- Hoge *int `json:"hoge" binding:"exists"`
- }
- var obj HogeStruct
- req := requestWithBody("POST", "/", `{"hoge": 0}`)
- err := JSON.Bind(req, &obj)
- assert.NoError(t, err)
- }
- func TestExistsFails(t *testing.T) {
- type HogeStruct struct {
- Hoge *int `json:"foo" binding:"exists"`
- }
- var obj HogeStruct
- req := requestWithBody("POST", "/", `{"boen": 0}`)
- err := JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- 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, ¬))
- assert.Equal(t, map[string]interface{}(nil), not.Name)
- }
- 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)
- }
- func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
- b := Form
- assert.Equal(t, "form", b.Name())
- 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)
- assert.Equal(t, "bar", obj.Foo)
- assert.Equal(t, "foo", obj.Bar)
- obj = FooBarStruct{}
- req = requestWithBody(method, badPath, badBody)
- 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)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- func TestFormBindingFail(t *testing.T) {
- b := Form
- assert.Equal(t, "form", b.Name())
- obj := FooBarStruct{}
- req, _ := http.NewRequest("POST", "/", nil)
- err := b.Bind(req, &obj)
- assert.Error(t, err)
- }
- 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)
- }
- func TestFormPostBindingFail(t *testing.T) {
- b := FormPost
- assert.Equal(t, "form-urlencoded", b.Name())
- obj := FooBarStruct{}
- req, _ := http.NewRequest("POST", "/", nil)
- err := b.Bind(req, &obj)
- assert.Error(t, err)
- }
- func TestFormMultipartBindingFail(t *testing.T) {
- b := FormMultipart
- assert.Equal(t, "multipart/form-data", b.Name())
- 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
- assert.Equal(t, "form", b.Name())
- 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)
- 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())
- obj = FooBarStructForTimeType{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- func testFormBindingForTimeNotFormat(t *testing.T, method, path, badPath, body, badBody string) {
- b := Form
- assert.Equal(t, "form", b.Name())
- 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
- assert.Equal(t, "form", b.Name())
- 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
- assert.Equal(t, "form", b.Name())
- 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)
- }
- 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)
- }
- func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBody string) {
- b := Form
- assert.Equal(t, "form", b.Name())
- 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)
- assert.Equal(t, "", obj.TestName)
- 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
- assert.Equal(t, "form", b.Name())
- 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
- assert.Equal(t, "form", b.Name())
- req := requestWithBody(method, path, body)
- if method == "POST" {
- req.Header.Add("Content-Type", MIMEPOSTForm)
- }
- switch typ {
- case "Int":
- obj := FooBarStructForIntType{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, int(0), obj.IntFoo)
- assert.Equal(t, int(-12), obj.IntBar)
- obj = FooBarStructForIntType{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Int8":
- obj := FooBarStructForInt8Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, int8(0), obj.Int8Foo)
- assert.Equal(t, int8(-12), obj.Int8Bar)
- obj = FooBarStructForInt8Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Int16":
- obj := FooBarStructForInt16Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, int16(0), obj.Int16Foo)
- assert.Equal(t, int16(-12), obj.Int16Bar)
- obj = FooBarStructForInt16Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Int32":
- obj := FooBarStructForInt32Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, int32(0), obj.Int32Foo)
- assert.Equal(t, int32(-12), obj.Int32Bar)
- obj = FooBarStructForInt32Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Int64":
- obj := FooBarStructForInt64Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, int64(0), obj.Int64Foo)
- assert.Equal(t, int64(-12), obj.Int64Bar)
- obj = FooBarStructForInt64Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Uint":
- obj := FooBarStructForUintType{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, uint(0x0), obj.UintFoo)
- assert.Equal(t, uint(0xc), obj.UintBar)
- obj = FooBarStructForUintType{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Uint8":
- obj := FooBarStructForUint8Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, uint8(0x0), obj.Uint8Foo)
- assert.Equal(t, uint8(0xc), obj.Uint8Bar)
- obj = FooBarStructForUint8Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Uint16":
- obj := FooBarStructForUint16Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, uint16(0x0), obj.Uint16Foo)
- assert.Equal(t, uint16(0xc), obj.Uint16Bar)
- obj = FooBarStructForUint16Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Uint32":
- obj := FooBarStructForUint32Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, uint32(0x0), obj.Uint32Foo)
- assert.Equal(t, uint32(0xc), obj.Uint32Bar)
- obj = FooBarStructForUint32Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Uint64":
- obj := FooBarStructForUint64Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, uint64(0x0), obj.Uint64Foo)
- assert.Equal(t, uint64(0xc), obj.Uint64Bar)
- obj = FooBarStructForUint64Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Float32":
- obj := FooBarStructForFloat32Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, float32(0.0), obj.Float32Foo)
- assert.Equal(t, float32(-12.34), obj.Float32Bar)
- obj = FooBarStructForFloat32Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Float64":
- obj := FooBarStructForFloat64Type{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, float64(0.0), obj.Float64Foo)
- assert.Equal(t, float64(-12.34), obj.Float64Bar)
- obj = FooBarStructForFloat64Type{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Bool":
- obj := FooBarStructForBoolType{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.False(t, obj.BoolFoo)
- assert.True(t, obj.BoolBar)
- obj = FooBarStructForBoolType{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- case "Slice":
- obj := FooStructForSliceType{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, []int{1, 2}, obj.SliceFoo)
- obj = FooStructForSliceType{}
- req = requestWithBody(method, badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- 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)
- case "Map":
- obj := FooStructForMapType{}
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, float64(123), obj.MapFoo["bar"].(float64))
- case "SliceMap":
- obj := FooStructForSliceMapType{}
- err := b.Bind(req, &obj)
- assert.Error(t, err)
- 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)
- }
- }
- func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {
- b := Query
- assert.Equal(t, "query", b.Name())
- 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)
- assert.Equal(t, "bar", obj.Foo)
- assert.Equal(t, "foo", obj.Bar)
- }
- func testQueryBindingFail(t *testing.T, method, path, badPath, body, badBody string) {
- b := Query
- assert.Equal(t, "query", b.Name())
- 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)
- }
- 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)
- }
- func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
- assert.Equal(t, name, b.Name())
- obj := FooStruct{}
- req := requestWithBody("POST", path, body)
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, "bar", obj.Foo)
- obj = FooStruct{}
- req = requestWithBody("POST", badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
- assert.Equal(t, name, b.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, int64(123), v)
- 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, name, b.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, float64(123), obj.Foo)
- 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) {
- assert.Equal(t, name, b.Name())
- obj := FooStruct{}
- req := requestWithBody("POST", path, body)
- err := b.Bind(req, &obj)
- assert.Error(t, err)
- assert.Equal(t, "", obj.Foo)
- obj = FooStruct{}
- req = requestWithBody("POST", badPath, badBody)
- err = JSON.Bind(req, &obj)
- assert.Error(t, err)
- }
- func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
- assert.Equal(t, name, b.Name())
- obj := protoexample.Test{}
- req := requestWithBody("POST", path, body)
- req.Header.Add("Content-Type", MIMEPROTOBUF)
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, "yes", *obj.Label)
- obj = protoexample.Test{}
- req = requestWithBody("POST", badPath, badBody)
- req.Header.Add("Content-Type", MIMEPROTOBUF)
- err = ProtoBuf.Bind(req, &obj)
- assert.Error(t, err)
- }
- 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) {
- assert.Equal(t, name, b.Name())
- obj := protoexample.Test{}
- 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)
- obj = protoexample.Test{}
- req = requestWithBody("POST", badPath, badBody)
- req.Header.Add("Content-Type", MIMEPROTOBUF)
- err = ProtoBuf.Bind(req, &obj)
- assert.Error(t, err)
- }
- func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
- assert.Equal(t, name, b.Name())
- obj := FooStruct{}
- req := requestWithBody("POST", path, body)
- req.Header.Add("Content-Type", MIMEMSGPACK)
- err := b.Bind(req, &obj)
- assert.NoError(t, err)
- assert.Equal(t, "bar", obj.Foo)
- obj = FooStruct{}
- req = requestWithBody("POST", badPath, badBody)
- req.Header.Add("Content-Type", MIMEMSGPACK)
- err = MsgPack.Bind(req, &obj)
- assert.Error(t, err)
- }
- func requestWithBody(method, path, body string) (req *http.Request) {
- req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
- return
- }
- func TestCanSet(t *testing.T) {
- type CanSetStruct struct {
- lowerStart string `form:"lower"`
- }
- var c CanSetStruct
- assert.Nil(t, mapForm(&c, nil))
- }
- func formPostRequest(path, body string) *http.Request {
- req := requestWithBody("POST", path, body)
- req.Header.Add("Content-Type", MIMEPOSTForm)
- return req
- }
- func TestBindingSliceDefault(t *testing.T) {
- var s struct {
- Friends []string `form:"friends,default=mike"`
- }
- req := formPostRequest("", "")
- err := Form.Bind(req, &s)
- assert.NoError(t, err)
- assert.Len(t, s.Friends, 1)
- assert.Equal(t, "mike", s.Friends[0])
- }
- func TestBindingStructField(t *testing.T) {
- var s struct {
- Opts struct {
- Port int
- } `form:"opts"`
- }
- req := formPostRequest("", `opts={"Port": 8000}`)
- err := Form.Bind(req, &s)
- assert.NoError(t, err)
- assert.Equal(t, 8000, s.Opts.Port)
- }
- func TestBindingUnknownTypeChan(t *testing.T) {
- var s struct {
- Stop chan bool `form:"stop"`
- }
- req := formPostRequest("", "stop=true")
- err := Form.Bind(req, &s)
- assert.Error(t, err)
- assert.Equal(t, errUnknownType, err)
- }
- func TestBindingTimeDuration(t *testing.T) {
- var s struct {
- Timeout time.Duration `form:"timeout"`
- }
- // ok
- req := formPostRequest("", "timeout=5s")
- err := Form.Bind(req, &s)
- assert.NoError(t, err)
- assert.Equal(t, 5*time.Second, s.Timeout)
- // error
- req = formPostRequest("", "timeout=wrong")
- err = Form.Bind(req, &s)
- assert.Error(t, err)
- }
- func TestBindingArray(t *testing.T) {
- var s struct {
- Nums [2]int `form:"nums,default=4"`
- }
- // default
- req := formPostRequest("", "")
- err := Form.Bind(req, &s)
- assert.Error(t, err)
- assert.Equal(t, [2]int{0, 0}, s.Nums)
- // ok
- req = formPostRequest("", "nums=3&nums=8")
- err = Form.Bind(req, &s)
- assert.NoError(t, err)
- assert.Equal(t, [2]int{3, 8}, s.Nums)
- // not enough vals
- req = formPostRequest("", "nums=3")
- err = Form.Bind(req, &s)
- assert.Error(t, err)
- // error
- req = formPostRequest("", "nums=3&nums=wrong")
- err = Form.Bind(req, &s)
- assert.Error(t, err)
- }
|