|
| 1 | +import { APIGatewayProxyHandler } from "./APIGatewayProxyHandler"; |
| 2 | +import { APIGatewayProxyEvent, Handler, APIGatewayProxyResult, Context } from "aws-lambda"; |
| 3 | +import { |
| 4 | + RequestTimeoutError, |
| 5 | + InternalServerError, |
| 6 | + NotFoundError, |
| 7 | + ValidationError, |
| 8 | + FormatError, |
| 9 | + ForbiddenError, |
| 10 | + BadRequestError, |
| 11 | +} from "../error"; |
| 12 | +import * as ContextFactory from "../../test/fixtures/ContextFactory"; |
| 13 | +import * as APIGatewayProxyEventFactory from "../../test/fixtures/APIGatewayProxyEventFactory"; |
| 14 | + |
| 15 | +describe(APIGatewayProxyHandler.name, () => { |
| 16 | + let handler: APIGatewayProxyHandler; |
| 17 | + let context: Context; |
| 18 | + let event: APIGatewayProxyEvent; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + handler = new APIGatewayProxyHandler(); |
| 22 | + context = ContextFactory.factory(); |
| 23 | + event = APIGatewayProxyEventFactory.factory(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("handles BadRequestError response correctly", async () => { |
| 27 | + const fn = handler.wrapper(() => { |
| 28 | + throw new BadRequestError("BadRequestError message"); |
| 29 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 30 | + |
| 31 | + const result = await fn(event, context, () => {}); |
| 32 | + expect(result).toMatchSnapshot(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("handles ForbiddenError response correctly", async () => { |
| 36 | + const fn = handler.wrapper(() => { |
| 37 | + throw new ForbiddenError("ForbiddenError message"); |
| 38 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 39 | + |
| 40 | + const result = await fn(event, context, () => {}); |
| 41 | + expect(result).toMatchSnapshot(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("handles FormatError response correctly", async () => { |
| 45 | + const fn = handler.wrapper(() => { |
| 46 | + throw new FormatError("FormatError message"); |
| 47 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 48 | + |
| 49 | + const result = await fn(event, context, () => {}); |
| 50 | + expect(result).toMatchSnapshot(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("handles InternalServerError response correctly", async () => { |
| 54 | + const fn = handler.wrapper(() => { |
| 55 | + throw new InternalServerError("InternalServerError message"); |
| 56 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 57 | + |
| 58 | + const result = await fn(event, context, () => {}); |
| 59 | + expect(result).toMatchSnapshot(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("handles NotFoundError response correctly", async () => { |
| 63 | + const fn = handler.wrapper(() => { |
| 64 | + throw new NotFoundError("NotFoundError message"); |
| 65 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 66 | + |
| 67 | + const result = await fn(event, context, () => {}); |
| 68 | + expect(result).toMatchSnapshot(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("handles RequestTimeoutError response correctly", async () => { |
| 72 | + const fn = handler.wrapper(() => { |
| 73 | + throw new RequestTimeoutError("RequestTimeoutError message"); |
| 74 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 75 | + |
| 76 | + const result = await fn(event, context, () => {}); |
| 77 | + expect(result).toMatchSnapshot(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("handles ValidationError response correctly", async () => { |
| 81 | + const fn = handler.wrapper(() => { |
| 82 | + throw new ValidationError("ValidationError message"); |
| 83 | + }) as Handler<APIGatewayProxyEvent, APIGatewayProxyResult>; |
| 84 | + |
| 85 | + const result = await fn(event, context, () => {}); |
| 86 | + expect(result).toMatchSnapshot(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments