2020-08-22 01:38:30 +08:00
|
|
|
import express, { Response } from "express";
|
2019-03-04 02:18:11 +08:00
|
|
|
// tslint:disable-next-line:no-var-requires
|
2020-08-22 01:38:30 +08:00
|
|
|
const sse: (
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction
|
|
|
|
) => void = require("connect-sse")();
|
|
|
|
import EventSource from "eventsource";
|
|
|
|
import http from "http";
|
|
|
|
import net from "net";
|
|
|
|
import nock from "nock";
|
|
|
|
import { getLog } from "../src/helpers/get-log";
|
|
|
|
import { createWebhookProxy } from "../src/helpers/webhook-proxy";
|
2019-03-04 02:18:11 +08:00
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
const targetPort = 999999;
|
2019-03-04 02:18:11 +08:00
|
|
|
|
|
|
|
interface SSEResponse extends Response {
|
2020-08-22 01:38:30 +08:00
|
|
|
json(body: any, status?: string): this;
|
2019-03-04 02:18:11 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
jest.setTimeout(10000);
|
|
|
|
|
|
|
|
describe("webhook-proxy", () => {
|
2019-03-04 02:18:11 +08:00
|
|
|
// tslint:disable-next-line:one-variable-per-declaration
|
2020-08-22 01:38:30 +08:00
|
|
|
let emit: SSEResponse["json"], proxy: EventSource, server: http.Server;
|
2019-03-04 02:18:11 +08:00
|
|
|
|
|
|
|
afterEach(() => {
|
2020-08-22 01:38:30 +08:00
|
|
|
server && server.close();
|
|
|
|
proxy && proxy.close();
|
|
|
|
});
|
2019-03-04 02:18:11 +08:00
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
describe("with a valid proxy server", () => {
|
2019-03-04 02:18:11 +08:00
|
|
|
beforeEach((done) => {
|
2020-08-22 01:38:30 +08:00
|
|
|
const app = express();
|
2019-03-04 02:18:11 +08:00
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
app.get("/events", sse, (req, res: SSEResponse) => {
|
|
|
|
res.json({}, "ready");
|
|
|
|
emit = res.json;
|
|
|
|
});
|
2019-03-04 02:18:11 +08:00
|
|
|
|
|
|
|
server = app.listen(0, () => {
|
2020-08-22 01:38:30 +08:00
|
|
|
const url = `http://127.0.0.1:${
|
|
|
|
(server.address() as net.AddressInfo).port
|
|
|
|
}/events`;
|
|
|
|
proxy = createWebhookProxy({
|
|
|
|
url,
|
|
|
|
port: targetPort,
|
|
|
|
path: "/test",
|
|
|
|
logger: getLog(),
|
|
|
|
})!;
|
2019-03-04 02:18:11 +08:00
|
|
|
|
|
|
|
// Wait for proxy to be ready
|
2020-08-22 01:38:30 +08:00
|
|
|
proxy.addEventListener("ready", () => done());
|
|
|
|
});
|
|
|
|
});
|
2019-03-04 02:18:11 +08:00
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
test("forwards events to server", (done) => {
|
|
|
|
nock(`http://localhost:${targetPort}`)
|
|
|
|
.post("/test")
|
|
|
|
.reply(200, () => {
|
|
|
|
done();
|
|
|
|
});
|
2019-03-04 02:18:11 +08:00
|
|
|
|
2020-08-22 01:38:30 +08:00
|
|
|
const body = { action: "foo" };
|
2019-03-04 02:18:11 +08:00
|
|
|
|
|
|
|
emit({
|
|
|
|
body,
|
2020-08-22 01:38:30 +08:00
|
|
|
"x-github-event": "test",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("logs an error when the proxy server is not found", (done) => {
|
|
|
|
const url = "http://bad.proxy/events";
|
|
|
|
nock("http://bad.proxy").get("/events").reply(404);
|
|
|
|
|
|
|
|
const log = getLog().child({});
|
|
|
|
log.error = jest.fn();
|
|
|
|
|
|
|
|
proxy = createWebhookProxy({ url, logger: log })!;
|
|
|
|
|
|
|
|
proxy.addEventListener("error", (error: any) => {
|
|
|
|
expect(error.status).toBe(404);
|
|
|
|
expect(log.error).toHaveBeenCalledWith(error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|