|
| 1 | +import { Endpoint } from "@aws-sdk/types"; |
| 2 | + |
| 3 | +import { resolveEndpointsConfig } from "./EndpointsConfig"; |
| 4 | + |
| 5 | +describe("EndpointsConfig", () => { |
| 6 | + const region = jest.fn(); |
| 7 | + const urlParser = jest.fn(); |
| 8 | + const regionInfoProvider = jest.fn(); |
| 9 | + |
| 10 | + const input = { region, urlParser, regionInfoProvider }; |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe("tls", () => { |
| 17 | + [true, false].forEach((tls) => { |
| 18 | + it(`returns input.tls when it's ${tls}`, () => { |
| 19 | + expect(resolveEndpointsConfig({ ...input, tls }).tls).toStrictEqual(tls); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns true is input.tls is undefined", () => { |
| 24 | + expect(resolveEndpointsConfig({ ...input }).tls).toStrictEqual(true); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("endpoint", () => { |
| 29 | + describe("if defined in input.endpoint", () => { |
| 30 | + const mockEndpoint: Endpoint = { protocol: "protocol", hostname: "hostname", path: "path" }; |
| 31 | + |
| 32 | + it("returns output of urlParser if endpoint is of type string", async () => { |
| 33 | + const endpoint = "endpoint"; |
| 34 | + urlParser.mockReturnValueOnce(mockEndpoint); |
| 35 | + const endpointOutput = await resolveEndpointsConfig({ ...input, endpoint }).endpoint(); |
| 36 | + expect(endpointOutput).toStrictEqual(mockEndpoint); |
| 37 | + expect(urlParser).toHaveBeenCalledTimes(1); |
| 38 | + expect(urlParser).toHaveBeenCalledWith(endpoint); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns promisified endpoint if it's of type object", async () => { |
| 42 | + const endpoint = mockEndpoint; |
| 43 | + const endpointOutput = await resolveEndpointsConfig({ ...input, endpoint }).endpoint(); |
| 44 | + expect(endpointOutput).toStrictEqual(endpoint); |
| 45 | + expect(urlParser).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns endpoint if it's already Provider<Endpoint>", async () => { |
| 49 | + const endpoint = () => Promise.resolve(mockEndpoint); |
| 50 | + const endpointOutput = await resolveEndpointsConfig({ ...input, endpoint }).endpoint(); |
| 51 | + expect(endpointOutput).toStrictEqual(mockEndpoint); |
| 52 | + expect(urlParser).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("if not defined in input.endpoint", () => { |
| 57 | + const mockRegion = "mockRegion"; |
| 58 | + const mockHostname = "mockHostname"; |
| 59 | + const mockEndpoint: Endpoint = { protocol: "protocol", hostname: "hostname", path: "path" }; |
| 60 | + |
| 61 | + describe("returns endpoint", () => { |
| 62 | + beforeEach(() => { |
| 63 | + region.mockResolvedValueOnce(mockRegion); |
| 64 | + regionInfoProvider.mockResolvedValueOnce({ hostname: mockHostname }); |
| 65 | + urlParser.mockReturnValueOnce(mockEndpoint); |
| 66 | + }); |
| 67 | + |
| 68 | + it("passes http in urlParser if tls is false", async () => { |
| 69 | + const endpoint = await resolveEndpointsConfig({ ...input, tls: false }).endpoint(); |
| 70 | + expect(endpoint).toStrictEqual(mockEndpoint); |
| 71 | + expect(urlParser).toHaveBeenCalledTimes(1); |
| 72 | + expect(urlParser).toHaveBeenCalledWith(`http://${mockHostname}`); |
| 73 | + }); |
| 74 | + |
| 75 | + [true, undefined].forEach((tls) => { |
| 76 | + it(`passes https in urlParser if tls is ${tls}`, async () => { |
| 77 | + const endpoint = await resolveEndpointsConfig({ ...input, tls }).endpoint(); |
| 78 | + expect(endpoint).toStrictEqual(mockEndpoint); |
| 79 | + expect(urlParser).toHaveBeenCalledTimes(1); |
| 80 | + expect(urlParser).toHaveBeenCalledWith(`https://${mockHostname}`); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("throws error", () => { |
| 86 | + const error = new Error("error"); |
| 87 | + |
| 88 | + it("if region throws error", () => { |
| 89 | + region.mockRejectedValueOnce(error); |
| 90 | + return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("if regionInfoProvider", () => { |
| 94 | + beforeEach(() => { |
| 95 | + region.mockResolvedValueOnce(mockRegion); |
| 96 | + }); |
| 97 | + |
| 98 | + it("throws error", () => { |
| 99 | + regionInfoProvider.mockRejectedValueOnce(error); |
| 100 | + return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error); |
| 101 | + }); |
| 102 | + |
| 103 | + it("returns undefined", () => { |
| 104 | + regionInfoProvider.mockResolvedValueOnce(undefined); |
| 105 | + return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual( |
| 106 | + new Error("Cannot resolve hostname from client config") |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments