Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Axios #66

Merged
merged 15 commits into from
Feb 24, 2023
51 changes: 26 additions & 25 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client } from "../../src/client";
import { endpoints } from "../../src/client-configuration";
import { HTTPClient, getDefaultHTTPClient } from "../../src/http-client";

beforeEach(() => {
delete process.env["FAUNA_SECRET"];
Expand Down Expand Up @@ -50,7 +51,6 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
secret: "secret",
timeout_ms: 60_000,
});
expect(client.client.defaults.baseURL).toEqual("http://localhost:7443/");
const result = await client.query<number>({ query: '"taco".length' });
expect(result.data).toEqual(4);
expect(result.txn_time).toBeDefined();
Expand All @@ -77,37 +77,38 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
type HeaderTestInput = {
fieldName: "linearized" | "max_contention_retries" | "tags" | "traceparent";
fieldValue: any;
expectedHeader: string;
expectedHeader: { key: string, value: string};
};

it.each`
fieldName | fieldValue | expectedHeader
${"linearized"} | ${true} | ${"x-linearized: true"}
${"max_contention_retries"} | ${3} | ${"x-max-contention-retries: 3"}
${"tags"} | ${{ t1: "v1", t2: "v2" }} | ${"x-fauna-tags: t1=v1,t2=v2"}
${"traceparent"} | ${"00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00"} | ${"traceparent: 00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00"}
${"linearized"} | ${true} | ${{ key: "x-linearized", value: "true" }}
${"max_contention_retries"} | ${3} | ${{ key: "x-max-contention-retries", value: "3" }}
${"tags"} | ${{ t1: "v1", t2: "v2" }} | ${{ key: "x-fauna-tags", value: "t1=v1,t2=v2" }}
${"traceparent"} | ${"00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00"} | ${{ key: "traceparent", value: "00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00" }}
`(
"Setting clientConfiguration $fieldName leads to it being sent in headers",
async ({ fieldName, fieldValue, expectedHeader }: HeaderTestInput) => {
const client = new Client({
endpoint: endpoints.local,
max_conns: 5,
secret: "secret",
timeout_ms: 5000,
[fieldName]: fieldValue,
});
client.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (response.request?._header) {
expect(response.request._header).toEqual(
expect.stringContaining("x-timeout-ms: 5000")
);
expect(response.request._header).toEqual(
expect.stringContaining(`\n${expectedHeader}`)
);
}
return response;
});
expect.assertions(2);
const httpClient: HTTPClient = {
async request(req) {
expect(req.headers["x-timeout-ms"]).toEqual("5000");
const _expectedHeader = expectedHeader
expect(req.headers[_expectedHeader.key]).toEqual(_expectedHeader.value);
return getDefaultHTTPClient().request(req);
},
};

const client = new Client(
{
endpoint: endpoints.local,
max_conns: 5,
secret: "secret",
timeout_ms: 5000,
[fieldName]: fieldValue,
},
httpClient
);
await client.query<number>({ query: '"taco".length' });
}
);
Expand Down
84 changes: 46 additions & 38 deletions __tests__/integration/client-last-txn-tracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ import { Client } from "../../src/client";
import { endpoints } from "../../src/client-configuration";
import { env } from "process";
import { fql } from "../../src/query-builder";
import {
HTTPClient,
getDefaultHTTPClient,
} from "../../src/http-client";

describe("last_txn tracking in client", () => {
it("Tracks the last_txn datetime and send in the headers", async () => {
const myClient = new Client({
endpoint: env["endpoint"] ? new URL(env["endpoint"]) : endpoints.local,
max_conns: 5,
secret: env["secret"] || "secret",
timeout_ms: 60_000,
});
let expectedLastTxn: string | undefined = undefined;
myClient.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (expectedLastTxn === undefined) {
expect(response.request?._header).not.toEqual(
expect.stringContaining("x-last-txn")
);
} else {
expect(response.request?._header).toEqual(
expect.stringContaining(`\nx-last-txn: ${expectedLastTxn}`)
);
}
return response;
});
const httpClient: HTTPClient = {
async request(req) {
if (expectedLastTxn === undefined) {
expect(req.headers["x-last-txn"]).toBeUndefined()
} else {
expect(req.headers["x-last-txn"]).toEqual(expectedLastTxn)
}
return getDefaultHTTPClient().request(req);
},
};

const myClient = new Client(
{
endpoint: env["endpoint"] ? new URL(env["endpoint"]) : endpoints.local,
max_conns: 5,
secret: env["secret"] || "secret",
timeout_ms: 60_000,
},
httpClient
);

const resultOne = await myClient.query({
query:
"\
Expand Down Expand Up @@ -55,26 +61,28 @@ if (Collection.byName('Products') == null) {\
});

it("Accepts an override of the last_txn datetime and sends in the headers", async () => {
const myClient = new Client({
endpoint: env["endpoint"] ? new URL(env["endpoint"]) : endpoints.local,
max_conns: 5,
secret: env["secret"] || "secret",
timeout_ms: 60_000,
});
let expectedLastTxn: string | undefined = undefined;
myClient.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (expectedLastTxn === undefined) {
expect(response.request?._header).not.toEqual(
expect.stringContaining("x-last-txn")
);
} else {
expect(response.request?._header).toEqual(
expect.stringContaining(`\nx-last-txn: ${expectedLastTxn}`)
);
}
return response;
});
const httpClient: HTTPClient = {
async request(req) {
if (expectedLastTxn === undefined) {
expect(req.headers["x-last-txn"]).toBeUndefined()
} else {
expect(req.headers["x-last-txn"]).toEqual(expectedLastTxn)
}
return getDefaultHTTPClient().request(req);
},
};

const myClient = new Client(
{
endpoint: env["endpoint"] ? new URL(env["endpoint"]) : endpoints.local,
max_conns: 5,
secret: env["secret"] || "secret",
timeout_ms: 60_000,
},
httpClient
);

const resultOne = await myClient.query({
query:
"\
Expand Down
180 changes: 0 additions & 180 deletions __tests__/integration/connection-pool.test.ts

This file was deleted.

Loading