69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
|
import { SmartRequest } from '../ts/index.js';
|
|
import { Readable } from 'stream';
|
|
|
|
tap.test('should have stream() method that returns web ReadableStream', async () => {
|
|
const response = await SmartRequest.create()
|
|
.url('https://httpbin.org/get')
|
|
.get();
|
|
|
|
// Verify stream() method exists
|
|
expect(response.stream).toBeDefined();
|
|
expect(typeof response.stream).toEqual('function');
|
|
|
|
// Get web stream
|
|
const webStream = response.stream();
|
|
expect(webStream).toBeDefined();
|
|
|
|
// Verify it's a web ReadableStream
|
|
expect(typeof webStream.getReader).toEqual('function');
|
|
expect(typeof webStream.cancel).toEqual('function');
|
|
|
|
// Convert to Node.js stream using Readable.fromWeb()
|
|
// Known TypeScript limitation: @types/node ReadableStream differs from web ReadableStream
|
|
const nodeStream = Readable.fromWeb(webStream as any);
|
|
expect(nodeStream).toBeDefined();
|
|
|
|
// Verify it's a Node.js readable stream
|
|
expect(typeof nodeStream.pipe).toEqual('function');
|
|
expect(typeof nodeStream.on).toEqual('function');
|
|
|
|
// Consume the stream to avoid hanging
|
|
nodeStream.resume();
|
|
});
|
|
|
|
tap.test('should convert web stream to Node.js stream correctly', async () => {
|
|
const response = await SmartRequest.create()
|
|
.url('https://httpbin.org/get')
|
|
.get();
|
|
|
|
const webStream = response.stream();
|
|
const nodeStream = Readable.fromWeb(webStream as any);
|
|
|
|
// Collect data from stream
|
|
const chunks: Buffer[] = [];
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
nodeStream.on('data', (chunk) => {
|
|
chunks.push(chunk);
|
|
});
|
|
|
|
nodeStream.on('end', () => {
|
|
resolve();
|
|
});
|
|
|
|
nodeStream.on('error', reject);
|
|
});
|
|
|
|
// Verify we received data
|
|
const data = Buffer.concat(chunks);
|
|
expect(data.length).toBeGreaterThan(0);
|
|
|
|
// Verify it's valid JSON
|
|
const json = JSON.parse(data.toString('utf-8'));
|
|
expect(json).toBeDefined();
|
|
expect(json.url).toEqual('https://httpbin.org/get');
|
|
});
|
|
|
|
export default tap.start();
|