gin_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package gin
  2. import(
  3. "testing"
  4. "html/template"
  5. "net/http"
  6. "net/http/httptest"
  7. )
  8. // TestRouterGroupGETRouteOK tests that GET route is correctly invoked.
  9. func TestRouterGroupGETRouteOK(t *testing.T) {
  10. req, _ := http.NewRequest("GET", "/test", nil)
  11. w := httptest.NewRecorder()
  12. passed := false
  13. r := Default()
  14. r.GET("/test", func (c *Context) {
  15. passed = true
  16. })
  17. r.ServeHTTP(w, req)
  18. if passed == false {
  19. t.Errorf("GET route handler was not invoked.")
  20. }
  21. if w.Code != http.StatusOK {
  22. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  23. }
  24. }
  25. // TestRouterGroupGETNoRootExistsRouteOK tests that a GET requse to root is correctly
  26. // handled (404) when no root route exists.
  27. func TestRouterGroupGETNoRootExistsRouteOK(t *testing.T) {
  28. req, _ := http.NewRequest("GET", "/", nil)
  29. w := httptest.NewRecorder()
  30. r := Default()
  31. r.GET("/test", func (c *Context) {
  32. })
  33. r.ServeHTTP(w, req)
  34. if w.Code != http.StatusNotFound {
  35. // If this fails, it's because httprouter needs to be updated to at least f78f58a0db
  36. t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
  37. }
  38. }
  39. // TestRouterGroupPOSTRouteOK tests that POST route is correctly invoked.
  40. func TestRouterGroupPOSTRouteOK(t *testing.T) {
  41. req, _ := http.NewRequest("POST", "/test", nil)
  42. w := httptest.NewRecorder()
  43. passed := false
  44. r := Default()
  45. r.POST("/test", func (c *Context) {
  46. passed = true
  47. })
  48. r.ServeHTTP(w, req)
  49. if passed == false {
  50. t.Errorf("POST route handler was not invoked.")
  51. }
  52. if w.Code != http.StatusOK {
  53. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  54. }
  55. }
  56. // TestRouterGroupDELETERouteOK tests that DELETE route is correctly invoked.
  57. func TestRouterGroupDELETERouteOK(t *testing.T) {
  58. req, _ := http.NewRequest("DELETE", "/test", nil)
  59. w := httptest.NewRecorder()
  60. passed := false
  61. r := Default()
  62. r.DELETE("/test", func (c *Context) {
  63. passed = true
  64. })
  65. r.ServeHTTP(w, req)
  66. if passed == false {
  67. t.Errorf("DELETE route handler was not invoked.")
  68. }
  69. if w.Code != http.StatusOK {
  70. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  71. }
  72. }
  73. // TestRouterGroupPATCHRouteOK tests that PATCH route is correctly invoked.
  74. func TestRouterGroupPATCHRouteOK(t *testing.T) {
  75. req, _ := http.NewRequest("PATCH", "/test", nil)
  76. w := httptest.NewRecorder()
  77. passed := false
  78. r := Default()
  79. r.PATCH("/test", func (c *Context) {
  80. passed = true
  81. })
  82. r.ServeHTTP(w, req)
  83. if passed == false {
  84. t.Errorf("PATCH route handler was not invoked.")
  85. }
  86. if w.Code != http.StatusOK {
  87. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  88. }
  89. }
  90. // TestRouterGroupPUTRouteOK tests that PUT route is correctly invoked.
  91. func TestRouterGroupPUTRouteOK(t *testing.T) {
  92. req, _ := http.NewRequest("PUT", "/test", nil)
  93. w := httptest.NewRecorder()
  94. passed := false
  95. r := Default()
  96. r.PUT("/test", func (c *Context) {
  97. passed = true
  98. })
  99. r.ServeHTTP(w, req)
  100. if passed == false {
  101. t.Errorf("PUT route handler was not invoked.")
  102. }
  103. if w.Code != http.StatusOK {
  104. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  105. }
  106. }
  107. // TestRouterGroupOPTIONSRouteOK tests that OPTIONS route is correctly invoked.
  108. func TestRouterGroupOPTIONSRouteOK(t *testing.T) {
  109. req, _ := http.NewRequest("OPTIONS", "/test", nil)
  110. w := httptest.NewRecorder()
  111. passed := false
  112. r := Default()
  113. r.OPTIONS("/test", func (c *Context) {
  114. passed = true
  115. })
  116. r.ServeHTTP(w, req)
  117. if passed == false {
  118. t.Errorf("OPTIONS route handler was not invoked.")
  119. }
  120. if w.Code != http.StatusOK {
  121. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  122. }
  123. }
  124. // TestRouterGroupHEADRouteOK tests that HEAD route is correctly invoked.
  125. func TestRouterGroupHEADRouteOK(t *testing.T) {
  126. req, _ := http.NewRequest("HEAD", "/test", nil)
  127. w := httptest.NewRecorder()
  128. passed := false
  129. r := Default()
  130. r.HEAD("/test", func (c *Context) {
  131. passed = true
  132. })
  133. r.ServeHTTP(w, req)
  134. if passed == false {
  135. t.Errorf("HEAD route handler was not invoked.")
  136. }
  137. if w.Code != http.StatusOK {
  138. t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
  139. }
  140. }
  141. // TestRouterGroup404 tests that 404 is returned for a route that does not exist.
  142. func TestEngine404(t *testing.T) {
  143. req, _ := http.NewRequest("GET", "/", nil)
  144. w := httptest.NewRecorder()
  145. r := Default()
  146. r.ServeHTTP(w, req)
  147. if w.Code != http.StatusNotFound {
  148. t.Errorf("Response code should be %v, was %d", http.StatusNotFound, w.Code)
  149. }
  150. }
  151. // TestContextParamsGet tests that a parameter can be parsed from the URL.
  152. func TestContextParamsByName(t *testing.T) {
  153. req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
  154. w := httptest.NewRecorder()
  155. name := ""
  156. r := Default()
  157. r.GET("/test/:name", func (c *Context) {
  158. name = c.Params.ByName("name")
  159. })
  160. r.ServeHTTP(w, req)
  161. if name != "alexandernyquist" {
  162. t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
  163. }
  164. }
  165. // TestContextSetGet tests that a parameter is set correctly on the
  166. // current context and can be retrieved using Get.
  167. func TestContextSetGet(t *testing.T) {
  168. req, _ := http.NewRequest("GET", "/test", nil)
  169. w := httptest.NewRecorder()
  170. r := Default()
  171. r.GET("/test", func (c *Context) {
  172. // Key should be lazily created
  173. if c.Keys != nil {
  174. t.Error("Keys should be nil")
  175. }
  176. // Set
  177. c.Set("foo", "bar")
  178. if v := c.Get("foo"); v != "bar" {
  179. t.Errorf("Value should be bar, was %s", v)
  180. }
  181. })
  182. r.ServeHTTP(w, req)
  183. }
  184. // TestContextJSON tests that the response is serialized as JSON
  185. // and Content-Type is set to application/json
  186. func TestContextJSON(t *testing.T) {
  187. req, _ := http.NewRequest("GET", "/test", nil)
  188. w := httptest.NewRecorder()
  189. r := Default()
  190. r.GET("/test", func (c *Context) {
  191. c.JSON(200, H{"foo": "bar"})
  192. })
  193. r.ServeHTTP(w, req)
  194. if w.Body.String() != "{\"foo\":\"bar\"}\n" {
  195. t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
  196. }
  197. if w.HeaderMap.Get("Content-Type") != "application/json" {
  198. t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
  199. }
  200. }
  201. // TestContextHTML tests that the response executes the templates
  202. // and responds with Content-Type set to text/html
  203. func TestContextHTML(t *testing.T) {
  204. req, _ := http.NewRequest("GET", "/test", nil)
  205. w := httptest.NewRecorder()
  206. r := Default()
  207. r.HTMLTemplates = template.Must(template.New("t").Parse(`Hello {{.Name}}`))
  208. type TestData struct { Name string }
  209. r.GET("/test", func (c *Context) {
  210. c.HTML(200, "t", TestData{"alexandernyquist"})
  211. })
  212. r.ServeHTTP(w, req)
  213. if w.Body.String() != "Hello alexandernyquist" {
  214. t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String())
  215. }
  216. if w.HeaderMap.Get("Content-Type") != "text/html" {
  217. t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type"))
  218. }
  219. }
  220. // TestContextString tests that the response is returned
  221. // with Content-Type set to text/plain
  222. func TestContextString(t *testing.T) {
  223. req, _ := http.NewRequest("GET", "/test", nil)
  224. w := httptest.NewRecorder()
  225. r := Default()
  226. r.GET("/test", func (c *Context) {
  227. c.String(200, "test")
  228. })
  229. r.ServeHTTP(w, req)
  230. if w.Body.String() != "test" {
  231. t.Errorf("Response should be test, was: %s", w.Body.String())
  232. }
  233. if w.HeaderMap.Get("Content-Type") != "text/plain" {
  234. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  235. }
  236. }