0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-27 16:06:24 +01:00
hono/runtime-tests/lambda/stream.test.ts
Yusuke Wada dfbd717263
chore: rename runtime_tests to runtime-tests (#3419)
* chore: rename `runtime_tests` to `runtime-tests`

* fixed GitHub workflow
2024-09-17 14:51:43 +09:00

76 lines
2.0 KiB
TypeScript

import { streamHandle } from '../../src/adapter/aws-lambda/handler'
import type { LambdaEvent } from '../../src/adapter/aws-lambda/handler'
import type {
ApiGatewayRequestContext,
ApiGatewayRequestContextV2,
LambdaContext,
} from '../../src/adapter/aws-lambda/types'
import { Hono } from '../../src/hono'
import './stream-mock'
type Bindings = {
event: LambdaEvent
lambdaContext: LambdaContext
requestContext: ApiGatewayRequestContext | ApiGatewayRequestContextV2
}
const testApiGatewayRequestContextV2 = {
accountId: '123456789012',
apiId: 'urlid',
authentication: null,
authorizer: {
iam: {
accessKey: 'AKIA...',
accountId: '111122223333',
callerId: 'AIDA...',
cognitoIdentity: null,
principalOrgId: null,
userArn: 'arn:aws:iam::111122223333:user/example-user',
userId: 'AIDA...',
},
},
domainName: 'example.com',
domainPrefix: '<url-id>',
http: {
method: 'GET',
path: '/my/path',
protocol: 'HTTP/1.1',
sourceIp: '123.123.123.123',
userAgent: 'agent',
},
requestId: 'id',
routeKey: '$default',
stage: '$default',
time: '12/Mar/2020:19:03:58 +0000',
timeEpoch: 1583348638390,
customProperty: 'customValue',
}
describe('streamHandle function', () => {
const app = new Hono<{ Bindings: Bindings }>()
app.get('/cookies', async (c) => {
c.res.headers.append('Set-Cookie', 'cookie1=value1')
c.res.headers.append('Set-Cookie', 'cookie2=value2')
return c.text('Cookies Set')
})
const handler = streamHandle(app)
it('to write multiple cookies into the headers', async () => {
const event = {
headers: { 'content-type': 'text/plain' },
rawPath: '/cookies',
rawQueryString: '',
body: null,
isBase64Encoded: false,
requestContext: testApiGatewayRequestContextV2,
}
const stream = await handler(event)
const metadata = JSON.parse(stream.chunks[0].toString())
expect(metadata.cookies).toEqual(['cookie1=value1', 'cookie2=value2'])
})
})