-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson.test.ts
More file actions
217 lines (198 loc) · 10.1 KB
/
json.test.ts
File metadata and controls
217 lines (198 loc) · 10.1 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const expect = chai.expect;
import { load } from "./exportedApi.js";
import { MAX_TIME_OUT, mockAppConfigurationClientListConfigurationSettings, restoreMocks, createMockedConnectionString, createMockedKeyVaultReference, createMockedJsonKeyValue } from "./utils/testHelper.js";
const jsonKeyValue = createMockedJsonKeyValue("json.settings.logging", '{"Test":{"Level":"Debug"},"Prod":{"Level":"Warning"}}');
const keyVaultKeyValue = createMockedKeyVaultReference("TestKey", "https://fake-vault-name.vault.azure.net/secrets/fakeSecretName");
describe("json", function () {
this.timeout(MAX_TIME_OUT);
beforeEach(() => {
});
afterEach(() => {
restoreMocks();
});
it("should load and parse if content type is application/json", async () => {
mockAppConfigurationClientListConfigurationSettings([[jsonKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const logging = settings.get<any>("json.settings.logging");
expect(logging).not.undefined;
expect(logging.Test).not.undefined;
expect(logging.Test.Level).eq("Debug");
expect(logging.Prod).not.undefined;
expect(logging.Prod.Level).eq("Warning");
});
it("should not parse key-vault reference", async () => {
mockAppConfigurationClientListConfigurationSettings([[jsonKeyValue, keyVaultKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString, {
keyVaultOptions: {
secretResolver: (url) => `Resolved: ${url.toString()}`
}
});
expect(settings).not.undefined;
const resolvedSecret = settings.get<any>("TestKey");
expect(resolvedSecret).not.undefined;
expect(resolvedSecret.uri).undefined;
expect(typeof resolvedSecret).eq("string");
});
it("should parse different kinds of legal values", async () => {
mockAppConfigurationClientListConfigurationSettings([[
/**
* A JSON value MUST be an object, array, number, or string, false, null, true
* See https://www.ietf.org/rfc/rfc4627.txt
*/
createMockedJsonKeyValue("json.settings.object", "{}"),
createMockedJsonKeyValue("json.settings.array", "[]"),
createMockedJsonKeyValue("json.settings.number", "8"),
createMockedJsonKeyValue("json.settings.string", "\"string\""),
createMockedJsonKeyValue("json.settings.false", "false"),
createMockedJsonKeyValue("json.settings.true", "true"),
createMockedJsonKeyValue("json.settings.null", "null"),
createMockedJsonKeyValue("json.settings.literalNull", null), // possible value via Portal's advanced edit.
// Special tricky values related to JavaScript
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion
createMockedJsonKeyValue("json.settings.zero", 0),
createMockedJsonKeyValue("json.settings.emptyString", ""), // should fail JSON.parse and use string value as fallback
createMockedJsonKeyValue("json.settings.illegalString", "[unclosed"), // should fail JSON.parse
]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
expect(typeof settings.get("json.settings.object")).eq("object", "is object");
expect(Object.keys(settings.get<any>("json.settings.object")).length).eq(0, "is empty object");
expect(Array.isArray(settings.get("json.settings.array"))).eq(true, "is array");
expect(settings.get("json.settings.number")).eq(8, "is number");
expect(settings.get("json.settings.string")).eq("string", "is string");
expect(settings.get("json.settings.false")).eq(false, "is false");
expect(settings.get("json.settings.true")).eq(true, "is true");
expect(settings.get("json.settings.null")).eq(null, "is null");
expect(settings.get("json.settings.literalNull")).eq(null, "is literal null");
expect(settings.get("json.settings.zero")).eq(0, "is zero");
expect(settings.get("json.settings.emptyString")).eq("", "is empty string");
expect(settings.get("json.settings.illegalString")).eq("[unclosed", "is illegal string");
});
it("should parse json with single-line comments", async () => {
const jsoncValue = `{
// This is a single-line comment
"database": {
"host": "localhost", // Another comment
"port": 5432
},
"debug": true
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.withComments", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.withComments");
expect(config).not.undefined;
expect(config.database).not.undefined;
expect(config.database.host).eq("localhost");
expect(config.database.port).eq(5432);
expect(config.debug).eq(true);
});
it("should parse json with multi-line comments", async () => {
const jsoncValue = `{
/*
* This is a multi-line comment
* describing the configuration
*/
"app": {
"name": "TestApp",
/* inline multi-line comment */ "version": "1.0.0"
},
/*
"disabled": "this entire section is commented out"
*/
"enabled": true
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.multilineComments", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.multilineComments");
expect(config).not.undefined;
expect(config.app).not.undefined;
expect(config.app.name).eq("TestApp");
expect(config.app.version).eq("1.0.0");
expect(config.enabled).eq(true);
expect(config.disabled).undefined; // Should be undefined as it's commented out
});
it("should parse json with mixed comment types", async () => {
const jsoncValue = `{
// Configuration for the application
"application": {
"name": "Azure App Config Test", // Application name
"version": "2.0.0",
/*
* Environment settings
* These can be overridden per environment
*/
"environment": {
"development": {
"debug": true,
"logLevel": "debug"
},
"production": {
"debug": false,
"logLevel": "error"
}
}
},
// Features configuration
"features": [
"authentication",
"logging",
/* "experimental-feature", */ // Commented out feature
"monitoring"
]
}`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.complex", jsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get<any>("jsonc.settings.complex");
expect(config).not.undefined;
expect(config.application).not.undefined;
expect(config.application.name).eq("Azure App Config Test");
expect(config.application.version).eq("2.0.0");
expect(config.application.environment).not.undefined;
expect(config.application.environment.development).not.undefined;
expect(config.application.environment.development.debug).eq(true);
expect(config.application.environment.development.logLevel).eq("debug");
expect(config.application.environment.production).not.undefined;
expect(config.application.environment.production.debug).eq(false);
expect(config.application.environment.production.logLevel).eq("error");
expect(config.features).not.undefined;
expect(Array.isArray(config.features)).eq(true);
expect(config.features.length).eq(3);
expect(config.features[0]).eq("authentication");
expect(config.features[1]).eq("logging");
expect(config.features[2]).eq("monitoring");
// Should not contain the commented out "experimental-feature"
expect(config.features.includes("experimental-feature")).eq(false);
});
it("should fallback to string value if json with comments parsing fails", async () => {
const invalidJsoncValue = `{
// This is invalid JSON with unclosed bracket
"test": "value"`;
const jsoncKeyValue = createMockedJsonKeyValue("jsonc.settings.invalid", invalidJsoncValue);
mockAppConfigurationClientListConfigurationSettings([[jsoncKeyValue]]);
const connectionString = createMockedConnectionString();
const settings = await load(connectionString);
expect(settings).not.undefined;
const config = settings.get("jsonc.settings.invalid");
expect(config).not.undefined;
expect(typeof config).eq("string", "should fallback to string value");
expect(config).eq(invalidJsoncValue);
});
});