utils_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 TestWrapH(t *testing.T) {
  40. }
  41. func TestLastChar(t *testing.T) {
  42. assert.Equal(t, lastChar("hola"), uint8('a'))
  43. assert.Equal(t, lastChar("adios"), uint8('s'))
  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, parts[0], "text/html")
  50. assert.Equal(t, parts[1], "application/xhtml+xml")
  51. assert.Equal(t, parts[2], "application/xml")
  52. assert.Equal(t, parts[3], "*/*")
  53. }
  54. func TestChooseData(t *testing.T) {
  55. A := "a"
  56. B := "b"
  57. assert.Equal(t, chooseData(A, B), A)
  58. assert.Equal(t, chooseData(nil, B), 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, result, "text/html")
  64. result = filterFlags("text/html;")
  65. assert.Equal(t, result, "text/html")
  66. }
  67. func TestFunctionName(t *testing.T) {
  68. assert.Equal(t, nameOfFunction(somefunction), "github.com/gin-gonic/gin.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, joinPaths("/a", ""), "/a")
  77. assert.Equal(t, joinPaths("/a/", ""), "/a/")
  78. assert.Equal(t, joinPaths("/a/", "/"), "/a/")
  79. assert.Equal(t, joinPaths("/a", "/"), "/a/")
  80. assert.Equal(t, joinPaths("/a", "/hola"), "/a/hola")
  81. assert.Equal(t, joinPaths("/a/", "/hola"), "/a/hola")
  82. assert.Equal(t, joinPaths("/a/", "/hola/"), "/a/hola/")
  83. assert.Equal(t, joinPaths("/a/", "/hola//"), "/a/hola/")
  84. }