Skip to content

Commit

Permalink
Add exposed HTTP handler factory
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhoibm committed Jun 8, 2017
1 parent 1fb912f commit 96d4faf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/core/src/handlers/http.ts
@@ -0,0 +1,14 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/core
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Application} from '../application';
import {ServerRequest, ServerResponse} from 'http';
import {Handler} from '../internal-types';

export function httpHandler(app: Application): Handler {
return async (req: ServerRequest, res: ServerResponse): Promise<void> => {
await app.handleHttp(req, res);
};
}
6 changes: 6 additions & 0 deletions packages/core/src/handlers/index.ts
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/core
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './http';
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Expand Up @@ -6,6 +6,7 @@
// package dependencies
export {Application} from './application';
export {Component} from './component';
export {httpHandler} from './handlers';
export {api} from './router/metadata';
export {Sequence} from './sequence';
export {Server, ServerConfig, ServerState} from './server';
Expand All @@ -27,6 +28,7 @@ export {HttpErrors};
export {
ParsedRequest,
OperationRetval,
Handler,
} from './internal-types';
export {parseOperationArgs} from './parser';
export {parseRequestUrl} from './router/routing-table';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/internal-types.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {ServerRequest} from 'http';
import {ServerRequest, ServerResponse} from 'http';
import {ResolvedRoute} from './router/routing-table';

export interface ParsedRequest extends ServerRequest {
Expand All @@ -18,6 +18,7 @@ export interface ParsedRequest extends ServerRequest {
}

export type FindRoute = (request: ParsedRequest) => ResolvedRoute<string>;
export type Handler = (request: ServerRequest, response: ServerResponse) => Promise<void>;
export type InvokeMethod = (controller: string, method: string, args: OperationArgs) => Promise<OperationRetval>;
export type LogError = (err: Error, statusCode: number, request: ServerRequest) => void;

Expand Down
42 changes: 42 additions & 0 deletions packages/core/test/acceptance/handlers.acceptance.ts
@@ -0,0 +1,42 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/core
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
Application,
Handler,
httpHandler,
} from '../..';
import {expect, sinon} from '@loopback/testlab';

describe('http handler', () => {
let app: Application;
before(givenApp);

it('returns a Handler', () => {
const handler: Handler = httpHandler(app);
expect(handler).to.be.type('function');
});

context('Handler', () => {
// tslint:disable-next-line
let stub: any;
after(() => {
stub.restore();
});

it('calls the injected app\'s handler function when invoked', async () => {
stub = sinon.stub(app, 'handleHttp', () => true);
const handler = httpHandler(app);
expect(stub.calledOnce).to.be.false();
// tslint:disable-next-line
await handler({} as any, {} as any);
expect(stub.calledOnce).to.be.true();
});
});

function givenApp() {
app = new Application();
}
});

0 comments on commit 96d4faf

Please sign in to comment.