Skip to content

Commit

Permalink
#585: refactored fetch for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
basmasking committed Dec 30, 2024
1 parent dcd6ac1 commit 2e0a056
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/http/src/HttpRemote.ts
Original file line number Diff line number Diff line change
@@ -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<Response>;

readonly #errorConverter = new ErrorConverter();
readonly #validator = new Validator();

constructor(url: string)
constructor(url: string, fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>)
{
this.#url = url;
this.#fetch = fetch;
}

connect(): Promise<void>
Expand Down Expand Up @@ -68,7 +71,7 @@ export default class HttpRemote implements Remote
async addWorker(url: string, procedureNames: string[], trustKey?: string): Promise<string>
{
const remoteUrl = `${this.#url}/workers`;
const body = { url, procedureNames, trustKey };
const body: AddWorkerRequest = { url, procedureNames, trustKey };
const options =
{
method: 'POST',
Expand Down Expand Up @@ -143,7 +146,7 @@ export default class HttpRemote implements Remote

async #callRemote(remoteUrl: string, options: object, throwOnError = true): Promise<Response>
{
const response = await fetch(remoteUrl, options);
const response = await this.#fetch(remoteUrl, options);

if (throwOnError && this.#isErrorResponse(response))
{
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/HttpRemoteBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export default class HttpRemoteBuilder implements RemoteBuilder
{
build(url: string): Remote
{
return new HttpRemote(url);
return new HttpRemote(url, fetch);
}
}
11 changes: 11 additions & 0 deletions packages/http/test/fixtures/dummyFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { RESPONSES } from './Responses.fixture';

export function dummyFetch(input: RequestInfo, init?: RequestInit): Promise<Response>
{
const url = input instanceof Request
? input.url
: input;

return Promise.resolve(RESPONSES[url] ?? new Response(null, { status: 404 }));
}
8 changes: 8 additions & 0 deletions packages/http/test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import { HttpRemote } from '../../src';

import { dummyFetch } from './DummyFetch';

const remote = new HttpRemote('http://dummy.remote', dummyFetch);

export { remote };

0 comments on commit 2e0a056

Please sign in to comment.