routes_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "io/ioutil"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "path"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
  15. req, _ := http.NewRequest(method, path, nil)
  16. w := httptest.NewRecorder()
  17. r.ServeHTTP(w, req)
  18. return w
  19. }
  20. func testRouteOK(method string, t *testing.T) {
  21. // SETUP
  22. passed := false
  23. r := New()
  24. r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
  25. passed = true
  26. }})
  27. // RUN
  28. w := performRequest(r, method, "/test")
  29. // TEST
  30. assert.True(t, passed)
  31. assert.Equal(t, w.Code, http.StatusOK)
  32. }
  33. // TestSingleRouteOK tests that POST route is correctly invoked.
  34. func testRouteNotOK(method string, t *testing.T) {
  35. // SETUP
  36. passed := false
  37. router := New()
  38. router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
  39. passed = true
  40. }})
  41. // RUN
  42. w := performRequest(router, method, "/test")
  43. // TEST
  44. assert.False(t, passed)
  45. assert.Equal(t, w.Code, http.StatusNotFound)
  46. }
  47. // TestSingleRouteOK tests that POST route is correctly invoked.
  48. func testRouteNotOK2(method string, t *testing.T) {
  49. // SETUP
  50. passed := false
  51. router := New()
  52. var methodRoute string
  53. if method == "POST" {
  54. methodRoute = "GET"
  55. } else {
  56. methodRoute = "POST"
  57. }
  58. router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
  59. passed = true
  60. }})
  61. // RUN
  62. w := performRequest(router, method, "/test")
  63. // TEST
  64. assert.False(t, passed)
  65. assert.Equal(t, w.Code, http.StatusMethodNotAllowed)
  66. }
  67. func TestRouterGroupRouteOK(t *testing.T) {
  68. testRouteOK("POST", t)
  69. testRouteOK("DELETE", t)
  70. testRouteOK("PATCH", t)
  71. testRouteOK("PUT", t)
  72. testRouteOK("OPTIONS", t)
  73. testRouteOK("HEAD", t)
  74. }
  75. // TestSingleRouteOK tests that POST route is correctly invoked.
  76. func TestRouteNotOK(t *testing.T) {
  77. testRouteNotOK("POST", t)
  78. testRouteNotOK("DELETE", t)
  79. testRouteNotOK("PATCH", t)
  80. testRouteNotOK("PUT", t)
  81. testRouteNotOK("OPTIONS", t)
  82. testRouteNotOK("HEAD", t)
  83. }
  84. // TestSingleRouteOK tests that POST route is correctly invoked.
  85. func TestRouteNotOK2(t *testing.T) {
  86. testRouteNotOK2("POST", t)
  87. testRouteNotOK2("DELETE", t)
  88. testRouteNotOK2("PATCH", t)
  89. testRouteNotOK2("PUT", t)
  90. testRouteNotOK2("OPTIONS", t)
  91. testRouteNotOK2("HEAD", t)
  92. }
  93. // TestContextParamsGet tests that a parameter can be parsed from the URL.
  94. func TestRouteParamsByName(t *testing.T) {
  95. name := ""
  96. lastName := ""
  97. router := New()
  98. router.GET("/test/:name/:last_name", func(c *Context) {
  99. name = c.Params.ByName("name")
  100. lastName = c.Params.ByName("last_name")
  101. })
  102. // RUN
  103. w := performRequest(router, "GET", "/test/john/smith")
  104. // TEST
  105. assert.Equal(t, w.Code, 200)
  106. assert.Equal(t, name, "john")
  107. assert.Equal(t, lastName, "smith")
  108. }
  109. // TestHandleStaticFile - ensure the static file handles properly
  110. func TestRouteStaticFile(t *testing.T) {
  111. // SETUP file
  112. testRoot, _ := os.Getwd()
  113. f, err := ioutil.TempFile(testRoot, "")
  114. if err != nil {
  115. t.Error(err)
  116. }
  117. defer os.Remove(f.Name())
  118. filePath := path.Join("/", path.Base(f.Name()))
  119. f.WriteString("Gin Web Framework")
  120. f.Close()
  121. // SETUP gin
  122. r := New()
  123. r.Static("./", testRoot)
  124. // RUN
  125. w := performRequest(r, "GET", filePath)
  126. // TEST
  127. assert.Equal(t, w.Code, 200)
  128. assert.Equal(t, w.Body.String(), "Gin Web Framework")
  129. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
  130. }
  131. // TestHandleStaticDir - ensure the root/sub dir handles properly
  132. func TestRouteStaticDir(t *testing.T) {
  133. // SETUP
  134. r := New()
  135. r.Static("/", "./")
  136. // RUN
  137. w := performRequest(r, "GET", "/")
  138. // TEST
  139. bodyAsString := w.Body.String()
  140. assert.Equal(t, w.Code, 200)
  141. assert.NotEmpty(t, bodyAsString)
  142. assert.Contains(t, bodyAsString, "gin.go")
  143. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  144. }
  145. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  146. func TestRouteHeadToDir(t *testing.T) {
  147. // SETUP
  148. router := New()
  149. router.Static("/", "./")
  150. // RUN
  151. w := performRequest(router, "HEAD", "/")
  152. // TEST
  153. bodyAsString := w.Body.String()
  154. assert.Equal(t, w.Code, 200)
  155. assert.NotEmpty(t, bodyAsString)
  156. assert.Contains(t, bodyAsString, "gin.go")
  157. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  158. }