binding_test.go 32 KB

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