release: WRNexusJS 0.4.0
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createContext, type Context } from "@wrnexus/core";
|
||||
import { captchaPageGate } from "../src/middleware.ts";
|
||||
import type { CaptchaEngine } from "../src/types.ts";
|
||||
|
||||
@@ -26,17 +27,30 @@ function fakeEngine(): CaptchaEngine {
|
||||
|
||||
function sessionFixture() {
|
||||
const values = new Map<string, unknown>();
|
||||
let sessionId = "session-1";
|
||||
|
||||
return {
|
||||
id: () => "session-1",
|
||||
id(): string {
|
||||
return sessionId;
|
||||
},
|
||||
get<T>(key: string): T | undefined {
|
||||
return values.get(key) as T | undefined;
|
||||
},
|
||||
set<T>(key: string, value: T): void {
|
||||
getAll(): Record<string, unknown> {
|
||||
return Object.fromEntries(values);
|
||||
},
|
||||
set(key: string, value: unknown): void {
|
||||
values.set(key, value);
|
||||
},
|
||||
delete(key: string): void {
|
||||
values.delete(key);
|
||||
},
|
||||
regenerate(): void {
|
||||
sessionId = `${sessionId}-regenerated`;
|
||||
},
|
||||
clear(): void {
|
||||
values.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -63,12 +77,11 @@ describe("CAPTCHA page gate", () => {
|
||||
headers: { "content-type": "application/x-www-form-urlencoded" },
|
||||
body,
|
||||
});
|
||||
const context = {
|
||||
req: request,
|
||||
url: new URL(request.url),
|
||||
const requestUrl = new URL(request.url);
|
||||
const context: Context = {
|
||||
...createContext(request, requestUrl),
|
||||
session,
|
||||
ip: "127.0.0.1",
|
||||
locals: {},
|
||||
};
|
||||
|
||||
const granted = await gate(context, () =>
|
||||
@@ -77,11 +90,11 @@ describe("CAPTCHA page gate", () => {
|
||||
expect(granted.status).toBe(303);
|
||||
|
||||
const protectedRequest = new Request("https://example.test/protected");
|
||||
const protectedContext = {
|
||||
...context,
|
||||
req: protectedRequest,
|
||||
url: new URL(protectedRequest.url),
|
||||
locals: {},
|
||||
const protectedUrl = new URL(protectedRequest.url);
|
||||
const protectedContext: Context = {
|
||||
...createContext(protectedRequest, protectedUrl),
|
||||
session,
|
||||
ip: "127.0.0.1",
|
||||
};
|
||||
const allowed = await gate(protectedContext, () => new Response("unlocked"));
|
||||
expect(await allowed.text()).toBe("unlocked");
|
||||
|
||||
Reference in New Issue
Block a user