Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it, vi } from 'vitest'; |
| 2 | import { readResponseBodyWithLimit } from '../../src/utils/readResponseBody'; |
| 3 | |
| 4 | describe('readResponseBodyWithLimit', () => { |
| 5 | it('rejects an oversized Content-Length before reading the body', async () => { |
| 6 | const cancel = vi.fn(); |
| 7 | const response = new Response(new ReadableStream({ cancel }), { |
| 8 | headers: { 'content-length': '5' }, |
| 9 | }); |
| 10 | |
| 11 | await expect(readResponseBodyWithLimit(response, 4)).rejects.toThrow( |
| 12 | 'exceeds maximum allowed size' |
| 13 | ); |
| 14 | expect(cancel).toHaveBeenCalledOnce(); |
| 15 | expect(response.bodyUsed).toBe(true); |
| 16 | }); |
| 17 | |
| 18 | it('rejects and cancels a stream that exceeds the limit without a size header', async () => { |
| 19 | const cancel = vi.fn(); |
| 20 | const response = new Response( |
| 21 | new ReadableStream<Uint8Array>({ |
| 22 | start(controller) { |
| 23 | controller.enqueue(new Uint8Array([1, 2, 3])); |
| 24 | controller.enqueue(new Uint8Array([4, 5])); |
| 25 | }, |
| 26 | cancel, |
| 27 | }) |
| 28 | ); |
| 29 | |
| 30 | await expect(readResponseBodyWithLimit(response, 4)).rejects.toThrow( |
| 31 | 'exceeds maximum allowed size' |
| 32 | ); |
| 33 | expect(cancel).toHaveBeenCalledOnce(); |
| 34 | }); |
| 35 | |
| 36 | it('accepts a body exactly at the limit', async () => { |
| 37 | const response = new Response(new Uint8Array([1, 2, 3, 4]), { |
| 38 | headers: { 'content-length': '4' }, |
| 39 | }); |
| 40 | |
| 41 | await expect(readResponseBodyWithLimit(response, 4)).resolves.toEqual( |
| 42 | new Uint8Array([1, 2, 3, 4]) |
| 43 | ); |
| 44 | }); |
| 45 | |
| 46 | it('rejects invalid limits', async () => { |
| 47 | await expect(readResponseBodyWithLimit(new Response(), -1)).rejects.toThrow(RangeError); |
| 48 | }); |
| 49 | }); |