gin_test.go 5.0 KB

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