gin_integration_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2017 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bufio"
  7. "crypto/tls"
  8. "fmt"
  9. "html/template"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "net/http/httptest"
  14. "os"
  15. "sync"
  16. "testing"
  17. "time"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func testRequest(t *testing.T, url string) {
  21. tr := &http.Transport{
  22. TLSClientConfig: &tls.Config{
  23. InsecureSkipVerify: true,
  24. },
  25. }
  26. client := &http.Client{Transport: tr}
  27. resp, err := client.Get(url)
  28. assert.NoError(t, err)
  29. defer resp.Body.Close()
  30. body, ioerr := ioutil.ReadAll(resp.Body)
  31. assert.NoError(t, ioerr)
  32. assert.Equal(t, "it worked", string(body), "resp body should match")
  33. assert.Equal(t, "200 OK", resp.Status, "should get a 200")
  34. }
  35. func TestRunEmpty(t *testing.T) {
  36. os.Setenv("PORT", "")
  37. router := New()
  38. go func() {
  39. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  40. assert.NoError(t, router.Run())
  41. }()
  42. // have to wait for the goroutine to start and run the server
  43. // otherwise the main thread will complete
  44. time.Sleep(5 * time.Millisecond)
  45. assert.Error(t, router.Run(":8080"))
  46. testRequest(t, "http://localhost:8080/example")
  47. }
  48. func TestRunTLS(t *testing.T) {
  49. router := New()
  50. go func() {
  51. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  52. assert.NoError(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  53. }()
  54. // have to wait for the goroutine to start and run the server
  55. // otherwise the main thread will complete
  56. time.Sleep(5 * time.Millisecond)
  57. assert.Error(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  58. testRequest(t, "https://localhost:8443/example")
  59. }
  60. func TestPusher(t *testing.T) {
  61. var html = template.Must(template.New("https").Parse(`
  62. <html>
  63. <head>
  64. <title>Https Test</title>
  65. <script src="/assets/app.js"></script>
  66. </head>
  67. <body>
  68. <h1 style="color:red;">Welcome, Ginner!</h1>
  69. </body>
  70. </html>
  71. `))
  72. router := New()
  73. router.Static("./assets", "./assets")
  74. router.SetHTMLTemplate(html)
  75. go func() {
  76. router.GET("/pusher", func(c *Context) {
  77. if pusher := c.Writer.Pusher(); pusher != nil {
  78. pusher.Push("/assets/app.js", nil)
  79. }
  80. c.String(http.StatusOK, "it worked")
  81. })
  82. assert.NoError(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  83. }()
  84. // have to wait for the goroutine to start and run the server
  85. // otherwise the main thread will complete
  86. time.Sleep(5 * time.Millisecond)
  87. assert.Error(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  88. testRequest(t, "https://localhost:8449/pusher")
  89. }
  90. func TestRunEmptyWithEnv(t *testing.T) {
  91. os.Setenv("PORT", "3123")
  92. router := New()
  93. go func() {
  94. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  95. assert.NoError(t, router.Run())
  96. }()
  97. // have to wait for the goroutine to start and run the server
  98. // otherwise the main thread will complete
  99. time.Sleep(5 * time.Millisecond)
  100. assert.Error(t, router.Run(":3123"))
  101. testRequest(t, "http://localhost:3123/example")
  102. }
  103. func TestRunTooMuchParams(t *testing.T) {
  104. router := New()
  105. assert.Panics(t, func() {
  106. assert.NoError(t, router.Run("2", "2"))
  107. })
  108. }
  109. func TestRunWithPort(t *testing.T) {
  110. router := New()
  111. go func() {
  112. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  113. assert.NoError(t, router.Run(":5150"))
  114. }()
  115. // have to wait for the goroutine to start and run the server
  116. // otherwise the main thread will complete
  117. time.Sleep(5 * time.Millisecond)
  118. assert.Error(t, router.Run(":5150"))
  119. testRequest(t, "http://localhost:5150/example")
  120. }
  121. func TestUnixSocket(t *testing.T) {
  122. router := New()
  123. go func() {
  124. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  125. assert.NoError(t, router.RunUnix("/tmp/unix_unit_test"))
  126. }()
  127. // have to wait for the goroutine to start and run the server
  128. // otherwise the main thread will complete
  129. time.Sleep(5 * time.Millisecond)
  130. c, err := net.Dial("unix", "/tmp/unix_unit_test")
  131. assert.NoError(t, err)
  132. fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
  133. scanner := bufio.NewScanner(c)
  134. var response string
  135. for scanner.Scan() {
  136. response += scanner.Text()
  137. }
  138. assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
  139. assert.Contains(t, response, "it worked", "resp body should match")
  140. }
  141. func TestBadUnixSocket(t *testing.T) {
  142. router := New()
  143. assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
  144. }
  145. func TestFileDescriptor(t *testing.T) {
  146. router := New()
  147. addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
  148. assert.NoError(t, err)
  149. listener, err := net.ListenTCP("tcp", addr)
  150. assert.NoError(t, err)
  151. socketFile, err := listener.File()
  152. assert.NoError(t, err)
  153. go func() {
  154. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  155. assert.NoError(t, router.RunFd(int(socketFile.Fd())))
  156. }()
  157. // have to wait for the goroutine to start and run the server
  158. // otherwise the main thread will complete
  159. time.Sleep(5 * time.Millisecond)
  160. c, err := net.Dial("tcp", listener.Addr().String())
  161. assert.NoError(t, err)
  162. fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
  163. scanner := bufio.NewScanner(c)
  164. var response string
  165. for scanner.Scan() {
  166. response += scanner.Text()
  167. }
  168. assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
  169. assert.Contains(t, response, "it worked", "resp body should match")
  170. }
  171. func TestBadFileDescriptor(t *testing.T) {
  172. router := New()
  173. assert.Error(t, router.RunFd(0))
  174. }
  175. func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
  176. router := New()
  177. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  178. ts := httptest.NewServer(router)
  179. defer ts.Close()
  180. testRequest(t, ts.URL+"/example")
  181. }
  182. func TestConcurrentHandleContext(t *testing.T) {
  183. router := New()
  184. router.GET("/", func(c *Context) {
  185. c.Request.URL.Path = "/example"
  186. router.HandleContext(c)
  187. })
  188. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  189. var wg sync.WaitGroup
  190. iterations := 200
  191. wg.Add(iterations)
  192. for i := 0; i < iterations; i++ {
  193. go func() {
  194. testGetRequestHandler(t, router, "/")
  195. wg.Done()
  196. }()
  197. }
  198. wg.Wait()
  199. }
  200. // func TestWithHttptestWithSpecifiedPort(t *testing.T) {
  201. // router := New()
  202. // router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  203. // l, _ := net.Listen("tcp", ":8033")
  204. // ts := httptest.Server{
  205. // Listener: l,
  206. // Config: &http.Server{Handler: router},
  207. // }
  208. // ts.Start()
  209. // defer ts.Close()
  210. // testRequest(t, "http://localhost:8033/example")
  211. // }
  212. func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
  213. req, err := http.NewRequest("GET", url, nil)
  214. assert.NoError(t, err)
  215. w := httptest.NewRecorder()
  216. h.ServeHTTP(w, req)
  217. assert.Equal(t, "it worked", w.Body.String(), "resp body should match")
  218. assert.Equal(t, 200, w.Code, "should get a 200")
  219. }