Skip to content

Commit

Permalink
Adding support for setting the fetch API credentials mode (smithy-lan…
Browse files Browse the repository at this point in the history
…g#1317)

* feat(fetch-http-handler): formatting and types

---------

Co-authored-by: George Fu <kuhe@users.noreply.github.com>
  • Loading branch information
qnesingh and kuhe authored Jun 27, 2024
1 parent b69b742 commit 4784fb9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/polite-fans-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/fetch-http-handler": minor
"@smithy/types": minor
---

Adding support for setting the fetch API credentials mode
29 changes: 29 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@ describe(FetchHttpHandler.name, () => {
expect(await blobToText(response.body)).toBe("FOO");
});

it.each(["include", "omit", "same-origin"])(
"will pass credentials mode '%s' from a provider to a request",
async (credentialsMode) => {
const mockResponse = {
headers: { entries: jest.fn().mockReturnValue([]) },
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);

(global as any).fetch = mockFetch;

const httpRequest = new HttpRequest({
headers: {},
hostname: "foo.amazonaws.com",
method: "GET",
path: "/",
body: "will be omitted",
});
const fetchHttpHandler = new FetchHttpHandler();
fetchHttpHandler.updateHttpClientConfig("credentials", credentialsMode as RequestCredentials);

await fetchHttpHandler.handle(httpRequest, {});

expect(mockFetch.mock.calls.length).toBe(1);
const requestCall = mockRequest.mock.calls[0];
expect(requestCall[1].credentials).toBe(credentialsMode);
}
);

describe("#destroy", () => {
it("should be callable and return nothing", () => {
const httpHandler = new FetchHttpHandler();
Expand Down
2 changes: 2 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
}
const requestTimeoutInMs = this.config!.requestTimeout;
const keepAlive = this.config!.keepAlive === true;
const credentials = this.config!.credentials as RequestInit["credentials"];

// if the request was already aborted, prevent doing extra work
if (abortSignal?.aborted) {
Expand Down Expand Up @@ -110,6 +111,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
body,
headers: new Headers(request.headers),
method: method,
credentials,
};
if (body) {
requestOptions.duplex = "half";
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/http/httpHandlerInitialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ export interface FetchHttpHandlerOptions {
* these limitations before enabling keepalive.
*/
keepAlive?: boolean;

/**
* A string indicating whether credentials will be sent with the request always, never, or
* only when sent to a same-origin URL.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
*/
credentials?: "include" | "omit" | "same-origin" | undefined | string;
}

0 comments on commit 4784fb9

Please sign in to comment.