Skip to content

Commit 0214bef

Browse files
Only executing when customRequests available
1 parent 61cf42a commit 0214bef

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

lib/core/CustomRequestHandler.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ class CustomRequestHandler {
2828
// Return the existing instance
2929
return CustomRequestHandler.instance;
3030
}
31+
32+
isCustomRequestListEmpty() {
33+
for (const prop in this.customRequestList) {
34+
if (this.customRequestList.hasOwnProperty(prop)) {
35+
return false;
36+
}
37+
}
38+
39+
return true;
40+
}
41+
42+
// only for specs purpose, don't use it in code.
43+
static resetInstance() {
44+
CustomRequestHandler.instance = null;
45+
}
3146
// Method to add an item to the list
3247
addCustomRequest(request_id) {
3348
let resolveFunc;

lib/core/OutgoingWebSocket.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class OutgoingWebSocket extends EventEmitter {
110110
return;
111111
}
112112

113-
if (customRequestEnabled) {
114-
const customReqInstance = CustomRequestHandler.getInstance();
113+
const customReqInstance = CustomRequestHandler.getInstance();
114+
if (customRequestEnabled && !customReqInstance.isCustomRequestListEmpty()) {
115115
let resp;
116116
try {
117117
resp = JSON.parse(msg);
@@ -152,8 +152,8 @@ class OutgoingWebSocket extends EventEmitter {
152152
* Triggers when error occured on socket.
153153
*/
154154
errorHandler(error) {
155-
if (customRequestEnabled) {
156-
const customReqInstance = CustomRequestHandler.getInstance();
155+
const customReqInstance = CustomRequestHandler.getInstance();
156+
if (customRequestEnabled && !customReqInstance.isCustomRequestListEmpty()) {
157157
let resp;
158158
try {
159159
resp = JSON.parse(error);

test/core/customRequestHandler.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,46 @@ describe('CustomRequestHandler', () => {
5555

5656
describe('getList method', () => {
5757
it('should return the customRequestList', () => {
58+
CustomRequestHandler.resetInstance();
5859
const customRequestHandler = new CustomRequestHandler();
5960
const list = customRequestHandler.getList();
6061
assert.isObject(list);
6162
assert.isEmpty(list);
6263
});
6364
});
65+
66+
describe('#isCustomRequestListEmpty', () => {
67+
beforeEach(() => {
68+
CustomRequestHandler.resetInstance();
69+
});
70+
it('should return true if the custom request list is empty', () => {
71+
const customRequestHandler = new CustomRequestHandler();
72+
const result = customRequestHandler.isCustomRequestListEmpty();
73+
assert.isTrue(result);
74+
});
75+
76+
it('should return false if the custom request list is not empty', () => {
77+
const customRequestHandler = new CustomRequestHandler();
78+
customRequestHandler.addCustomRequest('request_1');
79+
const result = customRequestHandler.isCustomRequestListEmpty();
80+
assert.isFalse(result);
81+
});
82+
83+
it('should handle multiple items in the custom request list', () => {
84+
const customRequestHandler = new CustomRequestHandler();
85+
customRequestHandler.addCustomRequest('request_1');
86+
customRequestHandler.addCustomRequest('request_2');
87+
const result = customRequestHandler.isCustomRequestListEmpty();
88+
assert.isFalse(result);
89+
});
90+
91+
it('should return true if some properties are directly defined on the object', () => {
92+
const customRequestHandler = new CustomRequestHandler();
93+
customRequestHandler.customRequestList = Object.create({
94+
name: 'inherited',
95+
});
96+
const result = customRequestHandler.isCustomRequestListEmpty();
97+
assert.isTrue(result);
98+
});
99+
});
64100
});

test/core/outgoingWebSocket.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ describe('OutgoingWebSocket', () => {
211211
const mockCustomRequestHandler = {
212212
getList: stub().returns({ customRequestId: {} }),
213213
customRequestList: { customRequestId: { resolve: spy() } },
214+
isCustomRequestListEmpty: stub().returns(false),
214215
};
215216
customRequestHandler = stub(
216217
CustomRequestHandler,
@@ -233,6 +234,7 @@ describe('OutgoingWebSocket', () => {
233234
const mockCustomRequestHandler = {
234235
getList: stub().returns({ customRequestId: {} }),
235236
customRequestList: { customRequestId: { resolve: spy() } },
237+
isCustomRequestListEmpty: stub().returns(false),
236238
};
237239
customRequestHandler = stub(
238240
CustomRequestHandler,
@@ -256,6 +258,7 @@ describe('OutgoingWebSocket', () => {
256258
const mockCustomRequestHandler = {
257259
getList: stub().returns({ customRequestId: {} }),
258260
customRequestList: { customRequestId: { resolve: spy() } },
261+
isCustomRequestListEmpty: stub().returns(false),
259262
};
260263
customRequestHandler = stub(
261264
CustomRequestHandler,
@@ -297,6 +300,25 @@ describe('OutgoingWebSocket', () => {
297300
assert(msgSpy.calledOnce);
298301
assert(msgSpy.calledWith(kMessageReceived, msg));
299302
});
303+
304+
it('should not handle custom requests when no custom requests are pending', () => {
305+
const mockCustomRequestHandler = {
306+
getList: stub().returns({ customRequestId: {} }),
307+
customRequestList: { customRequestId: { resolve: spy() } },
308+
isCustomRequestListEmpty: stub().returns(true),
309+
};
310+
customRequestHandler = stub(
311+
CustomRequestHandler,
312+
'getInstance'
313+
).returns(mockCustomRequestHandler);
314+
outgoingWsCustom = new OutgoingWebSocketWithMock(upstreamUrl, headers);
315+
msgSpy = spy();
316+
outgoingWsCustom.emit = msgSpy;
317+
const msg = '{"id": "customRequestId"}';
318+
outgoingWsCustom.messageHandler(msg);
319+
assert(msgSpy.calledOnce);
320+
assert(msgSpy.calledWith(kMessageReceived, msg));
321+
});
300322
});
301323
});
302324

@@ -385,6 +407,7 @@ describe('OutgoingWebSocket', () => {
385407
const mockCustomRequestHandler = {
386408
getList: stub().returns({ customRequestId: {} }),
387409
customRequestList: { customRequestId: { reject: spy() } },
410+
isCustomRequestListEmpty: stub().returns(false),
388411
};
389412
customRequestHandler = stub(
390413
CustomRequestHandler,
@@ -407,6 +430,7 @@ describe('OutgoingWebSocket', () => {
407430
const mockCustomRequestHandler = {
408431
getList: stub().returns({ customRequestId: {} }),
409432
customRequestList: { customRequestId: { reject: spy() } },
433+
isCustomRequestListEmpty: stub().returns(false),
410434
};
411435
customRequestHandler = stub(
412436
CustomRequestHandler,
@@ -430,6 +454,7 @@ describe('OutgoingWebSocket', () => {
430454
const mockCustomRequestHandler = {
431455
getList: stub().returns({ customRequestId: {} }),
432456
customRequestList: { customRequestId: { reject: spy() } },
457+
isCustomRequestListEmpty: stub().returns(false),
433458
};
434459
customRequestHandler = stub(
435460
CustomRequestHandler,
@@ -471,6 +496,26 @@ describe('OutgoingWebSocket', () => {
471496
assert(msgSpy.calledOnce);
472497
assert(msgSpy.calledWith(kError, msg));
473498
});
499+
500+
it('should not handle custom requests when no custom requests are pending', () => {
501+
// Mock CustomRequestHandler
502+
const mockCustomRequestHandler = {
503+
getList: stub().returns({ customRequestId: {} }),
504+
customRequestList: { customRequestId: { resolve: spy() } },
505+
isCustomRequestListEmpty: stub().returns(true),
506+
};
507+
customRequestHandler = stub(
508+
CustomRequestHandler,
509+
'getInstance'
510+
).returns(mockCustomRequestHandler);
511+
outgoingWsCustom = new OutgoingWebSocketWithMock(upstreamUrl, headers);
512+
msgSpy = spy();
513+
outgoingWsCustom.emit = msgSpy;
514+
const msg = '{"id": "customRequestId"}';
515+
outgoingWsCustom.errorHandler(msg);
516+
assert(msgSpy.calledOnce);
517+
assert(msgSpy.calledWith(kError, msg));
518+
});
474519
});
475520
});
476521

0 commit comments

Comments
 (0)