binding_test.go 31 KB

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