gin_integration_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "fmt"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/http/httptest"
  12. "os"
  13. "sync"
  14. "testing"
  15. "time"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func testRequest(t *testing.T, url string) {
  19. resp, err := http.Get(url)
  20. assert.NoError(t, err)
  21. defer resp.Body.Close()
  22. body, ioerr := ioutil.ReadAll(resp.Body)
  23. assert.NoError(t, ioerr)
  24. assert.Equal(t, "it worked", string(body), "resp body should match")
  25. assert.Equal(t, "200 OK", resp.Status, "should get a 200")
  26. }
  27. func TestRunEmpty(t *testing.T) {
  28. os.Setenv("PORT", "")
  29. router := New()
  30. go func() {
  31. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  32. assert.NoError(t, router.Run())
  33. }()
  34. // have to wait for the goroutine to start and run the server
  35. // otherwise the main thread will complete
  36. time.Sleep(5 * time.Millisecond)
  37. assert.Error(t, router.Run(":8080"))
  38. testRequest(t, "http://localhost:8080/example")
  39. }
  40. func TestRunEmptyWithEnv(t *testing.T) {
  41. os.Setenv("PORT", "3123")
  42. router := New()
  43. go func() {
  44. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  45. assert.NoError(t, router.Run())
  46. }()
  47. // have to wait for the goroutine to start and run the server
  48. // otherwise the main thread will complete
  49. time.Sleep(5 * time.Millisecond)
  50. assert.Error(t, router.Run(":3123"))
  51. testRequest(t, "http://localhost:3123/example")
  52. }
  53. func TestRunTooMuchParams(t *testing.T) {
  54. router := New()
  55. assert.Panics(t, func() {
  56. router.Run("2", "2")
  57. })
  58. }
  59. func TestRunWithPort(t *testing.T) {
  60. router := New()
  61. go func() {
  62. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  63. assert.NoError(t, router.Run(":5150"))
  64. }()
  65. // have to wait for the goroutine to start and run the server
  66. // otherwise the main thread will complete
  67. time.Sleep(5 * time.Millisecond)
  68. assert.Error(t, router.Run(":5150"))
  69. testRequest(t, "http://localhost:5150/example")
  70. }
  71. func TestUnixSocket(t *testing.T) {
  72. router := New()
  73. go func() {
  74. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  75. assert.NoError(t, router.RunUnix("/tmp/unix_unit_test"))
  76. }()
  77. // have to wait for the goroutine to start and run the server
  78. // otherwise the main thread will complete
  79. time.Sleep(5 * time.Millisecond)
  80. c, err := net.Dial("unix", "/tmp/unix_unit_test")
  81. assert.NoError(t, err)
  82. fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
  83. scanner := bufio.NewScanner(c)
  84. var response string
  85. for scanner.Scan() {
  86. response += scanner.Text()
  87. }
  88. assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
  89. assert.Contains(t, response, "it worked", "resp body should match")
  90. }
  91. func TestBadUnixSocket(t *testing.T) {
  92. router := New()
  93. assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
  94. }
  95. func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
  96. router := New()
  97. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  98. ts := httptest.NewServer(router)
  99. defer ts.Close()
  100. testRequest(t, ts.URL+"/example")
  101. }
  102. func TestConcurrentHandleContext(t *testing.T) {
  103. router := New()
  104. router.GET("/", func(c *Context) {
  105. c.Request.URL.Path = "/example"
  106. router.HandleContext(c)
  107. })
  108. router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  109. ts := httptest.NewServer(router)
  110. defer ts.Close()
  111. var wg sync.WaitGroup
  112. iterations := 200
  113. wg.Add(iterations)
  114. for i := 0; i < iterations; i++ {
  115. go func() {
  116. testRequest(t, ts.URL+"/")
  117. wg.Done()
  118. }()
  119. }
  120. wg.Wait()
  121. }
  122. // func TestWithHttptestWithSpecifiedPort(t *testing.T) {
  123. // router := New()
  124. // router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
  125. // l, _ := net.Listen("tcp", ":8033")
  126. // ts := httptest.Server{
  127. // Listener: l,
  128. // Config: &http.Server{Handler: router},
  129. // }
  130. // ts.Start()
  131. // defer ts.Close()
  132. // testRequest(t, "http://localhost:8033/example")
  133. // }