Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit test coverage to the HTTPResponseAck class #1403

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/receivers/HTTPResponseAck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import sinon from 'sinon';
import { assert } from 'chai';
import { IncomingMessage, ServerResponse } from 'http';
import { HTTPResponseAck } from './HTTPResponseAck';
import { HTTPModuleFunctions } from './HTTPModuleFunctions';
import { ReceiverMultipleAckError } from '../errors';
import { createFakeLogger } from '../test-helpers';

describe('HTTPResponseAck', async () => {
Expand All @@ -19,4 +21,83 @@ describe('HTTPResponseAck', async () => {
assert.isDefined(ack.bind());
ack.ack(); // no exception
});
it('should trigger unhandledRequestHandler if unacknowledged', (done) => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const spy = sinon.spy();
// eslint-disable-next-line no-new
new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: false,
unhandledRequestTimeoutMillis: 1,
unhandledRequestHandler: spy,
httpRequest,
httpResponse,
});
setTimeout(() => {
assert(spy.calledOnce);
done();
}, 2);
});
it('should throw an error if a bound Ack invocation was already acknowledged', async () => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const ack = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: false,
httpRequest,
httpResponse,
});
const bound = ack.bind();
ack.ack();
try {
await bound();
assert.fail('No exception raised');
} catch (e) {
assert.instanceOf(e, ReceiverMultipleAckError);
}
});
it('should store response body if processBeforeResponse=true', async () => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const ack = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: true,
httpRequest,
httpResponse,
});
const bound = ack.bind();
const body = { some: 'thing' };
await bound(body);
assert.equal(ack.storedResponse, body, 'Body passed to bound handler not stored in Ack instance.');
});
it('should store an empty string if response body is falsy and processBeforeResponse=true', async () => {
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const ack = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: true,
httpRequest,
httpResponse,
});
const bound = ack.bind();
const body = false;
await bound(body);
assert.equal(ack.storedResponse, '', 'Falsy body passed to bound handler not stored as empty string in Ack instance.');
});
it('should call buildContentResponse with response body if processBeforeResponse=false', async () => {
const stub = sinon.stub(HTTPModuleFunctions, 'buildContentResponse');
const httpRequest = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const httpResponse: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;
const ack = new HTTPResponseAck({
logger: createFakeLogger(),
processBeforeResponse: false,
httpRequest,
httpResponse,
});
const bound = ack.bind();
const body = { some: 'thing' };
await bound(body);
assert(stub.calledWith(httpResponse, body), 'buildContentResponse called with HTTP Response object and response body.');
});
});