binding_test.go 32 KB

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