gin_integration_test.go 2.7 KB

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