binding_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. "mime/multipart"
  8. "net/http"
  9. "testing"
  10. "github.com/gin-gonic/gin/binding/example"
  11. "github.com/golang/protobuf/proto"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/ugorji/go/codec"
  14. )
  15. type FooStruct struct {
  16. Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required"`
  17. }
  18. type FooBarStruct struct {
  19. FooStruct
  20. Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`
  21. }
  22. func TestBindingDefault(t *testing.T) {
  23. assert.Equal(t, Default("GET", ""), Form)
  24. assert.Equal(t, Default("GET", MIMEJSON), Form)
  25. assert.Equal(t, Default("POST", MIMEJSON), JSON)
  26. assert.Equal(t, Default("PUT", MIMEJSON), JSON)
  27. assert.Equal(t, Default("POST", MIMEXML), XML)
  28. assert.Equal(t, Default("PUT", MIMEXML2), XML)
  29. assert.Equal(t, Default("POST", MIMEPOSTForm), Form)
  30. assert.Equal(t, Default("PUT", MIMEPOSTForm), Form)
  31. assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), Form)
  32. assert.Equal(t, Default("PUT", MIMEMultipartPOSTForm), Form)
  33. assert.Equal(t, Default("POST", MIMEPROTOBUF), ProtoBuf)
  34. assert.Equal(t, Default("PUT", MIMEPROTOBUF), ProtoBuf)
  35. assert.Equal(t, Default("POST", MIMEMSGPACK), MsgPack)
  36. assert.Equal(t, Default("PUT", MIMEMSGPACK2), MsgPack)
  37. }
  38. func TestBindingJSON(t *testing.T) {
  39. testBodyBinding(t,
  40. JSON, "json",
  41. "/", "/",
  42. `{"foo": "bar"}`, `{"bar": "foo"}`)
  43. }
  44. func TestBindingForm(t *testing.T) {
  45. testFormBinding(t, "POST",
  46. "/", "/",
  47. "foo=bar&bar=foo", "bar2=foo")
  48. }
  49. func TestBindingForm2(t *testing.T) {
  50. testFormBinding(t, "GET",
  51. "/?foo=bar&bar=foo", "/?bar2=foo",
  52. "", "")
  53. }
  54. func TestBindingQuery(t *testing.T) {
  55. testQueryBinding(t, "POST",
  56. "/?foo=bar&bar=foo", "/",
  57. "foo=unused", "bar2=foo")
  58. }
  59. func TestBindingQuery2(t *testing.T) {
  60. testQueryBinding(t, "GET",
  61. "/?foo=bar&bar=foo", "/?bar2=foo",
  62. "foo=unused", "")
  63. }
  64. func TestBindingXML(t *testing.T) {
  65. testBodyBinding(t,
  66. XML, "xml",
  67. "/", "/",
  68. "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
  69. }
  70. func createFormPostRequest() *http.Request {
  71. req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar&bar=foo"))
  72. req.Header.Set("Content-Type", MIMEPOSTForm)
  73. return req
  74. }
  75. func createFormMultipartRequest() *http.Request {
  76. boundary := "--testboundary"
  77. body := new(bytes.Buffer)
  78. mw := multipart.NewWriter(body)
  79. defer mw.Close()
  80. mw.SetBoundary(boundary)
  81. mw.WriteField("foo", "bar")
  82. mw.WriteField("bar", "foo")
  83. req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
  84. req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
  85. return req
  86. }
  87. func TestBindingFormPost(t *testing.T) {
  88. req := createFormPostRequest()
  89. var obj FooBarStruct
  90. FormPost.Bind(req, &obj)
  91. assert.Equal(t, obj.Foo, "bar")
  92. assert.Equal(t, obj.Bar, "foo")
  93. }
  94. func TestBindingFormMultipart(t *testing.T) {
  95. req := createFormMultipartRequest()
  96. var obj FooBarStruct
  97. FormMultipart.Bind(req, &obj)
  98. assert.Equal(t, obj.Foo, "bar")
  99. assert.Equal(t, obj.Bar, "foo")
  100. }
  101. func TestBindingProtoBuf(t *testing.T) {
  102. test := &example.Test{
  103. Label: proto.String("yes"),
  104. }
  105. data, _ := proto.Marshal(test)
  106. testProtoBodyBinding(t,
  107. ProtoBuf, "protobuf",
  108. "/", "/",
  109. string(data), string(data[1:]))
  110. }
  111. func TestBindingMsgPack(t *testing.T) {
  112. test := FooStruct{
  113. Foo: "bar",
  114. }
  115. h := new(codec.MsgpackHandle)
  116. assert.NotNil(t, h)
  117. buf := bytes.NewBuffer([]byte{})
  118. assert.NotNil(t, buf)
  119. err := codec.NewEncoder(buf, h).Encode(test)
  120. assert.NoError(t, err)
  121. data := buf.Bytes()
  122. testMsgPackBodyBinding(t,
  123. MsgPack, "msgpack",
  124. "/", "/",
  125. string(data), string(data[1:]))
  126. }
  127. func TestValidationFails(t *testing.T) {
  128. var obj FooStruct
  129. req := requestWithBody("POST", "/", `{"bar": "foo"}`)
  130. err := JSON.Bind(req, &obj)
  131. assert.Error(t, err)
  132. }
  133. func TestValidationDisabled(t *testing.T) {
  134. backup := Validator
  135. Validator = nil
  136. defer func() { Validator = backup }()
  137. var obj FooStruct
  138. req := requestWithBody("POST", "/", `{"bar": "foo"}`)
  139. err := JSON.Bind(req, &obj)
  140. assert.NoError(t, err)
  141. }
  142. func TestExistsSucceeds(t *testing.T) {
  143. type HogeStruct struct {
  144. Hoge *int `json:"hoge" binding:"exists"`
  145. }
  146. var obj HogeStruct
  147. req := requestWithBody("POST", "/", `{"hoge": 0}`)
  148. err := JSON.Bind(req, &obj)
  149. assert.NoError(t, err)
  150. }
  151. func TestExistsFails(t *testing.T) {
  152. type HogeStruct struct {
  153. Hoge *int `json:"foo" binding:"exists"`
  154. }
  155. var obj HogeStruct
  156. req := requestWithBody("POST", "/", `{"boen": 0}`)
  157. err := JSON.Bind(req, &obj)
  158. assert.Error(t, err)
  159. }
  160. func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
  161. b := Form
  162. assert.Equal(t, b.Name(), "form")
  163. obj := FooBarStruct{}
  164. req := requestWithBody(method, path, body)
  165. if method == "POST" {
  166. req.Header.Add("Content-Type", MIMEPOSTForm)
  167. }
  168. err := b.Bind(req, &obj)
  169. assert.NoError(t, err)
  170. assert.Equal(t, obj.Foo, "bar")
  171. assert.Equal(t, obj.Bar, "foo")
  172. obj = FooBarStruct{}
  173. req = requestWithBody(method, badPath, badBody)
  174. err = JSON.Bind(req, &obj)
  175. assert.Error(t, err)
  176. }
  177. func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {
  178. b := Query
  179. assert.Equal(t, b.Name(), "query")
  180. obj := FooBarStruct{}
  181. req := requestWithBody(method, path, body)
  182. if method == "POST" {
  183. req.Header.Add("Content-Type", MIMEPOSTForm)
  184. }
  185. err := b.Bind(req, &obj)
  186. assert.NoError(t, err)
  187. assert.Equal(t, obj.Foo, "bar")
  188. assert.Equal(t, obj.Bar, "foo")
  189. }
  190. func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  191. assert.Equal(t, b.Name(), name)
  192. obj := FooStruct{}
  193. req := requestWithBody("POST", path, body)
  194. err := b.Bind(req, &obj)
  195. assert.NoError(t, err)
  196. assert.Equal(t, obj.Foo, "bar")
  197. obj = FooStruct{}
  198. req = requestWithBody("POST", badPath, badBody)
  199. err = JSON.Bind(req, &obj)
  200. assert.Error(t, err)
  201. }
  202. func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  203. assert.Equal(t, b.Name(), name)
  204. obj := example.Test{}
  205. req := requestWithBody("POST", path, body)
  206. req.Header.Add("Content-Type", MIMEPROTOBUF)
  207. err := b.Bind(req, &obj)
  208. assert.NoError(t, err)
  209. assert.Equal(t, *obj.Label, "yes")
  210. obj = example.Test{}
  211. req = requestWithBody("POST", badPath, badBody)
  212. req.Header.Add("Content-Type", MIMEPROTOBUF)
  213. err = ProtoBuf.Bind(req, &obj)
  214. assert.Error(t, err)
  215. }
  216. func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  217. assert.Equal(t, b.Name(), name)
  218. obj := FooStruct{}
  219. req := requestWithBody("POST", path, body)
  220. req.Header.Add("Content-Type", MIMEMSGPACK)
  221. err := b.Bind(req, &obj)
  222. assert.NoError(t, err)
  223. assert.Equal(t, obj.Foo, "bar")
  224. obj = FooStruct{}
  225. req = requestWithBody("POST", badPath, badBody)
  226. req.Header.Add("Content-Type", MIMEMSGPACK)
  227. err = MsgPack.Bind(req, &obj)
  228. assert.Error(t, err)
  229. }
  230. func requestWithBody(method, path, body string) (req *http.Request) {
  231. req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
  232. return
  233. }