Skip to content

Commit

Permalink
Only executing when customRequests available
Browse files Browse the repository at this point in the history
  • Loading branch information
rishigupta1599 committed May 22, 2024
1 parent 61cf42a commit 0214bef
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
15 changes: 15 additions & 0 deletions lib/core/CustomRequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ class CustomRequestHandler {
// Return the existing instance
return CustomRequestHandler.instance;
}

isCustomRequestListEmpty() {
for (const prop in this.customRequestList) {
if (this.customRequestList.hasOwnProperty(prop)) {
return false;
}
}

return true;
}

// only for specs purpose, don't use it in code.
static resetInstance() {
CustomRequestHandler.instance = null;
}
// Method to add an item to the list
addCustomRequest(request_id) {
let resolveFunc;
Expand Down
8 changes: 4 additions & 4 deletions lib/core/OutgoingWebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class OutgoingWebSocket extends EventEmitter {
return;
}

if (customRequestEnabled) {
const customReqInstance = CustomRequestHandler.getInstance();
const customReqInstance = CustomRequestHandler.getInstance();
if (customRequestEnabled && !customReqInstance.isCustomRequestListEmpty()) {
let resp;
try {
resp = JSON.parse(msg);
Expand Down Expand Up @@ -152,8 +152,8 @@ class OutgoingWebSocket extends EventEmitter {
* Triggers when error occured on socket.
*/
errorHandler(error) {
if (customRequestEnabled) {
const customReqInstance = CustomRequestHandler.getInstance();
const customReqInstance = CustomRequestHandler.getInstance();
if (customRequestEnabled && !customReqInstance.isCustomRequestListEmpty()) {
let resp;
try {
resp = JSON.parse(error);
Expand Down
36 changes: 36 additions & 0 deletions test/core/customRequestHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,46 @@ describe('CustomRequestHandler', () => {

describe('getList method', () => {
it('should return the customRequestList', () => {
CustomRequestHandler.resetInstance();
const customRequestHandler = new CustomRequestHandler();
const list = customRequestHandler.getList();
assert.isObject(list);
assert.isEmpty(list);
});
});

describe('#isCustomRequestListEmpty', () => {
beforeEach(() => {
CustomRequestHandler.resetInstance();
});
it('should return true if the custom request list is empty', () => {
const customRequestHandler = new CustomRequestHandler();
const result = customRequestHandler.isCustomRequestListEmpty();
assert.isTrue(result);
});

it('should return false if the custom request list is not empty', () => {
const customRequestHandler = new CustomRequestHandler();
customRequestHandler.addCustomRequest('request_1');
const result = customRequestHandler.isCustomRequestListEmpty();
assert.isFalse(result);
});

it('should handle multiple items in the custom request list', () => {
const customRequestHandler = new CustomRequestHandler();
customRequestHandler.addCustomRequest('request_1');
customRequestHandler.addCustomRequest('request_2');
const result = customRequestHandler.isCustomRequestListEmpty();
assert.isFalse(result);
});

it('should return true if some properties are directly defined on the object', () => {
const customRequestHandler = new CustomRequestHandler();
customRequestHandler.customRequestList = Object.create({
name: 'inherited',
});
const result = customRequestHandler.isCustomRequestListEmpty();
assert.isTrue(result);
});
});
});
45 changes: 45 additions & 0 deletions test/core/outgoingWebSocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { resolve: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand All @@ -233,6 +234,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { resolve: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand All @@ -256,6 +258,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { resolve: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand Down Expand Up @@ -297,6 +300,25 @@ describe('OutgoingWebSocket', () => {
assert(msgSpy.calledOnce);
assert(msgSpy.calledWith(kMessageReceived, msg));
});

it('should not handle custom requests when no custom requests are pending', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { resolve: spy() } },
isCustomRequestListEmpty: stub().returns(true),
};
customRequestHandler = stub(
CustomRequestHandler,
'getInstance'
).returns(mockCustomRequestHandler);
outgoingWsCustom = new OutgoingWebSocketWithMock(upstreamUrl, headers);
msgSpy = spy();
outgoingWsCustom.emit = msgSpy;
const msg = '{"id": "customRequestId"}';
outgoingWsCustom.messageHandler(msg);
assert(msgSpy.calledOnce);
assert(msgSpy.calledWith(kMessageReceived, msg));
});
});
});

Expand Down Expand Up @@ -385,6 +407,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { reject: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand All @@ -407,6 +430,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { reject: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand All @@ -430,6 +454,7 @@ describe('OutgoingWebSocket', () => {
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { reject: spy() } },
isCustomRequestListEmpty: stub().returns(false),
};
customRequestHandler = stub(
CustomRequestHandler,
Expand Down Expand Up @@ -471,6 +496,26 @@ describe('OutgoingWebSocket', () => {
assert(msgSpy.calledOnce);
assert(msgSpy.calledWith(kError, msg));
});

it('should not handle custom requests when no custom requests are pending', () => {
// Mock CustomRequestHandler
const mockCustomRequestHandler = {
getList: stub().returns({ customRequestId: {} }),
customRequestList: { customRequestId: { resolve: spy() } },
isCustomRequestListEmpty: stub().returns(true),
};
customRequestHandler = stub(
CustomRequestHandler,
'getInstance'
).returns(mockCustomRequestHandler);
outgoingWsCustom = new OutgoingWebSocketWithMock(upstreamUrl, headers);
msgSpy = spy();
outgoingWsCustom.emit = msgSpy;
const msg = '{"id": "customRequestId"}';
outgoingWsCustom.errorHandler(msg);
assert(msgSpy.calledOnce);
assert(msgSpy.calledWith(kError, msg));
});
});
});

Expand Down

0 comments on commit 0214bef

Please sign in to comment.