0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 03:40:50 +01:00
hono/runtime_tests/lambda/stream-mock.ts
Markus Wolf a2a04a98a1
fix(aws-lambda): handle multiple cookies in streaming responses (#2926)
* fix: handle multiple cookies in streaming responses

This fix does address that cookies have to be handled
specially in lambda responses.

Fixes #2921

* test: check cookie handling
2024-06-12 19:25:37 +09:00

44 lines
1.3 KiB
TypeScript

import { Writable } from 'node:stream'
import { vi } from 'vitest'
import type {
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
} from '../../src/adapter/aws-lambda/handler'
import type { LambdaContext } from '../../src/adapter/aws-lambda/types'
type StreamifyResponseHandler = (
handlerFunc: (
event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
responseStream: Writable,
context: LambdaContext
) => Promise<void>
) => (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<NodeJS.WritableStream>
const mockStreamifyResponse: StreamifyResponseHandler = (handlerFunc) => {
return async (event, context) => {
const chunks = []
const mockWritableStream = new Writable({
write(chunk, encoding, callback) {
chunks.push(chunk)
callback()
},
})
mockWritableStream.chunks = chunks
await handlerFunc(event, mockWritableStream, context)
mockWritableStream.end()
return mockWritableStream
}
}
const awslambda = {
streamifyResponse: mockStreamifyResponse,
HttpResponseStream: {
from: (stream: Writable, httpResponseMetadata: unknown): Writable => {
stream.write(Buffer.from(JSON.stringify(httpResponseMetadata)))
return stream
},
},
}
vi.stubGlobal('awslambda', awslambda)