2020-08-18 15:47:53 +08:00
|
|
|
import { Application, NextFunction, Request, Response } from "express";
|
|
|
|
import request from "supertest";
|
|
|
|
import { getLog } from "../src/helpers/get-log";
|
|
|
|
import { createServer } from "../src/server/create-server";
|
2017-08-01 01:34:28 +08:00
|
|
|
|
2020-08-18 15:47:53 +08:00
|
|
|
describe("server", () => {
|
|
|
|
let server: Application;
|
|
|
|
let webhook: any;
|
2017-08-01 01:34:28 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-08-18 15:47:53 +08:00
|
|
|
webhook = jest.fn((req, res, next) => next());
|
|
|
|
server = createServer({ webhook, logger: getLog() });
|
2017-08-30 10:58:57 +08:00
|
|
|
|
|
|
|
// Error handler to avoid printing logs
|
2020-08-18 15:47:53 +08:00
|
|
|
server.use(
|
|
|
|
(error: Error, req: Request, res: Response, next: NextFunction) => {
|
|
|
|
res.status(500).send(error.message);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-08-01 01:34:28 +08:00
|
|
|
|
2020-08-18 15:47:53 +08:00
|
|
|
describe("GET /ping", () => {
|
|
|
|
it("returns a 200 response", () => {
|
|
|
|
return request(server).get("/ping").expect(200, "PONG");
|
|
|
|
});
|
|
|
|
});
|
2017-08-01 01:34:28 +08:00
|
|
|
|
2020-08-18 15:47:53 +08:00
|
|
|
describe("webhook handler", () => {
|
|
|
|
it("should 500 on a webhook error", () => {
|
|
|
|
webhook.mockImplementation(
|
|
|
|
(req: Request, res: Response, callback: NextFunction) =>
|
|
|
|
callback(new Error("webhook error"))
|
|
|
|
);
|
|
|
|
return request(server).post("/").expect(500);
|
|
|
|
});
|
|
|
|
});
|
2017-08-01 01:34:28 +08:00
|
|
|
|
2020-08-18 15:47:53 +08:00
|
|
|
describe("with an unknown url", () => {
|
|
|
|
it("responds with 404", () => {
|
|
|
|
return request(server).get("/lolnotfound").expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|