Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
337 changes: 335 additions & 2 deletions DevProxy.Integration.Tests/ProcessSmokeTests.cs

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions DevProxy.Tests/DetachedTokenPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DevProxy.State;
using System.Text.Json;
using Xunit;

namespace DevProxy.Tests;

public class DetachedTokenPolicyTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ShouldDisplayTokenFor_HonorsPersistedChildPolicy(bool suppressTokenOutput)
{
var state = new ProxyInstanceState { SuppressTokenOutput = suppressTokenOutput };
var json = JsonSerializer.Serialize(state);
var persisted = JsonSerializer.Deserialize<ProxyInstanceState>(json);

Assert.NotNull(persisted);
Assert.Equal(suppressTokenOutput, persisted.SuppressTokenOutput);
Assert.Equal(ApiSecurity.ShouldDisplayToken && !suppressTokenOutput,
ApiSecurity.ShouldDisplayTokenFor(persisted));
}

[Fact]
public void ShouldDisplayTokenFor_LegacyState_PreservesParentPolicy()
{
var state = JsonSerializer.Deserialize<ProxyInstanceState>("{}");

Assert.NotNull(state);
Assert.Equal(ApiSecurity.ShouldDisplayToken, ApiSecurity.ShouldDisplayTokenFor(state));
}
}
72 changes: 72 additions & 0 deletions DevProxy.Tests/DevProxyCommandNoColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,85 @@
// See the LICENSE file in the project root for more information.

using DevProxy.Commands;
using DevProxy.Proxy;
using Microsoft.Extensions.Configuration;

using Xunit;

namespace DevProxy.Tests;

[Collection("management-environment")]
public class DevProxyCommandNoColorTests
{
[Theory]
[InlineData("NO_COLOR", "1", "", false)]
[InlineData("NO_COLOR", "1", "", true)]
[InlineData("TERM", "dumb", "xterm-256color", false)]
[InlineData("TERM", "dumb", "xterm-256color", true)]
public void NoColor_AddedChangedAndRemovedEnvironmentOverride_UsesCurrentValues(
string name, string disablingValue, string enablingValue, bool inheritedDisablesColor)
{
var configuration = new ConfigurationBuilder().Build();
using var inherited = new ProcessEnvironment();
inherited.Apply(configuration, new Dictionary<string, string>
{
["NO_COLOR"] = "",
["TERM"] = "xterm-256color",
});
inherited.Apply(configuration, new Dictionary<string, string>
{
[name] = inheritedDisablesColor ? disablingValue : enablingValue,
});
Assert.Equal(inheritedDisablesColor, DevProxyCommand.NoColor);

foreach (var value in new[] { disablingValue, enablingValue, disablingValue, null })
{
var values = new Dictionary<string, string?>();
if (value is not null)
{
values[$"env:{name}"] = value;
}
var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build();
using var environment = new ProcessEnvironment();
environment.Apply(config, null);
Assert.Equal(value is null ? inheritedDisablesColor : value == disablingValue, DevProxyCommand.NoColor);
}

Assert.Equal(inheritedDisablesColor, DevProxyCommand.NoColor);
}

[Theory]
[InlineData("NO_COLOR", "", "1", true)]
[InlineData("TERM", "dumb", "xterm-256color", false)]
public void NoColor_ConfigAndCommandLineOverrides_CommandLineWinsAcrossRuns(
string name, string configValue, string commandLineValue, bool expected)
{
using var inherited = new ProcessEnvironment();
inherited.Apply(new ConfigurationBuilder().Build(), new Dictionary<string, string>
{
["NO_COLOR"] = "",
["TERM"] = "xterm-256color",
});
foreach (var value in new[] { configValue, commandLineValue })
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { [$"env:{name}"] = value })
.Build();
using var environment = new ProcessEnvironment();
environment.Apply(configuration, new Dictionary<string, string> { [name] = commandLineValue });
Assert.Equal(expected, DevProxyCommand.NoColor);
}
}

[Theory]
[InlineData(null, null)]
[InlineData("", "xterm-256color")]
[InlineData("1", "dumb")]
public void ShouldDisableColor_ExplicitOption_WinsOverEnvironmentValues(string? noColor, string? term)
{
Assert.True(DevProxyCommand.ShouldDisableColor(["--no-color"], noColor, term));
}

[Theory]
[InlineData(null, false)]
[InlineData("", false)]
Expand Down
137 changes: 137 additions & 0 deletions DevProxy.Tests/EnvOptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DevProxy.Commands;
using DevProxy.Proxy;
using Microsoft.Extensions.Configuration;

using Xunit;

namespace DevProxy.Tests;

public class EnvOptionTests
{
[Fact]
public void Env_ParsesCommandLineValues()
{
var options = new DevProxyConfigOptions();
options.ParseOptions(["--env", "FOO=bar", "-e", "URL=https://x?a=b"]);

var env = options.Env;

Assert.NotNull(env);
Assert.Equal("bar", env["FOO"]);
Assert.Equal("https://x?a=b", env["URL"]);
}

[Fact]
public void Env_NotSpecified_ReturnsNull()
{
var options = new DevProxyConfigOptions();
options.ParseOptions(["--port", "0"]);

Assert.Null(options.Env);
}

[Fact]
public void Apply_SetsConfigAndCommandLineValues_CommandLineWins()
{
var prefix = $"DEVPROXY_TEST_{Guid.NewGuid():N}_";
var fromConfig = prefix + "CONFIG";
var overridden = prefix + "OVERRIDDEN";
var fromCli = prefix + "CLI";

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"env:{fromConfig}"] = "config",
[$"env:{overridden}"] = "config",
})
.Build();

