servlimit_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package servlimit_test
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "time"
  9. . "github.com/onsi/ginkgo"
  10. . "github.com/onsi/gomega"
  11. "github.com/vladimirok5959/golang-utils/utils/http/servlimit"
  12. )
  13. var _ = Describe("servlimit", func() {
  14. Context("ReqPerSecond", func() {
  15. var srv *httptest.Server
  16. var client *http.Client
  17. var getTestHandler = func() http.HandlerFunc {
  18. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. if _, err := w.Write([]byte("Index")); err != nil {
  20. fmt.Printf("%s\n", err.Error())
  21. }
  22. })
  23. }
  24. BeforeEach(func() {
  25. servlimit.MRequests.Cleanup()
  26. srv = httptest.NewServer(servlimit.ReqPerSecond(getTestHandler(), 1))
  27. client = srv.Client()
  28. })
  29. AfterEach(func() {
  30. srv.Close()
  31. })
  32. It("process request", func() {
  33. resp, err := client.Get(srv.URL + "/")
  34. Expect(err).To(Succeed())
  35. defer resp.Body.Close()
  36. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  37. body, err := io.ReadAll(resp.Body)
  38. Expect(err).To(Succeed())
  39. Expect(string(body)).To(Equal("Index"))
  40. })
  41. It("process multiple requests", func() {
  42. resp, err := client.Get(srv.URL + "/")
  43. Expect(err).To(Succeed())
  44. resp.Body.Close()
  45. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  46. time.Sleep(1 * time.Second)
  47. resp, err = client.Get(srv.URL + "/")
  48. Expect(err).To(Succeed())
  49. defer resp.Body.Close()
  50. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  51. body, err := io.ReadAll(resp.Body)
  52. Expect(err).To(Succeed())
  53. Expect(string(body)).To(Equal("Index"))
  54. })
  55. It("block multiple requests", func() {
  56. resp, err := client.Get(srv.URL + "/")
  57. Expect(err).To(Succeed())
  58. resp.Body.Close()
  59. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  60. resp, err = client.Get(srv.URL + "/")
  61. Expect(err).To(Succeed())
  62. defer resp.Body.Close()
  63. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  64. body, err := io.ReadAll(resp.Body)
  65. Expect(err).To(Succeed())
  66. Expect(string(body)).To(Equal("Too Many Requests\n"))
  67. })
  68. It("block more multiple requests", func() {
  69. resp, err := client.Get(srv.URL + "/")
  70. Expect(err).To(Succeed())
  71. resp.Body.Close()
  72. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  73. resp, err = client.Get(srv.URL + "/")
  74. Expect(err).To(Succeed())
  75. resp.Body.Close()
  76. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  77. resp, err = client.Get(srv.URL + "/")
  78. Expect(err).To(Succeed())
  79. resp.Body.Close()
  80. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  81. resp, err = client.Get(srv.URL + "/")
  82. Expect(err).To(Succeed())
  83. resp.Body.Close()
  84. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  85. resp, err = client.Get(srv.URL + "/")
  86. Expect(err).To(Succeed())
  87. defer resp.Body.Close()
  88. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  89. body, err := io.ReadAll(resp.Body)
  90. Expect(err).To(Succeed())
  91. Expect(string(body)).To(Equal("Too Many Requests\n"))
  92. })
  93. It("clean requests data in memory", func() {
  94. resp, err := client.Get(srv.URL + "/")
  95. Expect(err).To(Succeed())
  96. resp.Body.Close()
  97. servlimit.MRequests.Cleanup()
  98. resp, err = client.Get(srv.URL + "/")
  99. Expect(err).To(Succeed())
  100. defer resp.Body.Close()
  101. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  102. body, err := io.ReadAll(resp.Body)
  103. Expect(err).To(Succeed())
  104. Expect(string(body)).To(Equal("Index"))
  105. })
  106. It("process 3 requests per second", func() {
  107. srv.Close()
  108. srv = httptest.NewServer(servlimit.ReqPerSecond(getTestHandler(), 3))
  109. client = srv.Client()
  110. resp, err := client.Get(srv.URL + "/")
  111. Expect(err).To(Succeed())
  112. resp.Body.Close()
  113. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  114. resp, err = client.Get(srv.URL + "/")
  115. Expect(err).To(Succeed())
  116. resp.Body.Close()
  117. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  118. resp, err = client.Get(srv.URL + "/")
  119. Expect(err).To(Succeed())
  120. resp.Body.Close()
  121. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  122. resp, err = client.Get(srv.URL + "/")
  123. Expect(err).To(Succeed())
  124. defer resp.Body.Close()
  125. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  126. body, err := io.ReadAll(resp.Body)
  127. Expect(err).To(Succeed())
  128. Expect(string(body)).To(Equal("Too Many Requests\n"))
  129. })
  130. })
  131. })
  132. func TestSuite(t *testing.T) {
  133. RegisterFailHandler(Fail)
  134. RunSpecs(t, "servlimit")
  135. }