Skip to content

Commit

Permalink
Added testing for reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
satcheluniverse committed Mar 30, 2023
1 parent 00eed6f commit d90256f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
41 changes: 39 additions & 2 deletions src/receivers/HTTPReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ describe('HTTPReceiver', function () {

it('should call custom route handler only if request matches multiple route paths and method including params', async function () {
const HTTPReceiver = await importHTTPReceiver();
const customRoutes = [{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake.throws('Should not be used.') }];
const customRoutes = [
{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake() },
];
const matchRegex = match(customRoutes[0].path, { decode: decodeURIComponent });
const receiver = new HTTPReceiver({
clientSecret: 'my-client-secret',
Expand All @@ -596,6 +598,41 @@ describe('HTTPReceiver', function () {
fakeReq.method = 'GET';
receiver.requestListener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));
assert(customRoutes[1].handler.notCalled);

fakeReq.method = 'POST';
receiver.requestListener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));

fakeReq.method = 'UNHANDLED_METHOD';
assert.throws(() => receiver.requestListener(fakeReq, fakeRes), HTTPReceiverDeferredRequestError);
});

it('should call custom route handler only if request matches multiple route paths and method including params reverse order', async function () {
const HTTPReceiver = await importHTTPReceiver();
const customRoutes = [
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
];
const matchRegex = match(customRoutes[0].path, { decode: decodeURIComponent });
const receiver = new HTTPReceiver({
clientSecret: 'my-client-secret',
signingSecret: 'secret',
customRoutes,
});

const fakeReq: IncomingMessage = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const fakeRes: ServerResponse = sinon.createStubInstance(ServerResponse) as unknown as ServerResponse;

fakeReq.url = '/test/123';
const tempMatch = matchRegex(fakeReq.url);
if (!tempMatch) throw new Error('match failed');
const params : ParamsDictionary = tempMatch.params as ParamsDictionary;

fakeReq.method = 'GET';
receiver.requestListener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));
assert(customRoutes[1].handler.notCalled);

fakeReq.method = 'POST';
receiver.requestListener(fakeReq, fakeRes);
Expand Down
53 changes: 51 additions & 2 deletions src/receivers/SocketModeReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ describe('SocketModeReceiver', function () {
withHttpsCreateServer(sinon.fake.throws('Should not be used.')),
);
const SocketModeReceiver = await importSocketModeReceiver(overrides);
const customRoutes = [{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake.throws('Should not be used.') }];
const customRoutes = [
{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake() },
];
const matchRegex = match(customRoutes[0].path, { decode: decodeURIComponent });

const receiver = new SocketModeReceiver({
Expand All @@ -579,6 +581,53 @@ describe('SocketModeReceiver', function () {
fakeReq.method = 'GET';
await this.listener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));
assert(customRoutes[1].handler.notCalled);

fakeReq.method = 'POST';
await this.listener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));
assert(customRoutes[1].handler.notCalled);

fakeReq.method = 'UNHANDLED_METHOD';
await this.listener(fakeReq, fakeRes);
assert(fakeRes.writeHead.calledWith(404, sinon.match.object));
assert(fakeRes.end.called);
});

it('should call custom route handler only if request matches multiple route paths and method including params reverse order', async function () {
// Arrange
const installProviderStub = sinon.createStubInstance(InstallProvider);
const overrides = mergeOverrides(
withHttpCreateServer(this.fakeCreateServer),
withHttpsCreateServer(sinon.fake.throws('Should not be used.')),
);
const SocketModeReceiver = await importSocketModeReceiver(overrides);
const customRoutes = [
{ path: '/test/:id', method: ['get', 'POST'], handler: sinon.fake() },
{ path: '/test/123', method: ['get', 'POST'], handler: sinon.fake() },
];
const matchRegex = match(customRoutes[0].path, { decode: decodeURIComponent });

const receiver = new SocketModeReceiver({
appToken: 'my-secret',
customRoutes,
});
assert.isNotNull(receiver);
receiver.installer = installProviderStub as unknown as InstallProvider;

const fakeReq: IncomingMessage = sinon.createStubInstance(IncomingMessage) as IncomingMessage;
const fakeRes = { writeHead: sinon.fake(), end: sinon.fake() };

fakeReq.url = '/test/123';
const tempMatch = matchRegex(fakeReq.url);
if (!tempMatch) throw new Error('match failed');
const params : ParamsDictionary = tempMatch.params as ParamsDictionary;
fakeReq.headers = { host: 'localhost' };

fakeReq.method = 'GET';
await this.listener(fakeReq, fakeRes);
assert(customRoutes[0].handler.calledWith({ ...fakeReq, params }, fakeRes));
assert(customRoutes[1].handler.notCalled);

fakeReq.method = 'POST';
await this.listener(fakeReq, fakeRes);
Expand Down

0 comments on commit d90256f

Please sign in to comment.