Skip to content

Commit 1068897

Browse files
committed
test: allow sample generators to change path/method
1 parent d47be8a commit 1068897

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

tests/integration-tests.test.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ describe('integration tests', () => {
2121
app = new Application();
2222
});
2323

24-
const makeRequestEvent = (path: string, base?: RequestEvent, method?: string): RequestEvent => {
25-
return _.extend(base || apiGatewayRequest(), { path: path, httpMethod: (method || 'GET') });
26-
};
27-
2824
const testWithLastResortHandler = (code: number, desc: string, testHeaders: string[] = [], expectedBody = ''): void => {
2925
const cb = spy(),
30-
evt = makeRequestEvent('/hello/world', albMultiValHeadersRequest()),
26+
evt = albMultiValHeadersRequest('/hello/world'),
3127
headers: StringMap = {},
3228
multiValueHeaders: StringArrayOfStringsMap = {};
3329

@@ -54,7 +50,7 @@ describe('integration tests', () => {
5450

5551
const testOutcome = (method: string, path: string, expectedBody: string): void => {
5652
const cb = spy(),
57-
evt = makeRequestEvent(path, apiGatewayRequest(), method);
53+
evt = apiGatewayRequest(path, method);
5854

5955
app.run(evt, handlerContext(), cb);
6056

@@ -121,7 +117,7 @@ describe('integration tests', () => {
121117
};
122118

123119
it('works - APIGW', () => {
124-
const cb = test(makeRequestEvent('/hello/world'));
120+
const cb = test(apiGatewayRequest('/hello/world'));
125121

126122
assert.calledWithExactly(cb, undefined, {
127123
statusCode: 200,
@@ -137,7 +133,7 @@ describe('integration tests', () => {
137133
});
138134

139135
it('works - ALB', () => {
140-
const cb = test(makeRequestEvent('/hello/world', albRequest()));
136+
const cb = test(albRequest('/hello/world'));
141137

142138
assert.calledWithExactly(cb, undefined, {
143139
statusCode: 200,
@@ -160,7 +156,7 @@ describe('integration tests', () => {
160156
});
161157

162158
it('works - ALBMV', () => {
163-
const cb = test(makeRequestEvent('/hello/world', albMultiValHeadersRequest()));
159+
const cb = test(albMultiValHeadersRequest('/hello/world'));
164160

165161
assert.calledWithExactly(cb, undefined, {
166162
statusCode: 200,
@@ -209,7 +205,7 @@ describe('integration tests', () => {
209205

210206
// "%EA" is the unicode code point for an "e with circumflex". The client should
211207
// be sending this character using UTF-8 encoding (i.e. %C3%AA)
212-
const evt = makeRequestEvent('/hello/%EA', albMultiValHeadersRequest()),
208+
const evt = albMultiValHeadersRequest('/hello/%EA'),
213209
cb = spy();
214210

215211
app.run(evt, handlerContext(), cb);
@@ -341,16 +337,16 @@ describe('integration tests', () => {
341337
describe('other HTTP methods', () => {
342338
// eslint-disable-next-line max-len,max-params
343339
const addTestsForMethod = (method: string, code: number, desc: string, hdrName: string, hdrVal: string, expectedBody: string, prep: () => void, contentType?: string): void => {
344-
const baseEvents = {
345-
'APIGW': apiGatewayRequest(),
346-
'ALB': albRequest(),
347-
'ALBMV': albMultiValHeadersRequest(),
340+
const baseEventGenerators = {
341+
'APIGW': apiGatewayRequest,
342+
'ALB': albRequest,
343+
'ALBMV': albMultiValHeadersRequest,
348344
};
349345

350-
_.each(baseEvents, (baseEvent, eventTypeName) => {
346+
_.each(baseEventGenerators, (baseEventGenerator, eventTypeName) => {
351347
it(`works with HTTP method ${method} - ${eventTypeName}`, () => {
352348
const cb = spy(),
353-
evt = makeRequestEvent('/hello/world', baseEvent, method);
349+
evt = baseEventGenerator('/hello/world', method);
354350

355351
// this request handler should get run for all methods:
356352
app.all('/hello/world', (_req: Request, resp: Response, next: NextCallback): void => {
@@ -760,7 +756,7 @@ describe('integration tests', () => {
760756
describe('request object', () => {
761757

762758
it('has an immutable context property', () => {
763-
let evt = makeRequestEvent('/test', apiGatewayRequest(), 'GET'),
759+
let evt = apiGatewayRequest('/test', 'GET'),
764760
ctx = handlerContext(true),
765761
handler;
766762

@@ -915,7 +911,7 @@ describe('integration tests', () => {
915911

916912
// "%EA" is the unicode code point for an "e with circumflex". The client should be
917913
// sending this character using UTF-8 encoding (i.e. %C3%AA)
918-
const evt = makeRequestEvent('/hello/%EA', albMultiValHeadersRequest()),
914+
const evt = albMultiValHeadersRequest('/hello/%EA'),
919915
cb = spy();
920916

921917
app.run(evt, handlerContext(), cb);

tests/samples.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import _ from 'underscore';
22
import {
3-
APIGatewayEventRequestContext,
3+
RequestEventRequestContext,
44
ApplicationLoadBalancerEventRequestContext,
55
APIGatewayRequestEvent,
6-
ApplicationLoadBalancerRequestEvent } from '../src/request-response-types';
6+
ApplicationLoadBalancerRequestEvent,
7+
RequestEvent,
8+
APIGatewayEventRequestContext,
9+
} from '../src/request-response-types';
710
import { Context } from 'aws-lambda';
811

912
export const handlerContext = (fillAllFields: boolean = false): Context => {
@@ -51,12 +54,15 @@ export const handlerContext = (fillAllFields: boolean = false): Context => {
5154
return ctx;
5255
};
5356

54-
export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {
57+
type SampleRequestContextGenerator<T extends RequestEventRequestContext> = (path?: string, method?: string) => T;
58+
type SampleRequestGenerator<T extends RequestEvent> = (path?: string, method?: string) => T;
59+
60+
export const apiGatewayRequestContext: SampleRequestContextGenerator<APIGatewayEventRequestContext> = (path?: string, method?: string) => {
5561
return {
5662
accountId: '123456789012',
5763
apiId: 'someapi',
5864
authorizer: null,
59-
httpMethod: 'GET',
65+
httpMethod: method || 'GET',
6066
identity: {
6167
accessKey: null,
6268
accountId: null,
@@ -74,7 +80,7 @@ export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {
7480
userAgent: 'curl/7.54.0',
7581
userArn: null,
7682
},
77-
path: '/prd',
83+
path: path || '/prd',
7884
protocol: 'HTTP/1.1',
7985
stage: 'prd',
8086
requestId: 'a507736b-259e-11e9-8fcf-4f1f08c4591e',
@@ -86,16 +92,16 @@ export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {
8692

8793
export const apiGatewayRequestRawQuery = '?&foo[a]=bar%20b&foo[a]=baz%20c&x=1&x=2&y=z';
8894

89-
export const apiGatewayRequest = (): APIGatewayRequestEvent => {
95+
export const apiGatewayRequest: SampleRequestGenerator<APIGatewayRequestEvent> = (path?: string, method?: string) => {
9096
return {
9197
body: null,
92-
httpMethod: 'GET',
98+
httpMethod: method || 'GET',
9399
isBase64Encoded: false,
94-
path: '/echo/asdf/a',
100+
path: path || '/echo/asdf/a',
95101
resource: '/{proxy+}',
96102
pathParameters: { proxy: 'echo/asdf/a' },
97103
stageVariables: null,
98-
requestContext: apiGatewayRequestContext(),
104+
requestContext: apiGatewayRequestContext(path, method),
99105
headers: {
100106
Accept: '*/*',
101107
'CloudFront-Forwarded-Proto': 'https',
@@ -148,28 +154,28 @@ export const apiGatewayRequest = (): APIGatewayRequestEvent => {
148154
};
149155
};
150156

151-
export const albRequestContext = (): ApplicationLoadBalancerEventRequestContext => {
157+
export const albRequestContext: SampleRequestContextGenerator<ApplicationLoadBalancerEventRequestContext> = () => {
152158
return {
153159
elb: {
154160
targetGroupArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-lambda-prd-tg1/180d40bbdb377b34',
155161
},
156162
};
157163
};
158164

159-
const albRequestBase = (): ApplicationLoadBalancerRequestEvent => {
165+
const albRequestBase = (path?: string, method?: string): ApplicationLoadBalancerRequestEvent => {
160166
return {
161167
body: '',
162-
httpMethod: 'GET',
168+
httpMethod: method || 'GET',
163169
isBase64Encoded: false,
164-
path: '/echo/asdf/a',
170+
path: path || '/echo/asdf/a',
165171
requestContext: albRequestContext(),
166172
};
167173
};
168174

169175
export const albRequestRawQuery = '?&foo%5Ba%5D=baz%20c&x=2&y=z';
170176

171-
export const albRequest = (): ApplicationLoadBalancerRequestEvent => {
172-
return _.extend({}, albRequestBase(), {
177+
export const albRequest: SampleRequestGenerator<ApplicationLoadBalancerRequestEvent> = (path?: string, method?: string) => {
178+
return _.extend({}, albRequestBase(path, method), {
173179
queryStringParameters: {
174180
'foo%5Ba%5D': 'baz%20c',
175181
x: '2',
@@ -193,8 +199,8 @@ export const albRequest = (): ApplicationLoadBalancerRequestEvent => {
193199

194200
export const albMultiValHeadersRawQuery = '?&foo%5Ba%5D=bar%20b&foo%5Ba%5D=baz%20c&x=1&x=2&y=z';
195201

196-
export const albMultiValHeadersRequest = (): ApplicationLoadBalancerRequestEvent => {
197-
return _.extend({}, albRequestBase(), {
202+
export const albMultiValHeadersRequest: SampleRequestGenerator<ApplicationLoadBalancerRequestEvent> = (path?: string, method?: string) => {
203+
return _.extend({}, albRequestBase(path, method), {
198204
multiValueQueryStringParameters: {
199205
'foo%5Ba%5D': [ 'bar%20b', 'baz c' ],
200206
x: [ '1', '2' ],

0 commit comments

Comments
 (0)