mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
FluentAssertions - WithBody and WithBodyAsJson and WithBodyAsBytes (#1014)
* WithBody * . * fix * . * .
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user