fix(client): Fix CI configuration, prevent socket hangs with auto-drain, and apply various client/core TypeScript fixes and test updates

This commit is contained in:
2025-08-18 00:21:14 +00:00
parent 9b9c8fd618
commit ee750dea58
34 changed files with 2144 additions and 892 deletions

View File

@@ -6,13 +6,15 @@ import { CoreRequest, CoreResponse } from '../ts/core/index.js';
import type { ICoreRequestOptions } from '../ts/core_base/types.js';
tap.test('browser: should request a JSON document over https', async () => {
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts/1');
const request = new CoreRequest(
'https://jsonplaceholder.typicode.com/posts/1',
);
const response = await request.fire();
expect(response).not.toBeNull();
expect(response).toHaveProperty('status');
expect(response.status).toEqual(200);
const data = await response.json();
expect(data).toHaveProperty('id');
expect(data.id).toEqual(1);
@@ -22,16 +24,19 @@ tap.test('browser: should request a JSON document over https', async () => {
tap.test('browser: should handle CORS requests', async () => {
const options: ICoreRequestOptions = {
headers: {
'Accept': 'application/vnd.github.v3+json'
}
Accept: 'application/vnd.github.v3+json',
},
};
const request = new CoreRequest('https://api.github.com/users/github', options);
const request = new CoreRequest(
'https://api.github.com/users/github',
options,
);
const response = await request.fire();
expect(response).not.toBeNull();
expect(response.status).toEqual(200);
const data = await response.json();
expect(data).toHaveProperty('login');
expect(data.login).toEqual('github');
@@ -39,21 +44,24 @@ tap.test('browser: should handle CORS requests', async () => {
tap.test('browser: should handle request timeouts', async () => {
let timedOut = false;
const options: ICoreRequestOptions = {
timeout: 1 // Extremely short timeout to guarantee failure
timeout: 1, // Extremely short timeout to guarantee failure
};
try {
// Use a URL that will definitely take longer than 1ms
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts/1', options);
const request = new CoreRequest(
'https://jsonplaceholder.typicode.com/posts/1',
options,
);
await request.fire();
} catch (error) {
timedOut = true;
// Accept any error since different browsers handle timeouts differently
expect(error).toBeDefined();
}
expect(timedOut).toEqual(true);
});
@@ -61,19 +69,22 @@ tap.test('browser: should handle POST requests with JSON', async () => {
const testData = {
title: 'foo',
body: 'bar',
userId: 1
userId: 1,
};
const options: ICoreRequestOptions = {
method: 'POST',
requestBody: testData
requestBody: testData,
};
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts', options);
const request = new CoreRequest(
'https://jsonplaceholder.typicode.com/posts',
options,
);
const response = await request.fire();
expect(response.status).toEqual(201);
const responseData = await response.json();
expect(responseData).toHaveProperty('id');
expect(responseData.title).toEqual(testData.title);
@@ -84,15 +95,18 @@ tap.test('browser: should handle POST requests with JSON', async () => {
tap.test('browser: should handle query parameters', async () => {
const options: ICoreRequestOptions = {
queryParams: {
userId: '2'
}
userId: '2',
},
};
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts', options);
const request = new CoreRequest(
'https://jsonplaceholder.typicode.com/posts',
options,
);
const response = await request.fire();
expect(response.status).toEqual(200);
const data = await response.json();
expect(Array.isArray(data)).toBeTrue();
// Verify we got posts filtered by userId 2
@@ -102,4 +116,4 @@ tap.test('browser: should handle query parameters', async () => {
}
});
export default tap.start();
export default tap.start();

View File

@@ -51,7 +51,10 @@ tap.test('client: should set headers correctly', async () => {
// Check if the header exists (headers might be lowercase)
const headers = body.headers;
const headerFound = headers[customHeader] || headers[customHeader.toLowerCase()] || headers['x-custom-header'];
const headerFound =
headers[customHeader] ||
headers[customHeader.toLowerCase()] ||
headers['x-custom-header'];
expect(headerFound).toEqual(headerValue);
});
@@ -81,7 +84,7 @@ tap.test('client: should handle timeout configuration', async () => {
const response = await client.get();
expect(response).toHaveProperty('ok');
expect(response.ok).toBeTrue();
// Consume the body to prevent socket hanging
await response.text();
});
@@ -95,34 +98,40 @@ tap.test('client: should handle retry configuration', async () => {
const response = await client.get();
expect(response).toHaveProperty('ok');
expect(response.ok).toBeTrue();
// Consume the body to prevent socket hanging
await response.text();
});
tap.test('client: should support keepAlive option for connection reuse', async () => {
// Simple test
const response = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.options({ keepAlive: true })
.get();
expect(response.ok).toBeTrue();
await response.text();
});
tap.test(
'client: should support keepAlive option for connection reuse',
async () => {
// Simple test
const response = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.options({ keepAlive: true })
.get();
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();
expect(response.ok).toBeTrue();
await response.text();
},
);
const response = await client.get();
expect(response.status).toEqual(200);
// Consume the body to prevent socket hanging
await response.text();
});
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);
// Consume the body to prevent socket hanging
await response.text();
},
);
tap.test('client: should handle 429 with custom config', async () => {
let rateLimitCallbackCalled = false;
@@ -139,65 +148,74 @@ tap.test('client: should handle 429 with custom config', async () => {
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();
// Consume the body to prevent socket hanging
await response.text();
});
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
});
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();
// Consume the body to prevent socket hanging
await response.text();
});
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
});
// Consume the body to prevent socket hanging
await response.text();
},
);
const response = await client.get();
expect(response.status).toEqual(200);
// Consume the body to prevent socket hanging
await response.text();
});
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,
});
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(200);
const response = await client.get();
expect(response.status).toEqual(404);
expect(response.ok).toBeFalse();
// Consume the body to prevent socket hanging
await response.text();
});
// Consume the body to prevent socket hanging
await response.text();
},
);
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();
// Consume the body to prevent socket hanging
await response.text();
},
);
tap.start();