import { expect } from 'chai'; import { responseError } from '../../../src/helpers/response-error'; import { BadRequestError, UnauthorizedError, PaymentRequiredError, ForbiddenError, NotFoundError, ServerError, UpstreamServiceError } from '../../../src/errors'; describe('helpers response-error', () => { describe("responseError", () => { let response: any; let request: any; beforeEach(() => { request = { url: "url", method: "GET", body: '{"id": "1234"}', sessionToken: "sessionToken", }; response = { ok: false, status: 500, text: () => new Promise((resolve) => resolve("test")), }; }); it("should return a promise", () => { expect(responseError(response, request)).to.have.property("then"); }); it("should throw with error containing the request and response data", async () => { const error = await responseError(response, request); expect(error.data).to.eql({ status: 500, responseBody: "test", url: "url", method: "GET", requestBody: { id: "1234" }, headers: { "X-Origin-System-Id": "", "X-Origin-User": "", "X-Request-Id": "", }, }); }); it("should pass tracing headers (X-Origin-System-Id, X-Origin-User, X-Request-Id) in the error data", async () => { response.text = () => new Promise((resolve) => resolve("{}")); request.headers = { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", "X-Custom-Header": "someRandomStuff", }; const error = await responseError(response, request); expect(error.data).to.eql({ status: 500, responseBody: {}, url: "url", method: "GET", requestBody: { id: "1234" }, headers: { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", }, }); }); it("should throw requestBody and responseBody as objects when they are valid JSON", async () => { response.text = () => new Promise((resolve) => resolve('{"name": "Jane"}')); request.headers = { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", "X-Custom-Header": "someRandomStuff", }; const error = await responseError(response, request); expect(error.data).to.eql({ status: 500, responseBody: { name: "Jane" }, url: "url", method: "GET", requestBody: { id: "1234" }, headers: { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", }, }); }); it("should throw requestBody as a string and responseBody as object when it is NOT valid JSON", async () => { response.text = () => new Promise((resolve) => resolve({})); request.body = "hello"; request.headers = { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", }; const error = await responseError(response, request); expect(error.data).to.eql({ status: 500, responseBody: {}, url: "url", method: "GET", requestBody: "hello", headers: { "X-Origin-System-Id": "someSystem", "X-Origin-User": "someUser", "X-Request-Id": "1234", }, }); }); it("should throw BadRequestError on 400", async () => { response.status = 400; const error = await responseError(response, request); expect(error instanceof BadRequestError).to.be.true; }); it("should throw UnauthorizedError on 401", async () => { response.status = 401; const error = await responseError(response, request); expect(error instanceof UnauthorizedError).to.be.true; }); it("should throw PaymentRequiredError on 402", async () => { response.status = 402; const error = await responseError(response, request); expect(error instanceof PaymentRequiredError).to.be.true; }); it("should throw ForbiddenError on 403", async () => { response.status = 403; const error = await responseError(response, request); expect(error instanceof ForbiddenError).to.be.true; }); it("should throw NotFoundError on 404", async () => { response.status = 404; const error = await responseError(response, request); expect(error instanceof NotFoundError).to.be.true; }); it("should throw ServerError for teapots", async () => { response.status = 418; const error = await responseError(response, request); expect(error instanceof ServerError).to.be.true; }); it("should throw UpstreamServiceError on any 5XX response", async () => { response.status = 500; const error = await responseError(response, request); expect(error instanceof UpstreamServiceError).to.be.true; }); }); });