binding_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "errors"
  9. "io/ioutil"
  10. "mime/multipart"
  11. "net/http"
  12. "testing"
  13. "time"
  14. "github.com/gin-gonic/gin/binding/example"
  15. "github.com/golang/protobuf/proto"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/ugorji/go/codec"
  18. )
  19. type FooStruct struct {
  20. Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required"`
  21. }
  22. type FooBarStruct struct {
  23. FooStruct
  24. Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
  25. }
  26. type FooDefaultBarStruct struct {
  27. FooStruct
  28. Bar string `msgpack:"bar" json:"bar" form:"bar,default=hello" xml:"bar" binding:"required"`
  29. }
  30. type FooStructUseNumber struct {
  31. Foo interface{} `json:"foo" binding:"required"`
  32. }
  33. type FooBarStructForTimeType struct {
  34. TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"Asia/Chongqing"`
  35. TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`
  36. }
  37. type FooStructForTimeTypeNotFormat struct {
  38. TimeFoo time.Time `form:"time_foo"`
  39. }
  40. type FooStructForTimeTypeFailFormat struct {
  41. TimeFoo time.Time `form:"time_foo" time_format:"2017-11-15"`
  42. }
  43. type FooStructForTimeTypeFailLocation struct {
  44. TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_location:"/asia/chongqing"`
  45. }
  46. type FooStructForMapType struct {
  47. // Unknown type: not support map
  48. MapFoo map[string]interface{} `form:"map_foo"`
  49. }
  50. type InvalidNameType struct {
  51. TestName string `invalid_name:"test_name"`
  52. }
  53. type InvalidNameMapType struct {
  54. TestName struct {
  55. MapFoo map[string]interface{} `form:"map_foo"`
  56. }
  57. }
  58. type FooStructForSliceType struct {
  59. SliceFoo []int `form:"slice_foo"`
  60. }
  61. type FooStructForStructType struct {
  62. StructFoo struct {
  63. Idx int `form:"idx"`
  64. }
  65. }
  66. type FooStructForStructPointerType struct {
  67. StructPointerFoo *struct {
  68. Name string `form:"name"`
  69. }
  70. }
  71. type FooStructForSliceMapType struct {
  72. // Unknown type: not support map
  73. SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
  74. }
  75. type FooBarStructForIntType struct {
  76. IntFoo int `form:"int_foo"`
  77. IntBar int `form:"int_bar" binding:"required"`
  78. }
  79. type FooBarStructForInt8Type struct {
  80. Int8Foo int8 `form:"int8_foo"`
  81. Int8Bar int8 `form:"int8_bar" binding:"required"`
  82. }
  83. type FooBarStructForInt16Type struct {
  84. Int16Foo int16 `form:"int16_foo"`
  85. Int16Bar int16 `form:"int16_bar" binding:"required"`
  86. }
  87. type FooBarStructForInt32Type struct {
  88. Int32Foo int32 `form:"int32_foo"`
  89. Int32Bar int32 `form:"int32_bar" binding:"required"`
  90. }
  91. type FooBarStructForInt64Type struct {
  92. Int64Foo int64 `form:"int64_foo"`
  93. Int64Bar int64 `form:"int64_bar" binding:"required"`
  94. }
  95. type FooBarStructForUintType struct {
  96. UintFoo uint `form:"uint_foo"`
  97. UintBar uint `form:"uint_bar" binding:"required"`
  98. }
  99. type FooBarStructForUint8Type struct {
  100. Uint8Foo uint8 `form:"uint8_foo"`
  101. Uint8Bar uint8 `form:"uint8_bar" binding:"required"`
  102. }
  103. type FooBarStructForUint16Type struct {
  104. Uint16Foo uint16 `form:"uint16_foo"`
  105. Uint16Bar uint16 `form:"uint16_bar" binding:"required"`
  106. }
  107. type FooBarStructForUint32Type struct {
  108. Uint32Foo uint32 `form:"uint32_foo"`
  109. Uint32Bar uint32 `form:"uint32_bar" binding:"required"`
  110. }
  111. type FooBarStructForUint64Type struct {
  112. Uint64Foo uint64 `form:"uint64_foo"`
  113. Uint64Bar uint64 `form:"uint64_bar" binding:"required"`
  114. }
  115. type FooBarStructForBoolType struct {
  116. BoolFoo bool `form:"bool_foo"`
  117. BoolBar bool `form:"bool_bar" binding:"required"`
  118. }
  119. type FooBarStructForFloat32Type struct {
  120. Float32Foo float32 `form:"float32_foo"`
  121. Float32Bar float32 `form:"float32_bar" binding:"required"`
  122. }
  123. type FooBarStructForFloat64Type struct {
  124. Float64Foo float64 `form:"float64_foo"`
  125. Float64Bar float64 `form:"float64_bar" binding:"required"`
  126. }
  127. type FooStructForStringPtrType struct {
  128. PtrFoo *string `form:"ptr_foo"`
  129. PtrBar *string `form:"ptr_bar" binding:"required"`
  130. }
  131. type FooStructForMapPtrType struct {
  132. PtrBar *map[string]interface{} `form:"ptr_bar"`
  133. }
  134. func TestBindingDefault(t *testing.T) {
  135. assert.Equal(t, Form, Default("GET", ""))
  136. assert.Equal(t, Form, Default("GET", MIMEJSON))
  137. assert.Equal(t, JSON, Default("POST", MIMEJSON))
  138. assert.Equal(t, JSON, Default("PUT", MIMEJSON))
  139. assert.Equal(t, XML, Default("POST", MIMEXML))
  140. assert.Equal(t, XML, Default("PUT", MIMEXML2))
  141. assert.Equal(t, Form, Default("POST", MIMEPOSTForm))
  142. assert.Equal(t, Form, Default("PUT", MIMEPOSTForm))
  143. assert.Equal(t, Form, Default("POST", MIMEMultipartPOSTForm))
  144. assert.Equal(t, Form, Default("PUT", MIMEMultipartPOSTForm))
  145. assert.Equal(t, ProtoBuf, Default("POST", MIMEPROTOBUF))
  146. assert.Equal(t, ProtoBuf, Default("PUT", MIMEPROTOBUF))
  147. assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK))
  148. assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2))
  149. }
  150. func TestBindingJSON(t *testing.T) {
  151. testBodyBinding(t,
  152. JSON, "json",
  153. "/", "/",
  154. `{"foo": "bar"}`, `{"bar": "foo"}`)
  155. }
  156. func TestBindingJSONUseNumber(t *testing.T) {
  157. testBodyBindingUseNumber(t,
  158. JSON, "json",
  159. "/", "/",
  160. `{"foo": 123}`, `{"bar": "foo"}`)
  161. }
  162. func TestBindingJSONUseNumber2(t *testing.T) {
  163. testBodyBindingUseNumber2(t,
  164. JSON, "json",
  165. "/", "/",
  166. `{"foo": 123}`, `{"bar": "foo"}`)
  167. }
  168. func TestBindingForm(t *testing.T) {
  169. testFormBinding(t, "POST",
  170. "/", "/",
  171. "foo=bar&bar=foo", "bar2=foo")
  172. }
  173. func TestBindingForm2(t *testing.T) {
  174. testFormBinding(t, "GET",
  175. "/?foo=bar&bar=foo", "/?bar2=foo",
  176. "", "")
  177. }
  178. func TestBindingFormDefaultValue(t *testing.T) {
  179. testFormBindingDefaultValue(t, "POST",
  180. "/", "/",
  181. "foo=bar", "bar2=foo")
  182. }
  183. func TestBindingFormDefaultValue2(t *testing.T) {
  184. testFormBindingDefaultValue(t, "GET",
  185. "/?foo=bar", "/?bar2=foo",
  186. "", "")
  187. }
  188. func TestBindingFormForTime(t *testing.T) {
  189. testFormBindingForTime(t, "POST",
  190. "/", "/",
  191. "time_foo=2017-11-15&time_bar=", "bar2=foo")
  192. testFormBindingForTimeNotFormat(t, "POST",
  193. "/", "/",
  194. "time_foo=2017-11-15", "bar2=foo")
  195. testFormBindingForTimeFailFormat(t, "POST",
  196. "/", "/",
  197. "time_foo=2017-11-15", "bar2=foo")
  198. testFormBindingForTimeFailLocation(t, "POST",
  199. "/", "/",
  200. "time_foo=2017-11-15", "bar2=foo")
  201. }
  202. func TestBindingFormForTime2(t *testing.T) {
  203. testFormBindingForTime(t, "GET",
  204. "/?time_foo=2017-11-15&time_bar=", "/?bar2=foo",
  205. "", "")
  206. testFormBindingForTimeNotFormat(t, "GET",
  207. "/?time_foo=2017-11-15", "/?bar2=foo",
  208. "", "")
  209. testFormBindingForTimeFailFormat(t, "GET",
  210. "/?time_foo=2017-11-15", "/?bar2=foo",
  211. "", "")
  212. testFormBindingForTimeFailLocation(t, "GET",
  213. "/?time_foo=2017-11-15", "/?bar2=foo",
  214. "", "")
  215. }
  216. func TestBindingFormInvalidName(t *testing.T) {
  217. testFormBindingInvalidName(t, "POST",
  218. "/", "/",
  219. "test_name=bar", "bar2=foo")
  220. }
  221. func TestBindingFormInvalidName2(t *testing.T) {
  222. testFormBindingInvalidName2(t, "POST",
  223. "/", "/",
  224. "map_foo=bar", "bar2=foo")
  225. }
  226. func TestBindingFormForType(t *testing.T) {
  227. testFormBindingForType(t, "POST",
  228. "/", "/",
  229. "map_foo=", "bar2=1", "Map")
  230. testFormBindingForType(t, "POST",
  231. "/", "/",
  232. "slice_foo=1&slice_foo=2", "bar2=1&bar2=2", "Slice")
  233. testFormBindingForType(t, "GET",
  234. "/?slice_foo=1&slice_foo=2", "/?bar2=1&bar2=2",
  235. "", "", "Slice")
  236. testFormBindingForType(t, "POST",
  237. "/", "/",
  238. "slice_map_foo=1&slice_map_foo=2", "bar2=1&bar2=2", "SliceMap")
  239. testFormBindingForType(t, "GET",
  240. "/?slice_map_foo=1&slice_map_foo=2", "/?bar2=1&bar2=2",
  241. "", "", "SliceMap")
  242. testFormBindingForType(t, "POST",
  243. "/", "/",
  244. "int_foo=&int_bar=-12", "bar2=-123", "Int")
  245. testFormBindingForType(t, "GET",
  246. "/?int_foo=&int_bar=-12", "/?bar2=-123",
  247. "", "", "Int")
  248. testFormBindingForType(t, "POST",
  249. "/", "/",
  250. "int8_foo=&int8_bar=-12", "bar2=-123", "Int8")
  251. testFormBindingForType(t, "GET",
  252. "/?int8_foo=&int8_bar=-12", "/?bar2=-123",
  253. "", "", "Int8")
  254. testFormBindingForType(t, "POST",
  255. "/", "/",
  256. "int16_foo=&int16_bar=-12", "bar2=-123", "Int16")
  257. testFormBindingForType(t, "GET",
  258. "/?int16_foo=&int16_bar=-12", "/?bar2=-123",
  259. "", "", "Int16")
  260. testFormBindingForType(t, "POST",
  261. "/", "/",
  262. "int32_foo=&int32_bar=-12", "bar2=-123", "Int32")
  263. testFormBindingForType(t, "GET",
  264. "/?int32_foo=&int32_bar=-12", "/?bar2=-123",
  265. "", "", "Int32")
  266. testFormBindingForType(t, "POST",
  267. "/", "/",
  268. "int64_foo=&int64_bar=-12", "bar2=-123", "Int64")
  269. testFormBindingForType(t, "GET",
  270. "/?int64_foo=&int64_bar=-12", "/?bar2=-123",
  271. "", "", "Int64")
  272. testFormBindingForType(t, "POST",
  273. "/", "/",
  274. "uint_foo=&uint_bar=12", "bar2=123", "Uint")
  275. testFormBindingForType(t, "GET",
  276. "/?uint_foo=&uint_bar=12", "/?bar2=123",
  277. "", "", "Uint")
  278. testFormBindingForType(t, "POST",
  279. "/", "/",
  280. "uint8_foo=&uint8_bar=12", "bar2=123", "Uint8")
  281. testFormBindingForType(t, "GET",
  282. "/?uint8_foo=&uint8_bar=12", "/?bar2=123",
  283. "", "", "Uint8")
  284. testFormBindingForType(t, "POST",
  285. "/", "/",
  286. "uint16_foo=&uint16_bar=12", "bar2=123", "Uint16")
  287. testFormBindingForType(t, "GET",
  288. "/?uint16_foo=&uint16_bar=12", "/?bar2=123",
  289. "", "", "Uint16")
  290. testFormBindingForType(t, "POST",
  291. "/", "/",
  292. "uint32_foo=&uint32_bar=12", "bar2=123", "Uint32")
  293. testFormBindingForType(t, "GET",
  294. "/?uint32_foo=&uint32_bar=12", "/?bar2=123",
  295. "", "", "Uint32")
  296. testFormBindingForType(t, "POST",
  297. "/", "/",
  298. "uint64_foo=&uint64_bar=12", "bar2=123", "Uint64")
  299. testFormBindingForType(t, "GET",
  300. "/?uint64_foo=&uint64_bar=12", "/?bar2=123",
  301. "", "", "Uint64")
  302. testFormBindingForType(t, "POST",
  303. "/", "/",
  304. "bool_foo=&bool_bar=true", "bar2=true", "Bool")
  305. testFormBindingForType(t, "GET",
  306. "/?bool_foo=&bool_bar=true", "/?bar2=true",
  307. "", "", "Bool")
  308. testFormBindingForType(t, "POST",
  309. "/", "/",
  310. "float32_foo=&float32_bar=-12.34", "bar2=12.3", "Float32")
  311. testFormBindingForType(t, "GET",
  312. "/?float32_foo=&float32_bar=-12.34", "/?bar2=12.3",
  313. "", "", "Float32")
  314. testFormBindingForType(t, "POST",
  315. "/", "/",
  316. "float64_foo=&float64_bar=-12.34", "bar2=12.3", "Float64")
  317. testFormBindingForType(t, "GET",
  318. "/?float64_foo=&float64_bar=-12.34", "/?bar2=12.3",
  319. "", "", "Float64")
  320. testFormBindingForType(t, "POST",
  321. "/", "/",
  322. "ptr_bar=test", "bar2=test", "Ptr")
  323. testFormBindingForType(t, "GET",
  324. "/?ptr_bar=test", "/?bar2=test",
  325. "", "", "Ptr")
  326. testFormBindingForType(t, "POST",
  327. "/", "/",
  328. "idx=123", "id1=1", "Struct")
  329. testFormBindingForType(t, "GET",
  330. "/?idx=123", "/?id1=1",
  331. "", "", "Struct")
  332. testFormBindingForType(t, "POST",
  333. "/", "/",
  334. "name=thinkerou", "name1=ou", "StructPointer")
  335. testFormBindingForType(t, "GET",
  336. "/?name=thinkerou", "/?name1=ou",
  337. "", "", "StructPointer")
  338. }
  339. func TestBindingQuery(t *testing.T) {
  340. testQueryBinding(t, "POST",
  341. "/?foo=bar&bar=foo", "/",
  342. "foo=unused", "bar2=foo")
  343. }
  344. func TestBindingQuery2(t *testing.T) {
  345. testQueryBinding(t, "GET",
  346. "/?foo=bar&bar=foo", "/?bar2=foo",
  347. "foo=unused", "")
  348. }
  349. func TestBindingQueryFail(t *testing.T) {
  350. testQueryBindingFail(t, "POST",
  351. "/?map_foo=", "/",
  352. "map_foo=unused", "bar2=foo")
  353. }
  354. func TestBindingQueryFail2(t *testing.T) {
  355. testQueryBindingFail(t, "GET",
  356. "/?map_foo=", "/?bar2=foo",
  357. "map_foo=unused", "")
  358. }
  359. func TestBindingXML(t *testing.T) {
  360. testBodyBinding(t,
  361. XML, "xml",
  362. "/", "/",
  363. "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
  364. }
  365. func TestBindingXMLFail(t *testing.T) {
  366. testBodyBindingFail(t,
  367. XML, "xml",
  368. "/", "/",
  369. "<map><foo>bar<foo></map>", "<map><bar>foo</bar></map>")
  370. }
  371. func createFormPostRequest() *http.Request {
  372. req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar&bar=foo"))
  373. req.Header.Set("Content-Type", MIMEPOSTForm)
  374. return req
  375. }
  376. func createDefaultFormPostRequest() *http.Request {
  377. req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar"))
  378. req.Header.Set("Content-Type", MIMEPOSTForm)
  379. return req
  380. }
  381. func createFormPostRequestFail() *http.Request {
  382. req, _ := http.NewRequest("POST", "/?map_foo=getfoo", bytes.NewBufferString("map_foo=bar"))
  383. req.Header.Set("Content-Type", MIMEPOSTForm)
  384. return req
  385. }
  386. func createFormMultipartRequest() *http.Request {
  387. boundary := "--testboundary"
  388. body := new(bytes.Buffer)
  389. mw := multipart.NewWriter(body)
  390. defer mw.Close()
  391. mw.SetBoundary(boundary)
  392. mw.WriteField("foo", "bar")
  393. mw.WriteField("bar", "foo")
  394. req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
  395. req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
  396. return req
  397. }
  398. func createFormMultipartRequestFail() *http.Request {
  399. boundary := "--testboundary"
  400. body := new(bytes.Buffer)
  401. mw := multipart.NewWriter(body)
  402. defer mw.Close()
  403. mw.SetBoundary(boundary)
  404. mw.WriteField("map_foo", "bar")
  405. req, _ := http.NewRequest("POST", "/?map_foo=getfoo", body)
  406. req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
  407. return req
  408. }
  409. func TestBindingFormPost(t *testing.T) {
  410. req := createFormPostRequest()
  411. var obj FooBarStruct
  412. FormPost.Bind(req, &obj)
  413. assert.Equal(t, "form-urlencoded", FormPost.Name())
  414. assert.Equal(t, "bar", obj.Foo)
  415. assert.Equal(t, "foo", obj.Bar)
  416. }
  417. func TestBindingDefaultValueFormPost(t *testing.T) {
  418. req := createDefaultFormPostRequest()
  419. var obj FooDefaultBarStruct
  420. FormPost.Bind(req, &obj)
  421. assert.Equal(t, "bar", obj.Foo)
  422. assert.Equal(t, "hello", obj.Bar)
  423. }
  424. func TestBindingFormPostFail(t *testing.T) {
  425. req := createFormPostRequestFail()
  426. var obj FooStructForMapType
  427. err := FormPost.Bind(req, &obj)
  428. assert.Error(t, err)
  429. }
  430. func TestBindingFormMultipart(t *testing.T) {
  431. req := createFormMultipartRequest()
  432. var obj FooBarStruct
  433. FormMultipart.Bind(req, &obj)
  434. assert.Equal(t, "multipart/form-data", FormMultipart.Name())
  435. assert.Equal(t, "bar", obj.Foo)
  436. assert.Equal(t, "foo", obj.Bar)
  437. }
  438. func TestBindingFormMultipartFail(t *testing.T) {
  439. req := createFormMultipartRequestFail()
  440. var obj FooStructForMapType
  441. err := FormMultipart.Bind(req, &obj)
  442. assert.Error(t, err)
  443. }
  444. func TestBindingProtoBuf(t *testing.T) {
  445. test := &example.Test{
  446. Label: proto.String("yes"),
  447. }
  448. data, _ := proto.Marshal(test)
  449. testProtoBodyBinding(t,
  450. ProtoBuf, "protobuf",
  451. "/", "/",
  452. string(data), string(data[1:]))
  453. }
  454. func TestBindingProtoBufFail(t *testing.T) {
  455. test := &example.Test{
  456. Label: proto.String("yes"),
  457. }
  458. data, _ := proto.Marshal(test)
  459. testProtoBodyBindingFail(t,
  460. ProtoBuf, "protobuf",
  461. "/", "/",
  462. string(data), string(data[1:]))
  463. }
  464. func TestBindingMsgPack(t *testing.T) {
  465. test := FooStruct{
  466. Foo: "bar",
  467. }
  468. h := new(codec.MsgpackHandle)
  469. assert.NotNil(t, h)
  470. buf := bytes.NewBuffer([]byte{})
  471. assert.NotNil(t, buf)
  472. err := codec.NewEncoder(buf, h).Encode(test)
  473. assert.NoError(t, err)
  474. data := buf.Bytes()
  475. testMsgPackBodyBinding(t,
  476. MsgPack, "msgpack",
  477. "/", "/",
  478. string(data), string(data[1:]))
  479. }
  480. func TestValidationFails(t *testing.T) {
  481. var obj FooStruct
  482. req := requestWithBody("POST", "/", `{"bar": "foo"}`)
  483. err := JSON.Bind(req, &obj)
  484. assert.Error(t, err)
  485. }
  486. func TestValidationDisabled(t *testing.T) {
  487. backup := Validator
  488. Validator = nil
  489. defer func() { Validator = backup }()
  490. var obj FooStruct
  491. req := requestWithBody("POST", "/", `{"bar": "foo"}`)
  492. err := JSON.Bind(req, &obj)
  493. assert.NoError(t, err)
  494. }
  495. func TestExistsSucceeds(t *testing.T) {
  496. type HogeStruct struct {
  497. Hoge *int `json:"hoge" binding:"exists"`
  498. }
  499. var obj HogeStruct
  500. req := requestWithBody("POST", "/", `{"hoge": 0}`)
  501. err := JSON.Bind(req, &obj)
  502. assert.NoError(t, err)
  503. }
  504. func TestExistsFails(t *testing.T) {
  505. type HogeStruct struct {
  506. Hoge *int `json:"foo" binding:"exists"`
  507. }
  508. var obj HogeStruct
  509. req := requestWithBody("POST", "/", `{"boen": 0}`)
  510. err := JSON.Bind(req, &obj)
  511. assert.Error(t, err)
  512. }
  513. func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
  514. b := Form
  515. assert.Equal(t, "form", b.Name())
  516. obj := FooBarStruct{}
  517. req := requestWithBody(method, path, body)
  518. if method == "POST" {
  519. req.Header.Add("Content-Type", MIMEPOSTForm)
  520. }
  521. err := b.Bind(req, &obj)
  522. assert.NoError(t, err)
  523. assert.Equal(t, "bar", obj.Foo)
  524. assert.Equal(t, "foo", obj.Bar)
  525. obj = FooBarStruct{}
  526. req = requestWithBody(method, badPath, badBody)
  527. err = JSON.Bind(req, &obj)
  528. assert.Error(t, err)
  529. }
  530. func testFormBindingDefaultValue(t *testing.T, method, path, badPath, body, badBody string) {
  531. b := Form
  532. assert.Equal(t, "form", b.Name())
  533. obj := FooDefaultBarStruct{}
  534. req := requestWithBody(method, path, body)
  535. if method == "POST" {
  536. req.Header.Add("Content-Type", MIMEPOSTForm)
  537. }
  538. err := b.Bind(req, &obj)
  539. assert.NoError(t, err)
  540. assert.Equal(t, "bar", obj.Foo)
  541. assert.Equal(t, "hello", obj.Bar)
  542. obj = FooDefaultBarStruct{}
  543. req = requestWithBody(method, badPath, badBody)
  544. err = JSON.Bind(req, &obj)
  545. assert.Error(t, err)
  546. }
  547. func TestFormBindingFail(t *testing.T) {
  548. b := Form
  549. assert.Equal(t, "form", b.Name())
  550. obj := FooBarStruct{}
  551. req, _ := http.NewRequest("POST", "/", nil)
  552. err := b.Bind(req, &obj)
  553. assert.Error(t, err)
  554. }
  555. func TestFormPostBindingFail(t *testing.T) {
  556. b := FormPost
  557. assert.Equal(t, "form-urlencoded", b.Name())
  558. obj := FooBarStruct{}
  559. req, _ := http.NewRequest("POST", "/", nil)
  560. err := b.Bind(req, &obj)
  561. assert.Error(t, err)
  562. }
  563. func TestFormMultipartBindingFail(t *testing.T) {
  564. b := FormMultipart
  565. assert.Equal(t, "multipart/form-data", b.Name())
  566. obj := FooBarStruct{}
  567. req, _ := http.NewRequest("POST", "/", nil)
  568. err := b.Bind(req, &obj)
  569. assert.Error(t, err)
  570. }
  571. func testFormBindingForTime(t *testing.T, method, path, badPath, body, badBody string) {
  572. b := Form
  573. assert.Equal(t, "form", b.Name())
  574. obj := FooBarStructForTimeType{}
  575. req := requestWithBody(method, path, body)
  576. if method == "POST" {
  577. req.Header.Add("Content-Type", MIMEPOSTForm)
  578. }
  579. err := b.Bind(req, &obj)
  580. assert.NoError(t, err)
  581. assert.Equal(t, int64(1510675200), obj.TimeFoo.Unix())
  582. assert.Equal(t, "Asia/Chongqing", obj.TimeFoo.Location().String())
  583. assert.Equal(t, int64(-62135596800), obj.TimeBar.Unix())
  584. assert.Equal(t, "UTC", obj.TimeBar.Location().String())
  585. obj = FooBarStructForTimeType{}
  586. req = requestWithBody(method, badPath, badBody)
  587. err = JSON.Bind(req, &obj)
  588. assert.Error(t, err)
  589. }
  590. func testFormBindingForTimeNotFormat(t *testing.T, method, path, badPath, body, badBody string) {
  591. b := Form
  592. assert.Equal(t, "form", b.Name())
  593. obj := FooStructForTimeTypeNotFormat{}
  594. req := requestWithBody(method, path, body)
  595. if method == "POST" {
  596. req.Header.Add("Content-Type", MIMEPOSTForm)
  597. }
  598. err := b.Bind(req, &obj)
  599. assert.Error(t, err)
  600. obj = FooStructForTimeTypeNotFormat{}
  601. req = requestWithBody(method, badPath, badBody)
  602. err = JSON.Bind(req, &obj)
  603. assert.Error(t, err)
  604. }
  605. func testFormBindingForTimeFailFormat(t *testing.T, method, path, badPath, body, badBody string) {
  606. b := Form
  607. assert.Equal(t, "form", b.Name())
  608. obj := FooStructForTimeTypeFailFormat{}
  609. req := requestWithBody(method, path, body)
  610. if method == "POST" {
  611. req.Header.Add("Content-Type", MIMEPOSTForm)
  612. }
  613. err := b.Bind(req, &obj)
  614. assert.Error(t, err)
  615. obj = FooStructForTimeTypeFailFormat{}
  616. req = requestWithBody(method, badPath, badBody)
  617. err = JSON.Bind(req, &obj)
  618. assert.Error(t, err)
  619. }
  620. func testFormBindingForTimeFailLocation(t *testing.T, method, path, badPath, body, badBody string) {
  621. b := Form
  622. assert.Equal(t, "form", b.Name())
  623. obj := FooStructForTimeTypeFailLocation{}
  624. req := requestWithBody(method, path, body)
  625. if method == "POST" {
  626. req.Header.Add("Content-Type", MIMEPOSTForm)
  627. }
  628. err := b.Bind(req, &obj)
  629. assert.Error(t, err)
  630. obj = FooStructForTimeTypeFailLocation{}
  631. req = requestWithBody(method, badPath, badBody)
  632. err = JSON.Bind(req, &obj)
  633. assert.Error(t, err)
  634. }
  635. func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBody string) {
  636. b := Form
  637. assert.Equal(t, "form", b.Name())
  638. obj := InvalidNameType{}
  639. req := requestWithBody(method, path, body)
  640. if method == "POST" {
  641. req.Header.Add("Content-Type", MIMEPOSTForm)
  642. }
  643. err := b.Bind(req, &obj)
  644. assert.NoError(t, err)
  645. assert.Equal(t, "", obj.TestName)
  646. obj = InvalidNameType{}
  647. req = requestWithBody(method, badPath, badBody)
  648. err = JSON.Bind(req, &obj)
  649. assert.Error(t, err)
  650. }
  651. func testFormBindingInvalidName2(t *testing.T, method, path, badPath, body, badBody string) {
  652. b := Form
  653. assert.Equal(t, "form", b.Name())
  654. obj := InvalidNameMapType{}
  655. req := requestWithBody(method, path, body)
  656. if method == "POST" {
  657. req.Header.Add("Content-Type", MIMEPOSTForm)
  658. }
  659. err := b.Bind(req, &obj)
  660. assert.Error(t, err)
  661. obj = InvalidNameMapType{}
  662. req = requestWithBody(method, badPath, badBody)
  663. err = JSON.Bind(req, &obj)
  664. assert.Error(t, err)
  665. }
  666. func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody string, typ string) {
  667. b := Form
  668. assert.Equal(t, "form", b.Name())
  669. req := requestWithBody(method, path, body)
  670. if method == "POST" {
  671. req.Header.Add("Content-Type", MIMEPOSTForm)
  672. }
  673. switch typ {
  674. case "Int":
  675. obj := FooBarStructForIntType{}
  676. err := b.Bind(req, &obj)
  677. assert.NoError(t, err)
  678. assert.Equal(t, int(0), obj.IntFoo)
  679. assert.Equal(t, int(-12), obj.IntBar)
  680. obj = FooBarStructForIntType{}
  681. req = requestWithBody(method, badPath, badBody)
  682. err = JSON.Bind(req, &obj)
  683. assert.Error(t, err)
  684. case "Int8":
  685. obj := FooBarStructForInt8Type{}
  686. err := b.Bind(req, &obj)
  687. assert.NoError(t, err)
  688. assert.Equal(t, int8(0), obj.Int8Foo)
  689. assert.Equal(t, int8(-12), obj.Int8Bar)
  690. obj = FooBarStructForInt8Type{}
  691. req = requestWithBody(method, badPath, badBody)
  692. err = JSON.Bind(req, &obj)
  693. assert.Error(t, err)
  694. case "Int16":
  695. obj := FooBarStructForInt16Type{}
  696. err := b.Bind(req, &obj)
  697. assert.NoError(t, err)
  698. assert.Equal(t, int16(0), obj.Int16Foo)
  699. assert.Equal(t, int16(-12), obj.Int16Bar)
  700. obj = FooBarStructForInt16Type{}
  701. req = requestWithBody(method, badPath, badBody)
  702. err = JSON.Bind(req, &obj)
  703. assert.Error(t, err)
  704. case "Int32":
  705. obj := FooBarStructForInt32Type{}
  706. err := b.Bind(req, &obj)
  707. assert.NoError(t, err)
  708. assert.Equal(t, int32(0), obj.Int32Foo)
  709. assert.Equal(t, int32(-12), obj.Int32Bar)
  710. obj = FooBarStructForInt32Type{}
  711. req = requestWithBody(method, badPath, badBody)
  712. err = JSON.Bind(req, &obj)
  713. assert.Error(t, err)
  714. case "Int64":
  715. obj := FooBarStructForInt64Type{}
  716. err := b.Bind(req, &obj)
  717. assert.NoError(t, err)
  718. assert.Equal(t, int64(0), obj.Int64Foo)
  719. assert.Equal(t, int64(-12), obj.Int64Bar)
  720. obj = FooBarStructForInt64Type{}
  721. req = requestWithBody(method, badPath, badBody)
  722. err = JSON.Bind(req, &obj)
  723. assert.Error(t, err)
  724. case "Uint":
  725. obj := FooBarStructForUintType{}
  726. err := b.Bind(req, &obj)
  727. assert.NoError(t, err)
  728. assert.Equal(t, uint(0x0), obj.UintFoo)
  729. assert.Equal(t, uint(0xc), obj.UintBar)
  730. obj = FooBarStructForUintType{}
  731. req = requestWithBody(method, badPath, badBody)
  732. err = JSON.Bind(req, &obj)
  733. assert.Error(t, err)
  734. case "Uint8":
  735. obj := FooBarStructForUint8Type{}
  736. err := b.Bind(req, &obj)
  737. assert.NoError(t, err)
  738. assert.Equal(t, uint8(0x0), obj.Uint8Foo)
  739. assert.Equal(t, uint8(0xc), obj.Uint8Bar)
  740. obj = FooBarStructForUint8Type{}
  741. req = requestWithBody(method, badPath, badBody)
  742. err = JSON.Bind(req, &obj)
  743. assert.Error(t, err)
  744. case "Uint16":
  745. obj := FooBarStructForUint16Type{}
  746. err := b.Bind(req, &obj)
  747. assert.NoError(t, err)
  748. assert.Equal(t, uint16(0x0), obj.Uint16Foo)
  749. assert.Equal(t, uint16(0xc), obj.Uint16Bar)
  750. obj = FooBarStructForUint16Type{}
  751. req = requestWithBody(method, badPath, badBody)
  752. err = JSON.Bind(req, &obj)
  753. assert.Error(t, err)
  754. case "Uint32":
  755. obj := FooBarStructForUint32Type{}
  756. err := b.Bind(req, &obj)
  757. assert.NoError(t, err)
  758. assert.Equal(t, uint32(0x0), obj.Uint32Foo)
  759. assert.Equal(t, uint32(0xc), obj.Uint32Bar)
  760. obj = FooBarStructForUint32Type{}
  761. req = requestWithBody(method, badPath, badBody)
  762. err = JSON.Bind(req, &obj)
  763. assert.Error(t, err)
  764. case "Uint64":
  765. obj := FooBarStructForUint64Type{}
  766. err := b.Bind(req, &obj)
  767. assert.NoError(t, err)
  768. assert.Equal(t, uint64(0x0), obj.Uint64Foo)
  769. assert.Equal(t, uint64(0xc), obj.Uint64Bar)
  770. obj = FooBarStructForUint64Type{}
  771. req = requestWithBody(method, badPath, badBody)
  772. err = JSON.Bind(req, &obj)
  773. assert.Error(t, err)
  774. case "Float32":
  775. obj := FooBarStructForFloat32Type{}
  776. err := b.Bind(req, &obj)
  777. assert.NoError(t, err)
  778. assert.Equal(t, float32(0.0), obj.Float32Foo)
  779. assert.Equal(t, float32(-12.34), obj.Float32Bar)
  780. obj = FooBarStructForFloat32Type{}
  781. req = requestWithBody(method, badPath, badBody)
  782. err = JSON.Bind(req, &obj)
  783. assert.Error(t, err)
  784. case "Float64":
  785. obj := FooBarStructForFloat64Type{}
  786. err := b.Bind(req, &obj)
  787. assert.NoError(t, err)
  788. assert.Equal(t, float64(0.0), obj.Float64Foo)
  789. assert.Equal(t, float64(-12.34), obj.Float64Bar)
  790. obj = FooBarStructForFloat64Type{}
  791. req = requestWithBody(method, badPath, badBody)
  792. err = JSON.Bind(req, &obj)
  793. assert.Error(t, err)
  794. case "Bool":
  795. obj := FooBarStructForBoolType{}
  796. err := b.Bind(req, &obj)
  797. assert.NoError(t, err)
  798. assert.False(t, obj.BoolFoo)
  799. assert.True(t, obj.BoolBar)
  800. obj = FooBarStructForBoolType{}
  801. req = requestWithBody(method, badPath, badBody)
  802. err = JSON.Bind(req, &obj)
  803. assert.Error(t, err)
  804. case "Slice":
  805. obj := FooStructForSliceType{}
  806. err := b.Bind(req, &obj)
  807. assert.NoError(t, err)
  808. assert.Equal(t, []int{1, 2}, obj.SliceFoo)
  809. obj = FooStructForSliceType{}
  810. req = requestWithBody(method, badPath, badBody)
  811. err = JSON.Bind(req, &obj)
  812. assert.Error(t, err)
  813. case "Struct":
  814. obj := FooStructForStructType{}
  815. err := b.Bind(req, &obj)
  816. assert.NoError(t, err)
  817. assert.Equal(t,
  818. struct {
  819. Idx int "form:\"idx\""
  820. }(struct {
  821. Idx int "form:\"idx\""
  822. }{Idx: 123}),
  823. obj.StructFoo)
  824. case "StructPointer":
  825. obj := FooStructForStructPointerType{}
  826. err := b.Bind(req, &obj)
  827. assert.NoError(t, err)
  828. assert.Equal(t,
  829. struct {
  830. Name string "form:\"name\""
  831. }(struct {
  832. Name string "form:\"name\""
  833. }{Name: "thinkerou"}),
  834. *obj.StructPointerFoo)
  835. case "Map":
  836. obj := FooStructForMapType{}
  837. err := b.Bind(req, &obj)
  838. assert.Error(t, err)
  839. case "SliceMap":
  840. obj := FooStructForSliceMapType{}
  841. err := b.Bind(req, &obj)
  842. assert.Error(t, err)
  843. case "Ptr":
  844. obj := FooStructForStringPtrType{}
  845. err := b.Bind(req, &obj)
  846. assert.NoError(t, err)
  847. assert.Nil(t, obj.PtrFoo)
  848. assert.Equal(t, "test", *obj.PtrBar)
  849. obj = FooStructForStringPtrType{}
  850. obj.PtrBar = new(string)
  851. err = b.Bind(req, &obj)
  852. assert.NoError(t, err)
  853. assert.Equal(t, "test", *obj.PtrBar)
  854. objErr := FooStructForMapPtrType{}
  855. err = b.Bind(req, &objErr)
  856. assert.Error(t, err)
  857. obj = FooStructForStringPtrType{}
  858. req = requestWithBody(method, badPath, badBody)
  859. err = b.Bind(req, &obj)
  860. assert.Error(t, err)
  861. }
  862. }
  863. func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {
  864. b := Query
  865. assert.Equal(t, "query", b.Name())
  866. obj := FooBarStruct{}
  867. req := requestWithBody(method, path, body)
  868. if method == "POST" {
  869. req.Header.Add("Content-Type", MIMEPOSTForm)
  870. }
  871. err := b.Bind(req, &obj)
  872. assert.NoError(t, err)
  873. assert.Equal(t, "bar", obj.Foo)
  874. assert.Equal(t, "foo", obj.Bar)
  875. }
  876. func testQueryBindingFail(t *testing.T, method, path, badPath, body, badBody string) {
  877. b := Query
  878. assert.Equal(t, "query", b.Name())
  879. obj := FooStructForMapType{}
  880. req := requestWithBody(method, path, body)
  881. if method == "POST" {
  882. req.Header.Add("Content-Type", MIMEPOSTForm)
  883. }
  884. err := b.Bind(req, &obj)
  885. assert.Error(t, err)
  886. }
  887. func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  888. assert.Equal(t, name, b.Name())
  889. obj := FooStruct{}
  890. req := requestWithBody("POST", path, body)
  891. err := b.Bind(req, &obj)
  892. assert.NoError(t, err)
  893. assert.Equal(t, "bar", obj.Foo)
  894. obj = FooStruct{}
  895. req = requestWithBody("POST", badPath, badBody)
  896. err = JSON.Bind(req, &obj)
  897. assert.Error(t, err)
  898. }
  899. func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  900. assert.Equal(t, name, b.Name())
  901. obj := FooStructUseNumber{}
  902. req := requestWithBody("POST", path, body)
  903. EnableDecoderUseNumber = true
  904. err := b.Bind(req, &obj)
  905. assert.NoError(t, err)
  906. // we hope it is int64(123)
  907. v, e := obj.Foo.(json.Number).Int64()
  908. assert.NoError(t, e)
  909. assert.Equal(t, int64(123), v)
  910. obj = FooStructUseNumber{}
  911. req = requestWithBody("POST", badPath, badBody)
  912. err = JSON.Bind(req, &obj)
  913. assert.Error(t, err)
  914. }
  915. func testBodyBindingUseNumber2(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  916. assert.Equal(t, name, b.Name())
  917. obj := FooStructUseNumber{}
  918. req := requestWithBody("POST", path, body)
  919. EnableDecoderUseNumber = false
  920. err := b.Bind(req, &obj)
  921. assert.NoError(t, err)
  922. // it will return float64(123) if not use EnableDecoderUseNumber
  923. // maybe it is not hoped
  924. assert.Equal(t, float64(123), obj.Foo)
  925. obj = FooStructUseNumber{}
  926. req = requestWithBody("POST", badPath, badBody)
  927. err = JSON.Bind(req, &obj)
  928. assert.Error(t, err)
  929. }
  930. func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  931. assert.Equal(t, name, b.Name())
  932. obj := FooStruct{}
  933. req := requestWithBody("POST", path, body)
  934. err := b.Bind(req, &obj)
  935. assert.Error(t, err)
  936. assert.Equal(t, "", obj.Foo)
  937. obj = FooStruct{}
  938. req = requestWithBody("POST", badPath, badBody)
  939. err = JSON.Bind(req, &obj)
  940. assert.Error(t, err)
  941. }
  942. func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  943. assert.Equal(t, name, b.Name())
  944. obj := example.Test{}
  945. req := requestWithBody("POST", path, body)
  946. req.Header.Add("Content-Type", MIMEPROTOBUF)
  947. err := b.Bind(req, &obj)
  948. assert.NoError(t, err)
  949. assert.Equal(t, "yes", *obj.Label)
  950. obj = example.Test{}
  951. req = requestWithBody("POST", badPath, badBody)
  952. req.Header.Add("Content-Type", MIMEPROTOBUF)
  953. err = ProtoBuf.Bind(req, &obj)
  954. assert.Error(t, err)
  955. }
  956. type hook struct{}
  957. func (h hook) Read([]byte) (int, error) {
  958. return 0, errors.New("error")
  959. }
  960. func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  961. assert.Equal(t, name, b.Name())
  962. obj := example.Test{}
  963. req := requestWithBody("POST", path, body)
  964. req.Body = ioutil.NopCloser(&hook{})
  965. req.Header.Add("Content-Type", MIMEPROTOBUF)
  966. err := b.Bind(req, &obj)
  967. assert.Error(t, err)
  968. obj = example.Test{}
  969. req = requestWithBody("POST", badPath, badBody)
  970. req.Header.Add("Content-Type", MIMEPROTOBUF)
  971. err = ProtoBuf.Bind(req, &obj)
  972. assert.Error(t, err)
  973. }
  974. func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  975. assert.Equal(t, name, b.Name())
  976. obj := FooStruct{}
  977. req := requestWithBody("POST", path, body)
  978. req.Header.Add("Content-Type", MIMEMSGPACK)
  979. err := b.Bind(req, &obj)
  980. assert.NoError(t, err)
  981. assert.Equal(t, "bar", obj.Foo)
  982. obj = FooStruct{}
  983. req = requestWithBody("POST", badPath, badBody)
  984. req.Header.Add("Content-Type", MIMEMSGPACK)
  985. err = MsgPack.Bind(req, &obj)
  986. assert.Error(t, err)
  987. }
  988. func requestWithBody(method, path, body string) (req *http.Request) {
  989. req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
  990. return
  991. }