test(websub): add hub route tests

Add comprehensive tests for POST /hub validation (missing fields, unknown mode, non-HTTPS callback, invalid URL, wrong domain, secret > 200 bytes) and happy-path subscribe/unsubscribe (202). Also fix hub.ts to use a waitUntilSafe wrapper so executionCtx.waitUntil doesn't throw in Node test environments.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Julien Herr
2026-05-21 23:03:57 +02:00
parent 4165774667
commit 09db52bb4d
2 changed files with 227 additions and 3 deletions
+14 -3
View File
@@ -1,10 +1,19 @@
import { Hono } from "hono";
import { Hono, type Context } from "hono";
import { Env } from "../types";
import {
verifyAndStoreSubscription,
verifyAndDeleteSubscription,
} from "../utils/websub";
function waitUntilSafe(c: Context, promise: Promise<unknown>) {
// Hono throws when ExecutionContext isn't present (e.g. Node unit tests).
try {
c.executionCtx.waitUntil(promise);
} catch {
// ignore
}
}
const DEFAULT_LEASE_SECONDS = 86400;
const MAX_LEASE_SECONDS = 30 * 24 * 3600; // 30 days
@@ -82,7 +91,8 @@ hubRouter.post("/", async (c) => {
// Return 202 immediately; verification is async
if (mode === "subscribe") {
c.executionCtx.waitUntil(
waitUntilSafe(
c,
verifyAndStoreSubscription(
feedId,
callbackUrl as string,
@@ -92,7 +102,8 @@ hubRouter.post("/", async (c) => {
),
);
} else {
c.executionCtx.waitUntil(
waitUntilSafe(
c,
verifyAndDeleteSubscription(feedId, callbackUrl as string, env),
);
}