|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as chai from "chai"; |
| 5 | +import * as chaiAsPromised from "chai-as-promised"; |
| 6 | +chai.use(chaiAsPromised); |
| 7 | +const expect = chai.expect; |
| 8 | +import { CDN_TOKEN_LOOKUP_HEADER, loadFromAzureFrontDoor } from "./exportedApi.js"; |
| 9 | +import { MAX_TIME_OUT, mockAppConfigurationClientListConfigurationSettings, mockAppConfigurationClientGetConfigurationSetting, restoreMocks, createMockedEndpoint, createMockedKeyValue, sleepInMs } from "./utils/testHelper.js"; |
| 10 | +import * as uuid from "uuid"; |
| 11 | +import { ListConfigurationSettingsOptions, GetConfigurationSettingOptions } from "@azure/app-configuration"; |
| 12 | + |
| 13 | +let mockedKVs: any[] = []; |
| 14 | + |
| 15 | +function updateSetting(key: string, value: any) { |
| 16 | + const setting = mockedKVs.find(elem => elem.key === key); |
| 17 | + if (setting) { |
| 18 | + setting.value = value; |
| 19 | + setting.etag = uuid.v4(); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe("load from Azure Front Door", function () { |
| 24 | + this.timeout(MAX_TIME_OUT); |
| 25 | + |
| 26 | + before(() => { |
| 27 | + mockedKVs = [ |
| 28 | + { value: "red", key: "app.settings.fontColor" }, |
| 29 | + { value: "40", key: "app.settings.fontSize" } |
| 30 | + ].map(createMockedKeyValue); |
| 31 | + mockAppConfigurationClientListConfigurationSettings([mockedKVs]); |
| 32 | + }); |
| 33 | + |
| 34 | + after(() => { |
| 35 | + restoreMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should load data from Azure Front Door", async () => { |
| 39 | + const endpoint = createMockedEndpoint(); |
| 40 | + const settings = await loadFromAzureFrontDoor(endpoint); |
| 41 | + expect(settings).not.undefined; |
| 42 | + expect(settings.get("app.settings.fontColor")).eq("red"); |
| 43 | + expect(settings.get("app.settings.fontSize")).eq("40"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should throw error when replica discovery is enabled", async () => { |
| 47 | + const endpoint = createMockedEndpoint(); |
| 48 | + return expect(loadFromAzureFrontDoor(endpoint, { |
| 49 | + replicaDiscoveryEnabled: true |
| 50 | + })).eventually.rejectedWith("Replica discovery is not supported when loading from Azure Front Door."); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should throw error when load balancing is enabled", async () => { |
| 54 | + const endpoint = createMockedEndpoint(); |
| 55 | + return expect(loadFromAzureFrontDoor(endpoint, { |
| 56 | + loadBalancingEnabled: true |
| 57 | + })).eventually.rejectedWith("Load balancing is not supported when loading from Azure Front Door."); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +let cdnTokenLookup; |
| 62 | +const listKvFromAfdCallback = (options: ListConfigurationSettingsOptions) => { |
| 63 | + cdnTokenLookup = options.requestOptions?.customHeaders?.[CDN_TOKEN_LOOKUP_HEADER]; |
| 64 | +}; |
| 65 | +const getKvFromAfdCallback = (options: GetConfigurationSettingOptions) => { |
| 66 | + cdnTokenLookup = options.requestOptions?.customHeaders?.[CDN_TOKEN_LOOKUP_HEADER]; |
| 67 | +}; |
| 68 | +describe("dynamic refresh when loading from Azure Front Door", function () { |
| 69 | + this.timeout(MAX_TIME_OUT); |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + mockedKVs = [ |
| 73 | + { value: "red", key: "app.settings.fontColor" }, |
| 74 | + { value: "40", key: "app.settings.fontSize" } |
| 75 | + ].map(createMockedKeyValue); |
| 76 | + mockAppConfigurationClientListConfigurationSettings([mockedKVs], listKvFromAfdCallback); |
| 77 | + mockAppConfigurationClientGetConfigurationSetting(mockedKVs, getKvFromAfdCallback); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + restoreMocks(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should append cdn token to the watch request", async () => { |
| 85 | + const endpoint = createMockedEndpoint(); |
| 86 | + const settings = await loadFromAzureFrontDoor(endpoint, { |
| 87 | + refreshOptions: { |
| 88 | + enabled: true, |
| 89 | + refreshIntervalInMs: 2_000, |
| 90 | + watchedSettings: [ |
| 91 | + { key: "app.settings.fontColor" } |
| 92 | + ] |
| 93 | + } |
| 94 | + }); |
| 95 | + expect(settings).not.undefined; |
| 96 | + expect(settings.get("app.settings.fontColor")).eq("red"); |
| 97 | + expect(settings.get("app.settings.fontSize")).eq("40"); |
| 98 | + |
| 99 | + updateSetting("app.settings.fontColor", "blue"); |
| 100 | + |
| 101 | + await settings.refresh(); |
| 102 | + expect(settings.get("app.settings.fontColor")).eq("red"); |
| 103 | + expect(cdnTokenLookup).is.undefined; |
| 104 | + |
| 105 | + await sleepInMs(2 * 1000 + 1); |
| 106 | + await settings.refresh(); |
| 107 | + expect(settings.get("app.settings.fontColor")).eq("blue"); |
| 108 | + expect(cdnTokenLookup).is.not.undefined; |
| 109 | + const previousCdnToken = cdnTokenLookup; |
| 110 | + |
| 111 | + updateSetting("app.settings.fontColor", "green"); |
| 112 | + |
| 113 | + await sleepInMs(2 * 1000 + 1); |
| 114 | + await settings.refresh(); |
| 115 | + expect(settings.get("app.settings.fontColor")).eq("green"); |
| 116 | + expect(cdnTokenLookup).is.not.undefined; |
| 117 | + expect(cdnTokenLookup).to.not.eq(previousCdnToken); |
| 118 | + }); |
| 119 | +}); |
0 commit comments