From eebd9d62b7bf40cd77355ffa5bb71e2c60899734 Mon Sep 17 00:00:00 2001 From: Routmoute Date: Thu, 30 Dec 2021 12:54:32 +0100 Subject: [PATCH] add ErrorService tests --- tests/integration/error_service_test.ts | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/integration/error_service_test.ts diff --git a/tests/integration/error_service_test.ts b/tests/integration/error_service_test.ts new file mode 100644 index 000000000..c357d4888 --- /dev/null +++ b/tests/integration/error_service_test.ts @@ -0,0 +1,49 @@ +import { Rhum, TestHelpers } from "../deps.ts"; +import { ErrorService, Errors, Response, Server } from "../../mod.ts"; + +class MyErrorService extends ErrorService { + runAfterResource(error: Errors.HttpError, response: Response): Response { + response.status = error.code; + response.json({error: error.message}); + return response; + } +} + +const serverWithoutErrorService = new Server({ + protocol: "http", + hostname: "localhost", + port: 3000, + resources: [] +}); + +const serverWithErrorService = new Server({ + protocol: "http", + hostname: "localhost", + port: 3000, + resources: [], + error_service: new MyErrorService() +}); + +Rhum.testPlan("error_service_test.ts", () => { + Rhum.testSuite("/inexistent_path", () => { + Rhum.testCase("test default ErrorService", async () => { + serverWithoutErrorService.run(); + const response = await TestHelpers.makeRequest.get("http://localhost:3000/inexistent_path"); + await serverWithoutErrorService.close(); + Rhum.asserts.assertEquals(await response.text(), + `Error: Not Found + at Server. (file:///home/tom/drash/src/http/server.ts:225:17) + at async Server.#respond (https://deno.land/std@0.119.0/http/server.ts:298:18)` + ); + }); + + Rhum.testCase("test MyErrorService", async () => { + serverWithErrorService.run(); + const response = await TestHelpers.makeRequest.get("http://localhost:3000/inexistent_path"); + await serverWithErrorService.close(); + Rhum.asserts.assertEquals(await response.json(), {error: "Not Found"}); + }); + }); +}); + +Rhum.run();