41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
		
		
			
		
	
	
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
|  | 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(); |