diff --git a/.eslintignore b/.eslintignore index 35865b2..4291df0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ dist/ jest.config.js tests/ -.eslintrc.cjs \ No newline at end of file +.eslintrc.cjs +coverage/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 04c01ba..94934f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -dist/ \ No newline at end of file +dist/ +coverage/ \ No newline at end of file diff --git a/src/error.ts b/src/error.ts index c542a80..985e8b5 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,10 +1,10 @@ export class RequestError extends Error { - public code: number; + public statusCode: number; - constructor(msg: string, code: number) { + constructor(msg: string, statusCode: number) { super(msg); - this.code = code; + this.statusCode = statusCode; } } \ No newline at end of file diff --git a/src/service.ts b/src/service.ts index 3dcd894..70cb6f0 100644 --- a/src/service.ts +++ b/src/service.ts @@ -38,7 +38,7 @@ export default class Service { const rawRes = await this.adaptors[adaptor].request(`${req.service}.${req.subject}`, rawReq); const res: Response = JSON.parse(rawRes); - if(res.statusCode < 200 || res.statusCode >= 599) { + if(res.statusCode < 200 || res.statusCode >= 299) { throw new RequestError("error while request", res.statusCode); } diff --git a/tests/error.test.ts b/tests/error.test.ts new file mode 100644 index 0000000..757a308 --- /dev/null +++ b/tests/error.test.ts @@ -0,0 +1,11 @@ +import { RequestError } from "../src/error"; + +test('request error', () => { + const expectedMsg = "This is my err"; + const expectedStatusCode = 500; + + const err = new RequestError(expectedMsg, expectedStatusCode); + + expect(err.message).toBe(expectedMsg); + expect(err.statusCode).toBe(expectedStatusCode); +}); \ No newline at end of file diff --git a/tests/lib.test.ts b/tests/service.test.ts similarity index 71% rename from tests/lib.test.ts rename to tests/service.test.ts index 0d81aca..5a6fc8f 100644 --- a/tests/lib.test.ts +++ b/tests/service.test.ts @@ -19,7 +19,7 @@ afterEach(async () => { await srv.stop(); }); -test('nats adaptor success', async () => { +test('request success', async () => { const dataExpected = "hello success"; const statusCodeExpected = 200; @@ -36,7 +36,7 @@ test('nats adaptor success', async () => { expect(res.statusCode).toBe(statusCodeExpected); }); -test('nats adaptor error', async () => { +test('request error', async () => { const dataExpected = "hello error"; const statusCodeExpected = 500; @@ -49,3 +49,16 @@ test('nats adaptor error', async () => { expect(srv.request(adaptorName, {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(RequestError); }); +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) => { + return {data: msg.data, statusCode: statusCodeExpected}; + }); + + await srv.listen(); + expect(srv.request("bad adaptor", {service: serviceName, subject: subject, data: dataExpected})).rejects.toThrow(Error); +}); +