Skip to content

Commit c94e214

Browse files
committed
handle ALB get and patch request bodies
1 parent 1cf1cf8 commit c94e214

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

packages/event-handler/src/rest/converters.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
V1Headers,
1919
WebResponseToProxyResultOptions,
2020
} from '../types/rest.js';
21-
import { HttpStatusCodes, HttpStatusText } from './constants.js';
21+
import { HttpStatusCodes, HttpStatusText, HttpVerbs } from './constants.js';
2222
import { InvalidHttpMethodError } from './errors.js';
2323
import {
2424
isALBEvent,
@@ -177,10 +177,16 @@ const albEventToWebRequest = (event: ALBEvent): Request => {
177177
const url = new URL(path, `${protocol}://${hostname}/`);
178178
populateV1QueryParams(url, event);
179179

180+
// ALB events represent GET and PATCH request bodies as empty strings
181+
const body =
182+
httpMethod === HttpVerbs.GET || httpMethod === HttpVerbs.PATCH
183+
? null
184+
: createBody(event.body ?? null, event.isBase64Encoded);
185+
180186
return new Request(url.toString(), {
181187
method: httpMethod,
182188
headers,
183-
body: createBody(event.body ?? null, event.isBase64Encoded),
189+
body: body,
184190
});
185191
};
186192

packages/event-handler/tests/unit/rest/Router/basic-routing.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,25 @@ describe('Class: Router - ALB Support', () => {
367367
});
368368
});
369369

370+
it('handles ALB POST request with body', async () => {
371+
// Prepare
372+
const app = new Router();
373+
app.post('/test', async ({ req }) => {
374+
const body = await req.json();
375+
return { received: body };
376+
});
377+
378+
// Act
379+
const result = await app.resolve(
380+
createTestALBEvent('/test', 'POST', {}, { data: 'test' }),
381+
context
382+
);
383+
384+
// Assess
385+
expect(result.statusCode).toBe(200);
386+
expect(result.body).toBe(JSON.stringify({ received: { data: 'test' } }));
387+
});
388+
370389
it.each(
371390
Object.entries(HttpStatusText).map(([code, text]) => ({
372391
statusCode: Number(code),

packages/event-handler/tests/unit/rest/helpers.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Writable } from 'node:stream';
2+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
23
import type {
34
ALBEvent,
45
ALBResult,
@@ -8,6 +9,7 @@ import type {
89
APIGatewayProxyStructuredResultV2,
910
Context,
1011
} from 'aws-lambda';
12+
import { HttpVerbs } from '../../../src/rest/constants.js';
1113
import type { Router } from '../../../src/rest/Router.js';
1214
import type {
1315
HandlerResponse,
@@ -73,23 +75,37 @@ export const createTestEventV2 = (
7375
export const createTestALBEvent = (
7476
path: string,
7577
httpMethod: string,
76-
headers: Record<string, string> = {}
77-
): ALBEvent => ({
78-
path,
79-
httpMethod,
80-
headers,
81-
body: null,
82-
multiValueHeaders: {},
83-
isBase64Encoded: false,
84-
queryStringParameters: undefined,
85-
multiValueQueryStringParameters: undefined,
86-
requestContext: {
87-
elb: {
88-
targetGroupArn:
89-
'arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/test/50dc6c495c0c9188',
78+
headers: Record<string, string> = {},
79+
body: string | JSONValue = null
80+
): ALBEvent => {
81+
const stringBody =
82+
typeof body === 'string'
83+
? body
84+
: body !== null
85+
? JSON.stringify(body)
86+
: null;
87+
88+
return {
89+
path,
90+
httpMethod,
91+
headers,
92+
// ALB events represent GET and PATCH request bodies as empty strings
93+
body:
94+
httpMethod === HttpVerbs.GET || httpMethod === HttpVerbs.PATCH
95+
? ''
96+
: stringBody,
97+
multiValueHeaders: {},
98+
isBase64Encoded: false,
99+
queryStringParameters: undefined,
100+
multiValueQueryStringParameters: undefined,
101+
requestContext: {
102+
elb: {
103+
targetGroupArn:
104+
'arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/test/50dc6c495c0c9188',
105+
},
90106
},
91-
},
92-
});
107+
};
108+
};
93109

94110
export const createTrackingMiddleware = (
95111
name: string,

0 commit comments

Comments
 (0)