gin_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. // TestRouterGroupGETRouteOK tests that GET route is correctly invoked.
  12. func TestRouterGroupGETRouteOK(t *testing.T) {
  13. req, _ := http.NewRequest("GET", "/test", nil)
  14. w := httptest.NewRecorder()
  15. passed := false
  16. r := Default()
  17. r.GET("/test", func(c *Context) {
  18. passed = true
  19. })
  20. r.ServeHTTP(w, req)
  21. if passed == false {
  22. t.Errorf("GET route handler was not invoked.")
  23. }
  24. if w.Code != http.StatusOK {
  25. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  26. }
  27. }
  28. // TestRouterGroupGETNoRootExistsRouteOK tests that a GET requse to root is correctly
  29. // handled (404) when no root route exists.
  30. func TestRouterGroupGETNoRootExistsRouteOK(t *testing.T) {
  31. req, _ := http.NewRequest("GET", "/", nil)
  32. w := httptest.NewRecorder()
  33. r := Default()
  34. r.GET("/test", func(c *Context) {
  35. })
  36. r.ServeHTTP(w, req)
  37. if w.Code != http.StatusNotFound {
  38. // If this fails, it's because httprouter needs to be updated to at least f78f58a0db
  39. t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
  40. }
  41. }
  42. // TestRouterGroupPOSTRouteOK tests that POST route is correctly invoked.
  43. func TestRouterGroupPOSTRouteOK(t *testing.T) {
  44. req, _ := http.NewRequest("POST", "/test", nil)
  45. w := httptest.NewRecorder()
  46. passed := false
  47. r := Default()
  48. r.POST("/test", func(c *Context) {
  49. passed = true
  50. })
  51. r.ServeHTTP(w, req)
  52. if passed == false {
  53. t.Errorf("POST route handler was not invoked.")
  54. }
  55. if w.Code != http.StatusOK {
  56. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  57. }
  58. }
  59. // TestRouterGroupDELETERouteOK tests that DELETE route is correctly invoked.
  60. func TestRouterGroupDELETERouteOK(t *testing.T) {
  61. req, _ := http.NewRequest("DELETE", "/test", nil)
  62. w := httptest.NewRecorder()
  63. passed := false
  64. r := Default()
  65. r.DELETE("/test", func(c *Context) {
  66. passed = true
  67. })
  68. r.ServeHTTP(w, req)
  69. if passed == false {
  70. t.Errorf("DELETE route handler was not invoked.")
  71. }
  72. if w.Code != http.StatusOK {
  73. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  74. }
  75. }
  76. // TestRouterGroupPATCHRouteOK tests that PATCH route is correctly invoked.
  77. func TestRouterGroupPATCHRouteOK(t *testing.T) {
  78. req, _ := http.NewRequest("PATCH", "/test", nil)
  79. w := httptest.NewRecorder()
  80. passed := false
  81. r := Default()
  82. r.PATCH("/test", func(c *Context) {
  83. passed = true
  84. })
  85. r.ServeHTTP(w, req)
  86. if passed == false {
  87. t.Errorf("PATCH route handler was not invoked.")
  88. }
  89. if w.Code != http.StatusOK {
  90. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  91. }
  92. }
  93. // TestRouterGroupPUTRouteOK tests that PUT route is correctly invoked.
  94. func TestRouterGroupPUTRouteOK(t *testing.T) {
  95. req, _ := http.NewRequest("PUT", "/test", nil)
  96. w := httptest.NewRecorder()
  97. passed := false
  98. r := Default()
  99. r.PUT("/test", func(c *Context) {
  100. passed = true
  101. })
  102. r.ServeHTTP(w, req)
  103. if passed == false {
  104. t.Errorf("PUT route handler was not invoked.")
  105. }
  106. if w.Code != http.StatusOK {
  107. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  108. }
  109. }
  110. // TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked.
  111. func TestRouterGroupOPTIONSRouteOK(t *testing.T) {
  112. req, _ := http.NewRequest("OPTIONS", "/test", nil)
  113. w := httptest.NewRecorder()
  114. passed := false
  115. r := Default()
  116. r.OPTIONS("/test", func(c *Context) {
  117. passed = true
  118. })
  119. r.ServeHTTP(w, req)
  120. if passed == false {
  121. t.Errorf("OPTIONS route handler was not invoked.")
  122. }
  123. if w.Code != http.StatusOK {
  124. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  125. }
  126. }
  127. // TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked.
  128. func TestRouterGroupHEADRouteOK(t *testing.T) {
  129. req, _ := http.NewRequest("HEAD", "/test", nil)
  130. w := httptest.NewRecorder()
  131. passed := false
  132. r := Default()
  133. r.HEAD("/test", func(c *Context) {
  134. passed = true
  135. })
  136. r.ServeHTTP(w, req)
  137. if passed == false {
  138. t.Errorf("HEAD route handler was not invoked.")
  139. }
  140. if w.Code != http.StatusOK {
  141. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  142. }
  143. }
  144. // TestRouterGroup404 tests that 404 is returned for a route that does not exist.
  145. func TestEngine404(t *testing.T) {
  146. req, _ := http.NewRequest("GET", "/", nil)
  147. w := httptest.NewRecorder()
  148. r := Default()
  149. r.ServeHTTP(w, req)
  150. if w.Code != http.StatusNotFound {
  151. t.Errorf("Response code should be %v, was %d", http.StatusNotFound, w.Code)
  152. }
  153. }
  154. // TestHandleStaticFile - ensure the static file handles properly
  155. func TestHandleStaticFile(t *testing.T) {
  156. testRoot, _ := os.Getwd()
  157. f, err := ioutil.TempFile(testRoot, "")
  158. defer os.Remove(f.Name())
  159. if err != nil {
  160. t.Error(err)
  161. }
  162. filePath := path.Join("/", path.Base(f.Name()))
  163. req, _ := http.NewRequest("GET", filePath, nil)
  164. f.WriteString("Gin Web Framework")
  165. f.Close()
  166. w := httptest.NewRecorder()
  167. r := Default()
  168. r.Static("./", testRoot)
  169. r.ServeHTTP(w, req)
  170. if w.Code != 200 {
  171. t.Errorf("Response code should be Ok, was: %s", w.Code)
  172. }
  173. if w.Body.String() != "Gin Web Framework" {
  174. t.Errorf("Response should be test, was: %s", w.Body.String())
  175. }
  176. if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
  177. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  178. }
  179. }
  180. // TestHandleStaticDir - ensure the root/sub dir handles properly
  181. func TestHandleStaticDir(t *testing.T) {
  182. req, _ := http.NewRequest("GET", "/", nil)
  183. w := httptest.NewRecorder()
  184. r := Default()
  185. r.Static("/", "./")
  186. r.ServeHTTP(w, req)
  187. if w.Code != 200 {
  188. t.Errorf("Response code should be Ok, was: %s", w.Code)
  189. }
  190. bodyAsString := w.Body.String()
  191. if len(bodyAsString) == 0 {
  192. t.Errorf("Got empty body instead of file tree")
  193. }
  194. if !strings.Contains(bodyAsString, "gin.go") {
  195. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  196. }
  197. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  198. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  199. }
  200. }
  201. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  202. func TestHandleHeadToDir(t *testing.T) {
  203. req, _ := http.NewRequest("HEAD", "/", nil)
  204. w := httptest.NewRecorder()
  205. r := Default()
  206. r.Static("/", "./")
  207. r.ServeHTTP(w, req)
  208. if w.Code != 200 {
  209. t.Errorf("Response code should be Ok, was: %s", w.Code)
  210. }
  211. bodyAsString := w.Body.String()
  212. if len(bodyAsString) == 0 {
  213. t.Errorf("Got empty body instead of file tree")
  214. }
  215. if !strings.Contains(bodyAsString, "gin.go") {
  216. t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
  217. }
  218. if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
  219. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  220. }
  221. }