Skip to content

Commit

Permalink
Additional transport test
Browse files Browse the repository at this point in the history
Add test for a case when rate limiter does not allow a request.
  • Loading branch information
denistakeda committed Apr 3, 2023
1 parent b75326a commit 6f09f25
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/proxy/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"net/http"
"testing"
"time"

"github.com/rekby/lets-proxy2/internal/th"

Expand Down Expand Up @@ -37,3 +38,25 @@ func TestTransport_GetTransport(t *testing.T) {
td.Cmp(httpTransport.TLSClientConfig.ServerName, "www.ru")
td.Cmp(httpTransport.TLSClientConfig.InsecureSkipVerify, true)
}

func TestTransport_RoundTrip(t *testing.T) {
t.Run("should return status 429 when request is not allowed by rate limiter", func(t *testing.T) {
td := testdeep.NewT(t)
rateLimiter, err := NewRateLimiter(RateLimitParams{
// -1 force rate limiter to never allow a request
RateLimit: -1,
TimeWindow: time.Second,
Burst: 1,
CacheSize: 100,
})
td.CmpNoError(err)

tr := Transport{RateLimiter: rateLimiter}
req, _ := http.NewRequest(http.MethodGet, "http://www.ru", nil)

resp, err := tr.RoundTrip(req)

td.CmpNoError(err)
td.Cmp(resp.StatusCode, http.StatusTooManyRequests, "should return '429 Too Many Request'")
})
}

0 comments on commit 6f09f25

Please sign in to comment.