|
1 | | -import { Readable } from 'node:stream'; |
| 1 | +import { Duplex, PassThrough, Readable } from 'node:stream'; |
2 | 2 | import context from '@aws-lambda-powertools/testing-utils/context'; |
3 | 3 | import { describe, expect, it, vi } from 'vitest'; |
4 | 4 | import { UnauthorizedError } from '../../../../src/rest/errors.js'; |
@@ -306,4 +306,29 @@ describe('Class: Router - Streaming', () => { |
306 | 306 | app.resolveStream(invalidEvent, context, { responseStream }) |
307 | 307 | ).rejects.toThrow(); |
308 | 308 | }); |
| 309 | + |
| 310 | + it('handles duplex stream body', async () => { |
| 311 | + // Prepare |
| 312 | + const app = new Router(); |
| 313 | + const passThrough = new PassThrough(); |
| 314 | + passThrough.write(Buffer.from('{"message":"duplex stream body"}')); |
| 315 | + passThrough.end(); |
| 316 | + |
| 317 | + app.get('/test', () => ({ |
| 318 | + statusCode: 200, |
| 319 | + body: Duplex.from(passThrough), |
| 320 | + })); |
| 321 | + |
| 322 | + const responseStream = new MockResponseStream(); |
| 323 | + |
| 324 | + // Act |
| 325 | + await app.resolveStream(createTestEvent('/test', 'GET'), context, { |
| 326 | + responseStream, |
| 327 | + }); |
| 328 | + |
| 329 | + // Assess |
| 330 | + const { prelude, body } = parseStreamOutput(responseStream.chunks); |
| 331 | + expect(prelude.statusCode).toBe(200); |
| 332 | + expect(JSON.parse(body)).toEqual({ message: 'duplex stream body' }); |
| 333 | + }); |
309 | 334 | }); |
0 commit comments