2024-01-04 21:20:47 +01:00
|
|
|
import { z } from "zod";
|
|
|
|
import NatsAdaptor from "../src/adaptors/nats.ts";
|
|
|
|
import Service from "../src/service.ts";
|
2024-01-10 22:28:27 +01:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "std/assert/mod.ts";
|
2024-01-04 21:20:47 +01:00
|
|
|
import { afterEach, beforeEach, it } from "std/testing/bdd.ts";
|
2024-01-11 23:03:46 +01:00
|
|
|
import { RequestError } from "../src/error.ts";
|
2023-10-19 21:48:44 +02:00
|
|
|
|
|
|
|
let srv!: Service;
|
|
|
|
|
2024-01-04 23:06:31 +01:00
|
|
|
const natsServer = Deno.env.get("NATS_HOST") || "127.0.0.1:4222";
|
2023-10-19 21:48:44 +02:00
|
|
|
const serviceName = "test-service";
|
|
|
|
const adaptorName = "nats";
|
|
|
|
const subject = "test";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-01-04 21:20:47 +01:00
|
|
|
srv = new Service(serviceName);
|
2023-10-19 21:48:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2024-01-04 21:20:47 +01:00
|
|
|
await srv.stop();
|
2023-10-19 21:48:44 +02:00
|
|
|
});
|
|
|
|
|
2024-01-04 23:01:46 +01:00
|
|
|
it("request success", {
|
|
|
|
sanitizeExit: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
|
|
|
}, async () => {
|
2024-01-04 21:20:47 +01:00
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
2023-10-19 22:53:31 +02:00
|
|
|
|
2024-01-04 21:20:47 +01:00
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(adaptorName, subject, async (msg) => {
|
|
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
|
|
}, z.string());
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-04 21:20:47 +01:00
|
|
|
await srv.listen();
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-04 21:20:47 +01:00
|
|
|
const res = await srv.request(adaptorName, {
|
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
|
|
|
}, z.string());
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-04 21:20:47 +01:00
|
|
|
assertEquals(res.data, dataExpected);
|
|
|
|
assertEquals(res.statusCode, statusCodeExpected);
|
|
|
|
});
|
2023-10-19 22:53:31 +02:00
|
|
|
|
2024-01-04 23:01:46 +01:00
|
|
|
it("request error", {
|
|
|
|
sanitizeExit: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
|
|
|
}, async () => {
|
2024-01-04 21:20:47 +01:00
|
|
|
const dataExpected = "hello error";
|
|
|
|
const statusCodeExpected = 500;
|
|
|
|
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(adaptorName, subject, async (msg) => {
|
2024-01-11 23:03:46 +01:00
|
|
|
throw new RequestError("request error", 500);
|
2024-01-04 21:20:47 +01:00
|
|
|
}, z.string());
|
|
|
|
|
|
|
|
await srv.listen();
|
2024-01-10 22:28:27 +01:00
|
|
|
assertRejects(() => {
|
|
|
|
return srv.request(adaptorName, {
|
2024-01-04 21:20:47 +01:00
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
2024-01-04 23:01:46 +01:00
|
|
|
}, z.string());
|
2024-01-04 21:20:47 +01:00
|
|
|
});
|
2023-10-19 22:53:31 +02:00
|
|
|
});
|
|
|
|
|
2024-01-04 23:01:46 +01:00
|
|
|
it("request adaptor not found", {
|
|
|
|
sanitizeExit: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
sanitizeResources: false,
|
|
|
|
}, async () => {
|
2024-01-04 21:20:47 +01:00
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
|
|
|
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(adaptorName, subject, async (msg) => {
|
|
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
|
|
});
|
|
|
|
|
|
|
|
await srv.listen();
|
2024-01-10 22:28:27 +01:00
|
|
|
assertRejects(() => {
|
|
|
|
return srv.request("bad adaptor", {
|
2024-01-04 21:20:47 +01:00
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
2023-10-19 23:27:51 +02:00
|
|
|
});
|
2024-01-04 21:20:47 +01:00
|
|
|
});
|
2023-10-19 23:27:51 +02:00
|
|
|
});
|