|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +const testRemoteAddr = "1.2.3.4:1234" |
| 14 | + |
| 15 | +func TestConnLimit_AllowsWithinLimit(t *testing.T) { |
| 16 | + handler := connLimit(2, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + w.WriteHeader(http.StatusOK) |
| 18 | + })) |
| 19 | + |
| 20 | + req := httptest.NewRequest("GET", "/", nil) |
| 21 | + w := httptest.NewRecorder() |
| 22 | + handler.ServeHTTP(w, req) |
| 23 | + assert.Equal(t, http.StatusOK, w.Code) |
| 24 | +} |
| 25 | + |
| 26 | +func TestConnLimit_RejectsOverLimit(t *testing.T) { |
| 27 | + blocked := make(chan struct{}) |
| 28 | + released := make(chan struct{}) |
| 29 | + handler := connLimit(1, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 | + if r.Header.Get("X-Block") == "true" { |
| 31 | + close(blocked) |
| 32 | + <-released |
| 33 | + } |
| 34 | + w.WriteHeader(http.StatusOK) |
| 35 | + })) |
| 36 | + |
| 37 | + var wg sync.WaitGroup |
| 38 | + wg.Add(1) |
| 39 | + go func() { |
| 40 | + defer wg.Done() |
| 41 | + req := httptest.NewRequest("GET", "/", nil) |
| 42 | + req.Header.Set("X-Block", "true") |
| 43 | + w := httptest.NewRecorder() |
| 44 | + handler.ServeHTTP(w, req) |
| 45 | + }() |
| 46 | + |
| 47 | + <-blocked |
| 48 | + |
| 49 | + req := httptest.NewRequest("GET", "/", nil) |
| 50 | + w := httptest.NewRecorder() |
| 51 | + handler.ServeHTTP(w, req) |
| 52 | + assert.Equal(t, http.StatusServiceUnavailable, w.Code) |
| 53 | + |
| 54 | + close(released) |
| 55 | + wg.Wait() |
| 56 | +} |
| 57 | + |
| 58 | +func TestConnLimit_ReleasesSlotAfterRequest(t *testing.T) { |
| 59 | + handler := connLimit(1, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + })) |
| 62 | + |
| 63 | + req := httptest.NewRequest("GET", "/", nil) |
| 64 | + w := httptest.NewRecorder() |
| 65 | + handler.ServeHTTP(w, req) |
| 66 | + require.Equal(t, http.StatusOK, w.Code) |
| 67 | + |
| 68 | + w = httptest.NewRecorder() |
| 69 | + handler.ServeHTTP(w, req) |
| 70 | + assert.Equal(t, http.StatusOK, w.Code) |
| 71 | +} |
| 72 | + |
| 73 | +func TestRateLimit_AllowsWithinLimit(t *testing.T) { |
| 74 | + handler := rateLimit(100, 10, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 75 | + w.WriteHeader(http.StatusOK) |
| 76 | + })) |
| 77 | + |
| 78 | + req := httptest.NewRequest("GET", "/", nil) |
| 79 | + req.RemoteAddr = testRemoteAddr |
| 80 | + w := httptest.NewRecorder() |
| 81 | + handler.ServeHTTP(w, req) |
| 82 | + assert.Equal(t, http.StatusOK, w.Code) |
| 83 | +} |
| 84 | + |
| 85 | +func TestRateLimit_RejectsOverBurst(t *testing.T) { |
| 86 | + handler := rateLimit(1, 3, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 87 | + w.WriteHeader(http.StatusOK) |
| 88 | + })) |
| 89 | + |
| 90 | + req := httptest.NewRequest("GET", "/", nil) |
| 91 | + req.RemoteAddr = testRemoteAddr |
| 92 | + |
| 93 | + for i := 0; i < 3; i++ { |
| 94 | + w := httptest.NewRecorder() |
| 95 | + handler.ServeHTTP(w, req) |
| 96 | + assert.Equal(t, http.StatusOK, w.Code, "request %d should succeed", i) |
| 97 | + } |
| 98 | + |
| 99 | + w := httptest.NewRecorder() |
| 100 | + handler.ServeHTTP(w, req) |
| 101 | + assert.Equal(t, http.StatusTooManyRequests, w.Code) |
| 102 | +} |
| 103 | + |
| 104 | +func TestRateLimit_DifferentIPsIndependent(t *testing.T) { |
| 105 | + handler := rateLimit(1, 1, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 106 | + w.WriteHeader(http.StatusOK) |
| 107 | + })) |
| 108 | + |
| 109 | + req1 := httptest.NewRequest("GET", "/", nil) |
| 110 | + req1.RemoteAddr = testRemoteAddr |
| 111 | + w := httptest.NewRecorder() |
| 112 | + handler.ServeHTTP(w, req1) |
| 113 | + assert.Equal(t, http.StatusOK, w.Code) |
| 114 | + |
| 115 | + w = httptest.NewRecorder() |
| 116 | + handler.ServeHTTP(w, req1) |
| 117 | + assert.Equal(t, http.StatusTooManyRequests, w.Code) |
| 118 | + |
| 119 | + req2 := httptest.NewRequest("GET", "/", nil) |
| 120 | + req2.RemoteAddr = "5.6.7.8:5678" |
| 121 | + w = httptest.NewRecorder() |
| 122 | + handler.ServeHTTP(w, req2) |
| 123 | + assert.Equal(t, http.StatusOK, w.Code) |
| 124 | +} |
| 125 | + |
| 126 | +func TestExtractIP(t *testing.T) { |
| 127 | + tests := []struct { |
| 128 | + remoteAddr string |
| 129 | + expected string |
| 130 | + }{ |
| 131 | + {testRemoteAddr, "1.2.3.4"}, |
| 132 | + {"[::1]:1234", "::1"}, |
| 133 | + {"1.2.3.4", "1.2.3.4"}, |
| 134 | + } |
| 135 | + for _, tt := range tests { |
| 136 | + r := &http.Request{RemoteAddr: tt.remoteAddr} |
| 137 | + assert.Equal(t, tt.expected, extractIP(r)) |
| 138 | + } |
| 139 | +} |
0 commit comments