From ba0dc0b8b46cc6cf8ed70612e4add0e0eeda975f Mon Sep 17 00:00:00 2001 From: David Griffin Date: Mon, 9 Dec 2024 12:40:04 -0800 Subject: [PATCH] Refactoring to make tests pass. --- __tests__/functional/client-configuration.test.ts | 6 ++++++ __tests__/functional/feed-client-configuration.test.ts | 2 ++ __tests__/functional/feed-client.test.ts | 7 +++++++ __tests__/functional/stream-client-configuration.test.ts | 3 +++ src/client-configuration.ts | 1 - src/client.ts | 7 ++++--- src/http-client/fetch-client.ts | 4 ++++ src/http-client/http-client.ts | 5 +++++ src/http-client/node-http2-client.ts | 4 ++++ src/util/logging.ts | 4 ++++ 10 files changed, 39 insertions(+), 4 deletions(-) diff --git a/__tests__/functional/client-configuration.test.ts b/__tests__/functional/client-configuration.test.ts index b9a1d0e3..319ee6e9 100644 --- a/__tests__/functional/client-configuration.test.ts +++ b/__tests__/functional/client-configuration.test.ts @@ -110,6 +110,9 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo }, close() {}, + getURL(): string { + return "http://foo.com/bar"; + }, }; const client = getClient( @@ -147,6 +150,9 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo }, close() {}, + getURL(): string { + return "https://foo.com/bar"; + }, }; const client = getClient( diff --git a/__tests__/functional/feed-client-configuration.test.ts b/__tests__/functional/feed-client-configuration.test.ts index 7c190e5b..ba25797b 100644 --- a/__tests__/functional/feed-client-configuration.test.ts +++ b/__tests__/functional/feed-client-configuration.test.ts @@ -5,6 +5,7 @@ import { FeedClient, } from "../../src"; import { getDefaultHTTPClientOptions } from "../client"; +import { defaultLogHandler } from "../../src/util/logging"; const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions()); const defaultConfig: FeedClientConfiguration = { @@ -14,6 +15,7 @@ const defaultConfig: FeedClientConfiguration = { max_backoff: 20, query_timeout_ms: 5000, client_timeout_buffer_ms: 5000, + logger: defaultLogHandler(), httpClient: defaultHttpClient, }; const dummyStreamToken = new StreamToken("dummy"); diff --git a/__tests__/functional/feed-client.test.ts b/__tests__/functional/feed-client.test.ts index a31ef149..7749b6bd 100644 --- a/__tests__/functional/feed-client.test.ts +++ b/__tests__/functional/feed-client.test.ts @@ -5,7 +5,11 @@ import { EventSource, StreamToken, ThrottlingError, + HTTPClient, + HTTPRequest, + HTTPResponse, } from "../../src"; +import { defaultLogHandler } from "../../src/util/logging"; const mockHttpResponse = { status: 200, @@ -27,7 +31,9 @@ const mockHttpClient = { .fn() .mockImplementation(() => Promise.resolve({ ...mockHttpResponse })), close: jest.fn(), + getURL: jest.fn(() => "bar"), }; + const defaultConfig: FeedClientConfiguration = { secret: "secret", long_type: "number", @@ -35,6 +41,7 @@ const defaultConfig: FeedClientConfiguration = { max_backoff: 20, query_timeout_ms: 5000, client_timeout_buffer_ms: 5000, + logger: defaultLogHandler(), httpClient: mockHttpClient, }; const testEventSource: EventSource = new StreamToken("dummy"); diff --git a/__tests__/functional/stream-client-configuration.test.ts b/__tests__/functional/stream-client-configuration.test.ts index 00fa3bec..02aa30df 100644 --- a/__tests__/functional/stream-client-configuration.test.ts +++ b/__tests__/functional/stream-client-configuration.test.ts @@ -3,8 +3,10 @@ import { StreamToken, getDefaultHTTPClient, StreamClientConfiguration, + ConsoleLogHandler, } from "../../src"; import { getDefaultHTTPClientOptions } from "../client"; +import { defaultLogHandler } from "../../src/util/logging"; const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions()); const defaultConfig: StreamClientConfiguration = { @@ -12,6 +14,7 @@ const defaultConfig: StreamClientConfiguration = { long_type: "number", max_attempts: 3, max_backoff: 20, + logger: defaultLogHandler(), httpStreamClient: defaultHttpClient, }; const dummyStreamToken = new StreamToken("dummy"); diff --git a/src/client-configuration.ts b/src/client-configuration.ts index 60bc9e9f..d4976f63 100644 --- a/src/client-configuration.ts +++ b/src/client-configuration.ts @@ -235,7 +235,6 @@ export type FeedClientConfiguration = Required< | "query_timeout_ms" | "secret" | "logger" - | "endpoint" > > & { /** diff --git a/src/client.ts b/src/client.ts index 90f98a4d..217016dd 100644 --- a/src/client.ts +++ b/src/client.ts @@ -626,6 +626,7 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\ this.#logger.debug( "Fauna HTTP %s Request to %s (timeout: %s), headers: %s", method, + this.#httpClient.getURL(), this.#clientConfiguration.endpoint.toString(), client_timeout_ms.toString(), JSON.stringify(headers), @@ -641,7 +642,7 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\ this.#logger.debug( "Fauna HTTP Response %s from %s, headers: %s", response.status, - this.#clientConfiguration.endpoint.toString(), + this.#httpClient.getURL(), JSON.stringify(response.headers), ); @@ -1107,7 +1108,7 @@ export class FeedClient { this.#logger.debug( "Fauna HTTP %s Request to %s (timeout: %s), headers: %s", request.method, - this.#clientConfiguration.endpoint.toString(), + httpClient.getURL(), request.client_timeout_ms, JSON.stringify(request.headers), ); @@ -1119,7 +1120,7 @@ export class FeedClient { this.#logger.debug( "Fauna HTTP Response %s from %s, headers: %s", response.status, - this.#clientConfiguration.endpoint.toString(), + httpClient.getURL(), JSON.stringify(response.headers), ); diff --git a/src/http-client/fetch-client.ts b/src/http-client/fetch-client.ts index cd3f8efe..6e6443e8 100644 --- a/src/http-client/fetch-client.ts +++ b/src/http-client/fetch-client.ts @@ -32,6 +32,10 @@ export class FetchClient implements HTTPClient, HTTPStreamClient { return new URL(path, this.#baseUrl).toString(); } + getURL(): string { + return this.#resolveURL(this.#defaultRequestPath); + } + /** {@inheritDoc HTTPClient.request} */ async request({ data, diff --git a/src/http-client/http-client.ts b/src/http-client/http-client.ts index 8ba4da14..aaf1991c 100644 --- a/src/http-client/http-client.ts +++ b/src/http-client/http-client.ts @@ -68,6 +68,11 @@ export interface HTTPClient { * is a no-op as there is no shared resource to close. */ close(): void; + + /** + * Return the full URL (path and endpoint) for the query endpoint for this client. + */ + getURL(): string; } /** diff --git a/src/http-client/node-http2-client.ts b/src/http-client/node-http2-client.ts index c412afd0..05551e86 100644 --- a/src/http-client/node-http2-client.ts +++ b/src/http-client/node-http2-client.ts @@ -54,6 +54,10 @@ export class NodeHTTP2Client implements HTTPClient, HTTPStreamClient { this.#session = null; } + getURL(): string { + return this.#url; + } + /** * Gets a {@link NodeHTTP2Client} matching the {@link HTTPClientOptions} * @param httpClientOptions - the {@link HTTPClientOptions} diff --git a/src/util/logging.ts b/src/util/logging.ts index 1f6e6acb..d74341be 100644 --- a/src/util/logging.ts +++ b/src/util/logging.ts @@ -84,3 +84,7 @@ export class ConsoleLogHandler implements LogHandler { } } } + +export function defaultLogHandler(): LogHandler { + return new ConsoleLogHandler(LOG_LEVELS.FATAL); +}