TestServer_RateLimit_HandleMessage tests rate limiting in message handling
(t *testing.T)
| 162 | |
| 163 | // TestServer_RateLimit_HandleMessage tests rate limiting in message handling |
| 164 | func TestServer_RateLimit_HandleMessage(t *testing.T) { |
| 165 | mock := newMockReadWriter() |
| 166 | logger := log.New(io.Discard, "", 0) |
| 167 | server := NewServer(mock.input, mock.output, logger) |
| 168 | |
| 169 | // Create a valid request |
| 170 | req := Request{ |
| 171 | JSONRPC: "2.0", |
| 172 | ID: 1, |
| 173 | Method: "shutdown", |
| 174 | } |
| 175 | msgBytes, err := json.Marshal(req) |
| 176 | if err != nil { |
| 177 | t.Fatalf("failed to marshal request: %v", err) |
| 178 | } |
| 179 | |
| 180 | // Send requests up to the limit |
| 181 | for i := 0; i < RateLimitRequests; i++ { |
| 182 | server.handleMessage(msgBytes) |
| 183 | } |
| 184 | |
| 185 | // Clear output buffer to check next response |
| 186 | mock.output.Reset() |
| 187 | |
| 188 | // Next request should be rate limited |
| 189 | server.handleMessage(msgBytes) |
| 190 | |
| 191 | // Check that a rate limit error response was sent |
| 192 | output := mock.output.String() |
| 193 | if output != "" { |
| 194 | // Rate limited requests should get an error response |
| 195 | if !contains(output, "RequestCancelled") && !contains(output, "rate limit") { |
| 196 | t.Logf("Output after rate limit: %s", output) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // TestServer_RateLimit_MultipleWindows tests behavior across multiple windows |
| 202 | func TestServer_RateLimit_MultipleWindows(t *testing.T) { |
nothing calls this directly
no test coverage detected