Skip to content

Commit

Permalink
fix(middleware): pass on to the next middleware in case of express (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
honnix authored Apr 17, 2021
1 parent 024e3ee commit 07f19fe
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ export async function middleware(

const isUnknownRoute = request.method !== "POST" || pathname !== options.path;
const isExpressMiddleware = typeof next === "function";
if (!isExpressMiddleware && isUnknownRoute) {
return options.onUnhandledRequest(request, response);
if (isUnknownRoute) {
if (isExpressMiddleware) {
return next!();
} else {
return options.onUnhandledRequest(request, response);
}
}

const missingHeaders = getMissingHeaders(request).join(", ");
Expand Down
93 changes: 91 additions & 2 deletions test/integration/node-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,102 @@ describe("createNodeMiddleware(webhooks)", () => {
expect(response.status).toEqual(202);
});

test("express middleware 404", async () => {
test("express middleware no mount path 404", async () => {
const app = express();
const webhooks = new Webhooks({
secret: "mySecret",
});

app.post("/test", createNodeMiddleware(webhooks));
app.use(createNodeMiddleware(webhooks));
app.all("*", (_request: any, response: any) =>
response.status(404).send("Dafuq")
);

const server = app.listen();

const { port } = server.address();

const response = await fetch(`http://localhost:${port}/test`, {
method: "POST",
body: JSON.stringify(pushEventPayload),
});

await expect(response.text()).resolves.toBe("Dafuq");
expect(response.status).toEqual(404);

server.close();
});

test("express middleware no mount path no next", async () => {
const app = express();
const webhooks = new Webhooks({
secret: "mySecret",
});

app.all("/foo", (_request: any, response: any) => response.end("ok\n"));
app.use(createNodeMiddleware(webhooks));

const server = app.listen();

const { port } = server.address();

const response = await fetch(`http://localhost:${port}/test`, {
method: "POST",
body: JSON.stringify(pushEventPayload),
});

await expect(response.text()).resolves.toContain("Cannot POST /test");
expect(response.status).toEqual(404);

const responseForFoo = await fetch(`http://localhost:${port}/foo`, {
method: "POST",
body: JSON.stringify(pushEventPayload),
});

await expect(responseForFoo.text()).resolves.toContain("ok\n");
expect(responseForFoo.status).toEqual(200);

server.close();
});

test("express middleware no mount path with options.path", async () => {
const app = express();
const webhooks = new Webhooks({
secret: "mySecret",
});

app.use(createNodeMiddleware(webhooks, { path: "/test" }));
app.all("*", (_request: any, response: any) =>
response.status(404).send("Dafuq")
);

const server = app.listen();

const { port } = server.address();

const response = await fetch(`http://localhost:${port}/test`, {
method: "POST",
headers: {
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000",
"X-GitHub-Event": "push",
"X-Hub-Signature-256": signatureSha256,
},
body: JSON.stringify(pushEventPayload),
});

await expect(response.text()).resolves.toBe("ok\n");
expect(response.status).toEqual(200);

server.close();
});

test("express middleware with mount path with options.path", async () => {
const app = express();
const webhooks = new Webhooks({
secret: "mySecret",
});

app.post("/test", createNodeMiddleware(webhooks, { path: "/test" }));
app.all("*", (_request: any, response: any) =>
response.status(404).send("Dafuq")
);
Expand Down

0 comments on commit 07f19fe

Please sign in to comment.