Skip to content

Commit

Permalink
fix tests and update schema to support Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexholtz committed Nov 15, 2023
1 parent 763d085 commit e61b8f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lambda/parse-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { bodySchema, CONTENT_TYPES, headersSchema } from './schema';

export function parseRequestBody(body: string, headers: IncomingHttpHeaders) {
const headersResult = headersSchema.parse(headers);
const contentType = headersResult['content-type'] || headersResult['Content-Type'] || undefined;
const contentType = headersResult['content-type'] || headersResult['Content-Type'];
switch (contentType) {
case CONTENT_TYPES.JSON:
return JSON.parse(body);
Expand Down
7 changes: 2 additions & 5 deletions lambda/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ describe('proxy', () => {
it('should forward a request if content-type header is Content-Type or content-type', async () => {
const destinationUrl = 'https://approved.host/github-webhook/';
const endpointId = encodeURIComponent(destinationUrl);

// create copy of baseEvent.headers without 'content-type' key
const { 'content-type': _, ...headers } = baseEvent.headers;

const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
...baseEvent,
headers: {
...headers,
...baseEvent.headers,
'content-type': undefined,
'Content-Type': 'application/json'
},
body: JSON.stringify(VALID_PUSH_PAYLOAD),
Expand Down
8 changes: 7 additions & 1 deletion lambda/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ export const CONTENT_TYPES = {
URL_ENCODED: 'application/x-www-form-urlencoded'
} as const;

// headerSchema that supports either 'Content-Type' or 'content-type'
export const headersSchema = z.object({
'content-type': z.nativeEnum(CONTENT_TYPES)
'content-type': z.string().optional(),
'Content-Type': z.string().optional()
}).refine((headers) => {
return headers['content-type'] || headers['Content-Type'];
}, {
message: 'Missing Content-Type header'
});

export const axiosErrorSchema = z.object({
Expand Down

0 comments on commit e61b8f3

Please sign in to comment.