middleware_test.go109 lines, 3.5 KB
| 1 | package ratelimit |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/netip" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Requests go through the middleware, and we read the status and the headers of every answer. |
| 14 | func TestLimit(t *testing.T) { |
| 15 | l, clock := limiter(2, 30) // two at once, and then one every two seconds |
| 16 | key := func(*http.Request) string { return "ann" } |
| 17 | h := Limit(l, key, func(w http.ResponseWriter, r *http.Request) { |
| 18 | w.WriteHeader(http.StatusNoContent) |
| 19 | }) |
| 20 | steps := []struct { |
| 21 | wait time.Duration |
| 22 | status int |
| 23 | left string // the RateLimit header |
| 24 | retry string // the Retry-After header |
| 25 | }{ |
| 26 | {0, 204, `"test";a=1`, ""}, |
| 27 | {0, 204, `"test";a=0`, ""}, |
| 28 | {0, 429, `"test";a=0`, "2"}, |
| 29 | {500 * time.Millisecond, 429, `"test";a=0`, "2"}, // a second and a half, rounded up |
| 30 | {time.Second, 429, `"test";a=0`, "1"}, |
| 31 | {500 * time.Millisecond, 204, `"test";a=0`, ""}, |
| 32 | } |
| 33 | for i, s := range steps { |
| 34 | *clock = clock.Add(s.wait) |
| 35 | rec := httptest.NewRecorder() |
| 36 | h(rec, httptest.NewRequest("GET", "/feed", nil)) |
| 37 | head := rec.Header() |
| 38 | if rec.Code != s.status || head.Get("RateLimit") != s.left || head.Get("Retry-After") != s.retry { |
| 39 | t.Fatalf("step %d: status %d, RateLimit %q, Retry-After %q", i+1, |
| 40 | rec.Code, head.Get("RateLimit"), head.Get("Retry-After")) |
| 41 | } |
| 42 | if got := head.Get("RateLimit-Policy"); got != `"test";q=30;w=60` { |
| 43 | t.Fatalf("step %d: RateLimit-Policy %q", i+1, got) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestByIP(t *testing.T) { |
| 49 | for addr, want := range map[string]string{ |
| 50 | "203.0.113.7:51234": "203.0.113.7", |
| 51 | "[2001:db8::7]:51234": "2001:db8::7", |
| 52 | } { |
| 53 | r := httptest.NewRequest("GET", "/feed", nil) |
| 54 | r.RemoteAddr = addr |
| 55 | if got := ByIP(r); got != want { |
| 56 | t.Errorf("ByIP(%q) = %q, want %q", addr, got, want) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Who is the client? It depends on who we hear it from. |
| 62 | func TestProxiesByIP(t *testing.T) { |
| 63 | ours := Proxies{netip.MustParsePrefix("127.0.0.1/32"), netip.MustParsePrefix("10.0.0.0/8")} |
| 64 | tests := []struct { |
| 65 | name string |
| 66 | peer string // where the connection comes from |
| 67 | forwarded string // the X-Forwarded-For header |
| 68 | want string |
| 69 | }{ |
| 70 | {"a stranger is never believed", "198.51.100.9:4000", "203.0.113.7", "198.51.100.9"}, |
| 71 | {"our proxy is", "127.0.0.1:4000", "203.0.113.7", "203.0.113.7"}, |
| 72 | {"what the client wrote is ignored", "127.0.0.1:4000", "1.2.3.4, 203.0.113.7", "203.0.113.7"}, |
| 73 | {"two proxies of ours in a row", "127.0.0.1:4000", "1.2.3.4, 203.0.113.7, 10.1.2.3", "203.0.113.7"}, |
| 74 | {"an IPv6 client", "127.0.0.1:4000", "2001:db8::7", "2001:db8::7"}, |
| 75 | {"our proxy without the header", "127.0.0.1:4000", "", "127.0.0.1"}, |
| 76 | } |
| 77 | for _, tt := range tests { |
| 78 | t.Run(tt.name, func(t *testing.T) { |
| 79 | r := httptest.NewRequest("GET", "/feed", nil) |
| 80 | r.RemoteAddr = tt.peer |
| 81 | if tt.forwarded != "" { |
| 82 | r.Header.Set("X-Forwarded-For", tt.forwarded) |
| 83 | } |
| 84 | if got := ours.ByIP(r); got != tt.want { |
| 85 | t.Errorf("got %q, want %q", got, tt.want) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // away is a limiter that can't answer, like Redis when it is stopped. |
| 92 | type away struct{ Allower } |
| 93 | |
| 94 | func (away) Allow(context.Context, string) (Result, error) { |
| 95 | return Result{}, errors.New("redis is away") |
| 96 | } |
| 97 | |
| 98 | // A limiter that can't answer, and has nothing to fall back on, lets nobody in. |
| 99 | func TestLimitFailsClosed(t *testing.T) { |
| 100 | l, _ := limiter(2, 30) |
| 101 | h := Limit(away{l}, ByIP, func(w http.ResponseWriter, r *http.Request) { |
| 102 | t.Fatal("the request got through") |
| 103 | }) |
| 104 | rec := httptest.NewRecorder() |
| 105 | h(rec, httptest.NewRequest("GET", "/feed", nil)) |
| 106 | if rec.Code != http.StatusServiceUnavailable { |
| 107 | t.Fatalf("status %d, want 503", rec.Code) |
| 108 | } |
| 109 | } |