|
| 1 | +import { hostHeaderMiddleware } from "./index"; |
| 2 | +import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 3 | +describe("hostHeaderMiddleware", () => { |
| 4 | + const mockNextHandler = jest.fn(); |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + jest.clearAllMocks(); |
| 8 | + }); |
| 9 | + |
| 10 | + it("should set host header if not already set", async () => { |
| 11 | + expect.assertions(2); |
| 12 | + const middleware = hostHeaderMiddleware({ requestHandler: {} as any }); |
| 13 | + const handler = middleware(mockNextHandler, {} as any); |
| 14 | + await handler({ |
| 15 | + input: {}, |
| 16 | + request: new HttpRequest({ hostname: "foo.amazonaws.com" }) |
| 17 | + }); |
| 18 | + expect(mockNextHandler.mock.calls.length).toEqual(1); |
| 19 | + expect(mockNextHandler.mock.calls[0][0].request.headers.host).toBe( |
| 20 | + "foo.amazonaws.com" |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should not set host header if already set", async () => { |
| 25 | + expect.assertions(2); |
| 26 | + const middleware = hostHeaderMiddleware({ requestHandler: {} as any }); |
| 27 | + const handler = middleware(mockNextHandler, {} as any); |
| 28 | + await handler({ |
| 29 | + input: {}, |
| 30 | + request: new HttpRequest({ |
| 31 | + hostname: "foo.amazonaws.com", |
| 32 | + headers: { host: "random host" } |
| 33 | + }) |
| 34 | + }); |
| 35 | + expect(mockNextHandler.mock.calls.length).toEqual(1); |
| 36 | + expect(mockNextHandler.mock.calls[0][0].request.headers.host).toBe( |
| 37 | + "random host" |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should set :authority header for H2 requests", async () => { |
| 42 | + expect.assertions(3); |
| 43 | + const middleware = hostHeaderMiddleware({ |
| 44 | + requestHandler: { metadata: { handlerProtocol: "h2" } } |
| 45 | + } as any); |
| 46 | + const handler = middleware(mockNextHandler, {} as any); |
| 47 | + await handler({ |
| 48 | + input: {}, |
| 49 | + request: new HttpRequest({ |
| 50 | + hostname: "foo.amazonaws.com", |
| 51 | + headers: { host: "random host" } |
| 52 | + }) |
| 53 | + }); |
| 54 | + expect(mockNextHandler.mock.calls.length).toEqual(1); |
| 55 | + expect( |
| 56 | + mockNextHandler.mock.calls[0][0].request.headers.host |
| 57 | + ).not.toBeDefined(); |
| 58 | + expect( |
| 59 | + mockNextHandler.mock.calls[0][0].request.headers[":authority"] |
| 60 | + ).toEqual(""); |
| 61 | + }); |
| 62 | +}); |
0 commit comments