-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter_test.go
96 lines (81 loc) · 2.73 KB
/
ratelimiter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// A simple rate limiter middleware.
// Copyright (c) 2020. Tamás Demeter-Haludka
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ratelimiter_test
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/tamasd/ratelimiter"
)
func TestMiddleware(t *testing.T) {
mw := ratelimiter.New(ratelimiter.CreateMiddlewareConfig(1))
w := httptest.NewRecorder()
called := false
mw.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil), func(w http.ResponseWriter, r *http.Request) {
called = true
})
require.True(t, called)
called = false
mw.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil), func(w http.ResponseWriter, r *http.Request) {
called = true
})
resp := w.Result()
require.False(t, called)
require.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
}
func TestChannelMiddleware(t *testing.T) {
mw := ratelimiter.NewChannel(ratelimiter.CreateMiddlewareConfig(10))
go mw.Start()
t.Cleanup(func() {
mw.Stop()
})
for i := 0; i < 10; i++ {
require.Equal(t, http.StatusOK, testResponseCode(mw))
}
require.Equal(t, http.StatusTooManyRequests, testResponseCode(mw))
// Make sure that enough time passes for a tick, even if the CPU is busy.
<-time.After(time.Second / 5)
require.Equal(t, http.StatusOK, testResponseCode(mw))
}
func testResponseCode(mw *ratelimiter.Middleware) int {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
mw.ServeHTTP(w, r, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
return w.Result().StatusCode
}
func TestDelay(t *testing.T) {
config := ratelimiter.CreateMiddlewareConfig(0)
(&config).SetRetryDelay(1000, 10)
mw := ratelimiter.NewMutex(config)
require.InDelta(t, 1000, testRetryAfterHeader(mw), 10)
}
func testRetryAfterHeader(mw *ratelimiter.Middleware) int {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
mw.ServeHTTP(w, r, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
code, err := strconv.Atoi(w.Result().Header.Get("Retry-After"))
if err != nil {
panic(err)
}
return code
}