FluentAssertions - WithBody and WithBodyAsJson and WithBodyAsBytes (#1014)

* WithBody

* .

* fix

* .

* .
This commit is contained in:
Stef Heyenrath
2023-11-04 16:17:23 +01:00
committed by GitHub
parent 2f29d80336
commit 7160dbdd19
11 changed files with 450 additions and 100 deletions

View File

@@ -6,6 +6,7 @@ using System.Net.Http.Headers;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.FluentAssertions;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
@@ -633,6 +634,194 @@ public class WireMockAssertionsTests : IDisposable
.AtUrl(_server.Url ?? string.Empty);
}
[Fact]
public async Task HaveReceived1Call_WithBodyAsString()
{
// Arrange
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath("/a").UsingPost().WithBody("x"))
.RespondWith(Response.Create().WithBody("A response"));
// Act
var httpClient = new HttpClient();
await httpClient.PostAsync($"{server.Url}/a", new StringContent("x"));
// Assert
server
.Should()
.HaveReceived(1)
.Calls()
.WithBody("*")
.And
.UsingPost();
server
.Should()
.HaveReceived(1)
.Calls()
.WithBody("x")
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBody("")
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBody("y")
.And
.UsingPost();
server.Stop();
}
[Fact]
public async Task HaveReceived1Call_WithBodyAsJson()
{
// Arrange
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath("/a").UsingPost().WithBodyAsJson(new { x = "y" }))
.RespondWith(Response.Create().WithBody("A response"));
// Act
var httpClient = new HttpClient();
await httpClient.PostAsync($"{server.Url}/a", new StringContent(@"{ ""x"": ""y"" }"));
// Assert
server
.Should()
.HaveReceived(1)
.Calls()
.WithBodyAsJson(new { x = "y" })
.And
.UsingPost();
server
.Should()
.HaveReceived(1)
.Calls()
.WithBodyAsJson(@"{ ""x"": ""y"" }")
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBodyAsJson(new { x = "?" })
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBodyAsJson(@"{ ""x"": 1234 }")
.And
.UsingPost();
server.Stop();
}
[Fact]
public async Task HaveReceived1Call_WithBodyAsBytes()
{
// Arrange
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath("/a").UsingPut().WithBody(new byte[] { 100 }))
.RespondWith(Response.Create().WithBody("A response"));
// Act
var httpClient = new HttpClient();
await httpClient.PutAsync($"{server.Url}/a", new ByteArrayContent(new byte[] { 100 }));
// Assert
server
.Should()
.HaveReceived(1)
.Calls()
.WithBodyAsBytes(new byte[] { 100 })
.And
.UsingPut();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBodyAsBytes(new byte[0])
.And
.UsingPut();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBodyAsBytes(new byte[] { 42 })
.And
.UsingPut();
server.Stop();
}
[Fact]
public async Task HaveReceived1Call_WithBodyAsString_UsingStringMatcher()
{
// Arrange
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath("/a").UsingPost().WithBody("x"))
.RespondWith(Response.Create().WithBody("A response"));
// Act
var httpClient = new HttpClient();
await httpClient.PostAsync($"{server.Url}/a", new StringContent("x"));
// Assert
server
.Should()
.HaveReceived(1)
.Calls()
.WithBody(new ExactMatcher("x"))
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBody(new ExactMatcher(""))
.And
.UsingPost();
server
.Should()
.HaveReceived(0)
.Calls()
.WithBody(new ExactMatcher("y"))
.And
.UsingPost();
server.Stop();
}
public void Dispose()
{
_server?.Stop();

View File

@@ -30,8 +30,7 @@ public class RegexUtilsTests
[InlineData(null, "test", false, false)]
[InlineData(".*", "test", true, true)]
[InlineData("invalid[", "test", false, false)]
public void MatchRegex_WithVariousPatterns_ReturnsExpectedResults(
string? pattern, string input, bool expectedIsValid, bool expectedResult)
public void MatchRegex_WithVariousPatterns_ReturnsExpectedResults(string? pattern, string input, bool expectedIsValid, bool expectedResult)
{
// Act
var (isValidResult, matchResult) = RegexUtils.MatchRegex(pattern, input);
@@ -46,8 +45,7 @@ public class RegexUtilsTests
[InlineData(null, "test", false, false)]
[InlineData(".*", "test", true, true)]
[InlineData("invalid[", "test", false, false)]
public void MatchRegex_WithVariousPatternsAndExtendedRegex_ReturnsExpectedResults(
string? pattern, string input, bool expectedIsValid, bool expectedResult)
public void MatchRegex_WithVariousPatternsAndExtendedRegex_ReturnsExpectedResults(string? pattern, string input, bool expectedIsValid, bool expectedResult)
{
// Act
var (isValidResult, matchResult) = RegexUtils.MatchRegex(pattern, input, useRegexExtended: true);