feat(429 handling): now handles 429 correctly

This commit is contained in:
2025-07-29 13:19:43 +00:00
parent cf24bf94b9
commit 4cbca08f43
7 changed files with 266 additions and 19 deletions

View File

@@ -73,7 +73,7 @@ tap.test('client: should handle query parameters', async () => {
tap.test('client: should handle timeout configuration', async () => {
// This test just verifies that the timeout method doesn't throw
const client = SmartRequest.create()
.url('https://httpbin.org/get')
.url('https://jsonplaceholder.typicode.com/posts/1')
.timeout(5000);
const response = await client.get();
@@ -84,7 +84,7 @@ tap.test('client: should handle timeout configuration', async () => {
tap.test('client: should handle retry configuration', async () => {
// This test just verifies that the retry method doesn't throw
const client = SmartRequest.create()
.url('https://httpbin.org/get')
.url('https://jsonplaceholder.typicode.com/posts/1')
.retry(1);
const response = await client.get();
@@ -92,4 +92,111 @@ tap.test('client: should handle retry configuration', async () => {
expect(response.ok).toBeTrue();
});
tap.test('client: should support keepAlive option for connection reuse', async () => {
// Test basic keepAlive functionality
const responses = [];
// Make multiple requests with keepAlive enabled
for (let i = 0; i < 3; i++) {
const response = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.options({ keepAlive: true })
.header('X-Request-Number', String(i))
.get();
expect(response.ok).toBeTrue();
responses.push(response);
}
// Verify all requests succeeded
expect(responses).toHaveLength(3);
// Also test that keepAlive: false works
const responseNoKeepAlive = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/2')
.options({ keepAlive: false })
.get();
expect(responseNoKeepAlive.ok).toBeTrue();
// Verify we can parse the responses
const data = await responses[0].json();
expect(data).toHaveProperty('id');
expect(data.id).toEqual(1);
});
tap.test('client: should handle 429 rate limiting with default config', async () => {
// Test that handle429Backoff can be configured without errors
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff();
const response = await client.get();
expect(response.status).toEqual(200);
});
tap.test('client: should handle 429 with custom config', async () => {
let rateLimitCallbackCalled = false;
let attemptCount = 0;
let waitTimeReceived = 0;
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 2,
fallbackDelay: 500,
maxWaitTime: 5000,
onRateLimit: (attempt, waitTime) => {
rateLimitCallbackCalled = true;
attemptCount = attempt;
waitTimeReceived = waitTime;
}
});
const response = await client.get();
expect(response.status).toEqual(200);
// The callback should not have been called for a 200 response
expect(rateLimitCallbackCalled).toBeFalse();
});
tap.test('client: should respect Retry-After header format (seconds)', async () => {
// Test the configuration works - actual 429 testing would require a mock server
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 1,
respectRetryAfter: true
});
const response = await client.get();
expect(response.ok).toBeTrue();
});
tap.test('client: should handle rate limiting with exponential backoff', async () => {
// Test exponential backoff configuration
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 3,
fallbackDelay: 100,
backoffFactor: 2,
maxWaitTime: 1000
});
const response = await client.get();
expect(response.status).toEqual(200);
});
tap.test('client: should not retry non-429 errors with rate limit handler', async () => {
// Test that 404 errors are not retried by rate limit handler
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/999999')
.handle429Backoff();
const response = await client.get();
expect(response.status).toEqual(404);
expect(response.ok).toBeFalse();
});
tap.start();