Skip to content

Commit f2a317f

Browse files
committed
refactor: replace usage of apiGatewayRequest by makeAPIGatewayRequestEvent on
Request tests - makeAPIGatewayRequestEvent accepts an input object, allowing the developer to change values of desired fields when needed, while apiGatewayRequest would always return the same object
1 parent b243995 commit f2a317f

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

tests/Request.test.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { expect } from 'chai';
33
import { Request, Application } from '../src';
44
import { RequestEvent } from '../src/request-response-types';
55
import {
6-
apiGatewayRequest,
76
handlerContext,
87
albRequest,
98
albMultiValHeadersRequest,
109
albRequestRawQuery,
1110
apiGatewayRequestRawQuery,
1211
albMultiValHeadersRawQuery,
12+
makeAPIGatewayRequestEvent,
1313
} from './samples';
1414
import { isKeyValueStringObject, Optional } from '@silvermine/toolbox';
1515
import ConsoleLogger from '../src/logging/ConsoleLogger';
@@ -21,9 +21,9 @@ describe('Request', () => {
2121

2222
beforeEach(() => {
2323
app = new Application();
24-
allEventTypes = [ apiGatewayRequest(), albRequest(), albMultiValHeadersRequest() ];
24+
allEventTypes = [ makeAPIGatewayRequestEvent(), albRequest(), albMultiValHeadersRequest() ];
2525
allRequestTypes = [
26-
new Request(app, apiGatewayRequest(), handlerContext()),
26+
new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
2727
new Request(app, albRequest(), handlerContext()),
2828
new Request(app, albMultiValHeadersRequest(), handlerContext()),
2929
];
@@ -113,7 +113,7 @@ describe('Request', () => {
113113
describe('header functionality', () => {
114114

115115
it('works with multi-value headers provided in the event (and is case-insensitive)', () => {
116-
let apigw = new Request(app, apiGatewayRequest(), handlerContext()),
116+
let apigw = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
117117
albmv = new Request(app, albMultiValHeadersRequest(), handlerContext());
118118

119119
_.each([ apigw, albmv ], (req) => {
@@ -250,7 +250,7 @@ describe('Request', () => {
250250

251251
it('parses proper values - APIGW', () => {
252252
_.each(testCases, (testCase) => {
253-
let evt: RequestEvent = apiGatewayRequest(),
253+
let evt: RequestEvent = makeAPIGatewayRequestEvent(),
254254
req;
255255

256256
evt.headers.Host = testCase.host;
@@ -311,19 +311,19 @@ describe('Request', () => {
311311
describe('ip property', () => {
312312

313313
it('parses correctly', () => {
314-
let req = new Request(app, apiGatewayRequest(), handlerContext()),
314+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
315315
evt: RequestEvent;
316316

317317
expect(req.ip).to.eql('12.12.12.12');
318318

319319
// API Gateway requests always use the one from the request context, so it
320320
// shouldn't matter what the 'trust proxy' setting is set to.
321321
app.enable('trust proxy');
322-
req = new Request(app, apiGatewayRequest(), handlerContext());
322+
req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
323323
expect(req.ip).to.eql('12.12.12.12');
324324

325325
app.disable('trust proxy');
326-
req = new Request(app, apiGatewayRequest(), handlerContext());
326+
req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
327327
expect(req.ip).to.eql('12.12.12.12');
328328

329329
// ALB requests don't have the IP in the request context, so it's dependent on
@@ -360,7 +360,7 @@ describe('Request', () => {
360360
it('properly detects event source', () => {
361361
let alb = new Request(app, albRequest(), handlerContext()),
362362
albmv = new Request(app, albMultiValHeadersRequest(), handlerContext()),
363-
apigw = new Request(app, apiGatewayRequest(), handlerContext());
363+
apigw = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
364364

365365
expect(alb.eventSourceType).to.eql(Request.SOURCE_ALB);
366366
expect(alb.isALB()).to.eql(true);
@@ -382,15 +382,15 @@ describe('Request', () => {
382382
let evt, req;
383383

384384
// APIGW should always be HTTPS, and not care about headers
385-
req = new Request(app, apiGatewayRequest(), handlerContext());
385+
req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
386386
app.disable('trust proxy');
387387
expect(req.protocol).to.eql('https');
388388
expect(req.secure).to.eql(true);
389389
app.enable('trust proxy');
390390
expect(req.protocol).to.eql('https');
391391
expect(req.secure).to.eql(true);
392392

393-
evt = apiGatewayRequest();
393+
evt = makeAPIGatewayRequestEvent();
394394
evt.headers['X-Forwarded-Proto'] = 'http';
395395
evt.multiValueHeaders['X-Forwarded-Proto'] = [ 'http' ];
396396
app.disable('trust proxy');
@@ -479,7 +479,7 @@ describe('Request', () => {
479479
});
480480

481481
it('parses arrays of values correctly - when multi-value is supported', () => {
482-
_.each([ apiGatewayRequest(), albMultiValHeadersRequest() ], (evt) => {
482+
_.each([ makeAPIGatewayRequestEvent(), albMultiValHeadersRequest() ], (evt) => {
483483
const req = new Request(app, evt, handlerContext());
484484

485485
expect(req.query.x).to.eql([ '1', '2' ]);
@@ -503,7 +503,7 @@ describe('Request', () => {
503503
}
504504
};
505505

506-
test(apiGatewayRequest(), [ 'bar b', 'baz c' ]);
506+
test(makeAPIGatewayRequestEvent(), [ 'bar b', 'baz c' ]);
507507
test(albRequest(), 'baz c');
508508
test(albMultiValHeadersRequest(), [ 'bar b', 'baz c' ]);
509509
});
@@ -537,7 +537,7 @@ describe('Request', () => {
537537
});
538538

539539
it('does not throw an error when bad values supplied', () => {
540-
const apigwReq = apiGatewayRequest();
540+
const apigwReq = makeAPIGatewayRequestEvent();
541541

542542
// Simulate a bad value (from an injection attack) being supplied
543543
apigwReq.queryStringParameters = {
@@ -558,7 +558,7 @@ describe('Request', () => {
558558
});
559559

560560
it('only contains the expected data', () => {
561-
let req = new Request(app, apiGatewayRequest(), handlerContext());
561+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
562562

563563
expect(req.query).to.eql({
564564
foo: { a: [ 'bar b', 'baz c' ] },
@@ -589,7 +589,7 @@ describe('Request', () => {
589589

590590
it('sets body to null for empty values', () => {
591591
_.each([ null, undefined, '' ], (body) => {
592-
let req = new Request(app, _.extend(apiGatewayRequest(), { body }), handlerContext());
592+
let req = new Request(app, _.extend(makeAPIGatewayRequestEvent(), { body }), handlerContext());
593593

594594
expect(req.body).to.strictlyEqual(null);
595595
});
@@ -604,7 +604,7 @@ describe('Request', () => {
604604

605605
_.each(bodies, (o) => {
606606
let ext = { body: JSON.stringify(o), multiValueHeaders: { 'Content-Type': [ 'application/json; charset=utf-8' ] } },
607-
req = new Request(app, _.extend(apiGatewayRequest(), ext), handlerContext());
607+
req = new Request(app, _.extend(makeAPIGatewayRequestEvent(), ext), handlerContext());
608608

609609
expect(req.body).to.eql(o);
610610
});
@@ -615,7 +615,7 @@ describe('Request', () => {
615615

616616
_.each(bodies, (body) => {
617617
let ext = { body, multiValueHeaders: { 'Content-Type': [ 'application/json; charset=utf-8' ] } },
618-
req = new Request(app, _.extend(apiGatewayRequest(), ext), handlerContext());
618+
req = new Request(app, _.extend(makeAPIGatewayRequestEvent(), ext), handlerContext());
619619

620620
expect(req.body).to.strictlyEqual(null);
621621
});
@@ -627,7 +627,7 @@ describe('Request', () => {
627627

628628
_.each(bodies, (body) => {
629629
let ext = { body, multiValueHeaders: { 'Content-Type': [ 'foo/bar; charset=utf-8' ] } },
630-
req = new Request(app, _.extend(apiGatewayRequest(), ext), handlerContext());
630+
req = new Request(app, _.extend(makeAPIGatewayRequestEvent(), ext), handlerContext());
631631

632632
expect(req.body).to.strictlyEqual(body);
633633
});
@@ -638,7 +638,7 @@ describe('Request', () => {
638638
describe('`url` property', () => {
639639

640640
it('should be able to be updated', () => {
641-
let req = new Request(app, apiGatewayRequest(), handlerContext()),
641+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
642642
newURL = '/test';
643643

644644
// Assert that we have a valid test
@@ -649,7 +649,7 @@ describe('Request', () => {
649649
});
650650

651651
it('should accept blank values', () => {
652-
let req = new Request(app, apiGatewayRequest(), handlerContext()),
652+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
653653
newURL = '';
654654

655655
// Assert that we have a valid test
@@ -660,7 +660,7 @@ describe('Request', () => {
660660
});
661661

662662
it('should update `path` when `url` changes', () => {
663-
let req = new Request(app, apiGatewayRequest(), handlerContext()),
663+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
664664
newURL = '/test';
665665

666666
// Assert that we have a valid test
@@ -671,7 +671,7 @@ describe('Request', () => {
671671
});
672672

673673
it('should update the parent request\'s `url` and related properties when a sub-request\'s `url` is updated', () => {
674-
let event = apiGatewayRequest(),
674+
let event = makeAPIGatewayRequestEvent(),
675675
req, subReq, subSubReq;
676676

677677
// Assert that we have a valid test
@@ -724,13 +724,13 @@ describe('Request', () => {
724724
}
725725

726726
it('exists and logs messages', () => {
727-
let req = new Request(app, apiGatewayRequest(), handlerContext());
727+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
728728

729729
testLog(req);
730730
});
731731

732732
it('is inherited from parent requests to sub-requests', () => {
733-
let req = new Request(app, apiGatewayRequest(), handlerContext()),
733+
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
734734
subReq = req.makeSubRequest('');
735735

736736
testLog(subReq);

0 commit comments

Comments
 (0)