From 2e0a0563fe10569e555fc1bab7fec8163883cf76 Mon Sep 17 00:00:00 2001 From: Bas Meeuwissen Date: Mon, 30 Dec 2024 10:56:44 +0100 Subject: [PATCH] #585: refactored fetch for testing --- packages/http/src/HttpRemote.ts | 11 +++++++---- packages/http/src/HttpRemoteBuilder.ts | 2 +- packages/http/test/fixtures/dummyFetch.ts | 11 +++++++++++ packages/http/test/fixtures/index.ts | 8 ++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 packages/http/test/fixtures/dummyFetch.ts create mode 100644 packages/http/test/fixtures/index.ts diff --git a/packages/http/src/HttpRemote.ts b/packages/http/src/HttpRemote.ts index 17393552..4bbf5109 100644 --- a/packages/http/src/HttpRemote.ts +++ b/packages/http/src/HttpRemote.ts @@ -1,23 +1,26 @@ import { ErrorConverter, Request, Response as ResultResponse } from '@jitar/execution'; +import { AddWorkerRequest } from '@jitar/runtime'; import type { Remote } from '@jitar/services'; import { File } from '@jitar/sourcing'; +import { Validator } from '@jitar/validation'; import HeaderKeys from './definitions/HeaderKeys'; import HeaderValues from './definitions/HeaderValues'; -import { Validator } from '@jitar/validation'; import InvalidWorkerId from './errors/InvalidWorkerId'; export default class HttpRemote implements Remote { readonly #url: string; + readonly #fetch: (input: RequestInfo, init?: RequestInit) => Promise; readonly #errorConverter = new ErrorConverter(); readonly #validator = new Validator(); - constructor(url: string) + constructor(url: string, fetch: (input: RequestInfo, init?: RequestInit) => Promise) { this.#url = url; + this.#fetch = fetch; } connect(): Promise @@ -68,7 +71,7 @@ export default class HttpRemote implements Remote async addWorker(url: string, procedureNames: string[], trustKey?: string): Promise { const remoteUrl = `${this.#url}/workers`; - const body = { url, procedureNames, trustKey }; + const body: AddWorkerRequest = { url, procedureNames, trustKey }; const options = { method: 'POST', @@ -143,7 +146,7 @@ export default class HttpRemote implements Remote async #callRemote(remoteUrl: string, options: object, throwOnError = true): Promise { - const response = await fetch(remoteUrl, options); + const response = await this.#fetch(remoteUrl, options); if (throwOnError && this.#isErrorResponse(response)) { diff --git a/packages/http/src/HttpRemoteBuilder.ts b/packages/http/src/HttpRemoteBuilder.ts index 2e25f7fc..2c7146dc 100644 --- a/packages/http/src/HttpRemoteBuilder.ts +++ b/packages/http/src/HttpRemoteBuilder.ts @@ -7,6 +7,6 @@ export default class HttpRemoteBuilder implements RemoteBuilder { build(url: string): Remote { - return new HttpRemote(url); + return new HttpRemote(url, fetch); } } diff --git a/packages/http/test/fixtures/dummyFetch.ts b/packages/http/test/fixtures/dummyFetch.ts new file mode 100644 index 00000000..0008492e --- /dev/null +++ b/packages/http/test/fixtures/dummyFetch.ts @@ -0,0 +1,11 @@ + +import { RESPONSES } from './Responses.fixture'; + +export function dummyFetch(input: RequestInfo, init?: RequestInit): Promise +{ + const url = input instanceof Request + ? input.url + : input; + + return Promise.resolve(RESPONSES[url] ?? new Response(null, { status: 404 })); +} diff --git a/packages/http/test/fixtures/index.ts b/packages/http/test/fixtures/index.ts new file mode 100644 index 00000000..81f1c553 --- /dev/null +++ b/packages/http/test/fixtures/index.ts @@ -0,0 +1,8 @@ + +import { HttpRemote } from '../../src'; + +import { dummyFetch } from './DummyFetch'; + +const remote = new HttpRemote('http://dummy.remote', dummyFetch); + +export { remote };