binding_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "net/http"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type FooStruct struct {
  12. Foo string `json:"foo" form:"foo" xml:"foo" binding:"required"`
  13. }
  14. type FooBarStruct struct {
  15. FooStruct
  16. Bar string `json:"bar" form:"bar" xml:"bar" binding:"required"`
  17. }
  18. func TestBindingDefault(t *testing.T) {
  19. assert.Equal(t, Default("GET", ""), Form)
  20. assert.Equal(t, Default("GET", MIMEJSON), Form)
  21. assert.Equal(t, Default("POST", MIMEJSON), JSON)
  22. assert.Equal(t, Default("PUT", MIMEJSON), JSON)
  23. assert.Equal(t, Default("POST", MIMEXML), XML)
  24. assert.Equal(t, Default("PUT", MIMEXML2), XML)
  25. assert.Equal(t, Default("POST", MIMEPOSTForm), Form)
  26. assert.Equal(t, Default("DELETE", MIMEPOSTForm), Form)
  27. }
  28. func TestBindingJSON(t *testing.T) {
  29. testBodyBinding(t,
  30. JSON, "json",
  31. "/", "/",
  32. `{"foo": "bar"}`, `{"bar": "foo"}`)
  33. }
  34. func TestBindingForm(t *testing.T) {
  35. testFormBinding(t, "POST",
  36. "/", "/",
  37. "foo=bar&bar=foo", "bar2=foo")
  38. }
  39. func TestBindingForm2(t *testing.T) {
  40. testFormBinding(t, "GET",
  41. "/?foo=bar&bar=foo", "/?bar2=foo",
  42. "", "")
  43. }
  44. func TestBindingXML(t *testing.T) {
  45. testBodyBinding(t,
  46. XML, "xml",
  47. "/", "/",
  48. "<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
  49. }
  50. func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
  51. b := Form
  52. assert.Equal(t, b.Name(), "query")
  53. obj := FooBarStruct{}
  54. req := requestWithBody(method, path, body)
  55. if method == "POST" {
  56. req.Header.Add("Content-Type", MIMEPOSTForm)
  57. }
  58. err := b.Bind(req, &obj)
  59. assert.NoError(t, err)
  60. assert.Equal(t, obj.Foo, "bar")
  61. assert.Equal(t, obj.Bar, "foo")
  62. obj = FooBarStruct{}
  63. req = requestWithBody(method, badPath, badBody)
  64. err = JSON.Bind(req, &obj)
  65. assert.Error(t, err)
  66. }
  67. func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
  68. assert.Equal(t, b.Name(), name)
  69. obj := FooStruct{}
  70. req := requestWithBody("POST", path, body)
  71. err := b.Bind(req, &obj)
  72. assert.NoError(t, err)
  73. assert.Equal(t, obj.Foo, "bar")
  74. obj = FooStruct{}
  75. req = requestWithBody("POST", badPath, badBody)
  76. err = JSON.Bind(req, &obj)
  77. assert.Error(t, err)
  78. }
  79. func requestWithBody(method, path, body string) (req *http.Request) {
  80. req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
  81. return
  82. }