Skip to content

Commit

Permalink
fix(middleware-eventstream): lowercase headers more consistently (#6259)
Browse files Browse the repository at this point in the history
* fix(middleware-eventstream): lowercase headers more consistently

* fix: restore test in packages/middleware-websocket/src/middleware-websocket-endpoint.spec.ts

Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>

* test(middleware-websocket): update unit test for clarity

* test(middleware-websocket): update unit test assertion

---------

Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
  • Loading branch information
kuhe and trivikr authored Jul 9, 2024
1 parent 2697d89 commit 1965eb0
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const mockHandle = jest.fn().mockResolvedValue({
response: new HttpResponse({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"content-type": "application/json",
},
body: Readable.from([""]),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(getCredentials.name, () => {
const response = new HttpResponse({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"content-type": "application/json",
},
body: Readable.from(JSON.stringify(data)),
});
Expand All @@ -34,7 +34,7 @@ describe(getCredentials.name, () => {
const response = new HttpResponse({
statusCode: 400,
headers: {
"Content-Type": "application/json",
"content-type": "application/json",
},
body: Readable.from(
JSON.stringify({
Expand All @@ -57,7 +57,7 @@ describe(getCredentials.name, () => {
const response = new HttpResponse({
statusCode: 500,
headers: {
"Content-Type": "json",
"content-type": "json",
},
body: Readable.from(JSON.stringify({})),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const eventStreamHeaderMiddleware: BuildMiddleware<any, any> = (next) =>
if (!HttpRequest.isInstance(request)) return next(args);
request.headers = {
...request.headers,
"Content-Type": "application/vnd.amazon.eventstream",
"content-type": "application/vnd.amazon.eventstream",
"x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS",
};
return next({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("middleware-eventstream", () => {

requireRequestsFrom(client).toMatch({
headers: {
"Content-Type": "application/vnd.amazon.eventstream",
"content-type": "application/vnd.amazon.eventstream",
"x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS",
},
});
Expand Down Expand Up @@ -51,7 +51,7 @@ describe("middleware-eventstream", () => {

requireRequestsFrom(client).toMatch({
headers: {
"Content-Type": "application/vnd.amazon.eventstream",
"content-type": "application/vnd.amazon.eventstream",
"x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS",
},
});
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("middleware-eventstream", () => {

requireRequestsFrom(client).toMatch({
headers: {
"Content-Type": "application/vnd.amazon.eventstream",
"content-type": "application/vnd.amazon.eventstream",
"x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-EVENTS",
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("middleware-sdk-transcribe-streaming", () => {

requireRequestsFrom(client).toMatch({
headers: {
"Content-Type": /undefined/,
"content-type": /undefined/,
host: "transcribestreaming.us-west-2.amazonaws.com:8443",
},
query: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe(websocketEndpointMiddleware.name, () => {
mw(next as any, {} as any)({ request, input: {} });
});

it("should remove content-type and sha256 hash header", (done) => {
it("should remove content-type and sha256 hash header without transferring them to query parameters", (done) => {
const request = new HttpRequest({
headers: {
"content-type": "application/vnd.amazon.eventstream",
Expand All @@ -50,21 +50,22 @@ describe(websocketEndpointMiddleware.name, () => {
const next = (args: BuildHandlerArguments<any>) => {
expect(HttpRequest.isInstance(args.request)).toBeTruthy();
const processed = args.request as HttpRequest;
expect(processed.headers["content-type"]).toBeUndefined();
expect(processed.headers["Content-Type"]).toBeUndefined();
expect(processed.headers["x-amz-content-sha256"]).toBeUndefined();
expect(processed.headers["X-Amz-Content-Sha256"]).toBeUndefined();
const queryKeys = Object.keys(processed.query).map((key) => key.toLowerCase());
expect(queryKeys).not.toContain("content-type");
expect(queryKeys).not.toContain("x-amz-content-sha256");
done();
};
const mw = websocketEndpointMiddleware(config, handlerOption);
mw(next as any, {} as any)({ request, input: {} });
});

it("should contains host header after adjustment", (done) => {
it("should contain only a host header after adjustment", (done) => {
const request = new HttpRequest({});
const next = (args: BuildHandlerArguments<any>) => {
expect(HttpRequest.isInstance(args.request)).toBeTruthy();
const processed = args.request as HttpRequest;
const headerKeys = Object.keys(processed.headers).map((key) => key.toLowerCase());
expect(headerKeys).toEqual(["host"]);
expect(processed.headers["host"]).toBeDefined();
done();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const websocketEndpointMiddleware =
// 'Content-Type' and 'x-amz-content-sha256' headers are normally set for
// event stream, but WebSocket doesn't require it.
// See: 'eventStreamHeaderMiddleware' in @aws-sdk/middleware-eventstream
delete headers["Content-Type"];
delete headers["content-type"];
delete headers["x-amz-content-sha256"];

for (const name of Object.keys(headers)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("middleware-websocket", () => {
path: "/start-face-liveness-session-websocket",
headers: {
host: "streaming-rekognition.us-west-2.amazonaws.com",
"Content-Type": /^undefined$/,
"content-type": /^undefined$/,
"x-amz-content-sha256": /^undefined$/,
"user-agent": /^aws-sdk-js/,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/s3-request-presigner/src/presigner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("s3 presigner", () => {
...minimalRequest,
headers: {
...minimalRequest.headers,
"Content-Type": "application/octet-stream",
"content-type": "application/octet-stream",
},
};
const signed = await signer.presign(requestWithContentTypeHeader, presigningOptions);
Expand Down

0 comments on commit 1965eb0

Please sign in to comment.