gin_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.StatusMethodNotAllowed {
  98. t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusMethodNotAllowed, w.Code, w.HeaderMap.Get("Location"))
  99. }
  100. }
  101. // TestSingleRouteOK tests that POST route is correctly invoked.
  102. func TestRouteNotOK2(t *testing.T) {
  103. testRouteNotOK2("POST", t)
  104. testRouteNotOK2("DELETE", t)
  105. testRouteNotOK2("PATCH", t)
  106. testRouteNotOK2("PUT", t)
  107. testRouteNotOK2("OPTIONS", t)
  108. testRouteNotOK2("HEAD", t)
  109. }
  110. // TestHandleStaticFile - ensure the static file handles properly
  111. func TestHandleStaticFile(t *testing.T) {
  112. // SETUP file
  113. testRoot, _ := os.Getwd()
  114. f, err := ioutil.TempFile(testRoot, "")
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. defer os.Remove(f.Name())
  119. filePath := path.Join("/", path.Base(f.Name()))
  120. f.WriteString("Gin Web Framework")
  121. f.Close()
  122. // SETUP gin
  123. r := New()
  124. r.Static("./", testRoot)
  125. // RUN
  126. w := PerformRequest(r, "GET", filePath)
  127. // TEST
  128. if w.Code != 200 {
  129. t.Errorf("Response code should be 200, was: %d", w.Code)
  130. }
  131. if w.Body.String() != "Gin Web Framework" {
  132. t.Errorf("Response should be test, was: %s", w.Body.String())
  133. }
  134. if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
  135. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  136. }
  137. }
  138. // TestHandleStaticDir - ensure the root/sub dir handles properly
  139. func TestHandleStaticDir(t *testing.T) {
  140. // SETUP
  141. r := New()
  142. r.Static("/", "./")
  143. // RUN
  144. w := PerformRequest(r, "GET", "/")
  145. // TEST
  146. bodyAsString := w.Body.String()
  147. if w.Code != 200 {
  148. t.Errorf("Response code should be 200, was: %d", w.Code)
  149. }
  150. if len(bodyAsString) == 0 {
  151. t.Errorf("Got empty body instead of file tree")
  152. }
  153. if !strings.Contains(bodyAsString, "gin.go") {
  154. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  155. }
  156. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  157. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  158. }
  159. }
  160. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  161. func TestHandleHeadToDir(t *testing.T) {
  162. // SETUP
  163. r := New()
  164. r.Static("/", "./")
  165. // RUN
  166. w := PerformRequest(r, "HEAD", "/")
  167. // TEST
  168. bodyAsString := w.Body.String()
  169. if w.Code != 200 {
  170. t.Errorf("Response code should be Ok, was: %s", w.Code)
  171. }
  172. if len(bodyAsString) == 0 {
  173. t.Errorf("Got empty body instead of file tree")
  174. }
  175. if !strings.Contains(bodyAsString, "gin.go") {
  176. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  177. }
  178. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  179. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  180. }
  181. }