context_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package gin
  2. import (
  3. "errors"
  4. "html/template"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. // TestContextParamsGet tests that a parameter can be parsed from the URL.
  10. func TestContextParamsByName(t *testing.T) {
  11. req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
  12. w := httptest.NewRecorder()
  13. name := ""
  14. r := Default()
  15. r.GET("/test/:name", func(c *Context) {
  16. name = c.Params.ByName("name")
  17. })
  18. r.ServeHTTP(w, req)
  19. if name != "alexandernyquist" {
  20. t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
  21. }
  22. }
  23. // TestContextSetGet tests that a parameter is set correctly on the
  24. // current context and can be retrieved using Get.
  25. func TestContextSetGet(t *testing.T) {
  26. req, _ := http.NewRequest("GET", "/test", nil)
  27. w := httptest.NewRecorder()
  28. r := Default()
  29. r.GET("/test", func(c *Context) {
  30. // Key should be lazily created
  31. if c.Keys != nil {
  32. t.Error("Keys should be nil")
  33. }
  34. // Set
  35. c.Set("foo", "bar")
  36. v, err := c.Get("foo")
  37. if err != nil {
  38. t.Errorf("Error on exist key")
  39. }
  40. if v != "bar" {
  41. t.Errorf("Value should be bar, was %s", v)
  42. }
  43. })
  44. r.ServeHTTP(w, req)
  45. }
  46. // TestContextJSON tests that the response is serialized as JSON
  47. // and Content-Type is set to application/json
  48. func TestContextJSON(t *testing.T) {
  49. req, _ := http.NewRequest("GET", "/test", nil)
  50. w := httptest.NewRecorder()
  51. r := Default()
  52. r.GET("/test", func(c *Context) {
  53. c.JSON(200, H{"foo": "bar"})
  54. })
  55. r.ServeHTTP(w, req)
  56. if w.Body.String() != "{\"foo\":\"bar\"}\n" {
  57. t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
  58. }
  59. if w.HeaderMap.Get("Content-Type") != "application/json" {
  60. t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
  61. }
  62. }
  63. // TestContextHTML tests that the response executes the templates
  64. // and responds with Content-Type set to text/html
  65. func TestContextHTML(t *testing.T) {
  66. req, _ := http.NewRequest("GET", "/test", nil)
  67. w := httptest.NewRecorder()
  68. r := Default()
  69. templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
  70. r.SetHTMLTemplate(templ)
  71. type TestData struct{ Name string }
  72. r.GET("/test", func(c *Context) {
  73. c.HTML(200, "t", TestData{"alexandernyquist"})
  74. })
  75. r.ServeHTTP(w, req)
  76. if w.Body.String() != "Hello alexandernyquist" {
  77. t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String())
  78. }
  79. if w.HeaderMap.Get("Content-Type") != "text/html" {
  80. t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type"))
  81. }
  82. }
  83. // TestContextString tests that the response is returned
  84. // with Content-Type set to text/plain
  85. func TestContextString(t *testing.T) {
  86. req, _ := http.NewRequest("GET", "/test", nil)
  87. w := httptest.NewRecorder()
  88. r := Default()
  89. r.GET("/test", func(c *Context) {
  90. c.String(200, "test")
  91. })
  92. r.ServeHTTP(w, req)
  93. if w.Body.String() != "test" {
  94. t.Errorf("Response should be test, was: %s", w.Body.String())
  95. }
  96. if w.HeaderMap.Get("Content-Type") != "text/plain" {
  97. t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
  98. }
  99. }
  100. // TestHandlerFunc - ensure that custom middleware works properly
  101. func TestHandlerFunc(t *testing.T) {
  102. req, _ := http.NewRequest("GET", "/", nil)
  103. w := httptest.NewRecorder()
  104. r := Default()
  105. var stepsPassed int = 0
  106. r.Use(func(context *Context) {
  107. stepsPassed += 1
  108. context.Next()
  109. stepsPassed += 1
  110. })
  111. r.ServeHTTP(w, req)
  112. if w.Code != 404 {
  113. t.Errorf("Response code should be Not found, was: %s", w.Code)
  114. }
  115. if stepsPassed != 2 {
  116. t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
  117. }
  118. }
  119. // TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
  120. func TestBadAbortHandlersChain(t *testing.T) {
  121. req, _ := http.NewRequest("GET", "/", nil)
  122. w := httptest.NewRecorder()
  123. r := Default()
  124. var stepsPassed int = 0
  125. r.Use(func(context *Context) {
  126. stepsPassed += 1
  127. context.Next()
  128. stepsPassed += 1
  129. // after check and abort
  130. context.Abort(409)
  131. },
  132. func(context *Context) {
  133. stepsPassed += 1
  134. context.Next()
  135. stepsPassed += 1
  136. context.Abort(403)
  137. },
  138. )
  139. r.ServeHTTP(w, req)
  140. if w.Code != 403 {
  141. t.Errorf("Response code should be Forbiden, was: %s", w.Code)
  142. }
  143. if stepsPassed != 4 {
  144. t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
  145. }
  146. }
  147. // TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
  148. func TestAbortHandlersChain(t *testing.T) {
  149. req, _ := http.NewRequest("GET", "/", nil)
  150. w := httptest.NewRecorder()
  151. r := Default()
  152. var stepsPassed int = 0
  153. r.Use(func(context *Context) {
  154. stepsPassed += 1
  155. context.Abort(409)
  156. },
  157. func(context *Context) {
  158. stepsPassed += 1
  159. context.Next()
  160. stepsPassed += 1
  161. },
  162. )
  163. r.ServeHTTP(w, req)
  164. if w.Code != 409 {
  165. t.Errorf("Response code should be Conflict, was: %s", w.Code)
  166. }
  167. if stepsPassed != 1 {
  168. t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
  169. }
  170. }
  171. // TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
  172. // as well as Abort
  173. func TestFailHandlersChain(t *testing.T) {
  174. req, _ := http.NewRequest("GET", "/", nil)
  175. w := httptest.NewRecorder()
  176. r := Default()
  177. var stepsPassed int = 0
  178. r.Use(func(context *Context) {
  179. stepsPassed += 1
  180. context.Fail(500, errors.New("foo"))
  181. },
  182. func(context *Context) {
  183. stepsPassed += 1
  184. context.Next()
  185. stepsPassed += 1
  186. },
  187. )
  188. r.ServeHTTP(w, req)
  189. if w.Code != 500 {
  190. t.Errorf("Response code should be Server error, was: %s", w.Code)
  191. }
  192. if stepsPassed != 1 {
  193. t.Errorf("Falied to switch context in handler function: %s", stepsPassed)
  194. }
  195. }