|
1 | | -import { ALBEvent, APIGatewayProxyEvent, CloudFrontRequestEvent } from 'aws-lambda'; |
2 | | -import { UrlEvent } from '../http/request.url'; |
| 1 | +import { ALBEvent, APIGatewayProxyEvent, CloudFrontRequestEvent, Context } from 'aws-lambda'; |
| 2 | +import { LambdaAlbRequest } from '../http/request.alb.js'; |
| 3 | +import { LambdaApiGatewayRequest } from '../http/request.api.gateway.js'; |
| 4 | +import { LambdaCloudFrontRequest } from '../http/request.cloudfront.js'; |
| 5 | +import { LambdaUrlRequest, UrlEvent } from '../http/request.url.js'; |
| 6 | +import { fakeLog } from './log.js'; |
3 | 7 |
|
4 | 8 | export const ApiGatewayExample: APIGatewayProxyEvent = { |
5 | 9 | body: 'eyJ0ZXN0IjoiYm9keSJ9', |
@@ -157,7 +161,7 @@ export const AlbExample: ALBEvent = { |
157 | 161 | export const UrlExample: UrlEvent = { |
158 | 162 | version: '2.0', |
159 | 163 | routeKey: '$default', |
160 | | - rawPath: '/v1/🦄/🌈/🦄.json', |
| 164 | + rawPath: '/v1/%F0%9F%A6%84/%F0%9F%8C%88/%F0%9F%A6%84.json', |
161 | 165 | rawQueryString: '%F0%9F%A6%84=abc123', |
162 | 166 | headers: { |
163 | 167 | 'x-amzn-trace-id': 'Root=1-624e71a0-114297900a437c050c74f1fe', |
@@ -194,3 +198,53 @@ export const UrlExample: UrlEvent = { |
194 | 198 | export function clone<T>(c: T): T { |
195 | 199 | return JSON.parse(JSON.stringify(c)); |
196 | 200 | } |
| 201 | + |
| 202 | +const fakeContext = {} as Context; |
| 203 | + |
| 204 | +export const RequestTypes = [ |
| 205 | + { type: 'FunctionUrl', create: newRequestUrl }, |
| 206 | + { type: 'Alb', create: newRequestAlb }, |
| 207 | + { type: 'ApiGateway', create: newRequestApi }, |
| 208 | + { type: 'CloudFront', create: newRequestCloudFront }, |
| 209 | +]; |
| 210 | + |
| 211 | +export function newRequestUrl<T extends Record<string, string>>(path: string, query: string): LambdaUrlRequest<T> { |
| 212 | + const example = clone(UrlExample); |
| 213 | + example.rawPath = encodeURI(path); |
| 214 | + example.rawQueryString = encodeURI(query); |
| 215 | + example.requestContext.http.path = path; |
| 216 | + return new LambdaUrlRequest(example, fakeContext, fakeLog) as LambdaUrlRequest<T>; |
| 217 | +} |
| 218 | + |
| 219 | +export function newRequestAlb<T extends Record<string, string>>(path: string, query: string): LambdaAlbRequest<T> { |
| 220 | + const example = clone(AlbExample); |
| 221 | + example.path = encodeURI(path); |
| 222 | + example.queryStringParameters = {}; |
| 223 | + for (const [key, value] of new URLSearchParams(query).entries()) { |
| 224 | + example.queryStringParameters[key] = value; |
| 225 | + } |
| 226 | + return new LambdaAlbRequest(example, fakeContext, fakeLog) as LambdaAlbRequest<T>; |
| 227 | +} |
| 228 | + |
| 229 | +export function newRequestApi<T extends Record<string, string>>( |
| 230 | + path: string, |
| 231 | + query: string, |
| 232 | +): LambdaApiGatewayRequest<T> { |
| 233 | + const example = clone(ApiGatewayExample); |
| 234 | + example.path = encodeURI(path); |
| 235 | + example.multiValueQueryStringParameters = {}; |
| 236 | + for (const [key, value] of new URLSearchParams(query).entries()) { |
| 237 | + example.multiValueQueryStringParameters[key] = [value]; |
| 238 | + } |
| 239 | + return new LambdaApiGatewayRequest(example, fakeContext, fakeLog) as LambdaApiGatewayRequest<T>; |
| 240 | +} |
| 241 | + |
| 242 | +export function newRequestCloudFront<T extends Record<string, string>>( |
| 243 | + path: string, |
| 244 | + query: string, |
| 245 | +): LambdaCloudFrontRequest<T> { |
| 246 | + const example = clone(CloudfrontExample); |
| 247 | + example.Records[0].cf.request.uri = encodeURI(path); |
| 248 | + example.Records[0].cf.request.querystring = '?' + query; |
| 249 | + return new LambdaCloudFrontRequest(example, fakeContext, fakeLog) as LambdaCloudFrontRequest<T>; |
| 250 | +} |
0 commit comments