gin_test.go 5.0 KB

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