gin_integration_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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 TestWithHttptestWithAutoSelectedPort(t *testing.T) {
  115. router := New()
  116. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  117. ts := httptest.NewServer(router)
  118. defer ts.Close()
  119. testRequest(t, ts.URL+"/example")
  120. }
  121. func TestConcurrentHandleContext(t *testing.T) {
  122. router := New()
  123. router.GET("/", func(c *Context) {
  124. c.Request.URL.Path = "/example"
  125. router.HandleContext(c)
  126. })
  127. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  128. ts := httptest.NewServer(router)
  129. defer ts.Close()
  130. var wg sync.WaitGroup
  131. iterations := 200
  132. wg.Add(iterations)
  133. for i := 0; i < iterations; i++ {
  134. go func() {
  135. testRequest(t, ts.URL+"/")
  136. wg.Done()
  137. }()
  138. }
  139. wg.Wait()
  140. }
  141. // func TestWithHttptestWithSpecifiedPort(t *testing.T) {
  142. // router := New()
  143. // router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  144. // l, _ := net.Listen("tcp", ":8033")
  145. // ts := httptest.Server{
  146. // Listener: l,
  147. // Config: &http.Server{Handler: router},
  148. // }
  149. // ts.Start()
  150. // defer ts.Close()
  151. // testRequest(t, "http://localhost:8033/example")
  152. // }