index.test.ts
This file
Path
runtime_tests/lambda/index.test.ts
285 lines · 8.2 KiB · TypeScript
Last changed by
fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
History of this fileTake it
fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 1–4why, one step back1import type {2 ApiGatewayRequestContext,3 LambdaFunctionUrlRequestContext,4} from '../../src/adapter/aws-lambda/custom-context'chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 5–8why, one step back5import { handle } from '../../src/adapter/aws-lambda/handler'6import { Hono } from '../../src/hono'7import { basicAuth } from '../../src/middleware/basic-auth'8fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 9–12why, one step back9type Bindings = {10 requestContext: ApiGatewayRequestContext | LambdaFunctionUrlRequestContext11}12chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
line 13why, one step back13describe('AWS Lambda Adapter for Hono', () => {fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 14why, one step back14 const app = new Hono<{ Bindings: Bindings }>()chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 15–19why, one step back1516 app.get('/', (c) => {17 return c.text('Hello Lambda!')18 })19fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 20–25why, one step back20 app.get('/binary', (c) => {21 return c.body('Fake Image', 200, {22 'Content-Type': 'image/png',23 })24 })25chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 26–30why, one step back26 app.post('/post', async (c) => {27 const body = (await c.req.parseBody()) as { message: string }28 return c.text(body.message)29 })30fix: AWS Lambda: Avoid text-encoding binary data (#1235)Denis Washingtonbefore adoption2a6d175
lines 31–35why, one step back31 app.post('/post/binary', async (c) => {32 const body = await c.req.blob()33 return c.text(`${body.size} bytes`)34 })35chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 36–40why, one step back36 const username = 'hono-user-a'37 const password = 'hono-password-a'38 app.use('/auth/*', basicAuth({ username, password }))39 app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))40fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 41–50why, one step back41 app.get('/custom-context/apigw', (c) => {42 const lambdaContext = c.env.requestContext43 return c.json(lambdaContext)44 })4546 app.get('/custom-context/lambda', (c) => {47 const lambdaContext = c.env.requestContext48 return c.json(lambdaContext)49 })50chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 51–52why, one step back51 const handler = handle(app)52fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 53–109why, one step back53 const testApiGatewayRequestContext = {54 accountId: '123456789012',55 apiId: 'id',56 authorizer: {57 claims: null,58 scopes: null,59 },60 domainName: 'example.com',61 domainPrefix: 'id',62 extendedRequestId: 'request-id',63 httpMethod: 'GET',64 identity: {65 sourceIp: 'IP',66 userAgent: 'user-agent',67 },68 path: '/my/path',69 protocol: 'HTTP/1.1',70 requestId: 'id=',71 requestTime: '04/Mar/2020:19:15:17 +0000',72 requestTimeEpoch: 1583349317135,73 resourcePath: '/',74 stage: '$default',75 customProperty: 'customValue',76 }7778 const testLambdaFunctionUrlRequestContext = {79 accountId: '123456789012',80 apiId: 'urlid',81 authentication: null,82 authorizer: {83 iam: {84 accessKey: 'AKIA...',85 accountId: '111122223333',86 callerId: 'AIDA...',87 cognitoIdentity: null,88 principalOrgId: null,89 userArn: 'arn:aws:iam::111122223333:user/example-user',90 userId: 'AIDA...',91 },92 },93 domainName: 'example.com',94 domainPrefix: '<url-id>',95 http: {96 method: 'POST',97 path: '/my/path',98 protocol: 'HTTP/1.1',99 sourceIp: '123.123.123.123',100 userAgent: 'agent',101 },102 requestId: 'id',103 routeKey: '$default',104 stage: '$default',105 time: '12/Mar/2020:19:03:58 +0000',106 timeEpoch: 1583348638390,107 customProperty: 'customValue',108 }109chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 110–116why, one step back110 it('Should handle a GET request and return a 200 response', async () => {111 const event = {112 httpMethod: 'GET',113 headers: { 'content-type': 'text/plain' },114 path: '/',115 body: null,116 isBase64Encoded: false,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 117why, one step back117 requestContext: testApiGatewayRequestContext,chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 118–121why, one step back118 }119120 const response = await handler(event)121 expect(response.statusCode).toBe(200)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 122why, one step back122 expect(response.body).toBe('Hello Lambda!')chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
line 123why, one step back123 expect(response.headers['content-type']).toMatch(/^text\/plain/)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 124–133why, one step back124 expect(response.isBase64Encoded).toBe(false)125 })126127 it('Should handle a GET request and return a 200 response with binary', async () => {128 const event = {129 httpMethod: 'GET',130 headers: {},131 path: '/binary',132 body: null,133 isBase64Encoded: false,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 134why, one step back134 requestContext: testApiGatewayRequestContext,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 135–140why, one step back135 }136137 const response = await handler(event)138 expect(response.statusCode).toBe(200)139 expect(response.body).toBe('RmFrZSBJbWFnZQ==')140 expect(response.headers['content-type']).toMatch(/^image\/png/)feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 141why, one step back141 expect(response.isBase64Encoded).toBe(true)chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 142–143why, one step back142 })143feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 144–150why, one step back144 it('Should handle a GET request and return a 200 response (LambdaFunctionUrlEvent)', async () => {145 const event = {146 headers: { 'content-type': 'text/plain' },147 rawPath: '/',148 rawQueryString: '',149 body: null,150 isBase64Encoded: false,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 151why, one step back151 requestContext: testLambdaFunctionUrlRequestContext,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 152why, one step back152 }feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 153why, one step back153fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 154–155why, one step back154 testLambdaFunctionUrlRequestContext.http.method = 'GET'155fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 156–162why, one step back156 const response = await handler(event)157 expect(response.statusCode).toBe(200)158 expect(response.body).toBe('Hello Lambda!')159 expect(response.headers['content-type']).toMatch(/^text\/plain/)160 expect(response.isBase64Encoded).toBe(false)161 })162chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 163–169why, one step back163 it('Should handle a GET request and return a 404 response', async () => {164 const event = {165 httpMethod: 'GET',166 headers: { 'content-type': 'text/plain' },167 path: '/nothing',168 body: null,169 isBase64Encoded: false,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 170why, one step back170 requestContext: testApiGatewayRequestContext,chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 171–185why, one step back171 }172173 const response = await handler(event)174 expect(response.statusCode).toBe(404)175 })176177 it('Should handle a POST request and return a 200 response', async () => {178 const searchParam = new URLSearchParams()179 searchParam.append('message', 'Good Morning Lambda!')180 const event = {181 httpMethod: 'POST',182 headers: {183 'Content-Type': 'application/x-www-form-urlencoded',184 },185 path: '/post',fix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 186why, one step back186 body: Buffer.from(searchParam.toString()).toString('base64'),chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
line 187why, one step back187 isBase64Encoded: true,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 188why, one step back188 requestContext: testApiGatewayRequestContext,chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 189–192why, one step back189 }190191 const response = await handler(event)192 expect(response.statusCode).toBe(200)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 193why, one step back193 expect(response.body).toBe('Good Morning Lambda!')chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 194–195why, one step back194 })195feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 196why, one step back196 it('Should handle a POST request and return a 200 response (LambdaFunctionUrlEvent)', async () => {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 197–198why, one step back197 const searchParam = new URLSearchParams()198 searchParam.append('message', 'Good Morning Lambda!')feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 199–204why, one step back199 const event = {200 headers: {201 'Content-Type': 'application/x-www-form-urlencoded',202 },203 rawPath: '/post',204 rawQueryString: '',fix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 205why, one step back205 body: Buffer.from(searchParam.toString()).toString('base64'),feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 206why, one step back206 isBase64Encoded: true,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 207why, one step back207 requestContext: testLambdaFunctionUrlRequestContext,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 208–209why, one step back208 }209fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 210–211why, one step back210 testLambdaFunctionUrlRequestContext.http.method = 'POST'211fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 212–214why, one step back212 const response = await handler(event)213 expect(response.statusCode).toBe(200)214 expect(response.body).toBe('Good Morning Lambda!')fix: AWS Lambda: Avoid text-encoding binary data (#1235)Denis Washingtonbefore adoption2a6d175
lines 215–227why, one step back215 })216217 it('Should handle a POST request with binary and return a 200 response', async () => {218 const array = new Uint8Array([0xc0, 0xff, 0xee])219 const buffer = Buffer.from(array)220 const event = {221 httpMethod: 'POST',222 headers: {223 'Content-Type': 'application/x-www-form-urlencoded',224 },225 path: '/post/binary',226 body: buffer.toString('base64'),227 isBase64Encoded: true,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 228why, one step back228 requestContext: testApiGatewayRequestContext,fix: AWS Lambda: Avoid text-encoding binary data (#1235)Denis Washingtonbefore adoption2a6d175
lines 229–233why, one step back229 }230231 const response = await handler(event)232 expect(response.statusCode).toBe(200)233 expect(response.body).toBe('3 bytes')fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 234–235why, one step back234 })235chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 236–244why, one step back236 it('Should handle a request and return a 401 response with Basic auth', async () => {237 const event = {238 httpMethod: 'GET',239 headers: {240 'Content-Type': 'plain/text',241 },242 path: '/auth/abc',243 body: null,244 isBase64Encoded: true,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 245why, one step back245 requestContext: testApiGatewayRequestContext,chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 246–262why, one step back246 }247248 const response = await handler(event)249 expect(response.statusCode).toBe(401)250 })251252 it('Should handle a request and return a 200 response with Basic auth', async () => {253 const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'254 const event = {255 httpMethod: 'GET',256 headers: {257 'Content-Type': 'plain/text',258 Authorization: `Basic ${credential}`,259 },260 path: '/auth/abc',261 body: null,262 isBase64Encoded: true,fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 263why, one step back263 requestContext: testApiGatewayRequestContext,chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 264–267why, one step back264 }265266 const response = await handler(event)267 expect(response.statusCode).toBe(200)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 268why, one step back268 expect(response.body).toBe('Good Night Lambda!')fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 269–283why, one step back269 })270271 it('Should handle a GET request and return custom context', async () => {272 const event = {273 httpMethod: 'GET',274 headers: { 'content-type': 'application/json' },275 path: '/custom-context/apigw',276 body: null,277 isBase64Encoded: false,278 requestContext: testApiGatewayRequestContext,279 }280281 const response = await handler(event)282 expect(response.statusCode).toBe(200)283 expect(JSON.parse(response.body).customProperty).toEqual('customValue')chore: move runtime tests into `runtime_tests` (#1062)Yusuke Wadabefore adoptionf29761e
lines 284–285why, one step back284 })285})