Skip to content
67 changes: 32 additions & 35 deletions tests/Request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { expect } from 'chai';
import { Request, Application } from '../src';
import { RequestEvent } from '../src/request-response-types';
import {
apiGatewayRequest,
handlerContext,
albRequest,
albMultiValHeadersRequest,
albRequestRawQuery,
apiGatewayRequestRawQuery,
albMultiValHeadersRawQuery,
makeAPIGatewayRequestEvent,
} from './samples';
import { isKeyValueStringObject, Optional } from '@silvermine/toolbox';
import ConsoleLogger from '../src/logging/ConsoleLogger';
Expand All @@ -21,9 +21,9 @@ describe('Request', () => {

beforeEach(() => {
app = new Application();
allEventTypes = [ apiGatewayRequest(), albRequest(), albMultiValHeadersRequest() ];
allEventTypes = [ makeAPIGatewayRequestEvent(), albRequest(), albMultiValHeadersRequest() ];
allRequestTypes = [
new Request(app, apiGatewayRequest(), handlerContext()),
new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
new Request(app, albRequest(), handlerContext()),
new Request(app, albMultiValHeadersRequest(), handlerContext()),
];
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Request', () => {
describe('header functionality', () => {

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

_.each([ apigw, albmv ], (req) => {
Expand Down Expand Up @@ -250,15 +250,16 @@ describe('Request', () => {

it('parses proper values - APIGW', () => {
_.each(testCases, (testCase) => {
let evt: RequestEvent = apiGatewayRequest(),
req;
const evt: RequestEvent = makeAPIGatewayRequestEvent({
headers: {
'X-Forwarded-Host': testCase.xForwardedHost,
'Host': testCase.host,
},
});

evt.headers.Host = testCase.host;
let req;

if (testCase.xForwardedHost) {
evt.headers['X-Forwarded-Host'] = testCase.xForwardedHost;
evt.multiValueHeaders['X-Forwarded-Host'] = [ testCase.xForwardedHost ];
} else {
if (!testCase.xForwardedHost) {
delete evt.headers['X-Forwarded-Host'];
delete evt.multiValueHeaders['X-Forwarded-Host'];
}
Expand Down Expand Up @@ -311,19 +312,19 @@ describe('Request', () => {
describe('ip property', () => {

it('parses correctly', () => {
let req = new Request(app, apiGatewayRequest(), handlerContext()),
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
evt: RequestEvent;

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

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

app.disable('trust proxy');
req = new Request(app, apiGatewayRequest(), handlerContext());
req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
expect(req.ip).to.eql('12.12.12.12');

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

expect(alb.eventSourceType).to.eql(Request.SOURCE_ALB);
expect(alb.isALB()).to.eql(true);
Expand All @@ -379,20 +380,16 @@ describe('Request', () => {
describe('protocol / secure properties', () => {

it('parses proper values - APIGW', () => {
let evt, req;

// APIGW should always be HTTPS, and not care about headers
req = new Request(app, apiGatewayRequest(), handlerContext());
const req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());

app.disable('trust proxy');
expect(req.protocol).to.eql('https');
expect(req.secure).to.eql(true);
app.enable('trust proxy');
expect(req.protocol).to.eql('https');
expect(req.secure).to.eql(true);

evt = apiGatewayRequest();
evt.headers['X-Forwarded-Proto'] = 'http';
evt.multiValueHeaders['X-Forwarded-Proto'] = [ 'http' ];
app.disable('trust proxy');
expect(req.protocol).to.eql('https');
expect(req.secure).to.eql(true);
Expand Down Expand Up @@ -479,7 +476,7 @@ describe('Request', () => {
});

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

expect(req.query.x).to.eql([ '1', '2' ]);
Expand All @@ -503,7 +500,7 @@ describe('Request', () => {
}
};

test(apiGatewayRequest(), [ 'bar b', 'baz c' ]);
test(makeAPIGatewayRequestEvent(), [ 'bar b', 'baz c' ]);
test(albRequest(), 'baz c');
test(albMultiValHeadersRequest(), [ 'bar b', 'baz c' ]);
});
Expand Down Expand Up @@ -537,7 +534,7 @@ describe('Request', () => {
});

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

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

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

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

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

expect(req.body).to.strictlyEqual(null);
});
Expand All @@ -604,7 +601,7 @@ describe('Request', () => {

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

expect(req.body).to.eql(o);
});
Expand All @@ -615,7 +612,7 @@ describe('Request', () => {

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

expect(req.body).to.strictlyEqual(null);
});
Expand All @@ -627,7 +624,7 @@ describe('Request', () => {

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

expect(req.body).to.strictlyEqual(body);
});
Expand All @@ -638,7 +635,7 @@ describe('Request', () => {
describe('`url` property', () => {

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

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

it('should accept blank values', () => {
let req = new Request(app, apiGatewayRequest(), handlerContext()),
let req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
newURL = '';

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

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

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

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

// Assert that we have a valid test
Expand Down Expand Up @@ -724,13 +721,13 @@ describe('Request', () => {
}

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

testLog(req);
});

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

testLog(subReq);
Expand Down
Loading