Skip to content

Commit fe5a061

Browse files
Chase Coalwelltrivikr
Chase Coalwell
authored andcommitted
Feat: update and apply expect continue middleware (#477)
* feat: migrate expect-continue middleware * feat: apply expect-continue plugin
1 parent e084d49 commit fe5a061

File tree

4 files changed

+64
-99
lines changed

4 files changed

+64
-99
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public List<RuntimeClientPlugin> getClientPlugins() {
6262
.withConventions("@aws-sdk/middleware-sdk-s3", "^0.1.0-preview.2",
6363
"validateBucketNameMiddleware", HAS_MIDDLEWARE)
6464
.servicePredicate((m,s) -> s.getId().getName().equals("AmazonS3"))
65+
.build(),
66+
RuntimeClientPlugin.builder()
67+
.withConventions("@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5",
68+
"AddExpectContinue", HAS_MIDDLEWARE)
69+
.servicePredicate((m,s) -> s.getId().getName().equals("AmazonS3"))
6570
.build()
6671
);
6772
}

packages/middleware-expect-continue/src/index.spec.ts

+24-40
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
1-
import { BuildHandlerArguments } from "@aws-sdk/types";
2-
import { addExpectContinue } from "./index";
3-
import { PutObject } from "./operation.mock";
1+
import { addExpectContinueMiddleware } from "./index";
2+
import { HttpRequest } from "@aws-sdk/protocol-http";
43

5-
describe("addExpectContinue", () => {
4+
describe("addExpectContinueMiddleware", () => {
65
const mockNextHandler = jest.fn();
7-
const mockExecutionContext = {
8-
model: PutObject,
9-
logger: {} as any
10-
};
11-
const mockHandlerArgs: Partial<BuildHandlerArguments<any>> = {
12-
request: {
13-
headers: {},
14-
method: "PUT",
15-
path: "/",
16-
protocol: "https:",
17-
hostname: "foo.amazonaws.com"
18-
}
19-
};
206

217
beforeEach(() => {
228
jest.clearAllMocks();
239
});
2410

2511
it("sets the Expect header to 100-continue if there is a request body", async () => {
26-
const handler = addExpectContinue(mockNextHandler, mockExecutionContext);
27-
const handlerArgs = {
28-
request: {
29-
...mockHandlerArgs.request,
30-
body: "test"
31-
}
32-
};
33-
34-
await handler(handlerArgs as any);
35-
36-
// ensure the next handler was called
37-
expect(mockNextHandler.mock.calls.length).toBe(1);
12+
const handler = addExpectContinueMiddleware()(mockNextHandler, {} as any);
13+
await handler({
14+
input: {},
15+
request: new HttpRequest({
16+
body: "foo",
17+
headers: {}
18+
})
19+
});
20+
21+
const { calls } = (mockNextHandler as any).mock;
22+
expect(calls.length).toBe(1);
3823
const { request } = mockNextHandler.mock.calls[0][0];
3924
expect(request.headers["Expect"]).toBe("100-continue");
4025
});
4126

4227
it("does not set the Expect header to 100-continue if there is no request body", async () => {
43-
const handler = addExpectContinue(mockNextHandler, mockExecutionContext);
44-
const handlerArgs = {
45-
request: {
46-
...mockHandlerArgs.request
47-
}
48-
};
49-
50-
await handler(handlerArgs as any);
51-
52-
// ensure the next handler was called
53-
expect(mockNextHandler.mock.calls.length).toBe(1);
28+
const handler = addExpectContinueMiddleware()(mockNextHandler, {} as any);
29+
await handler({
30+
input: {},
31+
request: new HttpRequest({
32+
headers: {}
33+
})
34+
});
35+
36+
const { calls } = (mockNextHandler as any).mock;
37+
expect(calls.length).toBe(1);
5438
const { request } = mockNextHandler.mock.calls[0][0];
5539
expect(request.headers["Expect"]).toBeUndefined();
5640
});
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
import {
22
BuildHandler,
33
BuildHandlerArguments,
4-
HandlerExecutionContext
4+
BuildHandlerOptions,
5+
BuildHandlerOutput,
6+
BuildMiddleware,
7+
MetadataBearer,
8+
Pluggable
59
} from "@aws-sdk/types";
6-
import { headerDefault } from "@aws-sdk/middleware-header-default";
710
import { HttpRequest } from "@aws-sdk/protocol-http";
811

9-
export function addExpectContinue(
10-
next: BuildHandler<any, any>,
11-
context: HandlerExecutionContext
12-
) {
13-
return (args: BuildHandlerArguments<any>) => {
14-
if (HttpRequest.isInstance(args.request) && args.request.body) {
15-
return headerDefault({
12+
export function addExpectContinueMiddleware(): BuildMiddleware<any, any> {
13+
return <Output extends MetadataBearer>(
14+
next: BuildHandler<any, Output>
15+
): BuildHandler<any, Output> => async (
16+
args: BuildHandlerArguments<any>
17+
): Promise<BuildHandlerOutput<Output>> => {
18+
let request = { ...args.request };
19+
if (HttpRequest.isInstance(request) && request.body) {
20+
request.headers = {
21+
...request.headers,
1622
Expect: "100-continue"
17-
})(
18-
next,
19-
context
20-
)(args);
21-
} else {
22-
return next(args);
23+
};
2324
}
25+
return next({
26+
...args,
27+
request
28+
});
2429
};
2530
}
31+
32+
export const addExpectContinueMiddlewareOptions: BuildHandlerOptions = {
33+
step: "build",
34+
tags: ["SET_EXPECT_HEADER", "EXPECT_HEADER"],
35+
name: "addExpectContinueMiddleware"
36+
};
37+
38+
export const getAddExpectContinuePlugin = (): Pluggable<any, any> => ({
39+
applyToStack: clientStack => {
40+
clientStack.add(
41+
addExpectContinueMiddleware(),
42+
addExpectContinueMiddlewareOptions
43+
);
44+
}
45+
});

packages/middleware-expect-continue/src/operation.mock.ts

-44
This file was deleted.

0 commit comments

Comments
 (0)