Add env var substitution test for JSON

This commit is contained in:
Mike Bierlee 2022-09-28 23:54:38 +03:00
parent 84220739d8
commit 72af2f322e
2 changed files with 22 additions and 0 deletions

View file

@ -130,6 +130,8 @@ ConfigDictionary loadJsonConfig(const string filePath) {
} }
version (unittest) { version (unittest) {
import std.process : environment;
@("Parse JSON") @("Parse JSON")
unittest { unittest {
JSONValue serverJson = ["hostname": "hosty.com", "port": "1234"]; JSONValue serverJson = ["hostname": "hosty.com", "port": "1234"];
@ -193,4 +195,17 @@ version (unittest) {
assert(config.get("age") == "8728"); assert(config.get("age") == "8728");
assert(config.get("taxNumber") == null); assert(config.get("taxNumber") == null);
} }
@("Substitute env vars in JSON")
unittest {
environment["MIRAGE_TEST_APP_NAME"] = "Unittest";
environment["MIRAGE_TEST_HOSTNAME"] = "wonkeyhost";
environment.remove("MIRAGE_TEST_PORT");
auto config = loadJsonConfig("testfiles/server.json");
assert(config.get("server.host") == "wonkeyhost");
assert(config.get("server.port") == "8118");
assert(config.get("app") == "Unittest server - built with love");
}
} }

7
testfiles/server.json Normal file
View file

@ -0,0 +1,7 @@
{
"server": {
"host": "$MIRAGE_TEST_HOSTNAME",
"port": "${MIRAGE_TEST_PORT:8118}"
},
"app": "${MIRAGE_TEST_APP_NAME} server - built with love"
}