fix(tests): Fix tests and documentation: adjust test server routes and expectations, add timeout/fallback routes, and refresh README

This commit is contained in:
2025-10-20 13:41:03 +00:00
parent 54afcc46e2
commit c9c35169fc
6 changed files with 663 additions and 478 deletions

View File

@@ -48,6 +48,25 @@ tap.test('setup test server for v4 tests', async () => {
}),
);
// Route that always fails with 500 (for fallback testing)
testServer.addRoute(
'/always-fails',
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(500);
res.end();
}),
);
// Route that takes a long time to respond (for timeout testing)
testServer.addRoute(
'/slow',
new typedserver.servertools.Handler('GET', async (req, res) => {
await new Promise((resolve) => setTimeout(resolve, 5000));
res.status(200);
res.send({ data: 'slow response' });
}),
);
// Route that returns 304 when ETag matches
testServer.addRoute(
'/conditional',
@@ -162,7 +181,7 @@ tap.test('should retry failed requests', async () => {
// Test 7: Fallback URLs
tap.test('should support fallback URLs', async () => {
const response = await webrequest('http://localhost:2346/nonexistent', {
const response = await webrequest('http://localhost:2346/always-fails', {
fallbackUrls: ['http://localhost:2346/dynamic'],
retry: {
maxAttempts: 2
@@ -251,8 +270,8 @@ tap.test('should deduplicate simultaneous requests', async () => {
// Test 12: Timeout
tap.test('should support timeout', async () => {
try {
await webrequest('http://localhost:2346/dynamic', {
timeout: 1 // 1ms timeout should fail
await webrequest('http://localhost:2346/slow', {
timeout: 100 // 100ms timeout should fail (route takes 5000ms)
});
throw new Error('Should have timed out');
} catch (error) {
@@ -289,21 +308,6 @@ tap.test('should clear cache', async () => {
expect(response.ok).toEqual(true);
});
// Test 15: Backward compatibility with WebRequest class
tap.test('should maintain backward compatibility with v3 API', async () => {
const { WebRequest } = await import('../ts/index.js');
const client = new WebRequest({ logging: false });
const data = await client.getJson('http://localhost:2346/dynamic');
expect(data).toHaveProperty('data');
// Test POST
const postData = await client.postJson('http://localhost:2346/post', {
test: 'data'
});
expect(postData).toHaveProperty('received');
});
// Cleanup
tap.test('stop test server', async () => {
await testServer.stop();