|
1 | | -import { generateMock } from '@anatine/zod-mock'; |
2 | | -import type { |
3 | | - APIGatewayProxyEventV2, |
4 | | - LambdaFunctionURLEvent, |
5 | | -} from 'aws-lambda'; |
6 | 1 | import { describe, expect, it } from 'vitest'; |
7 | | -import { ZodError } from 'zod'; |
| 2 | +import { ZodError, z } from 'zod'; |
8 | 3 | import { ParseError } from '../../../src'; |
9 | 4 | import { LambdaFunctionUrlEnvelope } from '../../../src/envelopes/index.js'; |
10 | | -import { TestEvents, TestSchema } from '../schema/utils.js'; |
| 5 | +import { JSONStringified } from '../../../src/helpers'; |
| 6 | +import type { LambdaFunctionUrlEvent } from '../../../src/types'; |
| 7 | +import { getTestEvent, omit } from '../schema/utils.js'; |
11 | 8 |
|
12 | | -describe('Lambda Functions Url ', () => { |
13 | | - describe('parse', () => { |
14 | | - it('should parse custom schema in envelope', () => { |
15 | | - const testEvent = |
16 | | - TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; |
17 | | - const data = generateMock(TestSchema); |
| 9 | +describe('Envelope: Lambda function URL', () => { |
| 10 | + const schema = z |
| 11 | + .object({ |
| 12 | + message: z.string(), |
| 13 | + }) |
| 14 | + .strict(); |
18 | 15 |
|
19 | | - testEvent.body = JSON.stringify(data); |
| 16 | + const baseEvent = getTestEvent<LambdaFunctionUrlEvent>({ |
| 17 | + eventsPath: 'lambda', |
| 18 | + filename: 'base', |
| 19 | + }); |
| 20 | + |
| 21 | + describe('Method: parse', () => { |
| 22 | + it('throws if the payload does not match the schema', () => { |
| 23 | + // Prepare |
| 24 | + const event = structuredClone(baseEvent); |
20 | 25 |
|
21 | | - expect(LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema)).toEqual( |
22 | | - data |
| 26 | + // Act & Assess |
| 27 | + expect(() => LambdaFunctionUrlEnvelope.parse(event, schema)).toThrow( |
| 28 | + expect.objectContaining({ |
| 29 | + message: expect.stringContaining( |
| 30 | + 'Failed to parse Lambda function URL body' |
| 31 | + ), |
| 32 | + cause: expect.objectContaining({ |
| 33 | + issues: [ |
| 34 | + { |
| 35 | + code: 'invalid_type', |
| 36 | + expected: 'object', |
| 37 | + received: 'null', |
| 38 | + path: ['body'], |
| 39 | + message: 'Expected object, received null', |
| 40 | + }, |
| 41 | + ], |
| 42 | + }), |
| 43 | + }) |
23 | 44 | ); |
24 | 45 | }); |
25 | 46 |
|
26 | | - it('should throw when no body provided', () => { |
27 | | - const testEvent = |
28 | | - TestEvents.lambdaFunctionUrlEvent as LambdaFunctionURLEvent; |
29 | | - testEvent.body = undefined; |
| 47 | + it('parses a Lambda function URL event with plain text', () => { |
| 48 | + // Prepare |
| 49 | + const event = structuredClone(baseEvent); |
| 50 | + event.body = 'hello world'; |
30 | 51 |
|
31 | | - expect(() => |
32 | | - LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema) |
33 | | - ).toThrow(); |
| 52 | + // Act |
| 53 | + const result = LambdaFunctionUrlEnvelope.parse(event, z.string()); |
| 54 | + |
| 55 | + // Assess |
| 56 | + expect(result).toEqual('hello world'); |
34 | 57 | }); |
35 | 58 |
|
36 | | - it('should throw when envelope is not valid', () => { |
37 | | - expect(() => |
38 | | - LambdaFunctionUrlEnvelope.parse({ foo: 'bar' }, TestSchema) |
39 | | - ).toThrow(); |
| 59 | + it('parses a Lambda function URL event with JSON-stringified body', () => { |
| 60 | + // Prepare |
| 61 | + const event = structuredClone(baseEvent); |
| 62 | + event.body = JSON.stringify({ message: 'hello world' }); |
| 63 | + |
| 64 | + // Act |
| 65 | + const result = LambdaFunctionUrlEnvelope.parse( |
| 66 | + event, |
| 67 | + JSONStringified(schema) |
| 68 | + ); |
| 69 | + |
| 70 | + // Assess |
| 71 | + expect(result).toEqual({ message: 'hello world' }); |
40 | 72 | }); |
41 | 73 |
|
42 | | - it('should throw when body does not match schema', () => { |
43 | | - const testEvent = |
44 | | - TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; |
45 | | - testEvent.body = JSON.stringify({ foo: 'bar' }); |
| 74 | + it('parses a Lambda function URL event with binary body', () => { |
| 75 | + // Prepare |
| 76 | + const event = structuredClone(baseEvent); |
| 77 | + event.body = Buffer.from('hello world').toString('base64'); |
| 78 | + event.headers['content-type'] = 'application/octet-stream'; |
| 79 | + event.isBase64Encoded = true; |
| 80 | + |
| 81 | + // Act |
| 82 | + const result = LambdaFunctionUrlEnvelope.parse(event, z.string()); |
46 | 83 |
|
47 | | - expect(() => |
48 | | - LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema) |
49 | | - ).toThrow(); |
| 84 | + // Assess |
| 85 | + expect(result).toEqual('aGVsbG8gd29ybGQ='); |
50 | 86 | }); |
51 | 87 | }); |
52 | | - describe('safeParse', () => { |
53 | | - it('should parse custom schema in envelope', () => { |
54 | | - const testEvent = |
55 | | - TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; |
56 | | - const data = generateMock(TestSchema); |
| 88 | + describe('Method: safeParse', () => { |
| 89 | + it('parses Lambda function URL event', () => { |
| 90 | + // Prepare |
| 91 | + const event = structuredClone(baseEvent); |
| 92 | + event.body = JSON.stringify({ message: 'hello world' }); |
57 | 93 |
|
58 | | - testEvent.body = JSON.stringify(data); |
| 94 | + // Act |
| 95 | + const result = LambdaFunctionUrlEnvelope.safeParse( |
| 96 | + event, |
| 97 | + JSONStringified(schema) |
| 98 | + ); |
59 | 99 |
|
60 | | - expect( |
61 | | - LambdaFunctionUrlEnvelope.safeParse(testEvent, TestSchema) |
62 | | - ).toEqual({ |
| 100 | + // Assess |
| 101 | + expect(result).toEqual({ |
63 | 102 | success: true, |
64 | | - data, |
| 103 | + data: { message: 'hello world' }, |
65 | 104 | }); |
66 | 105 | }); |
67 | 106 |
|
68 | | - it('should return original event when envelope is not valid', () => { |
69 | | - expect( |
70 | | - LambdaFunctionUrlEnvelope.safeParse({ foo: 'bar' }, TestSchema) |
71 | | - ).toEqual({ |
72 | | - success: false, |
73 | | - error: expect.any(ParseError), |
74 | | - originalEvent: { foo: 'bar' }, |
75 | | - }); |
76 | | - }); |
| 107 | + it('returns an error when the event is not valid', () => { |
| 108 | + // Prepare |
| 109 | + const event = omit(['rawPath'], structuredClone(baseEvent)); |
77 | 110 |
|
78 | | - it('should return original event when body does not match schema', () => { |
79 | | - const testEvent = |
80 | | - TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; |
81 | | - testEvent.body = JSON.stringify({ foo: 'bar' }); |
| 111 | + // Act |
| 112 | + const result = LambdaFunctionUrlEnvelope.safeParse(event, schema); |
82 | 113 |
|
83 | | - const parseResult = LambdaFunctionUrlEnvelope.safeParse( |
84 | | - testEvent, |
85 | | - TestSchema |
86 | | - ); |
87 | | - expect(parseResult).toEqual({ |
| 114 | + // Assess |
| 115 | + expect(result).toEqual({ |
88 | 116 | success: false, |
89 | | - error: expect.any(ParseError), |
90 | | - originalEvent: testEvent, |
| 117 | + error: new ParseError('Failed to parse Lambda function URL body', { |
| 118 | + cause: new ZodError([ |
| 119 | + { |
| 120 | + code: 'invalid_type', |
| 121 | + expected: 'string', |
| 122 | + received: 'undefined', |
| 123 | + path: ['rawPath'], |
| 124 | + message: 'Required', |
| 125 | + }, |
| 126 | + { |
| 127 | + code: 'invalid_type', |
| 128 | + expected: 'object', |
| 129 | + received: 'null', |
| 130 | + path: ['body'], |
| 131 | + message: 'Expected object, received null', |
| 132 | + }, |
| 133 | + ]), |
| 134 | + }), |
| 135 | + originalEvent: event, |
91 | 136 | }); |
92 | | - |
93 | | - if (!parseResult.success && parseResult.error) { |
94 | | - expect(parseResult.error.cause).toBeInstanceOf(ZodError); |
95 | | - } |
96 | 137 | }); |
97 | 138 | }); |
98 | 139 | }); |
0 commit comments