|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Collections.ObjectModel;
|
7 |
| -using System.Configuration; |
8 | 7 | using System.Diagnostics;
|
9 | 8 | using System.IO;
|
10 | 9 | using System.IO.Abstractions.TestingHelpers;
|
@@ -1014,6 +1013,74 @@ public void Initialize_AppliesLoggerConfig()
|
1014 | 1013 | }
|
1015 | 1014 | }
|
1016 | 1015 |
|
| 1016 | + [Fact] |
| 1017 | + public void Initialize_Sanitizes_HostJsonLog() |
| 1018 | + { |
| 1019 | + TestTraceWriter traceWriter = new TestTraceWriter(TraceLevel.Info); |
| 1020 | + TestLoggerProvider loggerProvider = null; |
| 1021 | + var loggerFactoryHookMock = new Mock<ILoggerFactoryBuilder>(MockBehavior.Strict); |
| 1022 | + loggerFactoryHookMock |
| 1023 | + .Setup(m => m.AddLoggerProviders(It.IsAny<ILoggerFactory>(), It.IsAny<ScriptHostConfiguration>(), It.IsAny<ScriptSettingsManager>())) |
| 1024 | + .Callback<ILoggerFactory, ScriptHostConfiguration, ScriptSettingsManager>((factory, scriptConfig, settings) => |
| 1025 | + { |
| 1026 | + loggerProvider = new TestLoggerProvider(scriptConfig.LogFilter.Filter); |
| 1027 | + factory.AddProvider(loggerProvider); |
| 1028 | + }); |
| 1029 | + |
| 1030 | + string rootPath = Path.Combine(Environment.CurrentDirectory, "ScriptHostTests"); |
| 1031 | + if (!Directory.Exists(rootPath)) |
| 1032 | + { |
| 1033 | + Directory.CreateDirectory(rootPath); |
| 1034 | + } |
| 1035 | + |
| 1036 | + // Turn off all logging. We shouldn't see any output. |
| 1037 | + string hostJsonContent = @" |
| 1038 | + { |
| 1039 | + 'functionTimeout': '00:05:00', |
| 1040 | + 'functions': [ 'FunctionA', 'FunctionB' ], |
| 1041 | + 'logger': { |
| 1042 | + 'categoryFilter': { |
| 1043 | + 'defaultLevel': 'Information' |
| 1044 | + } |
| 1045 | + }, |
| 1046 | + 'Values': { |
| 1047 | + 'MyCustomValue': 'abc' |
| 1048 | + } |
| 1049 | + }"; |
| 1050 | + |
| 1051 | + File.WriteAllText(Path.Combine(rootPath, "host.json"), hostJsonContent); |
| 1052 | + |
| 1053 | + ScriptHostConfiguration config = new ScriptHostConfiguration() |
| 1054 | + { |
| 1055 | + RootScriptPath = rootPath, |
| 1056 | + TraceWriter = traceWriter |
| 1057 | + }; |
| 1058 | + |
| 1059 | + config.HostConfig.HostId = ID; |
| 1060 | + var environment = new Mock<IScriptHostEnvironment>(); |
| 1061 | + var eventManager = new Mock<IScriptEventManager>(); |
| 1062 | + |
| 1063 | + ScriptHost.Create(environment.Object, eventManager.Object, config, null, loggerFactoryHookMock.Object, null); |
| 1064 | + |
| 1065 | + string hostJsonSanitized = @" |
| 1066 | + { |
| 1067 | + 'functionTimeout': '00:05:00', |
| 1068 | + 'functions': [ 'FunctionA', 'FunctionB' ], |
| 1069 | + 'logger': { |
| 1070 | + 'categoryFilter': { |
| 1071 | + 'defaultLevel': 'Information' |
| 1072 | + } |
| 1073 | + } |
| 1074 | + }"; |
| 1075 | + |
| 1076 | + // for formatting |
| 1077 | + var hostJson = JObject.Parse(hostJsonSanitized); |
| 1078 | + |
| 1079 | + var logger = loggerProvider.CreatedLoggers.Single(l => l.Category == LogCategories.Startup); |
| 1080 | + var logMessage = logger.LogMessages.Single(l => l.FormattedMessage.StartsWith("Host configuration ")).FormattedMessage; |
| 1081 | + Assert.Equal($"Host configuration file read:{Environment.NewLine}{hostJson}", logMessage); |
| 1082 | + } |
| 1083 | + |
1017 | 1084 | [Fact]
|
1018 | 1085 | public void Initialize_LogsException_IfParseError()
|
1019 | 1086 | {
|
|
0 commit comments