mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 17:40:33 +01:00
Support setting WireMockServerSettings via Environment (#954)
* Support parsing environment variables (WireMockServerSettings__) * case ignore * fix * SimpleSettingsParserTests * . * int * more test
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using FluentAssertions;
|
||||
using WireMock.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Extensions;
|
||||
|
||||
public class DictionaryExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void TryGetStringValue_WhenKeyExistsAndValueIsString_ReturnsTrueAndStringValue()
|
||||
{
|
||||
// Arrange
|
||||
var dictionary = new Hashtable { { "key", "value" } };
|
||||
|
||||
// Act
|
||||
var result = dictionary.TryGetStringValue("key", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
value.Should().Be("value");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetStringValue_WhenKeyExistsAndValueIsNotString_ReturnsTrueAndStringValue()
|
||||
{
|
||||
// Arrange
|
||||
var dictionary = new Hashtable { { "key", 123 } };
|
||||
|
||||
// Act
|
||||
var result = dictionary.TryGetStringValue("key", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
value.Should().Be("123");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetStringValue_WhenKeyDoesNotExist_ReturnsFalseAndNull()
|
||||
{
|
||||
// Arrange
|
||||
var dictionary = new Hashtable { { "otherKey", "value" } };
|
||||
|
||||
// Act
|
||||
var result = dictionary.TryGetStringValue("key", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
value.Should().BeNull();
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
using NFluent;
|
||||
using WireMock.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Settings;
|
||||
|
||||
public class SimpleCommandLineParserTests
|
||||
{
|
||||
private readonly SimpleCommandLineParser _parser;
|
||||
|
||||
public SimpleCommandLineParserTests()
|
||||
{
|
||||
_parser = new SimpleCommandLineParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Arguments()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "one", "--test2", "two", "--test3", "three" });
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
string? value3 = _parser.GetStringValue("test3");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
Check.That(value3).IsEqualTo("three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsAsCombinedKeyAndValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2 two", "--test3 three" });
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
string? value3 = _parser.GetStringValue("test3");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
Check.That(value3).IsEqualTo("three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsMixed()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2", "two", "--test3 three" });
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
string? value3 = _parser.GetStringValue("test3");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
Check.That(value3).IsEqualTo("three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_GetBoolValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "'--test1", "false'", "--test2 true" });
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolValue("test1");
|
||||
bool value2 = _parser.GetBoolValue("test2");
|
||||
bool value3 = _parser.GetBoolValue("test3", true);
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(false);
|
||||
Check.That(value2).IsEqualTo(true);
|
||||
Check.That(value3).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_GetIntValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "42", "--test2 55" });
|
||||
|
||||
// Act
|
||||
int? value1 = _parser.GetIntValue("test1");
|
||||
int? value2 = _parser.GetIntValue("test2");
|
||||
int? value3 = _parser.GetIntValue("test3", 100);
|
||||
int? value4 = _parser.GetIntValue("test4");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(42);
|
||||
Check.That(value2).IsEqualTo(55);
|
||||
Check.That(value3).IsEqualTo(100);
|
||||
Check.That(value4).IsNull();
|
||||
}
|
||||
}
|
||||
177
test/WireMock.Net.Tests/Settings/SimpleSettingsParserTests.cs
Normal file
177
test/WireMock.Net.Tests/Settings/SimpleSettingsParserTests.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using NFluent;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Settings;
|
||||
|
||||
public class SimpleSettingsParserTests
|
||||
{
|
||||
private readonly SimpleSettingsParser _parser;
|
||||
|
||||
public SimpleSettingsParserTests()
|
||||
{
|
||||
_parser = new SimpleSettingsParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Arguments()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "one", "--test2", "2", "--test3", "3", "--test4", "true", "--test5", "Https" });
|
||||
|
||||
// Act
|
||||
string? stringValue = _parser.GetStringValue("test1");
|
||||
int? intOptional = _parser.GetIntValue("test2");
|
||||
int intWithDefault = _parser.GetIntValue("test3", 42);
|
||||
bool? boolWithDefault = _parser.GetBoolValue("test4");
|
||||
HostingScheme? enumOptional = _parser.GetEnumValue<HostingScheme>("test5");
|
||||
HostingScheme enumWithDefault = _parser.GetEnumValue("test99", HostingScheme.HttpAndHttps);
|
||||
|
||||
// Assert
|
||||
stringValue.Should().Be("one");
|
||||
intOptional.Should().Be(2);
|
||||
intWithDefault.Should().Be(3);
|
||||
boolWithDefault.Should().Be(true);
|
||||
enumOptional.Should().Be(HostingScheme.Https);
|
||||
enumWithDefault.Should().Be(HostingScheme.HttpAndHttps);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Environment()
|
||||
{
|
||||
// Assign
|
||||
var env = new Dictionary<string, string>
|
||||
{
|
||||
{ "WireMockServerSettings__test1", "one" },
|
||||
{ "WireMockServerSettings__test2", "two" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsAsCombinedKeyAndValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2 two", "--test3 three" });
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
string? value3 = _parser.GetStringValue("test3");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
Check.That(value3).IsEqualTo("three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_ArgumentsMixed()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1 one", "--test2", "two", "--test3 three" });
|
||||
|
||||
// Act
|
||||
string? value1 = _parser.GetStringValue("test1");
|
||||
string? value2 = _parser.GetStringValue("test2");
|
||||
string? value3 = _parser.GetStringValue("test3");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo("one");
|
||||
Check.That(value2).IsEqualTo("two");
|
||||
Check.That(value3).IsEqualTo("three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_GetBoolValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "'--test1", "false'", "--test2 true" });
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolValue("test1");
|
||||
bool value2 = _parser.GetBoolValue("test2");
|
||||
bool value3 = _parser.GetBoolValue("test3", true);
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(false);
|
||||
Check.That(value2).IsEqualTo(true);
|
||||
Check.That(value3).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Environment_GetBoolValue()
|
||||
{
|
||||
// Assign
|
||||
var env = new Dictionary<string, string>
|
||||
{
|
||||
{ "WireMockServerSettings__test1", "false" },
|
||||
{ "WireMockServerSettings__test2", "true" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
|
||||
// Act
|
||||
bool value1 = _parser.GetBoolValue("test1");
|
||||
bool value2 = _parser.GetBoolValue("test2");
|
||||
bool value3 = _parser.GetBoolValue("test3", true);
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(false);
|
||||
Check.That(value2).IsEqualTo(true);
|
||||
Check.That(value3).IsEqualTo(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_GetIntValue()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "42", "--test2 55" });
|
||||
|
||||
// Act
|
||||
int? value1 = _parser.GetIntValue("test1");
|
||||
int? value2 = _parser.GetIntValue("test2");
|
||||
int? value3 = _parser.GetIntValue("test3", 100);
|
||||
int? value4 = _parser.GetIntValue("test4");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(42);
|
||||
Check.That(value2).IsEqualTo(55);
|
||||
Check.That(value3).IsEqualTo(100);
|
||||
Check.That(value4).IsNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Environment_GetIntValue()
|
||||
{
|
||||
// Assign
|
||||
var env = new Dictionary<string, string>
|
||||
{
|
||||
{ "WireMockServerSettings__test1", "42" },
|
||||
{ "WireMockServerSETTINGS__TEST2", "55" }
|
||||
};
|
||||
_parser.Parse(new string[0], env);
|
||||
|
||||
// Act
|
||||
int? value1 = _parser.GetIntValue("test1");
|
||||
int? value2 = _parser.GetIntValue("test2");
|
||||
int? value3 = _parser.GetIntValue("test3", 100);
|
||||
int? value4 = _parser.GetIntValue("test4");
|
||||
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(42);
|
||||
Check.That(value2).IsEqualTo(55);
|
||||
Check.That(value3).IsEqualTo(100);
|
||||
Check.That(value4).IsNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user