Skip to content

Commit

Permalink
add runBeforeResource() and runAfterResource() errors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
routmoute committed Dec 31, 2021
1 parent 2ea504c commit 7a2fb22
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions tests/integration/service_error_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class ServiceError extends Service {
}
}

class ServiceError2 extends Service {
runBeforeResource(_request: Request, _response: Response) {
throw new Errors.HttpError(500, "runBeforeResource error");
}
}

class ServiceError3 extends Service {
runAfterResource(_request: Request, _response: Response) {
throw new Errors.HttpError(500, "runAfterResource error");
}
}

class Resource1 extends Resource {
paths = ["/"];

Expand Down Expand Up @@ -64,9 +76,17 @@ class Resource3 extends Resource {
}
}

class Resource4 extends Resource {
paths = ["/"];

public GET(_request: Request, response: Response) {
return response.text("ok");
}
}

Rhum.testPlan("service_error_test.ts", () => {
Rhum.testSuite("GET /", () => {
Rhum.testCase("First serviceError is MethodService", async () => {
Rhum.testCase("First runOnError is in MethodService", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
Expand All @@ -82,7 +102,7 @@ Rhum.testPlan("service_error_test.ts", () => {
Rhum.asserts.assertEquals(await res.json(), {error: "request invalid", service: "MethodService"});
});

Rhum.testCase("Second serviceError is ClassService", async () => {
Rhum.testCase("Second runOnError is in ClassService", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
Expand All @@ -98,7 +118,7 @@ Rhum.testPlan("service_error_test.ts", () => {
Rhum.asserts.assertEquals(await res.json(), {error: "request invalid", service: "ClassService"});
});

Rhum.testCase("Third serviceError is ServerService", async () => {
Rhum.testCase("Third runOnError is in ServerService", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
Expand Down Expand Up @@ -129,7 +149,7 @@ Rhum.testPlan("service_error_test.ts", () => {
Rhum.asserts.assertEquals((await res.text()).startsWith('Error: request invalid\n'), true);
});

Rhum.testCase("error on ServiceError", async () => {
Rhum.testCase("error on runOnError()", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
Expand All @@ -144,6 +164,38 @@ Rhum.testPlan("service_error_test.ts", () => {
Rhum.asserts.assertEquals(res.status, 500);
Rhum.asserts.assertEquals((await res.text()).startsWith('Error: error on serviceError\n'), true);
});

Rhum.testCase("error on runBeforeResource()", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
port: 3000,
resources: [Resource4],
services: [new ServiceError2(),new ServerService()]
});
server.run();
const res = await TestHelpers.makeRequest.get(server.address)
await server.close();

Rhum.asserts.assertEquals(res.status, 500);
Rhum.asserts.assertEquals(await res.json(), {error:"runBeforeResource error", service:"ServerService"});
});

Rhum.testCase("error on runAfterResource()", async () => {
const server = new Server({
protocol: "http",
hostname: "localhost",
port: 3000,
resources: [Resource4],
services: [new ServiceError3(),new ServerService()]
});
server.run();
const res = await TestHelpers.makeRequest.get(server.address)
await server.close();

Rhum.asserts.assertEquals(res.status, 500);
Rhum.asserts.assertEquals(await res.json(), {error:"runAfterResource error", service:"ServerService"});
});
});
});

Expand Down

0 comments on commit 7a2fb22

Please sign in to comment.