2023-10-19 21:48:44 +02:00
|
|
|
import NatsAdaptor from "../src/adaptors/nats";
|
2023-10-19 22:53:31 +02:00
|
|
|
import { RequestError } from "../src/error";
|
2023-10-19 21:48:44 +02:00
|
|
|
import { Message } from "../src/messages";
|
|
|
|
import Service from "../src/service";
|
|
|
|
|
|
|
|
let srv!: Service;
|
|
|
|
|
2023-10-19 22:53:31 +02:00
|
|
|
const natsServer = '127.0.0.1:4222';
|
2023-10-19 21:48:44 +02:00
|
|
|
const serviceName = "test-service";
|
|
|
|
const adaptorName = "nats";
|
|
|
|
const subject = "test";
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
srv = new Service(serviceName);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await srv.stop();
|
|
|
|
});
|
|
|
|
|
2023-10-19 23:27:51 +02:00
|
|
|
test('request success', async () => {
|
2023-10-19 22:53:31 +02:00
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
|
|
|
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
2023-10-19 21:48:44 +02:00
|
|
|
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
|
|
|
return {data: msg.data, statusCode: statusCodeExpected};
|
|
|
|
});
|
|
|
|
|
|
|
|
await srv.listen();
|
|
|
|
|
|
|
|
const res = await srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected});
|
|
|
|
|
|
|
|
expect(res.data).toBe(dataExpected);
|
|
|
|
expect(res.statusCode).toBe(statusCodeExpected);
|
|
|
|
});
|
|
|
|
|
2023-10-19 23:27:51 +02:00
|
|
|
test('request error', async () => {
|
2023-10-19 22:53:31 +02:00
|
|
|
const dataExpected = "hello error";
|
|
|
|
const statusCodeExpected = 500;
|
|
|
|
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
|
|
|
return {data: msg.data, statusCode: statusCodeExpected};
|
|
|
|
});
|
|
|
|
|
|
|
|
await srv.listen();
|
|
|
|
expect(srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(RequestError);
|
|
|
|
});
|
|
|
|
|
2023-10-19 23:27:51 +02:00
|
|
|
test('request adaptor not found', async () => {
|
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
|
|
|
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(adaptorName, subject, async (msg: Message<string>) => {
|
|
|
|
return {data: msg.data, statusCode: statusCodeExpected};
|
|
|
|
});
|
|
|
|
|
|
|
|
await srv.listen();
|
|
|
|
expect(srv.request("bad adaptor", {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(Error);
|
|
|
|
});
|
|
|
|
|