try
{
using var environment = new ProcessEnvironment();
environment.Apply(configuration, new Dictionary<string, string>
{
[overridden] = "cli",
[fromCli] = "cli",
});

Assert.Equal("config", Environment.GetEnvironmentVariable(fromConfig));
Assert.Equal("cli", Environment.GetEnvironmentVariable(overridden));
Assert.Equal("cli", Environment.GetEnvironmentVariable(fromCli));
}
finally
{
Environment.SetEnvironmentVariable(fromConfig, null);
Environment.SetEnvironmentVariable(overridden, null);
Environment.SetEnvironmentVariable(fromCli, null);
}
}

[Theory]
[InlineData(null)]
[InlineData("inherited")]
public void Dispose_BeforeReload_RestoresRemovedOverridesAndPreservesCommandLineValues(string? inherited)
{
var prefix = $"DEVPROXY_TEST_{Guid.NewGuid():N}_";
var removed = prefix + "REMOVED";
var overridden = prefix + "OVERRIDDEN";
var cli = new Dictionary<string, string> { [overridden] = "cli" };
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"env:{removed}"] = "config",
[$"env:{overridden}"] = "config",
})
.Build();

Environment.SetEnvironmentVariable(removed, inherited);
try
{
using (var environment = new ProcessEnvironment())
{
environment.Apply(configuration, cli);
Assert.Equal("config", Environment.GetEnvironmentVariable(removed));
Assert.Equal("cli", Environment.GetEnvironmentVariable(overridden));
}

var reloadedConfiguration = new ConfigurationBuilder().Build();
using (var environment = new ProcessEnvironment())
{
environment.Apply(reloadedConfiguration, cli);
Assert.Equal(inherited, Environment.GetEnvironmentVariable(removed));
Assert.Equal("cli", Environment.GetEnvironmentVariable(overridden));
}

Assert.Equal(inherited, Environment.GetEnvironmentVariable(removed));
Assert.Null(Environment.GetEnvironmentVariable(overridden));
}
finally
{
Environment.SetEnvironmentVariable(removed, null);
Environment.SetEnvironmentVariable(overridden, null);
}
}

[Fact]
public void Dispose_WhenApplyingOverridesFails_RestoresEarlierOverrides()
{
var name = $"DEVPROXY_TEST_{Guid.NewGuid():N}";
var configuration = new ConfigurationBuilder().Build();

Assert.Throws<ArgumentException>(() =>
{
using var environment = new ProcessEnvironment();
environment.Apply(configuration, new Dictionary<string, string>
{
[name] = "config",
["invalid=name"] = "invalid",
});
});

Assert.Null(Environment.GetEnvironmentVariable(name));
}
}
58 changes: 58 additions & 0 deletions DevProxy.Tests/HttpProxyEnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DevProxy.Proxy;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace DevProxy.Tests;

[Collection("management-environment")]
public class HttpProxyEnvironmentTests
{
[Theory]
[InlineData("HTTP_PROXY")]
[InlineData("HTTPS_PROXY")]
[InlineData("ALL_PROXY")]
[InlineData("NO_PROXY")]
[InlineData("http_proxy")]
[InlineData("https_proxy")]
[InlineData("all_proxy")]
[InlineData("no_proxy")]
public void GetChangedVariables_ChangedOrRemovedOverride_ReportsNameUntilInitialValueRestored(string name)
{
var configuration = new ConfigurationBuilder().Build();
using var environment = new ProcessEnvironment();
environment.Apply(configuration, new Dictionary<string, string> { [name] = "initial" });
var monitor = new HttpProxyEnvironment();
Assert.Empty(monitor.GetChangedVariables());

Environment.SetEnvironmentVariable(name, "changed");
var expectedName = OperatingSystem.IsWindows() ? name.ToUpperInvariant() : name;
Assert.Equal([expectedName], monitor.GetChangedVariables());
Assert.Equal([expectedName], monitor.GetChangedVariables());

Environment.SetEnvironmentVariable(name, null);
Assert.Equal([expectedName], monitor.GetChangedVariables());

Environment.SetEnvironmentVariable(name, "initial");
Assert.Empty(monitor.GetChangedVariables());
}

[Fact]
public void GetChangedVariables_ConfigChangesShadowedByCommandLine_DoesNotReportChange()
{
var monitor = new HttpProxyEnvironment();
var cli = new Dictionary<string, string> { ["HTTPS_PROXY"] = "cli" };
foreach (var value in new[] { "config", "changed" })
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["env:HTTPS_PROXY"] = value })
.Build();
using var environment = new ProcessEnvironment();
environment.Apply(configuration, cli);
Assert.Empty(monitor.GetChangedVariables());
}
}
}
Loading
Loading