utils_test.go 3.3 KB

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