mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-01 15:13:33 +02:00
Fix WithBody when using Pact and added more nullable annotations (#783)
* More nullable annotations * . * . * FIX * pact * . * p * xxx * ... * auth * array * ...
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -11,7 +15,7 @@ namespace WireMock.Net.Tests.Pact;
|
||||
public class PactTests
|
||||
{
|
||||
[Fact]
|
||||
public void SavePact_Get_Request()
|
||||
public void SavePact_Get_Request_And_Response_WithBodyAsJson()
|
||||
{
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
@@ -41,6 +45,62 @@ public class PactTests
|
||||
server.SavePact(Path.Combine("../../../", "Pact", "files"), "pact-get.json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SavePact_Get_Request_And_Response_WithBody_StringIsJson()
|
||||
{
|
||||
// Act
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingGet()
|
||||
.WithPath("/tester")
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(HttpStatusCode.OK)
|
||||
.WithBody(@"{ foo: ""bar"" }")
|
||||
);
|
||||
|
||||
var memoryStream = new MemoryStream();
|
||||
server.SavePact(memoryStream);
|
||||
|
||||
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
|
||||
var pact = JsonConvert.DeserializeObject<WireMock.Pact.Models.V2.Pact>(json)!;
|
||||
|
||||
// Assert
|
||||
pact.Interactions.Should().HaveCount(1);
|
||||
|
||||
var expectedBody = new JObject { { "foo", "bar" } };
|
||||
pact.Interactions[0].Response.Body.Should().BeEquivalentTo(expectedBody);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SavePact_Get_Request_And_Response_WithBody_StringIsString()
|
||||
{
|
||||
// Act
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingGet()
|
||||
.WithPath("/tester")
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(HttpStatusCode.OK)
|
||||
.WithBody("test")
|
||||
);
|
||||
|
||||
var memoryStream = new MemoryStream();
|
||||
server.SavePact(memoryStream);
|
||||
|
||||
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
|
||||
var pact = JsonConvert.DeserializeObject<WireMock.Pact.Models.V2.Pact>(json)!;
|
||||
|
||||
// Assert
|
||||
pact.Interactions.Should().HaveCount(1);
|
||||
pact.Interactions[0].Response.Body.Should().Be("test");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SavePact_Multiple_Requests()
|
||||
{
|
||||
|
||||
@@ -2,102 +2,101 @@ using NFluent;
|
||||
using WireMock.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Settings
|
||||
namespace WireMock.Net.Tests.Settings;
|
||||
|
||||
public class SimpleCommandLineParserTests
|
||||
{
|
||||
public class SimpleCommandLineParserTests
|
||||
private readonly SimpleCommandLineParser _parser;
|
||||
|
||||
public SimpleCommandLineParserTests()
|
||||
{
|
||||
private readonly SimpleCommandLineParser _parser;
|
||||
_parser = new SimpleCommandLineParser();
|
||||
}
|
||||
|
||||
public SimpleCommandLineParserTests()
|
||||
{
|
||||
_parser = new SimpleCommandLineParser();
|
||||
}
|
||||
[Fact]
|
||||
public void SimpleCommandLineParser_Parse_Arguments()
|
||||
{
|
||||
// Assign
|
||||
_parser.Parse(new[] { "--test1", "one", "--test2", "two", "--test3", "three" });
|
||||
|
||||
[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");
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
// 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" });
|
||||
|
||||
[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");
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
// 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" });
|
||||
|
||||
[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");
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
// 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" });
|
||||
|
||||
[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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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" });
|
||||
|
||||
[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");
|
||||
|
||||
// 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();
|
||||
}
|
||||
// Assert
|
||||
Check.That(value1).IsEqualTo(42);
|
||||
Check.That(value2).IsEqualTo(55);
|
||||
Check.That(value3).IsEqualTo(100);
|
||||
Check.That(value4).IsNull();
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace WireMock.Net.Tests.Util;
|
||||
public class JsonUtilsTests
|
||||
{
|
||||
[Fact]
|
||||
public void JsonUtils_ParseJTokenToObject()
|
||||
public void JsonUtils_ParseJTokenToObject_For_String()
|
||||
{
|
||||
// Assign
|
||||
object value = "test";
|
||||
@@ -22,7 +22,33 @@ public class JsonUtilsTests
|
||||
string result = JsonUtils.ParseJTokenToObject<string>(value);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(default(string));
|
||||
result.Should().Be("test");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonUtils_ParseJTokenToObject_For_Int()
|
||||
{
|
||||
// Assign
|
||||
object value = 123;
|
||||
|
||||
// Act
|
||||
var result = JsonUtils.ParseJTokenToObject<int>(value);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(123);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonUtils_ParseJTokenToObject_For_Invalid_Throws()
|
||||
{
|
||||
// Assign
|
||||
object value = "{ }";
|
||||
|
||||
// Act
|
||||
Action action = () => JsonUtils.ParseJTokenToObject<int>(value);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<NotSupportedException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -1,44 +1,43 @@
|
||||
using NFluent;
|
||||
using NFluent;
|
||||
using System.IO;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Util
|
||||
namespace WireMock.Net.Tests.Util;
|
||||
|
||||
public class PathUtilsTests
|
||||
{
|
||||
public class PathUtilsTests
|
||||
[Theory]
|
||||
[InlineData(@"subdirectory/MyXmlResponse.xml")]
|
||||
[InlineData(@"subdirectory\MyXmlResponse.xml")]
|
||||
public void PathUtils_CleanPath(string path)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(@"subdirectory/MyXmlResponse.xml")]
|
||||
[InlineData(@"subdirectory\MyXmlResponse.xml")]
|
||||
public void PathUtils_CleanPath(string path)
|
||||
{
|
||||
// Act
|
||||
var cleanPath = PathUtils.CleanPath(path);
|
||||
// Act
|
||||
var cleanPath = PathUtils.CleanPath(path);
|
||||
|
||||
// Assert
|
||||
Check.That(cleanPath).Equals("subdirectory" + Path.DirectorySeparatorChar + "MyXmlResponse.xml");
|
||||
}
|
||||
// Assert
|
||||
Check.That(cleanPath).Equals("subdirectory" + Path.DirectorySeparatorChar + "MyXmlResponse.xml");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData("", "")]
|
||||
[InlineData("a", "a")]
|
||||
[InlineData(@"/", "")]
|
||||
[InlineData(@"//", "")]
|
||||
[InlineData(@"//a", "a")]
|
||||
[InlineData(@"\", "")]
|
||||
[InlineData(@"\\", "")]
|
||||
[InlineData(@"\\a", "a")]
|
||||
public void PathUtils_CleanPath_RemoveLeadingDirectorySeparators(string path, string expected)
|
||||
{
|
||||
// Arrange
|
||||
var cleanPath = PathUtils.CleanPath(path);
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData("", "")]
|
||||
[InlineData("a", "a")]
|
||||
[InlineData(@"/", "")]
|
||||
[InlineData(@"//", "")]
|
||||
[InlineData(@"//a", "a")]
|
||||
[InlineData(@"\", "")]
|
||||
[InlineData(@"\\", "")]
|
||||
[InlineData(@"\\a", "a")]
|
||||
public void PathUtils_CleanPath_RemoveLeadingDirectorySeparators(string path, string expected)
|
||||
{
|
||||
// Arrange
|
||||
var cleanPath = PathUtils.CleanPath(path);
|
||||
|
||||
// Act
|
||||
var withoutDirectorySeparators = PathUtils.RemoveLeadingDirectorySeparators(cleanPath);
|
||||
// Act
|
||||
var withoutDirectorySeparators = PathUtils.RemoveLeadingDirectorySeparators(cleanPath);
|
||||
|
||||
// Assert
|
||||
Check.That(withoutDirectorySeparators).Equals(expected);
|
||||
}
|
||||
// Assert
|
||||
Check.That(withoutDirectorySeparators).Equals(expected);
|
||||
}
|
||||
}
|
||||
@@ -3,92 +3,92 @@ using NFluent;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Util
|
||||
namespace WireMock.Net.Tests.Util;
|
||||
|
||||
public class PortUtilsTests
|
||||
{
|
||||
public class PortUtilsTests
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_InvalidUrl_Returns_False()
|
||||
{
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_InvalidUrl_Returns_False()
|
||||
{
|
||||
// Assign
|
||||
string url = "test";
|
||||
// Assign
|
||||
string url = "test";
|
||||
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().BeNull();
|
||||
host.Should().BeNull();
|
||||
port.Should().Be(default(int));
|
||||
}
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().BeNull();
|
||||
host.Should().BeNull();
|
||||
port.Should().Be(default(int));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_UrlIsMissingPort_Returns_False()
|
||||
{
|
||||
// Assign
|
||||
string url = "http://0.0.0.0";
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_UrlIsMissingPort_Returns_False()
|
||||
{
|
||||
// Assign
|
||||
string url = "http://0.0.0.0";
|
||||
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().BeNull();
|
||||
host.Should().BeNull();
|
||||
port.Should().Be(default(int));
|
||||
}
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().BeNull();
|
||||
host.Should().BeNull();
|
||||
port.Should().Be(default(int));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Http_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "http://wiremock.net:1234";
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Http_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "http://wiremock.net:1234";
|
||||
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().Be("http");
|
||||
host.Should().Be("wiremock.net");
|
||||
port.Should().Be(1234);
|
||||
}
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
isHttps.Should().BeFalse();
|
||||
proto.Should().Be("http");
|
||||
host.Should().Be("wiremock.net");
|
||||
port.Should().Be(1234);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Https_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "https://wiremock.net:5000";
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Https_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "https://wiremock.net:5000";
|
||||
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
isHttps.Should().BeTrue();
|
||||
proto.Should().Be("https");
|
||||
host.Should().Be("wiremock.net");
|
||||
port.Should().Be(5000);
|
||||
}
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
isHttps.Should().BeTrue();
|
||||
proto.Should().Be("https");
|
||||
host.Should().Be("wiremock.net");
|
||||
port.Should().Be(5000);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Https0_0_0_0_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "https://0.0.0.0:5000";
|
||||
[Fact]
|
||||
public void PortUtils_TryExtract_Https0_0_0_0_Returns_True()
|
||||
{
|
||||
// Assign
|
||||
string url = "https://0.0.0.0:5000";
|
||||
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
// Act
|
||||
bool result = PortUtils.TryExtract(url, out bool isHttps, out string proto, out string host, out int port);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsTrue();
|
||||
Check.That(proto).IsEqualTo("https");
|
||||
Check.That(host).IsEqualTo("0.0.0.0");
|
||||
Check.That(port).IsEqualTo(5000);
|
||||
}
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
isHttps.Should().BeTrue();
|
||||
proto.Should().Be("https");
|
||||
host.Should().Be("0.0.0.0");
|
||||
port.Should().Be(5000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user