feat(client/smartrequest): Add streaming and raw buffer support to SmartRequest (buffer, stream, raw); update docs and tests

This commit is contained in:
2025-08-18 22:29:24 +00:00
parent 7b2081dc4d
commit 9c5a939499
6 changed files with 252 additions and 3 deletions

74
test/test.streaming.ts Normal file
View File

@@ -0,0 +1,74 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as fs from 'fs';
import { SmartRequest } from '../ts/index.js';
tap.test('should send a buffer using buffer() method', async () => {
const testBuffer = Buffer.from('Hello, World!');
const smartRequest = SmartRequest.create()
.url('https://httpbin.org/post')
.buffer(testBuffer, 'text/plain')
.method('POST');
const response = await smartRequest.post();
const data = await response.json();
expect(data).toHaveProperty('data');
expect(data.data).toEqual('Hello, World!');
expect(data.headers['Content-Type']).toEqual('text/plain');
});
tap.test('should send a stream using stream() method', async () => {
// Create a simple readable stream
const { Readable } = await import('stream');
const testData = 'Stream data test';
const stream = Readable.from([testData]);
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');
expect(data.data).toEqual(testData);
});
tap.test('should handle raw streaming with custom function', async () => {
const testData = 'Custom raw stream data';
const smartRequest = SmartRequest.create()
.url('https://httpbin.org/post')
.raw((request) => {
// Custom streaming logic
request.write(testData);
request.end();
})
.method('POST');
const response = await smartRequest.post();
const data = await response.json();
expect(data).toHaveProperty('data');
expect(data.data).toEqual(testData);
});
tap.test('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();
// Just verify that data was sent
expect(data).toHaveProperty('data');
expect(data.headers['Content-Type']).toEqual('application/octet-stream');
});
export default tap.start();