Skip to content

Commit 86862ea

Browse files
authored
chore(protocol-http): switch to HttpRequest static clone method (smithy-lang#1345)
* chore(protocol-http): switch to HttpRequest static clone method * update unit test
1 parent 9624938 commit 86862ea

File tree

13 files changed

+85
-97
lines changed

13 files changed

+85
-97
lines changed

.changeset/poor-dolls-cheer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@smithy/protocol-http": minor
3+
"@smithy/signature-v4": minor
4+
"@smithy/core": minor
5+
"@smithy/experimental-identity-and-auth": patch
6+
---
7+
8+
switch to static HttpRequest clone method

packages/core/src/util-identity-and-auth/httpAuthSchemes/httpApiKeyAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class HttpApiKeyAuthSigner implements HttpSigner {
2424
if (!identity.apiKey) {
2525
throw new Error("request could not be signed with `apiKey` since the `apiKey` is not defined");
2626
}
27-
const clonedRequest = httpRequest.clone();
27+
const clonedRequest = HttpRequest.clone(httpRequest);
2828
if (signingProperties.in === HttpApiKeyAuthLocation.QUERY) {
2929
clonedRequest.query[signingProperties.name] = identity.apiKey;
3030
} else if (signingProperties.in === HttpApiKeyAuthLocation.HEADER) {

packages/core/src/util-identity-and-auth/httpAuthSchemes/httpBearerAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class HttpBearerAuthSigner implements HttpSigner {
1111
identity: TokenIdentity,
1212
signingProperties: Record<string, any>
1313
): Promise<IHttpRequest> {
14-
const clonedRequest = httpRequest.clone();
14+
const clonedRequest = HttpRequest.clone(httpRequest);
1515
if (!identity.token) {
1616
throw new Error("request could not be signed with `token` since the `token` is not defined");
1717
}

packages/experimental-identity-and-auth/src/SigV4Signer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class SigV4Signer implements HttpSigner {
1313
identity: AwsCredentialIdentity,
1414
signingProperties: Record<string, any>
1515
): Promise<IHttpRequest> {
16-
const clonedRequest = httpRequest.clone();
16+
const clonedRequest = HttpRequest.clone(httpRequest);
1717
const signer = new SignatureV4({
1818
applyChecksum: signingProperties.applyChecksum !== undefined ? signingProperties.applyChecksum : true,
1919
credentials: identity,

packages/experimental-identity-and-auth/src/httpApiKeyAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class HttpApiKeyAuthSigner implements HttpSigner {
3535
if (!identity.apiKey) {
3636
throw new Error("request could not be signed with `apiKey` since the `apiKey` is not defined");
3737
}
38-
const clonedRequest = httpRequest.clone();
38+
const clonedRequest = HttpRequest.clone(httpRequest);
3939
if (signingProperties.in === HttpApiKeyAuthLocation.QUERY) {
4040
clonedRequest.query[signingProperties.name] = identity.apiKey;
4141
} else if (signingProperties.in === HttpApiKeyAuthLocation.HEADER) {

packages/experimental-identity-and-auth/src/httpBearerAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class HttpBearerAuthSigner implements HttpSigner {
1414
identity: TokenIdentity,
1515
signingProperties: Record<string, any>
1616
): Promise<IHttpRequest> {
17-
const clonedRequest = httpRequest.clone();
17+
const clonedRequest = HttpRequest.clone(httpRequest);
1818
if (!identity.token) {
1919
throw new Error("request could not be signed with `token` since the `token` is not defined");
2020
}

packages/protocol-http/src/httpRequest.spec.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { QueryParameterBag } from "@smithy/types";
2+
13
import { HttpRequest, IHttpRequest } from "./httpRequest";
24

35
describe("HttpRequest", () => {
@@ -44,8 +46,8 @@ describe("HttpRequest", () => {
4446
it("should maintain a deprecated instance clone method", () => {
4547
const httpRequestInstance = new HttpRequest(httpRequest);
4648

47-
const clone1 = httpRequestInstance.clone();
48-
const clone2 = httpRequestInstance.clone();
49+
const clone1 = HttpRequest.clone(httpRequestInstance);
50+
const clone2 = HttpRequest.clone(httpRequestInstance);
4951

5052
expect(httpRequestInstance).toEqual(clone1);
5153
expect(clone1).toEqual(clone2);
@@ -55,3 +57,60 @@ describe("HttpRequest", () => {
5557
expect(clone1.body).toBe(clone2.body);
5658
});
5759
});
60+
61+
const cloneRequest = HttpRequest.clone;
62+
63+
describe("cloneRequest", () => {
64+
const request: IHttpRequest = Object.freeze({
65+
method: "GET",
66+
protocol: "https:",
67+
hostname: "foo.us-west-2.amazonaws.com",
68+
path: "/",
69+
headers: Object.freeze({
70+
foo: "bar",
71+
compound: "value 1, value 2",
72+
}),
73+
query: Object.freeze({
74+
fizz: "buzz",
75+
snap: ["crackle", "pop"],
76+
}),
77+
});
78+
79+
it("should return an object matching the provided request", () => {
80+
expect(cloneRequest(request)).toEqual(request);
81+
});
82+
83+
it("should return an object that with a different identity", () => {
84+
expect(cloneRequest(request)).not.toBe(request);
85+
});
86+
87+
it("should should deep-copy the headers", () => {
88+
const clone = cloneRequest(request);
89+
90+
delete clone.headers.compound;
91+
expect(Object.keys(request.headers)).toEqual(["foo", "compound"]);
92+
expect(Object.keys(clone.headers)).toEqual(["foo"]);
93+
});
94+
95+
it("should should deep-copy the query", () => {
96+
const clone = cloneRequest(request);
97+
98+
const { snap } = clone.query as QueryParameterBag;
99+
(snap as Array<string>).shift();
100+
101+
expect((request.query as QueryParameterBag).snap).toEqual(["crackle", "pop"]);
102+
expect((clone.query as QueryParameterBag).snap).toEqual(["pop"]);
103+
});
104+
105+
it("should not copy the body", () => {
106+
const body = new Uint8Array(16);
107+
const req = { ...request, body };
108+
const clone = cloneRequest(req);
109+
110+
expect(clone.body).toBe(req.body);
111+
});
112+
113+
it("should handle requests without defined query objects", () => {
114+
expect(cloneRequest({ ...request, query: void 0 }).query).toEqual({});
115+
});
116+
});

packages/signature-v4/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"license": "Apache-2.0",
2626
"dependencies": {
2727
"@smithy/is-array-buffer": "workspace:^",
28+
"@smithy/protocol-http": "workspace:^",
2829
"@smithy/types": "workspace:^",
2930
"@smithy/util-hex-encoding": "workspace:^",
3031
"@smithy/util-middleware": "workspace:^",
@@ -34,7 +35,6 @@
3435
},
3536
"devDependencies": {
3637
"@aws-crypto/sha256-js": "5.2.0",
37-
"@smithy/protocol-http": "workspace:^",
3838
"concurrently": "7.0.0",
3939
"downlevel-dts": "0.10.1",
4040
"rimraf": "3.0.2",

packages/signature-v4/src/SignatureV4.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ describe("SignatureV4", () => {
410410
});
411411

412412
it("should sign requests without host header", async () => {
413-
const request = minimalRequest.clone();
413+
const request = HttpRequest.clone(minimalRequest);
414414
delete request.headers[HOST_HEADER];
415415
const { headers } = await signer.sign(request, {
416416
signingDate: new Date("2000-01-01T00:00:00.000Z"),

packages/signature-v4/src/cloneRequest.spec.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)