|
| 1 | +import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 2 | +import { FinalizeHandlerArguments, HandlerExecutionContext } from "@aws-sdk/types"; |
| 3 | + |
| 4 | +import { queueUrlMiddleware, QueueUrlResolvedConfig } from "./queue-url"; // Adjust the import path as necessary |
| 5 | + |
| 6 | +describe("queueUrlMiddleware", () => { |
| 7 | + const mockNextHandler = jest.fn(); |
| 8 | + let mockEndpointV2: { url: URL }; |
| 9 | + |
| 10 | + const mockContext: HandlerExecutionContext = { |
| 11 | + logger: console, |
| 12 | + endpointV2: { |
| 13 | + url: new URL("https://sqs.us-east-1.amazonaws.com"), |
| 14 | + }, |
| 15 | + }; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockNextHandler.mockReset(); |
| 19 | + mockNextHandler.mockResolvedValue({ output: {}, response: {} }); |
| 20 | + mockEndpointV2 = { url: new URL("https://sqs.us-east-1.amazonaws.com") }; // Default endpoint |
| 21 | + }); |
| 22 | + |
| 23 | + it("should use the QueueUrl as the endpoint if useQueueUrlAsEndpoint is true", async () => { |
| 24 | + const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: true }); |
| 25 | + const input = { QueueUrl: "https://xyz.com/123/MyQueue" }; |
| 26 | + const request = new HttpRequest({ |
| 27 | + hostname: "sqs.us-east-1.amazonaws.com", |
| 28 | + protocol: "https:", |
| 29 | + path: "/", |
| 30 | + headers: {}, |
| 31 | + method: "GET", |
| 32 | + }); |
| 33 | + const args: FinalizeHandlerArguments<any> = { input, request }; |
| 34 | + |
| 35 | + await middleware(mockNextHandler, mockContext)(args); |
| 36 | + |
| 37 | + // Verify that the resolvedEndpoint.url has been modified to match QueueUrl |
| 38 | + expect(mockEndpointV2.url.href).toEqual(input.QueueUrl); |
| 39 | + expect(mockNextHandler).toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should not modify the endpoint if useQueueUrlAsEndpoint is false", async () => { |
| 43 | + const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: false }); |
| 44 | + const input = { QueueUrl: "https://xyz.com/123/MyQueue" }; |
| 45 | + const request = new HttpRequest({ hostname: "sqs.us-east-1.amazonaws.com" }); |
| 46 | + const args: FinalizeHandlerArguments<any> = { input, request }; |
| 47 | + |
| 48 | + await middleware(mockNextHandler, mockContext)(args); |
| 49 | + |
| 50 | + expect(mockNextHandler).toHaveBeenCalledWith(args); |
| 51 | + }); |
| 52 | + |
| 53 | + it("when QueueUrl is not provided", async () => { |
| 54 | + const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: true }); |
| 55 | + const input = {}; // No QueueUrl provided |
| 56 | + const request = new HttpRequest({ hostname: "sqs.us-east-1.amazonaws.com" }); |
| 57 | + const args: FinalizeHandlerArguments<any> = { input, request }; |
| 58 | + |
| 59 | + await middleware(mockNextHandler, mockContext)(args); |
| 60 | + |
| 61 | + expect(mockNextHandler).toHaveBeenCalledWith(args); |
| 62 | + }); |
| 63 | +}); |
0 commit comments