From 5088cbee71be6c4cc7cf4fadc6dd103336ce67d0 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Mon, 16 Nov 2026 12:00:21 -0600 Subject: [PATCH] test(adapters): isolate honeytoken suites from ingest --- .../express/src/honeytoken-injection.test.ts | 36 +++++++++++++++++++ .../fastify/src/honeytoken-injection.test.ts | 30 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/packages/express/src/honeytoken-injection.test.ts b/packages/express/src/honeytoken-injection.test.ts index f0314da..fea841d 100644 --- a/packages/express/src/honeytoken-injection.test.ts +++ b/packages/express/src/honeytoken-injection.test.ts @@ -3,6 +3,38 @@ import http from 'node:http'; import { AddressInfo } from 'node:net'; import { webdecoy } from './middleware'; +const realFetch = global.fetch; + +function allowDetection(): Response { + return new Response( + JSON.stringify({ + decision: 'allow', + confidence: 0, + threat_level: 'MINIMAL', + bot_detected: false, + detection_id: 'express-test', + rule_enforced: false, + }), + { status: 200, headers: { 'content-type': 'application/json' } }, + ); +} + +beforeEach(() => { + // Keep local app traffic real, but never let a dummy API key turn these + // response-decoration tests into production ingest requests. + global.fetch = jest.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = + typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; + if (url.startsWith('http://127.0.0.1:')) return realFetch(input, init); + if (url === 'https://in.webdecoy.com/api/v1/sdk/detect') return allowDetection(); + throw new Error(`Unexpected external fetch in Express test: ${url}`); + }) as unknown as typeof fetch; +}); + +afterEach(() => { + global.fetch = realFetch; +}); + /** * Honeytoken injection through a real Express app. * @@ -52,6 +84,10 @@ describe('honeytoken injection', () => { expect(res.status).toBe(200); expect(res.body).toContain('

hi

'); expect(res.body).toMatch(/, not appended after the document. expect(res.body.indexOf('/__wd/')).toBeLessThan(res.body.indexOf('')); }); diff --git a/packages/fastify/src/honeytoken-injection.test.ts b/packages/fastify/src/honeytoken-injection.test.ts index 0a5f7d7..392cec4 100644 --- a/packages/fastify/src/honeytoken-injection.test.ts +++ b/packages/fastify/src/honeytoken-injection.test.ts @@ -2,6 +2,35 @@ import Fastify, { FastifyInstance } from 'fastify'; import { Readable } from 'node:stream'; import { webdecoyPlugin } from './plugin'; +const realFetch = global.fetch; + +beforeEach(() => { + // These tests exercise Fastify response hooks, not the live detector. Stub + // the only expected remote call so CI is deterministic and offline. + global.fetch = jest.fn(async (input: RequestInfo | URL) => { + const url = + typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; + if (url !== 'https://in.webdecoy.com/api/v1/sdk/detect') { + throw new Error(`Unexpected external fetch in Fastify test: ${url}`); + } + return new Response( + JSON.stringify({ + decision: 'allow', + confidence: 0, + threat_level: 'MINIMAL', + bot_detected: false, + detection_id: 'fastify-test', + rule_enforced: false, + }), + { status: 200, headers: { 'content-type': 'application/json' } }, + ); + }) as unknown as typeof fetch; +}); + +afterEach(() => { + global.fetch = realFetch; +}); + /** * Honeytoken injection through a real Fastify app. * @@ -45,6 +74,7 @@ describe('honeytoken injection', () => { expect(res.statusCode).toBe(200); expect(res.body).toContain('

hi

'); expect(res.body).toMatch(/
, not appended after the document — an anchor outside the // body can be relocated by the parser in ways that make it visible. expect(res.body.indexOf('__wd')).toBeLessThan(res.body.indexOf(''));