utils_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 gin
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "fmt"
  9. "net/http"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func init() {
  14. SetMode(TestMode)
  15. }
  16. type testStruct struct {
  17. T *testing.T
  18. }
  19. func (t *testStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  20. assert.Equal(t.T, "POST", req.Method)
  21. assert.Equal(t.T, "/path", req.URL.Path)
  22. w.WriteHeader(http.StatusInternalServerError)
  23. fmt.Fprint(w, "hello")
  24. }
  25. func TestWrap(t *testing.T) {
  26. router := New()
  27. router.POST("/path", WrapH(&testStruct{t}))
  28. router.GET("/path2", WrapF(func(w http.ResponseWriter, req *http.Request) {
  29. assert.Equal(t, "GET", req.Method)
  30. assert.Equal(t, "/path2", req.URL.Path)
  31. w.WriteHeader(http.StatusBadRequest)
  32. fmt.Fprint(w, "hola!")
  33. }))
  34. w := performRequest(router, "POST", "/path")
  35. assert.Equal(t, http.StatusInternalServerError, w.Code)
  36. assert.Equal(t, "hello", w.Body.String())
  37. w = performRequest(router, "GET", "/path2")
  38. assert.Equal(t, http.StatusBadRequest, w.Code)
  39. assert.Equal(t, "hola!", w.Body.String())
  40. }
  41. func TestLastChar(t *testing.T) {
  42. assert.Equal(t, uint8('a'), lastChar("hola"))
  43. assert.Equal(t, uint8('s'), lastChar("adios"))
  44. assert.Panics(t, func() { lastChar("") })
  45. }
  46. func TestParseAccept(t *testing.T) {
  47. parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
  48. assert.Len(t, parts, 4)
  49. assert.Equal(t, "text/html", parts[0])
  50. assert.Equal(t, "application/xhtml+xml", parts[1])
  51. assert.Equal(t, "application/xml", parts[2])
  52. assert.Equal(t, "*/*", parts[3])
  53. }
  54. func TestChooseData(t *testing.T) {
  55. A := "a"
  56. B := "b"
  57. assert.Equal(t, A, chooseData(A, B))
  58. assert.Equal(t, B, chooseData(nil, B))
  59. assert.Panics(t, func() { chooseData(nil, nil) })
  60. }
  61. func TestFilterFlags(t *testing.T) {
  62. result := filterFlags("text/html ")
  63. assert.Equal(t, "text/html", result)
  64. result = filterFlags("text/html;")
  65. assert.Equal(t, "text/html", result)
  66. }
  67. func TestFunctionName(t *testing.T) {
  68. assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))
  69. }
  70. func somefunction() {
  71. // this empty function is used by TestFunctionName()
  72. }
  73. func TestJoinPaths(t *testing.T) {
  74. assert.Equal(t, "", joinPaths("", ""))
  75. assert.Equal(t, "/", joinPaths("", "/"))
  76. assert.Equal(t, "/a", joinPaths("/a", ""))
  77. assert.Equal(t, "/a/", joinPaths("/a/", ""))
  78. assert.Equal(t, "/a/", joinPaths("/a/", "/"))
  79. assert.Equal(t, "/a/", joinPaths("/a", "/"))
  80. assert.Equal(t, "/a/hola", joinPaths("/a", "/hola"))
  81. assert.Equal(t, "/a/hola", joinPaths("/a/", "/hola"))
  82. assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola/"))
  83. assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola//"))
  84. }
  85. type bindTestStruct struct {
  86. Foo string `form:"foo" binding:"required"`
  87. Bar int `form:"bar" binding:"min=4"`
  88. }
  89. func TestBindMiddleware(t *testing.T) {
  90. var value *bindTestStruct
  91. var called bool
  92. router := New()
  93. router.GET("/", Bind(bindTestStruct{}), func(c *Context) {
  94. called = true
  95. value = c.MustGet(BindKey).(*bindTestStruct)
  96. })
  97. performRequest(router, "GET", "/?foo=hola&bar=10")
  98. assert.True(t, called)
  99. assert.Equal(t, "hola", value.Foo)
  100. assert.Equal(t, 10, value.Bar)
  101. called = false
  102. performRequest(router, "GET", "/?foo=hola&bar=1")
  103. assert.False(t, called)
  104. assert.Panics(t, func() {
  105. Bind(&bindTestStruct{})
  106. })
  107. }
  108. func TestMarshalXMLforH(t *testing.T) {
  109. h := H{
  110. "": "test",
  111. }
  112. var b bytes.Buffer
  113. enc := xml.NewEncoder(&b)
  114. var x xml.StartElement
  115. e := h.MarshalXML(enc, x)
  116. assert.Error(t, e)
  117. }