binding_test.go 32 KB

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