Skip to content

Commit 946741e

Browse files
fix: cleanup response payload, fix tests
1 parent b1f4553 commit 946741e

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ coverage
55
.github
66
.husky
77
.idea
8+
.run
89
.nyc_output
910
.gitattributes
1011
.editorconfig

__tests__/services/gateway-test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,20 @@ describe('services/gateway', () => {
7878
it('should correct start microservice without info route', async () => {
7979
const sandbox = sinon.createSandbox();
8080

81-
sandbox.stub(ms.getExpress(), 'listen').returns({ close: sinon.stub() } as unknown as Server);
81+
const localMs = Gateway.create({ infoRoute: null });
82+
const getStub = sinon.stub();
83+
84+
sandbox
85+
.stub(localMs.getExpress(), 'listen')
86+
.returns({ close: sinon.stub(), get: getStub } as unknown as Server);
8287
sandbox.stub(axios, 'request').rejects(new Error('ECONNREFUSED'));
8388
sandbox.stub(Gateway, 'instance' as any).value(undefined);
8489

85-
const localMs = Gateway.create({ infoRoute: null });
86-
8790
await localMs.start();
8891

89-
const infoRoute = _.findLast(localMs.getExpress()._router.stack, {
90-
route: { path: '/', methods: { get: true } },
91-
});
92-
9392
sandbox.restore();
9493

95-
expect(infoRoute).to.undefined;
94+
expect(getStub).to.not.called;
9695
});
9796

9897
it('should correct register microservice handler', () => {
@@ -303,8 +302,6 @@ describe('services/gateway', () => {
303302
const response = res.json.firstCall.firstArg;
304303
const { data, headers } = stubbed.firstCall.firstArg;
305304

306-
_.unset(response.result, 'payload');
307-
308305
expect(response.getResult()).to.deep.equal({ endpointTriggerMiddleware, middleware: 'after' });
309306
expect(data.method).to.equal(endpointTriggerMiddleware);
310307
expect(data.params.middleware).to.equal('before');

src/services/gateway.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ class Gateway extends AbstractMicroservice {
237237
this.logDriver(() => `End batch request: ${body.length} (${id}).`, LogType.RES_EXTERNAL, id);
238238
}
239239

240-
// cookie manipulations
241240
(Array.isArray(response) ? response : [response]).forEach((msRes) => {
242241
const reqPayload = msRes?.getResult()?.payload;
243242
const cookies = reqPayload?.cookies;
244243

244+
// cookie manipulations
245245
if (Array.isArray(cookies)) {
246246
_.unset(reqPayload, 'cookies');
247247

@@ -254,10 +254,16 @@ class Gateway extends AbstractMicroservice {
254254
res.clearCookie(cookie.name);
255255
}
256256
});
257+
}
258+
259+
// remove performance payload
260+
if (reqPayload?.performance) {
261+
_.unset(msRes?.getResult(), 'payload.performance');
262+
}
257263

258-
if (reqPayload && Object.keys(reqPayload).length === 0) {
259-
_.unset(msRes?.getResult(), 'payload');
260-
}
264+
// remove payload key if it empties
265+
if (reqPayload && Object.keys(reqPayload).length === 0) {
266+
_.unset(msRes?.getResult(), 'payload');
261267
}
262268
});
263269

0 commit comments

Comments
 (0)