binding_test.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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 testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
  536. b := Form
  537. assert.Equal(t, "form", b.Name())
  538. obj := FooBarStruct{}
  539. req := requestWithBody(method, path, body)
  540. if method == "POST" {
  541. req.Header.Add("Content-Type", MIMEPOSTForm)
  542. }
  543. err := b.Bind(req, &obj)
  544. assert.NoError(t, err)
  545. assert.Equal(t, "bar", obj.Foo)
  546. assert.Equal(t, "foo", obj.Bar)
  547. obj = FooBarStruct{}
  548. req = requestWithBody(method, badPath, badBody)
  549. err = JSON.Bind(req, &obj)
  550. assert.Error(t, err)
  551. }
  552. func testFormBindingDefaultValue(t *testing.T, method, path, badPath, body, badBody string) {
  553. b := Form
  554. assert.Equal(t, "form", b.Name())
  555. obj := FooDefaultBarStruct{}
  556. req := requestWithBody(method, path, body)
  557. if method == "POST" {
  558. req.Header.Add("Content-Type", MIMEPOSTForm)
  559. }
  560. err := b.Bind(req, &obj)
  561. assert.NoError(t, err)
  562. assert.Equal(t, "bar", obj.Foo)
  563. assert.Equal(t, "hello", obj.Bar)
  564. obj = FooDefaultBarStruct{}
  565. req = requestWithBody(method, badPath, badBody)
  566. err = JSON.Bind(req, &obj)
  567. assert.Error(t, err)
  568. }
  569. func TestFormBindingFail(t *testing.T) {
  570. b := Form
  571. assert.Equal(t, "form", b.Name())
  572. obj := FooBarStruct{}
  573. req, _ := http.NewRequest("POST", "/", nil)
  574. err := b.Bind(req, &obj)
  575. assert.Error(t, err)
  576. }
  577. func TestFormPostBindingFail(t *testing.T) {
  578. b := FormPost
  579. assert.Equal(t, "form-urlencoded", b.Name())
  580. obj := FooBarStruct{}
  581. req, _ := http.NewRequest("POST", "/", nil)
  582. err := b.Bind(req, &obj)
  583. assert.Error(t, err)
  584. }
  585. func TestFormMultipartBindingFail(t *testing.T) {
  586. b := FormMultipart
  587. assert.Equal(t, "multipart/form-data", b.Name())
  588. obj := FooBarStruct{}
  589. req, _ := http.NewRequest("POST", "/", nil)
  590. err := b.Bind(req, &obj)
  591. assert.Error(t, err)
  592. }
  593. func testFormBindingForTime(t *testing.T, method, path, badPath, body, badBody string) {
  594. b := Form
  595. assert.Equal(t, "form", b.Name())
  596. obj := FooBarStructForTimeType{}
  597. req := requestWithBody(method, path, body)
  598. if method == "POST" {
  599. req.Header.Add("Content-Type", MIMEPOSTForm)
  600. }
  601. err := b.Bind(req, &obj)
  602. assert.NoError(t, err)
  603. assert.Equal(t, int64(1510675200), obj.TimeFoo.Unix())
  604. assert.Equal(t, "Asia/Chongqing", obj.TimeFoo.Location().String())
  605. assert.Equal(t, int64(-62135596800), obj.TimeBar.Unix())
  606. assert.Equal(t, "UTC", obj.TimeBar.Location().String())
  607. obj = FooBarStructForTimeType{}
  608. req = requestWithBody(method, badPath, badBody)
  609. err = JSON.Bind(req, &obj)
  610. assert.Error(t, err)
  611. }
  612. func testFormBindingForTimeNotFormat(t *testing.T, method, path, badPath, body, badBody string) {
  613. b := Form
  614. assert.Equal(t, "form", b.Name())
  615. obj := FooStructForTimeTypeNotFormat{}
  616. req := requestWithBody(method, path, body)
  617. if method == "POST" {
  618. req.Header.Add("Content-Type", MIMEPOSTForm)
  619. }
  620. err := b.Bind(req, &obj)
  621. assert.Error(t, err)
  622. obj = FooStructForTimeTypeNotFormat{}
  623. req = requestWithBody(method, badPath, badBody)
  624. err = JSON.Bind(req, &obj)
  625. assert.Error(t, err)
  626. }
  627. func testFormBindingForTimeFailFormat(t *testing.T, method, path, badPath, body, badBody string) {
  628. b := Form
  629. assert.Equal(t, "form", b.Name())
  630. obj := FooStructForTimeTypeFailFormat{}
  631. req := requestWithBody(method, path, body)
  632. if method == "POST" {
  633. req.Header.Add("Content-Type", MIMEPOSTForm)
  634. }
  635. err := b.Bind(req, &obj)
  636. assert.Error(t, err)
  637. obj = FooStructForTimeTypeFailFormat{}
  638. req = requestWithBody(method, badPath, badBody)
  639. err = JSON.Bind(req, &obj)
  640. assert.Error(t, err)
  641. }
  642. func testFormBindingForTimeFailLocation(t *testing.T, method, path, badPath, body, badBody string) {
  643. b := Form
  644. assert.Equal(t, "form", b.Name())
  645. obj := FooStructForTimeTypeFailLocation{}
  646. req := requestWithBody(method, path, body)
  647. if method == "POST" {
  648. req.Header.Add("Content-Type", MIMEPOSTForm)
  649. }
  650. err := b.Bind(req, &obj)
  651. assert.Error(t, err)
  652. obj = FooStructForTimeTypeFailLocation{}
  653. req = requestWithBody(method, badPath, badBody)
  654. err = JSON.Bind(req, &obj)
  655. assert.Error(t, err)
  656. }
  657. func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBody string) {
  658. b := Form
  659. assert.Equal(t, "form", b.Name())
  660. obj := InvalidNameType{}
  661. req := requestWithBody(method, path, body)
  662. if method == "POST" {
  663. req.Header.Add("Content-Type", MIMEPOSTForm)
  664. }
  665. err := b.Bind(req, &obj)
  666. assert.NoError(t, err)
  667. assert.Equal(t, "", obj.TestName)
  668. obj = InvalidNameType{}
  669. req = requestWithBody(method, badPath, badBody)
  670. err = JSON.Bind(req, &obj)
  671. assert.Error(t, err)
  672. }
  673. func testFormBindingInvalidName2(t *testing.T, method, path, badPath, body, badBody string) {
  674. b := Form
  675. assert.Equal(t, "form", b.Name())
  676. obj := InvalidNameMapType{}
  677. req := requestWithBody(method, path, body)
  678. if method == "POST" {
  679. req.Header.Add("Content-Type", MIMEPOSTForm)
  680. }
  681. err := b.Bind(req, &obj)
  682. assert.Error(t, err)
  683. obj = InvalidNameMapType{}
  684. req = requestWithBody(method, badPath, badBody)
  685. err = JSON.Bind(req, &obj)
  686. assert.Error(t, err)
  687. }
  688. func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody string, typ string) {
  689. b := Form
  690. assert.Equal(t, "form", b.Name())
  691. req := requestWithBody(method, path, body)
  692. if method == "POST" {
  693. req.Header.Add("Content-Type", MIMEPOSTForm)
  694. }
  695. switch typ {
  696. case "Int":
  697. obj := FooBarStructForIntType{}
  698. err := b.Bind(req, &obj)
  699. assert.NoError(t, err)
  700. assert.Equal(t, int(0), obj.IntFoo)
  701. assert.Equal(t, int(-12), obj.IntBar)
  702. obj = FooBarStructForIntType{}
  703. req = requestWithBody(method, badPath, badBody)
  704. err = JSON.Bind(req, &obj)
  705. assert.Error(t, err)
  706. case "Int8":
  707. obj := FooBarStructForInt8Type{}
  708. err := b.Bind(req, &obj)
  709. assert.NoError(t, err)
  710. assert.Equal(t, int8(0), obj.Int8Foo)
  711. assert.Equal(t, int8(-12), obj.Int8Bar)
  712. obj = FooBarStructForInt8Type{}
  713. req = requestWithBody(method, badPath, badBody)
  714. err = JSON.Bind(req, &obj)
  715. assert.Error(t, err)
  716. case "Int16":
  717. obj := FooBarStructForInt16Type{}
  718. err := b.Bind(req, &obj)
  719. assert.NoError(t, err)
  720. assert.Equal(t, int16(0), obj.Int16Foo)
  721. assert.Equal(t, int16(-12), obj.Int16Bar)
  722. obj = FooBarStructForInt16Type{}
  723. req = requestWithBody(method, badPath, badBody)
  724. err = JSON.Bind(req, &obj)
  725. assert.Error(t, err)
  726. case "Int32":
  727. obj := FooBarStructForInt32Type{}
  728. err := b.Bind(req, &obj)
  729. assert.NoError(t, err)
  730. assert.Equal(t, int32(0), obj.Int32Foo)
  731. assert.Equal(t, int32(-12), obj.Int32Bar)
  732. obj = FooBarStructForInt32Type{}
  733. req = requestWithBody(method, badPath, badBody)
  734. err = JSON.Bind(req, &obj)
  735. assert.Error(t, err)
  736. case "Int64":
  737. obj := FooBarStructForInt64Type{}
  738. err := b.Bind(req, &obj)
  739. assert.NoError(t, err)
  740. assert.Equal(t, int64(0), obj.Int64Foo)
  741. assert.Equal(t, int64(-12), obj.Int64Bar)
  742. obj = FooBarStructForInt64Type{}
  743. req = requestWithBody(method, badPath, badBody)
  744. err = JSON.Bind(req, &obj)
  745. assert.Error(t, err)
  746. case "Uint":
  747. obj := FooBarStructForUintType{}
  748. err := b.Bind(req, &obj)
  749. assert.NoError(t, err)
  750. assert.Equal(t, uint(0x0), obj.UintFoo)
  751. assert.Equal(t, uint(0xc), obj.UintBar)
  752. obj = FooBarStructForUintType{}
  753. req = requestWithBody(method, badPath, badBody)
  754. err = JSON.Bind(req, &obj)
  755. assert.Error(t, err)
  756. case "Uint8":
  757. obj := FooBarStructForUint8Type{}
  758. err := b.Bind(req, &obj)
  759. assert.NoError(t, err)
  760. assert.Equal(t, uint8(0x0), obj.Uint8Foo)
  761. assert.Equal(t, uint8(0xc), obj.Uint8Bar)
  762. obj = FooBarStructForUint8Type{}
  763. req = requestWithBody(method, badPath, badBody)
  764. err = JSON.Bind(req, &obj)
  765. assert.Error(t, err)
  766. case "Uint16":
  767. obj := FooBarStructForUint16Type{}
  768. err := b.Bind(req, &obj)
  769. assert.NoError(t, err)
  770. assert.Equal(t, uint16(0x0), obj.Uint16Foo)
  771. assert.Equal(t, uint16(0xc), obj.Uint16Bar)
  772. obj = FooBarStructForUint16Type{}
  773. req = requestWithBody(method, badPath, badBody)
  774. err = JSON.Bind(req, &obj)
  775. assert.Error(t, err)
  776. case "Uint32":
  777. obj := FooBarStructForUint32Type{}
  778. err := b.Bind(req, &obj)
  779. assert.NoError(t, err)
  780. assert.Equal(t, uint32(0x0), obj.Uint32Foo)
  781. assert.Equal(t, uint32(0xc), obj.Uint32Bar)
  782. obj = FooBarStructForUint32Type{}
  783. req = requestWithBody(method, badPath, badBody)
  784. err = JSON.Bind(req, &obj)
  785. assert.Error(t, err)
  786. case "Uint64":
  787. obj := FooBarStructForUint64Type{}
  788. err := b.Bind(req, &obj)
  789. assert.NoError(t, err)
  790. assert.Equal(t, uint64(0x0), obj.Uint64Foo)
  791. assert.Equal(t, uint64(0xc), obj.Uint64Bar)
  792. obj = FooBarStructForUint64Type{}
  793. req = requestWithBody(method, badPath, badBody)
  794. err = JSON.Bind(req, &obj)
  795. assert.Error(t, err)
  796. case "Float32":
  797. obj := FooBarStructForFloat32Type{}
  798. err := b.Bind(req, &obj)
  799. assert.NoError(t, err)
  800. assert.Equal(t, float32(0.0), obj.Float32Foo)
  801. assert.Equal(t, float32(-12.34), obj.Float32Bar)
  802. obj = FooBarStructForFloat32Type{}
  803. req = requestWithBody(method, badPath, badBody)
  804. err = JSON.Bind(req, &obj)
  805. assert.Error(t, err)
  806. case "Float64":
  807. obj := FooBarStructForFloat64Type{}
  808. err := b.Bind(req, &obj)
  809. assert.NoError(t, err)
  810. assert.Equal(t, float64(0.0), obj.Float64Foo)
  811. assert.Equal(t, float64(-12.34), obj.Float64Bar)
  812. obj = FooBarStructForFloat64Type{}
  813. req = requestWithBody(method, badPath, badBody)
  814. err = JSON.Bind(req, &obj)
  815. assert.Error(t, err)
  816. case "Bool":
  817. obj := FooBarStructForBoolType{}
  818. err := b.Bind(req, &obj)
  819. assert.NoError(t, err)
  820. assert.False(t, obj.BoolFoo)
  821. assert.True(t, obj.BoolBar)
  822. obj = FooBarStructForBoolType{}
  823. req = requestWithBody(method, badPath, badBody)
  824. err = JSON.Bind(req, &obj)
  825. assert.Error(t, err)
  826. case "Slice":
  827. obj := FooStructForSliceType{}
  828. err := b.Bind(req, &obj)
  829. assert.NoError(t, err)
  830. assert.Equal(t, []int{1, 2}, obj.SliceFoo)
  831. obj = FooStructForSliceType{}
  832. req = requestWithBody(method, badPath, badBody)
  833. err = JSON.Bind(req, &obj)
  834. assert.Error(t, err)
  835. case "Struct":
  836. obj := FooStructForStructType{}
  837. err := b.Bind(req, &obj)
  838. assert.NoError(t, err)
  839. assert.Equal(t,
  840. struct {
  841. Idx int "form:\"idx\""
  842. }(struct {
  843. Idx int "form:\"idx\""
  844. }{Idx: 123}),
  845. obj.StructFoo)
  846. case "StructPointer":
  847. obj := FooStructForStructPointerType{}
  848. err := b.Bind(req, &obj)
  849. assert.NoError(t, err)
  850. assert.Equal(t,
  851. struct {
  852. Name string "form:\"name\""
  853. }(struct {
  854. Name string "form:\"name\""
  855. }{Name: "thinkerou"}),
  856. *obj.StructPointerFoo)
  857. case "Map":
  858. obj := FooStructForMapType{}
  859. err := b.Bind(req, &obj)
  860. assert.Error(t, err)
  861. case "SliceMap":
  862. obj := FooStructForSliceMapType{}
  863. err := b.Bind(req, &obj)
  864. assert.Error(t, err)
  865. case "Ptr":
  866. obj := FooStructForStringPtrType{}
  867. err := b.Bind(req, &obj)
  868. assert.NoError(t, err)
  869. assert.Nil(t, obj.PtrFoo)
  870. assert.Equal(t, "test", *obj.PtrBar)
  871. obj = FooStructForStringPtrType{}
  872. obj.PtrBar = new(string)
  873. err = b.Bind(req, &obj)
  874. assert.NoError(t, err)
  875. assert.Equal(t, "test", *obj.PtrBar)
  876. objErr := FooStructForMapPtrType{}
  877. err = b.Bind(req, &objErr)
  878. assert.Error(t, err)
  879. obj = FooStructForStringPtrType{}
  880. req = requestWithBody(method, badPath, badBody)
  881. err = b.Bind(req, &obj)
  882. assert.Error(t, err)
  883. }
  884. }
  885. func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {
  886. b := Query
  887. assert.Equal(t, "query", b.Name())
  888. obj := FooBarStruct{}
  889. req := requestWithBody(method, path, body)
  890. if method == "POST" {
  891. req.Header.Add("Content-Type", MIMEPOSTForm)
  892. }
  893. err := b.Bind(req, &obj)
  894. assert.NoError(t, err)
  895. assert.Equal(t, "bar", obj.Foo)
  896. assert.Equal(t, "foo", obj.Bar)
  897. }
  898. func testQueryBindingFail(t *testing.T, method, path, badPath, body, badBody string) {
  899. b := Query
  900. assert.Equal(t, "query", b.Name())
  901. obj := FooStructForMapType{}
  902. req := requestWithBody(method, path, body)
  903. if method == "POST" {
  904. req.Header.Add("Content-Type", MIMEPOSTForm)
  905. }
  906. err := b.Bind(req, &obj)
  907. assert.Error(t, err)
  908. }
  909. func testQueryBindingBoolFail(t *testing.T, method, path, badPath, body, badBody string) {
  910. b := Query
  911. assert.Equal(t, "query", b.Name())
  912. obj := FooStructForBoolType{}
  913. req := requestWithBody(method, path, body)
  914. if method == "POST" {
  915. req.Header.Add("Content-Type", MIMEPOSTForm)
  916. }
  917. err := b.Bind(req, &obj)
  918. assert.Error(t, err)
  919. }
  920. func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  921. assert.Equal(t, name, b.Name())
  922. obj := FooStruct{}
  923. req := requestWithBody("POST", path, body)
  924. err := b.Bind(req, &obj)
  925. assert.NoError(t, err)
  926. assert.Equal(t, "bar", obj.Foo)
  927. obj = FooStruct{}
  928. req = requestWithBody("POST", badPath, badBody)
  929. err = JSON.Bind(req, &obj)
  930. assert.Error(t, err)
  931. }
  932. func testBodyBindingUseNumber(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  933. assert.Equal(t, name, b.Name())
  934. obj := FooStructUseNumber{}
  935. req := requestWithBody("POST", path, body)
  936. EnableDecoderUseNumber = true
  937. err := b.Bind(req, &obj)
  938. assert.NoError(t, err)
  939. // we hope it is int64(123)
  940. v, e := obj.Foo.(json.Number).Int64()
  941. assert.NoError(t, e)
  942. assert.Equal(t, int64(123), v)
  943. obj = FooStructUseNumber{}
  944. req = requestWithBody("POST", badPath, badBody)
  945. err = JSON.Bind(req, &obj)
  946. assert.Error(t, err)
  947. }
  948. func testBodyBindingUseNumber2(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  949. assert.Equal(t, name, b.Name())
  950. obj := FooStructUseNumber{}
  951. req := requestWithBody("POST", path, body)
  952. EnableDecoderUseNumber = false
  953. err := b.Bind(req, &obj)
  954. assert.NoError(t, err)
  955. // it will return float64(123) if not use EnableDecoderUseNumber
  956. // maybe it is not hoped
  957. assert.Equal(t, float64(123), obj.Foo)
  958. obj = FooStructUseNumber{}
  959. req = requestWithBody("POST", badPath, badBody)
  960. err = JSON.Bind(req, &obj)
  961. assert.Error(t, err)
  962. }
  963. func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  964. assert.Equal(t, name, b.Name())
  965. obj := FooStruct{}
  966. req := requestWithBody("POST", path, body)
  967. err := b.Bind(req, &obj)
  968. assert.Error(t, err)
  969. assert.Equal(t, "", obj.Foo)
  970. obj = FooStruct{}
  971. req = requestWithBody("POST", badPath, badBody)
  972. err = JSON.Bind(req, &obj)
  973. assert.Error(t, err)
  974. }
  975. func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  976. assert.Equal(t, name, b.Name())
  977. obj := protoexample.Test{}
  978. req := requestWithBody("POST", path, body)
  979. req.Header.Add("Content-Type", MIMEPROTOBUF)
  980. err := b.Bind(req, &obj)
  981. assert.NoError(t, err)
  982. assert.Equal(t, "yes", *obj.Label)
  983. obj = protoexample.Test{}
  984. req = requestWithBody("POST", badPath, badBody)
  985. req.Header.Add("Content-Type", MIMEPROTOBUF)
  986. err = ProtoBuf.Bind(req, &obj)
  987. assert.Error(t, err)
  988. }
  989. type hook struct{}
  990. func (h hook) Read([]byte) (int, error) {
  991. return 0, errors.New("error")
  992. }
  993. func testProtoBodyBindingFail(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.Body = ioutil.NopCloser(&hook{})
  998. req.Header.Add("Content-Type", MIMEPROTOBUF)
  999. err := b.Bind(req, &obj)
  1000. assert.Error(t, err)
  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. func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  1008. assert.Equal(t, name, b.Name())
  1009. obj := FooStruct{}
  1010. req := requestWithBody("POST", path, body)
  1011. req.Header.Add("Content-Type", MIMEMSGPACK)
  1012. err := b.Bind(req, &obj)
  1013. assert.NoError(t, err)
  1014. assert.Equal(t, "bar", obj.Foo)
  1015. obj = FooStruct{}
  1016. req = requestWithBody("POST", badPath, badBody)
  1017. req.Header.Add("Content-Type", MIMEMSGPACK)
  1018. err = MsgPack.Bind(req, &obj)
  1019. assert.Error(t, err)
  1020. }
  1021. func requestWithBody(method, path, body string) (req *http.Request) {
  1022. req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
  1023. return
  1024. }