gin_integration_test.go 3.5 KB

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