74 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 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(); |