-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathafd.test.ts
More file actions
285 lines (236 loc) · 12.8 KB
/
afd.test.ts
File metadata and controls
285 lines (236 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/* eslint-disable @typescript-eslint/no-unused-expressions */
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const expect = chai.expect;
import { AppConfigurationClient } from "@azure/app-configuration";
import { load, loadFromAzureFrontDoor } from "../src/index.js";
import { ErrorMessages } from "../src/common/errorMessages.js";
import { createMockedKeyValue, createMockedFeatureFlag, HttpRequestHeadersPolicy, getCachedIterator, sinon, restoreMocks, createMockedConnectionString, createMockedAzureFrontDoorEndpoint, sleepInMs } from "./utils/testHelper.js";
import { X_MS_DATE_HEADER } from "../src/afd/constants.js";
import { isBrowser } from "../src/requestTracing/utils.js";
function createTimestampHeaders(timestamp: string | Date) {
const value = timestamp instanceof Date ? timestamp.toUTCString() : new Date(timestamp).toUTCString();
return {
get: (name: string) => name.toLowerCase() === X_MS_DATE_HEADER ? value : undefined
};
}
describe("loadFromAzureFrontDoor", function() {
afterEach(() => {
restoreMocks();
});
it("should throw if watched settings are provided", async () => {
await expect(loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
refreshOptions: {
enabled: true,
watchedSettings: [{ key: "sentinel" }]
}
})).to.be.rejectedWith(ErrorMessages.WATCHED_SETTINGS_NOT_SUPPORTED);
});
it("should throw if replica discovery is enabled", async () => {
await expect(loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
replicaDiscoveryEnabled: true
})).to.be.rejectedWith(ErrorMessages.REPLICA_DISCOVERY_NOT_SUPPORTED);
});
it("should throw if load balancing is enabled", async () => {
await expect(loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
loadBalancingEnabled: true
})).to.be.rejectedWith(ErrorMessages.LOAD_BALANCING_NOT_SUPPORTED);
});
it("should not include authorization and sync-token header when loading from Azure Front Door", async () => {
const headerPolicy = new HttpRequestHeadersPolicy();
const position: "perCall" | "perRetry" = "perCall";
const clientOptions = {
retryOptions: {
maxRetries: 0
},
additionalPolicies: [{
policy: headerPolicy,
position
}],
syncTokens: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
addSyncTokenFromHeaderValue: (syncTokenHeaderValue) => {},
getSyncTokenHeaderValue: () => { return "mockedSyncToken"; }
}
};
try {
await load(createMockedConnectionString(), {
clientOptions,
startupOptions: {
timeoutInMs: 1
}
});
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
expect(headerPolicy.headers.get("Authorization")).not.undefined;
expect(headerPolicy.headers.get("Sync-Token")).to.equal("mockedSyncToken");
try {
await loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
clientOptions,
startupOptions: {
timeoutInMs: 1
}
});
} catch { /* empty */ }
expect(headerPolicy.headers).not.undefined;
let userAgent;
// https://github.com/Azure/azure-sdk-for-js/pull/6528
if (isBrowser()) {
userAgent = headerPolicy.headers.get("x-ms-useragent");
} else {
userAgent = headerPolicy.headers.get("User-Agent");
}
expect(userAgent).satisfy((ua: string) => ua.startsWith("javascript-appconfiguration-provider"));
expect(headerPolicy.headers.get("Authorization")).to.be.undefined;
expect(headerPolicy.headers.get("Sync-Token")).to.be.undefined;
});
it("should load key-values and feature flags", async () => {
const kv1 = createMockedKeyValue({ key: "app.color", value: "red" });
const kv2 = createMockedKeyValue({ key: "app.size", value: "large" });
const ff = createMockedFeatureFlag("Beta");
const stub = sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettings");
stub.onCall(0).returns(getCachedIterator([
{ items: [kv1, kv2], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
stub.onCall(1).returns(getCachedIterator([
{ items: [ff], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
const appConfig = await loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
selectors: [{ keyFilter: "app.*" }],
featureFlagOptions: {
enabled: true
}
});
expect(appConfig.get("app.color")).to.equal("red");
expect(appConfig.get("app.size")).to.equal("large");
expect((appConfig.get<any>("feature_management").feature_flags as any[]).find(ff => ff.id === "Beta")).not.undefined;
});
it("should refresh key-values if any page changes", async () => {
const kv1 = createMockedKeyValue({ key: "app.key1", value: "value1" });
const kv2 = createMockedKeyValue({ key: "app.key2", value: "value2" });
const kv2_updated = createMockedKeyValue({ key: "app.key2", value: "value2-updated" });
const kv3 = createMockedKeyValue({ key: "app.key3", value: "value3" });
const listStub = sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettings");
const checkStub = sinon.stub(AppConfigurationClient.prototype, "checkConfigurationSettings" as any);
// Initial load
listStub.onCall(0).returns(getCachedIterator([
{ items: [kv1, kv2], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
// 1st refresh: check (HEAD) detects change, then reload (GET)
checkStub.onCall(0).returns(getCachedIterator([
{ items: [kv1, kv2_updated], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:01Z") } }
]));
listStub.onCall(1).returns(getCachedIterator([
{ items: [kv1, kv2_updated], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:01Z") } }
]));
// 2nd refresh: check (HEAD) detects change, then reload (GET)
checkStub.onCall(1).returns(getCachedIterator([
{ items: [kv1], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:03Z") } }
]));
listStub.onCall(2).returns(getCachedIterator([
{ items: [kv1], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:03Z") } }
]));
// 3rd refresh: check (HEAD) detects change, then reload (GET)
checkStub.onCall(2).returns(getCachedIterator([
{ items: [kv1, kv3], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:05Z") } }
]));
listStub.onCall(3).returns(getCachedIterator([
{ items: [kv1, kv3], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:05Z") } }
]));
const appConfig = await loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
selectors: [{ keyFilter: "app.*" }],
refreshOptions: {
enabled: true,
refreshIntervalInMs: 1000
}
}); // 1 call listConfigurationSettings
expect(appConfig.get("app.key1")).to.equal("value1");
expect(appConfig.get("app.key2")).to.equal("value2");
await sleepInMs(1500); // key2 updated
await appConfig.refresh(); // 1 call listConfigurationSettings for watching changes and 1 call for reloading
expect(appConfig.get("app.key2")).to.equal("value2-updated");
await sleepInMs(1500); // key2 deleted
await appConfig.refresh(); // 1 call listConfigurationSettings for watching changes and 1 call for reloading
expect(appConfig.get("app.key2")).to.be.undefined;
await sleepInMs(1500); // key3 added
await appConfig.refresh(); // 1 call listConfigurationSettings for watching changes and 1 call for reloading
expect(appConfig.get("app.key3")).to.equal("value3");
});
it("should refresh feature flags if any page changes", async () => {
const ff = createMockedFeatureFlag("Beta");
const ff_updated = createMockedFeatureFlag("Beta", { enabled: false });
const listStub = sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettings");
const checkStub = sinon.stub(AppConfigurationClient.prototype, "checkConfigurationSettings" as any);
// Initial load: onCall(0) = default KV selector, onCall(1) = feature flags
listStub.onCall(0).returns(getCachedIterator([
{ items: [ff], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
listStub.onCall(1).returns(getCachedIterator([
{ items: [ff], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
// 1st refresh: check (HEAD) detects change, then reload (GET)
checkStub.onCall(0).returns(getCachedIterator([
{ items: [ff_updated], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:03Z") } }
]));
listStub.onCall(2).returns(getCachedIterator([
{ items: [ff_updated], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:03Z") } }
]));
const appConfig = await loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
featureFlagOptions: {
enabled: true,
refresh: {
enabled: true,
refreshIntervalInMs: 1000
}
}
});
let featureFlags = appConfig.get<any>("feature_management").feature_flags;
expect(featureFlags[0].id).to.equal("Beta");
expect(featureFlags[0].enabled).to.equal(true);
await sleepInMs(1500);
await appConfig.refresh();
featureFlags = appConfig.get<any>("feature_management").feature_flags;
expect(featureFlags[0].id).to.equal("Beta");
expect(featureFlags[0].enabled).to.equal(false);
});
it("should not refresh if the response is stale", async () => {
const kv1 = createMockedKeyValue({ key: "app.key1", value: "value1" });
const kv1_stale = createMockedKeyValue({ key: "app.key1", value: "stale-value" });
const kv1_new = createMockedKeyValue({ key: "app.key1", value: "new-value" });
const listStub = sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettings");
const checkStub = sinon.stub(AppConfigurationClient.prototype, "checkConfigurationSettings" as any);
// Initial load
listStub.onCall(0).returns(getCachedIterator([
{ items: [kv1], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:01Z") } }
]));
// 1st refresh: check (HEAD) returns stale response, should not trigger refresh
checkStub.onCall(0).returns(getCachedIterator([
{ items: [kv1_stale], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:00Z") } }
]));
// 2nd refresh: check (HEAD) detects change, then reload (GET)
checkStub.onCall(1).returns(getCachedIterator([
{ items: [kv1_new], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:02Z") } }
]));
listStub.onCall(1).returns(getCachedIterator([
{ items: [kv1_new], response: { status: 200, headers: createTimestampHeaders("2025-09-07T00:00:02Z") } }
]));
const appConfig = await loadFromAzureFrontDoor(createMockedAzureFrontDoorEndpoint(), {
selectors: [{ keyFilter: "app.*" }],
refreshOptions: {
enabled: true,
refreshIntervalInMs: 1000
}
}); // 1 call listConfigurationSettings
expect(appConfig.get("app.key1")).to.equal("value1");
await sleepInMs(1500);
await appConfig.refresh(); // 1 call listConfigurationSettings for watching changes
expect(appConfig.get("app.key1")).to.equal("value1"); // value should not be updated
await sleepInMs(1500);
await appConfig.refresh(); // 1 call listConfigurationSettings for watching changes and 1 call for reloading
expect(appConfig.get("app.key1")).to.equal("new-value");
});
});
/* eslint-ensable @typescript-eslint/no-unused-expressions */