Skip to content

Commit 69c39f0

Browse files
authored
fix: use options.onUnhandledRequest in createNodeMiddleware(webhooks, options) (#519)
1 parent 995b48d commit 69c39f0

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/middleware/node/middleware.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { WebhookEventName } from "@octokit/webhooks-definitions/schema";
55
import { Webhooks } from "../../index";
66
import { WebhookEventHandlerError } from "../../types";
77
import { MiddlewareOptions } from "./types";
8-
import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
98
import { getMissingHeaders } from "./get-missing-headers";
109
import { getPayload } from "./get-payload";
1110

@@ -21,8 +20,7 @@ export async function middleware(
2120
const isUnknownRoute = request.method !== "POST" || pathname !== options.path;
2221
const isExpressMiddleware = typeof next === "function";
2322
if (!isExpressMiddleware && isUnknownRoute) {
24-
options.log.debug(`not found: ${request.method} ${request.url}`);
25-
return onUnhandledRequestDefault(request, response);
23+
return options.onUnhandledRequest(request, response);
2624
}
2725

2826
const missingHeaders = getMissingHeaders(request).join(", ");

test/integration/node-middleware.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,43 @@ describe("createNodeMiddleware(webhooks)", () => {
154154
server.close();
155155
});
156156

157+
test("custom non-found handler", async () => {
158+
const webhooks = new Webhooks({
159+
secret: "mySecret",
160+
});
161+
162+
const server = createServer(
163+
createNodeMiddleware(webhooks, {
164+
onUnhandledRequest(_request, response) {
165+
response.writeHead(404);
166+
response.end("nope");
167+
},
168+
})
169+
).listen();
170+
171+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
172+
const { port } = server.address();
173+
174+
const response = await fetch(
175+
`http://localhost:${port}/api/github/webhooks`,
176+
{
177+
method: "PUT",
178+
headers: {
179+
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000",
180+
"X-GitHub-Event": "push",
181+
"X-Hub-Signature-256": signatureSha256,
182+
},
183+
body: "invalid",
184+
}
185+
);
186+
187+
expect(response.status).toEqual(404);
188+
189+
await expect(response.text()).resolves.toEqual("nope");
190+
191+
server.close();
192+
});
193+
157194
test("Handles missing headers", async () => {
158195
const webhooks = new Webhooks({
159196
secret: "mySecret",

0 commit comments

Comments
 (0)