Skip to content

Commit

Permalink
Refactoring to make tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Griffin committed Dec 9, 2024
1 parent f394920 commit ba0dc0b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions __tests__/functional/feed-client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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");
Expand Down
7 changes: 7 additions & 0 deletions __tests__/functional/feed-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
EventSource,
StreamToken,
ThrottlingError,
HTTPClient,
HTTPRequest,
HTTPResponse,
} from "../../src";
import { defaultLogHandler } from "../../src/util/logging";

const mockHttpResponse = {
status: 200,
Expand All @@ -27,14 +31,17 @@ const mockHttpClient = {
.fn()
.mockImplementation(() => Promise.resolve({ ...mockHttpResponse })),
close: jest.fn(),
getURL: jest.fn(() => "bar"),
};

const defaultConfig: FeedClientConfiguration = {
secret: "secret",
long_type: "number",
max_attempts: 3,
max_backoff: 20,
query_timeout_ms: 5000,
client_timeout_buffer_ms: 5000,
logger: defaultLogHandler(),
httpClient: mockHttpClient,
};
const testEventSource: EventSource = new StreamToken("dummy");
Expand Down
3 changes: 3 additions & 0 deletions __tests__/functional/stream-client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import {
StreamToken,
getDefaultHTTPClient,
StreamClientConfiguration,
ConsoleLogHandler,
} from "../../src";
import { getDefaultHTTPClientOptions } from "../client";
import { defaultLogHandler } from "../../src/util/logging";

const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions());
const defaultConfig: StreamClientConfiguration = {
secret: "secret",
long_type: "number",
max_attempts: 3,
max_backoff: 20,
logger: defaultLogHandler(),
httpStreamClient: defaultHttpClient,
};
const dummyStreamToken = new StreamToken("dummy");
Expand Down
1 change: 0 additions & 1 deletion src/client-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ export type FeedClientConfiguration = Required<
| "query_timeout_ms"
| "secret"
| "logger"
| "endpoint"
>
> & {
/**
Expand Down
7 changes: 4 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
);

Expand Down Expand Up @@ -1107,7 +1108,7 @@ export class FeedClient<T extends QueryValue = any> {
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),
);
Expand All @@ -1119,7 +1120,7 @@ export class FeedClient<T extends QueryValue = any> {
this.#logger.debug(
"Fauna HTTP Response %s from %s, headers: %s",
response.status,
this.#clientConfiguration.endpoint.toString(),
httpClient.getURL(),
JSON.stringify(response.headers),
);

Expand Down
4 changes: 4 additions & 0 deletions src/http-client/fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = QueryRequest>({
data,
Expand Down
5 changes: 5 additions & 0 deletions src/http-client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/http-client/node-http2-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 4 additions & 0 deletions src/util/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export class ConsoleLogHandler implements LogHandler {
}
}
}

export function defaultLogHandler(): LogHandler {
return new ConsoleLogHandler(LOG_LEVELS.FATAL);
}

0 comments on commit ba0dc0b

Please sign in to comment.