|
| 1 | +package rest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/didip/tollbooth/v8" |
| 10 | + "github.com/didip/tollbooth/v8/limiter" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestLimitHandler(t *testing.T) { |
| 15 | + |
| 16 | + t.Run("basic request", func(t *testing.T) { |
| 17 | + lmt := tollbooth.NewLimiter(1, nil) |
| 18 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | + w.WriteHeader(http.StatusOK) |
| 20 | + }) |
| 21 | + wrapped := LimitHandler(lmt)(handler) |
| 22 | + w := httptest.NewRecorder() |
| 23 | + r := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 24 | + r.RemoteAddr = "127.0.0.1:12345" |
| 25 | + wrapped.ServeHTTP(w, r) |
| 26 | + assert.Equal(t, http.StatusOK, w.Code) |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("rate limit exceeded", func(t *testing.T) { |
| 30 | + lmt := tollbooth.NewLimiter(0.1, nil) // only allow one request per 10 seconds |
| 31 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 32 | + w.WriteHeader(http.StatusOK) |
| 33 | + }) |
| 34 | + wrapped := LimitHandler(lmt)(handler) |
| 35 | + |
| 36 | + // first request |
| 37 | + w1 := httptest.NewRecorder() |
| 38 | + r1 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 39 | + r1.RemoteAddr = "127.0.0.1:12345" |
| 40 | + wrapped.ServeHTTP(w1, r1) |
| 41 | + |
| 42 | + // immediate second request should fail |
| 43 | + w2 := httptest.NewRecorder() |
| 44 | + r2 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 45 | + r2.RemoteAddr = "127.0.0.1:12345" |
| 46 | + wrapped.ServeHTTP(w2, r2) |
| 47 | + |
| 48 | + assert.Equal(t, http.StatusTooManyRequests, w2.Code) |
| 49 | + assert.Contains(t, w2.Body.String(), "maximum request limit") |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("context cancelled", func(t *testing.T) { |
| 53 | + lmt := tollbooth.NewLimiter(1, nil) |
| 54 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + w.WriteHeader(http.StatusOK) |
| 56 | + }) |
| 57 | + wrapped := LimitHandler(lmt)(handler) |
| 58 | + w := httptest.NewRecorder() |
| 59 | + r := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 60 | + ctx, cancel := context.WithCancel(r.Context()) |
| 61 | + cancel() |
| 62 | + r = r.WithContext(ctx) |
| 63 | + wrapped.ServeHTTP(w, r) |
| 64 | + assert.Equal(t, http.StatusServiceUnavailable, w.Code) |
| 65 | + assert.Contains(t, w.Body.String(), "Context was canceled") |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("custom error handler", func(t *testing.T) { |
| 69 | + lmt := tollbooth.NewLimiter(0.1, nil) // only allow one request per 10 seconds |
| 70 | + customMsg := "custom limit reached" |
| 71 | + lmt.SetMessage(customMsg) |
| 72 | + |
| 73 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + w.WriteHeader(http.StatusOK) |
| 75 | + }) |
| 76 | + wrapped := LimitHandler(lmt)(handler) |
| 77 | + |
| 78 | + // first request |
| 79 | + w1 := httptest.NewRecorder() |
| 80 | + r1 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 81 | + r1.RemoteAddr = "127.0.0.1:12345" |
| 82 | + wrapped.ServeHTTP(w1, r1) |
| 83 | + |
| 84 | + // immediate second request should fail |
| 85 | + w2 := httptest.NewRecorder() |
| 86 | + r2 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 87 | + r2.RemoteAddr = "127.0.0.1:12345" |
| 88 | + wrapped.ServeHTTP(w2, r2) |
| 89 | + |
| 90 | + assert.Equal(t, http.StatusTooManyRequests, w2.Code) |
| 91 | + assert.Contains(t, w2.Body.String(), customMsg) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("default IP lookup", func(t *testing.T) { |
| 95 | + lmt := tollbooth.NewLimiter(0.1, nil) |
| 96 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 | + w.WriteHeader(http.StatusOK) |
| 98 | + }) |
| 99 | + wrapped := LimitHandler(lmt)(handler) |
| 100 | + |
| 101 | + // first request |
| 102 | + w1 := httptest.NewRecorder() |
| 103 | + r1 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 104 | + r1.RemoteAddr = "127.0.0.1:12345" |
| 105 | + wrapped.ServeHTTP(w1, r1) |
| 106 | + |
| 107 | + // second request should fail as default RemoteAddr will be used |
| 108 | + w2 := httptest.NewRecorder() |
| 109 | + r2 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 110 | + r2.RemoteAddr = "127.0.0.1:12345" |
| 111 | + wrapped.ServeHTTP(w2, r2) |
| 112 | + |
| 113 | + assert.Equal(t, http.StatusTooManyRequests, w2.Code) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("custom IP lookup", func(t *testing.T) { |
| 117 | + lmt := tollbooth.NewLimiter(0.1, nil) |
| 118 | + lmt.SetIPLookup(limiter.IPLookup{Name: "X-Real-IP"}) |
| 119 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 | + w.WriteHeader(http.StatusOK) |
| 121 | + }) |
| 122 | + wrapped := LimitHandler(lmt)(handler) |
| 123 | + |
| 124 | + // first request |
| 125 | + w1 := httptest.NewRecorder() |
| 126 | + r1 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 127 | + r1.Header.Set("X-Real-IP", "5.5.5.5") |
| 128 | + wrapped.ServeHTTP(w1, r1) |
| 129 | + |
| 130 | + // second request with same X-Real-IP should fail |
| 131 | + w2 := httptest.NewRecorder() |
| 132 | + r2 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 133 | + r2.Header.Set("X-Real-IP", "5.5.5.5") |
| 134 | + wrapped.ServeHTTP(w2, r2) |
| 135 | + |
| 136 | + assert.Equal(t, http.StatusTooManyRequests, w2.Code) |
| 137 | + |
| 138 | + // request with different X-Real-IP should pass |
| 139 | + w3 := httptest.NewRecorder() |
| 140 | + r3 := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 141 | + r3.Header.Set("X-Real-IP", "6.6.6.6") |
| 142 | + wrapped.ServeHTTP(w3, r3) |
| 143 | + |
| 144 | + assert.Equal(t, http.StatusOK, w3.Code) |
| 145 | + }) |
| 146 | +} |
0 commit comments