mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
fix(microservices/nats): Check replyTo channel existence
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user