servlimit_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  38. body, err := io.ReadAll(resp.Body)
  39. Expect(err).To(Succeed())
  40. Expect(string(body)).To(Equal("Index"))
  41. })
  42. It("process multiple requests", func() {
  43. resp, err := client.Get(srv.URL + "/")
  44. Expect(err).To(Succeed())
  45. Expect(resp.Body.Close()).To(Succeed())
  46. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  47. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  48. time.Sleep(1 * time.Second)
  49. resp, err = client.Get(srv.URL + "/")
  50. Expect(err).To(Succeed())
  51. defer resp.Body.Close()
  52. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  53. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  54. body, err := io.ReadAll(resp.Body)
  55. Expect(err).To(Succeed())
  56. Expect(string(body)).To(Equal("Index"))
  57. })
  58. It("block multiple requests", func() {
  59. resp, err := client.Get(srv.URL + "/")
  60. Expect(err).To(Succeed())
  61. Expect(resp.Body.Close()).To(Succeed())
  62. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  63. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  64. resp, err = client.Get(srv.URL + "/")
  65. Expect(err).To(Succeed())
  66. defer resp.Body.Close()
  67. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  68. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  69. body, err := io.ReadAll(resp.Body)
  70. Expect(err).To(Succeed())
  71. Expect(string(body)).To(Equal("Too Many Requests\n"))
  72. })
  73. It("block more multiple requests", func() {
  74. resp, err := client.Get(srv.URL + "/")
  75. Expect(err).To(Succeed())
  76. Expect(resp.Body.Close()).To(Succeed())
  77. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  78. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  79. resp, err = client.Get(srv.URL + "/")
  80. Expect(err).To(Succeed())
  81. Expect(resp.Body.Close()).To(Succeed())
  82. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  83. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  84. resp, err = client.Get(srv.URL + "/")
  85. Expect(err).To(Succeed())
  86. Expect(resp.Body.Close()).To(Succeed())
  87. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  88. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  89. resp, err = client.Get(srv.URL + "/")
  90. Expect(err).To(Succeed())
  91. Expect(resp.Body.Close()).To(Succeed())
  92. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  93. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  94. resp, err = client.Get(srv.URL + "/")
  95. Expect(err).To(Succeed())
  96. defer resp.Body.Close()
  97. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  98. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  99. body, err := io.ReadAll(resp.Body)
  100. Expect(err).To(Succeed())
  101. Expect(string(body)).To(Equal("Too Many Requests\n"))
  102. })
  103. It("clean requests data in memory", func() {
  104. resp, err := client.Get(srv.URL + "/")
  105. Expect(err).To(Succeed())
  106. Expect(resp.Body.Close()).To(Succeed())
  107. servlimit.MRequests.Cleanup()
  108. resp, err = client.Get(srv.URL + "/")
  109. Expect(err).To(Succeed())
  110. defer resp.Body.Close()
  111. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  112. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  113. body, err := io.ReadAll(resp.Body)
  114. Expect(err).To(Succeed())
  115. Expect(string(body)).To(Equal("Index"))
  116. })
  117. It("process 3 requests per second", func() {
  118. srv.Close()
  119. srv = httptest.NewServer(servlimit.ReqPerSecond(getTestHandler(), 3))
  120. client = srv.Client()
  121. resp, err := client.Get(srv.URL + "/")
  122. Expect(err).To(Succeed())
  123. Expect(resp.Body.Close()).To(Succeed())
  124. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  125. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  126. resp, err = client.Get(srv.URL + "/")
  127. Expect(err).To(Succeed())
  128. Expect(resp.Body.Close()).To(Succeed())
  129. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  130. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  131. resp, err = client.Get(srv.URL + "/")
  132. Expect(err).To(Succeed())
  133. Expect(resp.Body.Close()).To(Succeed())
  134. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  135. Expect(resp.Header.Get("Retry-After")).To(Equal(""))
  136. resp, err = client.Get(srv.URL + "/")
  137. Expect(err).To(Succeed())
  138. defer resp.Body.Close()
  139. Expect(resp.StatusCode).To(Equal(http.StatusTooManyRequests))
  140. Expect(resp.Header.Get("Retry-After")).To(Equal("1"))
  141. body, err := io.ReadAll(resp.Body)
  142. Expect(err).To(Succeed())
  143. Expect(string(body)).To(Equal("Too Many Requests\n"))
  144. })
  145. })
  146. })
  147. func TestSuite(t *testing.T) {
  148. RegisterFailHandler(Fail)
  149. RunSpecs(t, "servlimit")
  150. }