fix(core): Improve streaming support and timeout handling; add browser streaming & timeout tests and README clarifications

This commit is contained in:
2025-08-19 01:36:44 +00:00
parent 35867d9148
commit 361d97f440
8 changed files with 181 additions and 11 deletions

View File

@@ -0,0 +1,41 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { SmartRequest } from '../ts/index.js';
tap.test('browser: should send Uint8Array using buffer() method', async () => {
const testData = new Uint8Array([72, 101, 108, 108, 111]); // "Hello" in ASCII
const smartRequest = SmartRequest.create()
.url('https://httpbin.org/post')
.buffer(testData, 'application/octet-stream')
.method('POST');
const response = await smartRequest.post();
const data = await response.json();
expect(data).toHaveProperty('data');
expect(data.headers['Content-Type']).toEqual('application/octet-stream');
});
tap.test('browser: should send web ReadableStream using stream() method', async () => {
// Create a web ReadableStream
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode('Test stream data'));
controller.close();
}
});
const smartRequest = SmartRequest.create()
.url('https://httpbin.org/post')
.stream(stream, 'text/plain')
.method('POST');
const response = await smartRequest.post();
const data = await response.json();
expect(data).toHaveProperty('data');
// httpbin should receive the streamed data
});
export default tap.start();

60
test/test.timeout.ts Normal file
View File

@@ -0,0 +1,60 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { SmartRequest } from '../ts/index.js';
tap.test('should clear timeout when request completes before timeout', async () => {
// Set a long timeout that would keep the process alive if not cleared
const response = await SmartRequest.create()
.url('https://httpbin.org/delay/1') // 1 second delay
.timeout(10000) // 10 second timeout (much longer than needed)
.get();
const data = await response.json();
expect(data).toBeDefined();
// The test should complete quickly, not wait for the 10 second timeout
// If the timeout isn't cleared, the process would hang for 10 seconds
});
tap.test('should timeout when request takes longer than timeout', async () => {
let errorThrown = false;
try {
// Try to fetch with a very short timeout
await SmartRequest.create()
.url('https://httpbin.org/delay/3') // 3 second delay
.timeout(100) // 100ms timeout (will fail)
.get();
} catch (error) {
errorThrown = true;
expect(error.message).toContain('Request timed out');
}
expect(errorThrown).toBeTrue();
});
tap.test('should not leak timers with multiple successful requests', async () => {
// Make multiple requests with timeouts to ensure no timer leaks
const promises = [];
for (let i = 0; i < 5; i++) {
promises.push(
SmartRequest.create()
.url('https://httpbin.org/get')
.timeout(5000) // 5 second timeout
.get()
.then(response => response.json())
);
}
const results = await Promise.all(promises);
// All requests should complete successfully
expect(results).toHaveLength(5);
results.forEach(result => {
expect(result).toBeDefined();
});
// Process should exit cleanly after this test without hanging
});
export default tap.start();