Skip to content

Commit

Permalink
Migrate acceptsHeaderMiddleware (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell authored and trivikr committed Jan 3, 2020
1 parent 34b9fd7 commit d41f500
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public List<RuntimeClientPlugin> getClientPlugins() {
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.MIDDLEWARE_CONTENT_LENGTH.dependency, "ContentLength",
HAS_MIDDLEWARE)
.build(),
RuntimeClientPlugin.builder()
.withConventions("@aws-sdk/middleware-sdk-api-gateway", "^0.1.0-preview.5",
"AcceptsHeader", HAS_MIDDLEWARE)
.servicePredicate((m,s) -> s.getId().getName().equals("BackplaneControlService"))
.build()
);
}
Expand Down
28 changes: 11 additions & 17 deletions packages/middleware-sdk-api-gateway/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import { BuildHandler } from "@aws-sdk/types";
import { acceptsHeader } from "./index";
import { acceptsHeaderMiddleware } from "./index";
import { HttpRequest } from "@aws-sdk/protocol-http";

describe("acceptsHeader", () => {
const mockNextHandler = jest.fn();

const composedHandler: BuildHandler<any, any> = acceptsHeader(
mockNextHandler
);
describe("acceptsHeaderMiddleware", () => {
const next = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});

it("sets Accepts header to application/json", async () => {
await composedHandler({
const handler = acceptsHeaderMiddleware()(next, {} as any);
await handler({
input: {},
request: {
hostname: "foo.amazonaws.com",
path: "/",
protocol: "https:",
method: "GET",
request: new HttpRequest({
headers: {}
}
})
});

expect(mockNextHandler.mock.calls.length).toBe(1);
const { request } = mockNextHandler.mock.calls[0][0];
const { calls } = (next as any).mock;
expect(calls.length).toBe(1);
const { request } = next.mock.calls[0][0];
expect(request.headers["accepts"]).toBe("application/json");
});
});
46 changes: 36 additions & 10 deletions packages/middleware-sdk-api-gateway/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import { BuildHandler, BuildHandlerArguments } from "@aws-sdk/types";
import {
BuildHandler,
BuildHandlerArguments,
BuildHandlerOptions,
BuildHandlerOutput,
BuildMiddleware,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";
import { HttpRequest } from "@aws-sdk/protocol-http";

export function acceptsHeader(next: BuildHandler<any, any>) {
return async (args: BuildHandlerArguments<any, any>) => {
export function acceptsHeaderMiddleware(): BuildMiddleware<any, any> {
return <Output extends MetadataBearer>(
next: BuildHandler<any, Output>
): BuildHandler<any, Output> => async (
args: BuildHandlerArguments<any>
): Promise<BuildHandlerOutput<Output>> => {
let request = { ...args.request };
if (HttpRequest.isInstance(request)) {
request.headers = {
...request.headers,
accepts: "application/json"
};
}
return next({
...args,
request: {
...args.request,
headers: {
...args.request.headers,
accepts: "application/json"
}
}
request
});
};
}

export const acceptsHeaderMiddlewareOptions: BuildHandlerOptions = {
step: "build",
tags: ["SET_ACCEPTS_HEADER", "ACCEPTS_HEADER"],
name: "acceptsHeaderMiddleware"
};

export const getAcceptsHeaderPlugin = (): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(acceptsHeaderMiddleware(), acceptsHeaderMiddlewareOptions);
}
});

0 comments on commit d41f500

Please sign in to comment.