From 12d91ba0b44e5822210aca0b5087f2d272420072 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Mon, 2 Jun 2025 09:38:41 +0000 Subject: [PATCH] test: fix mocked Fetcher real calls by using same headers --- src/utils/Fetcher.ts | 2 +- src/utils/__mocks__/Fetcher.ts | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/utils/Fetcher.ts b/src/utils/Fetcher.ts index 78c961c..725a08f 100644 --- a/src/utils/Fetcher.ts +++ b/src/utils/Fetcher.ts @@ -28,7 +28,7 @@ export class Fetcher { return (await this.cachedFetch(ctx, url, { ...init, method: 'HEAD' })).policy.responseHeaders(); }; - private readonly getInit = (ctx: Context, url: URL, init?: RequestInit): RequestInit => ({ + public readonly getInit = (ctx: Context, url: URL, init?: RequestInit): RequestInit => ({ ...init, headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', diff --git a/src/utils/__mocks__/Fetcher.ts b/src/utils/__mocks__/Fetcher.ts index a181404..402b043 100644 --- a/src/utils/__mocks__/Fetcher.ts +++ b/src/utils/__mocks__/Fetcher.ts @@ -1,27 +1,35 @@ import fs from 'node:fs'; import slugify from 'slugify'; +import winston from 'winston'; import { Context } from '../../types'; +const { Fetcher } = jest.requireActual('../Fetcher'); -export class Fetcher { - readonly text = async (_ctx: Context, url: URL, init?: RequestInit): Promise => { +class MockedFetcher { + private readonly fetcher: typeof Fetcher; + + constructor() { + this.fetcher = new Fetcher(winston.createLogger({ transports: [new winston.transports.Console()] })); + } + + readonly text = async (ctx: Context, url: URL, init?: RequestInit): Promise => { const path = `${__dirname}/../__fixtures__/Fetcher/${slugify(url.href)}`; - return this.fetchy(path, url, init); + return this.fetch(path, ctx, url, init); }; - readonly textPost = async (_ctx: Context, url: URL, data: unknown, init?: RequestInit): Promise => { + readonly textPost = async (ctx: Context, url: URL, data: unknown, init?: RequestInit): Promise => { const path = `${__dirname}/../__fixtures__/Fetcher/post-${slugify(url.href)}-${slugify(JSON.stringify(data))}`; - return this.fetchy(path, url, { ...init, method: 'POST', body: JSON.stringify(data) }); + return this.fetch(path, ctx, url, { ...init, method: 'POST', body: JSON.stringify(data) }); }; - readonly head = async (_ctx: Context, url: URL, init?: RequestInit): Promise => { + readonly head = async (ctx: Context, url: URL, init?: RequestInit): Promise => { const path = `${__dirname}/../__fixtures__/Fetcher/head-${slugify(url.href)}`; - return JSON.parse(await this.fetchy(path, url, { ...init, method: 'HEAD' })); + return JSON.parse(await this.fetch(path, ctx, url, { ...init, method: 'HEAD' })); }; - private readonly fetchy = async (path: string, url: URL, init?: RequestInit): Promise => { + private readonly fetch = async (path: string, ctx: Context, url: URL, init?: RequestInit): Promise => { const errorPath = `${path}.error`; if (fs.existsSync(errorPath)) { @@ -32,7 +40,7 @@ export class Fetcher { let response; try { if (process.env['TEST_UPDATE_FIXTURES']) { - response = await fetch(url, init); + response = await fetch(url, this.fetcher.getInit(ctx, url, init)); } else { console.error(`No fixture found at "${path}".`); process.exit(1); @@ -61,3 +69,5 @@ export class Fetcher { } }; } + +export { MockedFetcher as Fetcher };