|
6 | 6 | Middleware,
|
7 | 7 | HandlerExecutionContext,
|
8 | 8 | FinalizeMiddleware,
|
9 |
| - FinalizeHandler |
| 9 | + FinalizeHandler, |
| 10 | + BuildMiddleware |
10 | 11 | } from "@aws-sdk/types";
|
11 | 12 |
|
12 | 13 | type input = Array<string>;
|
@@ -170,4 +171,69 @@ describe("MiddlewareStack", () => {
|
170 | 171 | {} as any
|
171 | 172 | )({ input: [] });
|
172 | 173 | });
|
| 174 | + |
| 175 | + it("should allow filtering of middlewares by middleware options", async () => { |
| 176 | + const stack = new MiddlewareStack<input, output>(); |
| 177 | + stack.add(getConcatMiddleware("first") as Middleware<input, output>, { |
| 178 | + priority: 1 |
| 179 | + }); |
| 180 | + stack.add(getConcatMiddleware("second") as Middleware<input, output>, { |
| 181 | + tags: { foo: true, bar: true } |
| 182 | + }); |
| 183 | + stack.add(getConcatMiddleware("third") as Middleware<input, output>, { |
| 184 | + step: "initialize" |
| 185 | + }); |
| 186 | + stack.add(getConcatMiddleware("fourth") as Middleware<input, output>, { |
| 187 | + step: "serialize" |
| 188 | + }); |
| 189 | + stack.add(getConcatMiddleware("fifth") as BuildMiddleware<input, output>, { |
| 190 | + step: "build" |
| 191 | + }); |
| 192 | + stack.add( |
| 193 | + getConcatMiddleware("sixth") as FinalizeMiddleware<input, output>, |
| 194 | + { |
| 195 | + step: "finalize" |
| 196 | + } |
| 197 | + ); |
| 198 | + const filteredStack = stack.filter(middlewareStats => { |
| 199 | + return ( |
| 200 | + middlewareStats.priority === 1 || |
| 201 | + (middlewareStats.tags && middlewareStats.tags.foo === true) || |
| 202 | + middlewareStats.step === "initialize" |
| 203 | + ); |
| 204 | + }); |
| 205 | + const handler = jest.fn(({ input }: FinalizeHandlerArguments<input>) => { |
| 206 | + expect(input).toEqual(["first", "third", "second"]); |
| 207 | + return {}; |
| 208 | + }); |
| 209 | + |
| 210 | + const composed = filteredStack.resolve(handler, {} as any); |
| 211 | + await composed({ input: [] }); |
| 212 | + |
| 213 | + expect(handler.mock.calls.length).toBe(1); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should not allow altering stack that to be filtered", async () => { |
| 217 | + const stack = new MiddlewareStack<input, output>(); |
| 218 | + stack.add(getConcatMiddleware("first") as Middleware<input, output>, { |
| 219 | + priority: 1 |
| 220 | + }); |
| 221 | + stack.add(getConcatMiddleware("second") as Middleware<input, output>, { |
| 222 | + tags: { foo: true, bar: true, baz: true } |
| 223 | + }); |
| 224 | + const filteredStack = stack.filter(middlewareStats => { |
| 225 | + if (middlewareStats.tags!.baz) { |
| 226 | + //try make "second" middleware prior to "first" middleware |
| 227 | + middlewareStats.priority = 100; |
| 228 | + } |
| 229 | + return true; |
| 230 | + }); |
| 231 | + let inner = jest.fn(({ input }: FinalizeHandlerArguments<input>) => { |
| 232 | + expect(input).toEqual(["first", "second"]); |
| 233 | + return Promise.resolve({}); |
| 234 | + }); |
| 235 | + await filteredStack.resolve(inner, {} as any)({ input: [] }); |
| 236 | + |
| 237 | + expect(inner.mock.calls.length).toBe(1); |
| 238 | + }); |
173 | 239 | });
|
0 commit comments