-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend HTTPRequest to support path overrides (#288)
- Loading branch information
Showing
7 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,62 @@ | ||
import http2 from "node:http2"; | ||
import { getDefaultHTTPClient, NodeHTTP2Client } from "../../src"; | ||
import { HTTPRequest } from "../../src"; | ||
import { getDefaultHTTPClientOptions } from "../client"; | ||
import { SupportedFaunaAPIPaths } from "../../src/http-client"; | ||
|
||
const mockRequest = jest.fn(); | ||
const mockClient = { | ||
request: mockRequest, | ||
once: () => mockClient, | ||
setTimeout: jest.fn(), | ||
}; | ||
jest.spyOn(http2, "connect").mockReturnValue(mockClient as any); | ||
|
||
const client = getDefaultHTTPClient(getDefaultHTTPClientOptions()); | ||
|
||
describe("node http2 client", () => { | ||
it("default client for Node.js is the NodeHTTP2Client", async () => { | ||
expect(client).toBeInstanceOf(NodeHTTP2Client); | ||
}); | ||
|
||
it("uses the default request path if none is provided", async () => { | ||
const request: HTTPRequest = { | ||
client_timeout_ms: 0, | ||
data: { query: "some-query" }, | ||
headers: {}, | ||
method: "POST", | ||
}; | ||
|
||
// We don't actually care about the status of this request for this specific test. | ||
// We're just testing the path is being set correctly. | ||
try { | ||
await client.request(request); | ||
} catch (_) {} | ||
|
||
expect(mockRequest).toHaveBeenCalledWith({ | ||
":method": "POST", | ||
":path": "/query/1", | ||
}); | ||
}); | ||
|
||
it("uses the path provided in HttpRequest if provided", async () => { | ||
const request: HTTPRequest = { | ||
client_timeout_ms: 0, | ||
data: { query: "some-query" }, | ||
headers: {}, | ||
method: "POST", | ||
path: "/some-path" as SupportedFaunaAPIPaths, | ||
}; | ||
|
||
// We don't actually care about the status of this request for this specific test. | ||
// We're just testing the path is being set correctly. | ||
try { | ||
await client.request(request); | ||
} catch (_) {} | ||
|
||
expect(mockRequest).toHaveBeenCalledWith({ | ||
":method": "POST", | ||
":path": "/some-path", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Readonly object representing the paths of the Fauna API to be used | ||
* with HTTP clients. | ||
*/ | ||
export const FaunaAPIPaths = { | ||
QUERY: "/query/1", | ||
STREAM: "/stream/1", | ||
} as const; | ||
|
||
export type SupportedFaunaAPIPaths = | ||
(typeof FaunaAPIPaths)[keyof typeof FaunaAPIPaths]; |