gin_integration_test.go 3.3 KB

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