|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using DevProxy.Commands; |
| 6 | +using DevProxy.Proxy; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using System.CommandLine; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace DevProxy.Tests; |
| 12 | + |
| 13 | +public class ProxyConfigurationCiDefaultsTests |
| 14 | +{ |
| 15 | + private static ProxyConfiguration Create(bool isCi, Dictionary<string, string?>? settings = null) |
| 16 | + { |
| 17 | + var configurationRoot = new ConfigurationBuilder() |
| 18 | + .AddInMemoryCollection(settings ?? []) |
| 19 | + .Build(); |
| 20 | + return new ProxyConfiguration(configurationRoot, isCi); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void NotCi_KeepsDefaults() |
| 25 | + { |
| 26 | + var config = Create(isCi: false); |
| 27 | + |
| 28 | + Assert.True(config.AsSystemProxy); |
| 29 | + Assert.True(config.InstallCert); |
| 30 | + Assert.False(config.AsSystemProxyDefaultedForCi); |
| 31 | + Assert.False(config.InstallCertDefaultedForCi); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Ci_NotSetInConfig_DisablesSystemProxyAndCertInstall() |
| 36 | + { |
| 37 | + var config = Create(isCi: true); |
| 38 | + |
| 39 | + Assert.False(config.AsSystemProxy); |
| 40 | + Assert.False(config.InstallCert); |
| 41 | + Assert.True(config.AsSystemProxyDefaultedForCi); |
| 42 | + Assert.True(config.InstallCertDefaultedForCi); |
| 43 | + } |
| 44 | + |
| 45 | + [Theory] |
| 46 | + [InlineData("true", true)] |
| 47 | + [InlineData("false", false)] |
| 48 | + public void Ci_SetInConfig_KeepsConfiguredValues(string value, bool expected) |
| 49 | + { |
| 50 | + var config = Create(isCi: true, new() |
| 51 | + { |
| 52 | + ["asSystemProxy"] = value, |
| 53 | + ["installCert"] = value, |
| 54 | + }); |
| 55 | + |
| 56 | + Assert.Equal(expected, config.AsSystemProxy); |
| 57 | + Assert.Equal(expected, config.InstallCert); |
| 58 | + Assert.False(config.AsSystemProxyDefaultedForCi); |
| 59 | + Assert.False(config.InstallCertDefaultedForCi); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void Ci_OnlyAsSystemProxyInConfig_DefaultsInstallCertOnly() |
| 64 | + { |
| 65 | + var config = Create(isCi: true, new() { ["asSystemProxy"] = "true" }); |
| 66 | + |
| 67 | + Assert.True(config.AsSystemProxy); |
| 68 | + Assert.False(config.InstallCert); |
| 69 | + Assert.False(config.AsSystemProxyDefaultedForCi); |
| 70 | + Assert.True(config.InstallCertDefaultedForCi); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void ShouldLogCiDefaults_NotCi_ReturnsFalse() |
| 75 | + { |
| 76 | + Assert.False(DevProxyCommand.ShouldLogCiDefaults(Create(isCi: false), null, null)); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void ShouldLogCiDefaults_CiDefaultsApplied_ReturnsTrue() |
| 81 | + { |
| 82 | + Assert.True(DevProxyCommand.ShouldLogCiDefaults(Create(isCi: true), null, null)); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void ShouldLogCiDefaults_OneOverriddenOnCommandLine_ReturnsTrue() |
| 87 | + { |
| 88 | + Assert.True(DevProxyCommand.ShouldLogCiDefaults(Create(isCi: true), true, null)); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void ShouldLogCiDefaults_BothOverriddenOnCommandLine_ReturnsFalse() |
| 93 | + { |
| 94 | + Assert.False(DevProxyCommand.ShouldLogCiDefaults(Create(isCi: true), true, true)); |
| 95 | + } |
| 96 | + |
| 97 | + private static ParseResult ParseInstallCert(ProxyConfiguration config, params string[] args) |
| 98 | + { |
| 99 | + var asSystemProxyOption = new Option<bool?>(DevProxyCommand.AsSystemProxyOptionName); |
| 100 | + var command = new RootCommand |
| 101 | + { |
| 102 | + asSystemProxyOption, |
| 103 | + DevProxyCommand.CreateInstallCertOption(asSystemProxyOption, config), |
| 104 | + }; |
| 105 | + return command.Parse(args); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void InstallCertFalse_Ci_WithoutAsSystemProxy_IsAccepted() |
| 110 | + { |
| 111 | + var result = ParseInstallCert(Create(isCi: true), "--install-cert", "false"); |
| 112 | + |
| 113 | + Assert.Empty(result.Errors); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void InstallCertFalse_AsSystemProxyFalseInConfig_WithoutAsSystemProxy_IsAccepted() |
| 118 | + { |
| 119 | + var config = Create(isCi: false, new() { ["asSystemProxy"] = "false" }); |
| 120 | + |
| 121 | + var result = ParseInstallCert(config, "--install-cert", "false"); |
| 122 | + |
| 123 | + Assert.Empty(result.Errors); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void InstallCertFalse_AsSystemProxyTrueInConfig_IsRejected() |
| 128 | + { |
| 129 | + var config = Create(isCi: true, new() { ["asSystemProxy"] = "true" }); |
| 130 | + |
| 131 | + var result = ParseInstallCert(config, "--install-cert", "false"); |
| 132 | + |
| 133 | + Assert.Contains(result.Errors, e => e.Message.Contains(DevProxyCommand.AsSystemProxyOptionName, StringComparison.Ordinal)); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void InstallCertFalse_AsSystemProxyTrueOnCommandLine_IsRejected() |
| 138 | + { |
| 139 | + var result = ParseInstallCert(Create(isCi: true), "--install-cert", "false", "--as-system-proxy", "true"); |
| 140 | + |
| 141 | + Assert.NotEmpty(result.Errors); |
| 142 | + } |
| 143 | +} |
0 commit comments