gin_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "strings"
  12. "testing"
  13. )
  14. func init() {
  15. SetMode(TestMode)
  16. }
  17. func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
  18. req, _ := http.NewRequest(method, path, nil)
  19. w := httptest.NewRecorder()
  20. r.ServeHTTP(w, req)
  21. return w
  22. }
  23. // TestSingleRouteOK tests that POST route is correctly invoked.
  24. func testRouteOK(method string, t *testing.T) {
  25. // SETUP
  26. passed := false
  27. r := New()
  28. r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
  29. passed = true
  30. }})
  31. // RUN
  32. w := PerformRequest(r, method, "/test")
  33. // TEST
  34. if passed == false {
  35. t.Errorf(method + " route handler was not invoked.")
  36. }
  37. if w.Code != http.StatusOK {
  38. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  39. }
  40. }
  41. func TestRouterGroupRouteOK(t *testing.T) {
  42. testRouteOK("POST", t)
  43. testRouteOK("DELETE", t)
  44. testRouteOK("PATCH", t)
  45. testRouteOK("PUT", t)
  46. testRouteOK("OPTIONS", t)
  47. testRouteOK("HEAD", t)
  48. }
  49. // TestSingleRouteOK tests that POST route is correctly invoked.
  50. func testRouteNotOK(method string, t *testing.T) {
  51. // SETUP
  52. passed := false
  53. r := New()
  54. r.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
  55. passed = true
  56. }})
  57. // RUN
  58. w := PerformRequest(r, method, "/test")
  59. // TEST
  60. if passed == true {
  61. t.Errorf(method + " route handler was invoked, when it should not")
  62. }
  63. if w.Code != http.StatusNotFound {
  64. // If this fails, it's because httprouter needs to be updated to at least f78f58a0db
  65. t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
  66. }
  67. }
  68. // TestSingleRouteOK tests that POST route is correctly invoked.
  69. func TestRouteNotOK(t *testing.T) {
  70. testRouteNotOK("POST", t)
  71. testRouteNotOK("DELETE", t)
  72. testRouteNotOK("PATCH", t)
  73. testRouteNotOK("PUT", t)
  74. testRouteNotOK("OPTIONS", t)
  75. testRouteNotOK("HEAD", t)
  76. }
  77. // TestSingleRouteOK tests that POST route is correctly invoked.
  78. func testRouteNotOK2(method string, t *testing.T) {
  79. // SETUP
  80. passed := false
  81. r := New()
  82. var methodRoute string
  83. if method == "POST" {
  84. methodRoute = "GET"
  85. } else {
  86. methodRoute = "POST"
  87. }
  88. r.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
  89. passed = true
  90. }})
  91. // RUN
  92. w := PerformRequest(r, method, "/test")
  93. // TEST
  94. if passed == true {
  95. t.Errorf(method + " route handler was invoked, when it should not")
  96. }
  97. if w.Code != http.StatusNotFound {
  98. // If this fails, it's because httprouter needs to be updated to at least f78f58a0db
  99. t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
  100. }
  101. }
  102. // TestSingleRouteOK tests that POST route is correctly invoked.
  103. func TestRouteNotOK2(t *testing.T) {
  104. testRouteNotOK2("POST", t)
  105. testRouteNotOK2("DELETE", t)
  106. testRouteNotOK2("PATCH", t)
  107. testRouteNotOK2("PUT", t)
  108. testRouteNotOK2("OPTIONS", t)
  109. testRouteNotOK2("HEAD", t)
  110. }
  111. // TestHandleStaticFile - ensure the static file handles properly
  112. func TestHandleStaticFile(t *testing.T) {
  113. // SETUP file
  114. testRoot, _ := os.Getwd()
  115. f, err := ioutil.TempFile(testRoot, "")
  116. if err != nil {
  117. t.Error(err)
  118. }
  119. defer os.Remove(f.Name())
  120. filePath := path.Join("/", path.Base(f.Name()))
  121. f.WriteString("Gin Web Framework")
  122. f.Close()
  123. // SETUP gin
  124. r := New()
  125. r.Static("./", testRoot)
  126. // RUN
  127. w := PerformRequest(r, "GET", filePath)
  128. // TEST
  129. if w.Code != 200 {
  130. t.Errorf("Response code should be Ok, was: %s", w.Code)
  131. }
  132. if w.Body.String() != "Gin Web Framework" {
  133. t.Errorf("Response should be test, was: %s", w.Body.String())
  134. }
  135. if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
  136. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  137. }
  138. }
  139. // TestHandleStaticDir - ensure the root/sub dir handles properly
  140. func TestHandleStaticDir(t *testing.T) {
  141. // SETUP
  142. r := New()
  143. r.Static("/", "./")
  144. // RUN
  145. w := PerformRequest(r, "GET", "/")
  146. // TEST
  147. bodyAsString := w.Body.String()
  148. if w.Code != 200 {
  149. t.Errorf("Response code should be Ok, was: %s", w.Code)
  150. }
  151. if len(bodyAsString) == 0 {
  152. t.Errorf("Got empty body instead of file tree")
  153. }
  154. if !strings.Contains(bodyAsString, "gin.go") {
  155. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  156. }
  157. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  158. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  159. }
  160. }
  161. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  162. func TestHandleHeadToDir(t *testing.T) {
  163. // SETUP
  164. r := New()
  165. r.Static("/", "./")
  166. // RUN
  167. w := PerformRequest(r, "HEAD", "/")
  168. // TEST
  169. bodyAsString := w.Body.String()
  170. if w.Code != 200 {
  171. t.Errorf("Response code should be Ok, was: %s", w.Code)
  172. }
  173. if len(bodyAsString) == 0 {
  174. t.Errorf("Got empty body instead of file tree")
  175. }
  176. if !strings.Contains(bodyAsString, "gin.go") {
  177. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  178. }
  179. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  180. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  181. }
  182. }