-
Notifications
You must be signed in to change notification settings - Fork 156
fix(parser): LambdaFunctionUrl envelope assumes JSON string in body #3514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"version": "2.0", | ||
"routeKey": "$default", | ||
"rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value", | ||
"cookies": ["cookie1", "cookie2"], | ||
"headers": { | ||
"header1": "value1", | ||
"header2": "value1,value2" | ||
}, | ||
"queryStringParameters": { | ||
"parameter1": "value1,value2", | ||
"parameter2": "value" | ||
}, | ||
"requestContext": { | ||
"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": "<url-id>.lambda-url.us-west-2.on.aws", | ||
"domainPrefix": "<url-id>", | ||
"http": { | ||
"method": "POST", | ||
"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 | ||
}, | ||
"body": null, | ||
"pathParameters": null, | ||
"isBase64Encoded": false, | ||
"stageVariables": null | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,139 @@ | ||
import { generateMock } from '@anatine/zod-mock'; | ||
import type { | ||
APIGatewayProxyEventV2, | ||
LambdaFunctionURLEvent, | ||
} from 'aws-lambda'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { ZodError } from 'zod'; | ||
import { ZodError, z } from 'zod'; | ||
import { ParseError } from '../../../src'; | ||
import { LambdaFunctionUrlEnvelope } from '../../../src/envelopes/index.js'; | ||
import { TestEvents, TestSchema } from '../schema/utils.js'; | ||
import { JSONStringified } from '../../../src/helpers'; | ||
import type { LambdaFunctionUrlEvent } from '../../../src/types'; | ||
import { getTestEvent, omit } from '../schema/utils.js'; | ||
|
||
describe('Lambda Functions Url ', () => { | ||
describe('parse', () => { | ||
it('should parse custom schema in envelope', () => { | ||
const testEvent = | ||
TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; | ||
const data = generateMock(TestSchema); | ||
describe('Envelope: Lambda function URL', () => { | ||
const schema = z | ||
.object({ | ||
message: z.string(), | ||
}) | ||
.strict(); | ||
|
||
testEvent.body = JSON.stringify(data); | ||
const baseEvent = getTestEvent<LambdaFunctionUrlEvent>({ | ||
eventsPath: 'lambda', | ||
filename: 'base', | ||
}); | ||
|
||
describe('Method: parse', () => { | ||
it('throws if the payload does not match the schema', () => { | ||
// Prepare | ||
const event = structuredClone(baseEvent); | ||
|
||
expect(LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema)).toEqual( | ||
data | ||
// Act & Assess | ||
expect(() => LambdaFunctionUrlEnvelope.parse(event, schema)).toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining( | ||
'Failed to parse Lambda function URL body' | ||
), | ||
cause: expect.objectContaining({ | ||
issues: [ | ||
{ | ||
code: 'invalid_type', | ||
expected: 'object', | ||
received: 'null', | ||
path: ['body'], | ||
message: 'Expected object, received null', | ||
}, | ||
], | ||
}), | ||
}) | ||
); | ||
}); | ||
|
||
it('should throw when no body provided', () => { | ||
const testEvent = | ||
TestEvents.lambdaFunctionUrlEvent as LambdaFunctionURLEvent; | ||
testEvent.body = undefined; | ||
it('parses a Lambda function URL event with plain text', () => { | ||
// Prepare | ||
const event = structuredClone(baseEvent); | ||
event.body = 'hello world'; | ||
|
||
expect(() => | ||
LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema) | ||
).toThrow(); | ||
// Act | ||
const result = LambdaFunctionUrlEnvelope.parse(event, z.string()); | ||
|
||
// Assess | ||
expect(result).toEqual('hello world'); | ||
}); | ||
|
||
it('should throw when envelope is not valid', () => { | ||
expect(() => | ||
LambdaFunctionUrlEnvelope.parse({ foo: 'bar' }, TestSchema) | ||
).toThrow(); | ||
it('parses a Lambda function URL event with JSON-stringified body', () => { | ||
// Prepare | ||
const event = structuredClone(baseEvent); | ||
event.body = JSON.stringify({ message: 'hello world' }); | ||
|
||
// Act | ||
const result = LambdaFunctionUrlEnvelope.parse( | ||
event, | ||
JSONStringified(schema) | ||
); | ||
|
||
// Assess | ||
expect(result).toEqual({ message: 'hello world' }); | ||
}); | ||
|
||
it('should throw when body does not match schema', () => { | ||
const testEvent = | ||
TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; | ||
testEvent.body = JSON.stringify({ foo: 'bar' }); | ||
it('parses a Lambda function URL event with binary body', () => { | ||
// Prepare | ||
const event = structuredClone(baseEvent); | ||
event.body = Buffer.from('hello world').toString('base64'); | ||
event.headers['content-type'] = 'application/octet-stream'; | ||
event.isBase64Encoded = true; | ||
|
||
// Act | ||
const result = LambdaFunctionUrlEnvelope.parse(event, z.string()); | ||
|
||
expect(() => | ||
LambdaFunctionUrlEnvelope.parse(testEvent, TestSchema) | ||
).toThrow(); | ||
// Assess | ||
expect(result).toEqual('aGVsbG8gd29ybGQ='); | ||
}); | ||
}); | ||
describe('safeParse', () => { | ||
it('should parse custom schema in envelope', () => { | ||
const testEvent = | ||
TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; | ||
const data = generateMock(TestSchema); | ||
describe('Method: safeParse', () => { | ||
it('parses Lambda function URL event', () => { | ||
// Prepare | ||
const event = structuredClone(baseEvent); | ||
event.body = JSON.stringify({ message: 'hello world' }); | ||
|
||
testEvent.body = JSON.stringify(data); | ||
// Act | ||
const result = LambdaFunctionUrlEnvelope.safeParse( | ||
event, | ||
JSONStringified(schema) | ||
); | ||
|
||
expect( | ||
LambdaFunctionUrlEnvelope.safeParse(testEvent, TestSchema) | ||
).toEqual({ | ||
// Assess | ||
expect(result).toEqual({ | ||
success: true, | ||
data, | ||
data: { message: 'hello world' }, | ||
}); | ||
}); | ||
|
||
it('should return original event when envelope is not valid', () => { | ||
expect( | ||
LambdaFunctionUrlEnvelope.safeParse({ foo: 'bar' }, TestSchema) | ||
).toEqual({ | ||
success: false, | ||
error: expect.any(ParseError), | ||
originalEvent: { foo: 'bar' }, | ||
}); | ||
}); | ||
it('returns an error when the event is not valid', () => { | ||
// Prepare | ||
const event = omit(['rawPath'], structuredClone(baseEvent)); | ||
|
||
it('should return original event when body does not match schema', () => { | ||
const testEvent = | ||
TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; | ||
testEvent.body = JSON.stringify({ foo: 'bar' }); | ||
// Act | ||
const result = LambdaFunctionUrlEnvelope.safeParse(event, schema); | ||
|
||
const parseResult = LambdaFunctionUrlEnvelope.safeParse( | ||
testEvent, | ||
TestSchema | ||
); | ||
expect(parseResult).toEqual({ | ||
// Assess | ||
expect(result).toEqual({ | ||
success: false, | ||
error: expect.any(ParseError), | ||
originalEvent: testEvent, | ||
error: new ParseError('Failed to parse Lambda function URL body', { | ||
cause: new ZodError([ | ||
{ | ||
code: 'invalid_type', | ||
expected: 'string', | ||
received: 'undefined', | ||
path: ['rawPath'], | ||
message: 'Required', | ||
}, | ||
{ | ||
code: 'invalid_type', | ||
expected: 'object', | ||
received: 'null', | ||
path: ['body'], | ||
message: 'Expected object, received null', | ||
}, | ||
]), | ||
}), | ||
originalEvent: event, | ||
}); | ||
|
||
if (!parseResult.success && parseResult.error) { | ||
expect(parseResult.error.cause).toBeInstanceOf(ZodError); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { LambdaFunctionUrlSchema } from '../../../src/schemas/'; | ||
import { TestEvents } from './utils.js'; | ||
import { getTestEvent } from './utils.js'; | ||
|
||
describe('Lambda ', () => { | ||
it('should parse lambda event', () => { | ||
const lambdaFunctionUrlEvent = TestEvents.lambdaFunctionUrlEvent; | ||
describe('Schema: LambdaFunctionUrl', () => { | ||
const eventsPath = 'lambda'; | ||
|
||
expect(LambdaFunctionUrlSchema.parse(lambdaFunctionUrlEvent)).toEqual( | ||
lambdaFunctionUrlEvent | ||
); | ||
it('throw when the event is invalid', () => { | ||
// Prepare | ||
const event = getTestEvent({ eventsPath, filename: 'invalid' }); | ||
|
||
// Act & Assess | ||
expect(() => LambdaFunctionUrlSchema.parse(event)).toThrow(); | ||
}); | ||
|
||
it('parses a valid event', () => { | ||
// Prepare | ||
const event = getTestEvent({ eventsPath, filename: 'get-request' }); | ||
|
||
// Act | ||
const parsedEvent = LambdaFunctionUrlSchema.parse(event); | ||
|
||
// Assess | ||
expect(parsedEvent).toEqual(event); | ||
}); | ||
|
||
it('should parse url IAM event', () => { | ||
const urlIAMEvent = TestEvents.lambdaFunctionUrlIAMEvent; | ||
it('parses iam event', () => { | ||
// Prepare | ||
const event = getTestEvent({ eventsPath, filename: 'iam-auth' }); | ||
|
||
// Act | ||
const parsedEvent = LambdaFunctionUrlSchema.parse(event); | ||
|
||
expect(LambdaFunctionUrlSchema.parse(urlIAMEvent)).toEqual(urlIAMEvent); | ||
// | ||
expect(parsedEvent).toEqual(event); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.