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

fix(middleware-sdk-api-gateway): only set default accept header if none provided #6110

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/middleware-sdk-api-gateway/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@ describe("acceptHeaderMiddleware", () => {
const { request } = next.mock.calls[0][0];
expect(request.headers["accept"]).toBe("application/json");
});

it("doesn't override user Accept header", async () => {
const handler = acceptHeaderMiddleware()(next, {} as any);
await handler({
input: {},
request: new HttpRequest({
headers: {
accept: "application/yaml",
},
}),
});

const { calls } = (next as any).mock;
expect(calls.length).toBe(1);
const { request } = next.mock.calls[0][0];
expect(request.headers["accept"]).toBe("application/yaml");
});
});
7 changes: 3 additions & 4 deletions packages/middleware-sdk-api-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export function acceptHeaderMiddleware(): BuildMiddleware<any, any> {
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (HttpRequest.isInstance(request)) {
request.headers = {
...request.headers,
accept: "application/json",
};
if (request.headers?.accept === undefined) {
request.headers.accept = "application/json";
}
}
return next({
...args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { requireRequestsFrom } from "../../../private/aws-util-test/src";

describe("middleware-sdk-api-gateway", () => {
describe(APIGateway.name, () => {
it("should add accept header", async () => {
it("should add default accept header", async () => {
const client = new APIGateway({
region: "us-west-2",
});
Expand All @@ -19,5 +19,26 @@ describe("middleware-sdk-api-gateway", () => {

expect.hasAssertions();
});

it("should not override accept header", async () => {
const client = new APIGateway({
region: "us-west-2",
});

requireRequestsFrom(client).toMatch({
headers: {
accept: "application/yaml",
},
});

await client.getExport({
restApiId: "rest-api-id",
stageName: "stage-name",
exportType: "oas30",
accepts: "application/yaml",
});

expect.hasAssertions();
});
});
});
Loading