Skip to content

Commit

Permalink
chore: create a more robust check for NodeApplication type in http pr…
Browse files Browse the repository at this point in the history
…oxy (#191)
  • Loading branch information
tjholm committed Jun 28, 2023
2 parents 87e7348 + 124e118 commit d0ddcd3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
41 changes: 36 additions & 5 deletions src/resources/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('HTTP Proxy', () => {
.spyOn(faas.Faas.prototype, 'start')
.mockReturnValue(Promise.resolve());

const mockCallback = jest.fn();
const mockApp = {
listen: () => {},
};
Expand All @@ -29,7 +28,7 @@ describe('HTTP Proxy', () => {
jest.clearAllMocks();
});

describe('when creating a new http proxy', () => {
describe('when creating a new http proxy with an app', () => {
let error = undefined;
afterAll(() => {
jest.resetAllMocks();
Expand All @@ -48,21 +47,53 @@ describe('HTTP Proxy', () => {
});
});

describe(`when creating a new http proxy`, () => {
describe(`when creating a new http proxy with an app and callback`, () => {
const fakeCallback = () => {};

afterAll(() => {
jest.resetAllMocks();
});

beforeAll(async () => {
http(mockApp, 1234, fakeCallback);
});

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with HttpWorkerOptions', () => {
const expectedOpts = new HttpWorkerOptions(mockApp, 1234, fakeCallback);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});

describe(`when creating a new http proxy with a bootstrap function`, () => {
const fakeFunc = () => {};
const fakeCallback = () => {};

afterAll(() => {
jest.resetAllMocks();
});

beforeAll(async () => {
http(mockApp, 1234, mockCallback);
http(fakeFunc, 1234, fakeCallback);
});

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with HttpWorkerOptions', () => {
const expectedOpts = new HttpWorkerOptions(mockApp, 1234, mockCallback);
const expectedOpts = new HttpWorkerOptions(
{ listen: fakeFunc },
1234,
fakeCallback
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

Expand Down
12 changes: 7 additions & 5 deletions src/resources/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export const http = (
port: number,
callback?: () => void
) => {
new HttpWorker(
app instanceof Function ? { listen: app } : app,
port,
callback
);
const nodeApp =
app.hasOwnProperty('listen') &&
typeof (app as NodeApplication).listen === 'function'
? (app as NodeApplication)
: { listen: app as ListenerFunction };

new HttpWorker(nodeApp, port, callback);
};

0 comments on commit d0ddcd3

Please sign in to comment.