Skip to content

Commit

Permalink
fixup! address reviews, fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Mar 11, 2019
1 parent 7bb7ce8 commit 46b4322
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
35 changes: 25 additions & 10 deletions packages/rest/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context} from '@loopback/core';
import {
ReferenceObject,
RequestBodyObject,
SchemaObject,
} from '@loopback/openapi-v3-types';
import {ShotRequestOptions, stubExpressContext} from '@loopback/testlab';
import {IncomingMessage} from 'http';
import {LogError} from '..';
import {RestServerResolvedConfig} from '../rest.server';
import {LogError, RequestContext} from '..';
import {RestServerConfig, RestServerResolvedConfig} from '../rest.server';

export function createUnexpectedHttpErrorLogger(
expectedStatusCode: number = 0,
Expand Down Expand Up @@ -53,12 +55,25 @@ export function aBodySpec(
return spec as RequestBodyObject;
}

export function aRestServerConfig(): RestServerResolvedConfig {
return {
port: 3000,
openApiSpec: {disabled: true},
apiExplorer: {disabled: true},
cors: {},
expressSettings: {},
};
export function aRestServerConfig(
customConfig?: RestServerConfig,
): RestServerResolvedConfig {
return Object.assign(
{
port: 3000,
openApiSpec: {disabled: true},
apiExplorer: {disabled: true},
cors: {},
expressSettings: {},
},
customConfig,
);
}

export function givenRequestContext(
serverConfig: RestServerResolvedConfig = aRestServerConfig(),
requestOptions?: ShotRequestOptions,
): RequestContext {
const {request, response} = stubExpressContext(requestOptions);
return new RequestContext(request, response, new Context(), serverConfig);
}
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ describe('HttpHandler', () => {
.bind(RestBindings.REQUEST_BODY_PARSER)
.toClass(RequestBodyParser);

handler = new HttpHandler(rootContext, aRestServerConfig());
handler = new HttpHandler(rootContext, aRestServerConfig());
rootContext.bind(RestBindings.HANDLER).to(handler);
}

Expand Down
72 changes: 72 additions & 0 deletions packages/rest/src/__tests__/unit/request-context.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {aRestServerConfig, givenRequestContext} from '../helpers';

describe('RequestContext', () => {
describe('requestedProtocol', () => {
it('defaults to "http"', () => {
const ctx = givenRequestContext();
expect(ctx.requestedProtocol).to.equal('http');
});

it('honors "x-forwarded-proto" header', () => {
const ctx = givenRequestContext(aRestServerConfig(), {
url: '/',
headers: {'x-forwarded-proto': 'https'},
});
expect(ctx.requestedProtocol).to.equal('https');
});

it('honors protocol provided by Express request', () => {
const ctx = givenRequestContext(aRestServerConfig());

// tslint:disable-next-line:no-any
(ctx.request.connection as any).encrypted = true;
expect(ctx.request.protocol).to.equal('https');

expect(ctx.requestedProtocol).to.equal('https');
});
});

describe('basePath', () => {
it('defaults to an empty string', () => {
const ctx = givenRequestContext(aRestServerConfig());
expect(ctx.basePath).to.equal('');
});

it('honors baseUrl when mounted on a sub-path', () => {
const ctx = givenRequestContext(aRestServerConfig());
// emulate `expressApp.mount('/api', lb4app)
ctx.request.baseUrl = '/api';
expect(ctx.basePath).to.equal('/api');
});
});

describe('requestedBaseUrl', () => {
it('defaults to data from the HTTP connection', () => {
const ctx = givenRequestContext(
aRestServerConfig({
host: undefined,
port: 0,
}),
);
expect(ctx.requestedBaseUrl).to.equal('http://localhost');
});

it('honors "x-forwarded-*" headers', () => {
const ctx = givenRequestContext(aRestServerConfig(), {
url: '/products',
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-host': 'example.com',
'x-forwarded-port': '8080',
},
});
expect(ctx.requestedBaseUrl).to.equal('https://example.com:8080');
});
});
});

0 comments on commit 46b4322

Please sign in to comment.