gin_integration_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "io/ioutil"
  10. "net"
  11. "net/http"
  12. "net/http/httptest"
  13. "os"
  14. "sync"
  15. "testing"
  16. "time"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func testRequest(t *testing.T, url string) {
  20. tr := &http.Transport{
  21. TLSClientConfig: &tls.Config{
  22. InsecureSkipVerify: true,
  23. },
  24. }
  25. client := &http.Client{Transport: tr}
  26. resp, err := client.Get(url)
  27. assert.NoError(t, err)
  28. defer resp.Body.Close()
  29. body, ioerr := ioutil.ReadAll(resp.Body)
  30. assert.NoError(t, ioerr)
  31. assert.Equal(t, "it worked", string(body), "resp body should match")
  32. assert.Equal(t, "200 OK", resp.Status, "should get a 200")
  33. }
  34. func TestRunEmpty(t *testing.T) {
  35. os.Setenv("PORT", "")
  36. router := New()
  37. go func() {
  38. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  39. assert.NoError(t, router.Run())
  40. }()
  41. // have to wait for the goroutine to start and run the server
  42. // otherwise the main thread will complete
  43. time.Sleep(5 * time.Millisecond)
  44. assert.Error(t, router.Run(":8080"))
  45. testRequest(t, "http://localhost:8080/example")
  46. }
  47. func TestRunTLS(t *testing.T) {
  48. router := New()
  49. go func() {
  50. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  51. assert.NoError(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  52. }()
  53. // have to wait for the goroutine to start and run the server
  54. // otherwise the main thread will complete
  55. time.Sleep(5 * time.Millisecond)
  56. assert.Error(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
  57. testRequest(t, "https://localhost:8443/example")
  58. }
  59. func TestRunEmptyWithEnv(t *testing.T) {
  60. os.Setenv("PORT", "3123")
  61. router := New()
  62. go func() {
  63. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  64. assert.NoError(t, router.Run())
  65. }()
  66. // have to wait for the goroutine to start and run the server
  67. // otherwise the main thread will complete
  68. time.Sleep(5 * time.Millisecond)
  69. assert.Error(t, router.Run(":3123"))
  70. testRequest(t, "http://localhost:3123/example")
  71. }
  72. func TestRunTooMuchParams(t *testing.T) {
  73. router := New()
  74. assert.Panics(t, func() {
  75. assert.NoError(t, router.Run("2", "2"))
  76. })
  77. }
  78. func TestRunWithPort(t *testing.T) {
  79. router := New()
  80. go func() {
  81. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  82. assert.NoError(t, router.Run(":5150"))
  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.Run(":5150"))
  88. testRequest(t, "http://localhost:5150/example")
  89. }
  90. func TestUnixSocket(t *testing.T) {
  91. router := New()
  92. go func() {
  93. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  94. assert.NoError(t, router.RunUnix("/tmp/unix_unit_test"))
  95. }()
  96. // have to wait for the goroutine to start and run the server
  97. // otherwise the main thread will complete
  98. time.Sleep(5 * time.Millisecond)
  99. c, err := net.Dial("unix", "/tmp/unix_unit_test")
  100. assert.NoError(t, err)
  101. fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
  102. scanner := bufio.NewScanner(c)
  103. var response string
  104. for scanner.Scan() {
  105. response += scanner.Text()
  106. }
  107. assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
  108. assert.Contains(t, response, "it worked", "resp body should match")
  109. }
  110. func TestBadUnixSocket(t *testing.T) {
  111. router := New()
  112. assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
  113. }
  114. func TestFileDescriptor(t *testing.T) {
  115. router := New()
  116. addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
  117. assert.NoError(t, err)
  118. listener, err := net.ListenTCP("tcp", addr)
  119. assert.NoError(t, err)
  120. socketFile, err := listener.File()
  121. assert.NoError(t, err)
  122. go func() {
  123. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  124. assert.NoError(t, router.RunFd(int(socketFile.Fd())))
  125. }()
  126. // have to wait for the goroutine to start and run the server
  127. // otherwise the main thread will complete
  128. time.Sleep(5 * time.Millisecond)
  129. c, err := net.Dial("tcp", listener.Addr().String())
  130. assert.NoError(t, err)
  131. fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
  132. scanner := bufio.NewScanner(c)
  133. var response string
  134. for scanner.Scan() {
  135. response += scanner.Text()
  136. }
  137. assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
  138. assert.Contains(t, response, "it worked", "resp body should match")
  139. }
  140. func TestBadFileDescriptor(t *testing.T) {
  141. router := New()
  142. assert.Error(t, router.RunFd(0))
  143. }
  144. func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
  145. router := New()
  146. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  147. ts := httptest.NewServer(router)
  148. defer ts.Close()
  149. testRequest(t, ts.URL+"/example")
  150. }
  151. func TestConcurrentHandleContext(t *testing.T) {
  152. router := New()
  153. router.GET("/", func(c *Context) {
  154. c.Request.URL.Path = "/example"
  155. router.HandleContext(c)
  156. })
  157. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  158. var wg sync.WaitGroup
  159. iterations := 200
  160. wg.Add(iterations)
  161. for i := 0; i < iterations; i++ {
  162. go func() {
  163. testGetRequestHandler(t, router, "/")
  164. wg.Done()
  165. }()
  166. }
  167. wg.Wait()
  168. }
  169. // func TestWithHttptestWithSpecifiedPort(t *testing.T) {
  170. // router := New()
  171. // router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  172. // l, _ := net.Listen("tcp", ":8033")
  173. // ts := httptest.Server{
  174. // Listener: l,
  175. // Config: &http.Server{Handler: router},
  176. // }
  177. // ts.Start()
  178. // defer ts.Close()
  179. // testRequest(t, "http://localhost:8033/example")
  180. // }
  181. func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
  182. req, err := http.NewRequest("GET", url, nil)
  183. assert.NoError(t, err)
  184. w := httptest.NewRecorder()
  185. h.ServeHTTP(w, req)
  186. assert.Equal(t, "it worked", w.Body.String(), "resp body should match")
  187. assert.Equal(t, 200, w.Code, "should get a 200")
  188. }