fix(microservices/nats): Check replyTo channel existence

This commit is contained in:
Vladimir Borovik
2020-10-05 16:57:50 +03:00
parent a7a427def7
commit 4d7c2651dc
2 changed files with 23 additions and 9 deletions

View File

@@ -123,11 +123,17 @@ export class ServerNats extends Server implements CustomTransportStrategy {
}
public getPublisher(publisher: Client, replyTo: string, id: string) {
return (response: any) => {
Object.assign(response, { id });
const outgoingResponse = this.serializer.serialize(response);
return publisher.publish(replyTo, outgoingResponse);
};
if (replyTo) {
return (response: any) => {
Object.assign(response, { id });
const outgoingResponse = this.serializer.serialize(response);
return publisher.publish(replyTo, outgoingResponse);
};
}
// request doesn't need reply, return noop function
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
}
public handleError(stream: any) {

View File

@@ -142,23 +142,31 @@ describe('ServerNats', () => {
let pub, publisher;
const id = '1';
const replyTo = 'test';
beforeEach(() => {
publisherSpy = sinon.spy();
pub = {
publish: publisherSpy,
};
publisher = server.getPublisher(pub, replyTo, id);
});
it(`should return function`, () => {
expect(typeof server.getPublisher(null, null, id)).to.be.eql('function');
});
it(`should call "publish" with expected arguments`, () => {
it(`should call "publish" when replyTo provided`, () => {
const replyTo = 'test';
publisher = server.getPublisher(pub, replyTo, id);
const respond = 'test';
publisher({ respond, id });
expect(publisherSpy.calledWith(replyTo, { respond, id })).to.be.true;
});
it(`should not call "publish" when replyTo NOT provided`, () => {
const replyTo = undefined;
publisher = server.getPublisher(pub, replyTo, id);
const respond = 'test';
publisher({ respond, id });
expect(publisherSpy.notCalled);
});
});
describe('handleEvent', () => {
const channel = 'test';