WireMock.Net.RestClient.AwesomeAssertions (#1427)

* WireMock.Net.RestClient.AwesomeAssertions

* ok

* atpath

* fix test

* sonar fixes

* ports
This commit is contained in:
Stef Heyenrath
2026-02-22 10:12:32 +01:00
committed by GitHub
parent d8353fcd94
commit 06b5a7ab84
30 changed files with 2265 additions and 94 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -52,7 +52,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
public async Task HaveReceived1Call_AtAbsoluteUrl_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
{
await _httpClient.GetAsync("anyurl", _ct);
@@ -62,7 +62,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrl2_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
public async Task HaveReceived1Call_AtAbsoluteUrl2_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
{
await _httpClient.GetAsync("anyurl", _ct);
@@ -72,7 +72,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrlUsingPost_WhenAPostCallWasMadeToAbsoluteUrl_Should_BeOK()
public async Task HaveReceived1Call_AtAbsoluteUrlUsingPost_WhenAPostCallWasMadeToAbsoluteUrl_Should_BeOK()
{
await _httpClient.PostAsync("anyurl", new StringContent(""), _ct);
@@ -162,7 +162,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsolutePath_WhenACallWasMadeToAbsolutePath_Should_BeOK()
public async Task HaveReceived1Call_AtAbsolutePath_WhenACallWasMadeToAbsolutePath_Should_BeOK()
{
await _httpClient.GetAsync("anypath", _ct);
@@ -172,7 +172,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsolutePathUsingPost_WhenAPostCallWasMadeToAbsolutePath_Should_BeOK()
public async Task HaveReceived1Call_AtAbsolutePathUsingPost_WhenAPostCallWasMadeToAbsolutePath_Should_BeOK()
{
await _httpClient.PostAsync("anypath", new StringContent(""), _ct);
@@ -241,6 +241,106 @@ public class WireMockAssertionsTests : IDisposable
.WithMessage($"Expected _server to have been called at address matching the absolute path \"/anypath\", but didn't find it among the calls to {{\"/\"}}.");
}
[Fact]
public async Task HaveReceivedNoCalls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("xxx", _ct);
_server.Should()
.HaveReceivedNoCalls()
.AtPath("anypath");
}
[Fact]
public async Task HaveReceived0Calls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("xxx", _ct);
_server.Should()
.HaveReceived(0).Calls()
.AtPath("anypath");
}
[Fact]
public async Task HaveReceived1Call_AtPath_WhenACallWasMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("anypath", _ct);
_server.Should()
.HaveReceived(1).Calls()
.AtPath("/anypath");
}
[Fact]
public async Task HaveReceived1Call_AtPathUsingPost_WhenAPostCallWasMadeToPath_Should_BeOK()
{
await _httpClient.PostAsync("anypath", new StringContent(""), _ct);
_server.Should()
.HaveReceived(1).Calls()
.AtPath("/anypath")
.And
.UsingPost();
}
[Fact]
public async Task HaveReceived2Calls_AtPath_WhenACallWasMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("anypath", _ct);
await _httpClient.GetAsync("anypath", _ct);
_server.Should()
.HaveReceived(2).Calls()
.AtPath("/anypath");
}
[Fact]
public async Task HaveReceivedACall_AtPath_WhenACallWasMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("anypath", _ct);
_server.Should()
.HaveReceivedACall()
.AtPath(new WildcardMatcher("/any*"));
}
[Fact]
public async Task HaveReceivedACall_AtPathWildcardMatcher_WhenACallWasMadeToPath_Should_BeOK()
{
await _httpClient.GetAsync("anypath", _ct);
_server.Should()
.HaveReceivedACall()
.AtPath("/anypath");
}
[Fact]
public void HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsWereMade()
{
Action act = () => _server.Should()
.HaveReceivedACall()
.AtPath("anypath");
act.Should()
.Throw<Exception>()
.WithMessage("Expected _server to have been called at address matching the path \"anypath\", but no calls were made.");
}
[Fact]
public async Task HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsMatchingThePathWereMade()
{
await _httpClient.GetAsync("", _ct);
Action act = () => _server.Should()
.HaveReceivedACall()
.AtPath("/anypath");
act.Should()
.Throw<Exception>()
.WithMessage($"Expected _server to have been called at address matching the path \"/anypath\", but didn't find it among the calls to {{\"/\"}}.");
}
[Fact]
public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeader_Should_BeOK()
{
@@ -332,7 +432,6 @@ public class WireMockAssertionsTests : IDisposable
public async Task HaveReceivedACall_WithHeader_ShouldCheckAllRequests()
{
// Arrange
var cancellationToken = _ct;
using var server = WireMockServer.Start();
using var client1 = server.CreateClient();
@@ -346,7 +445,7 @@ public class WireMockAssertionsTests : IDisposable
{
Authorization = new AuthenticationHeaderValue("Bearer", "invalidToken")
}
}, cancellationToken);
}, _ct);
// Act 2
var task2 = client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
@@ -355,7 +454,7 @@ public class WireMockAssertionsTests : IDisposable
{
Authorization = new AuthenticationHeaderValue("Bearer", "validToken")
}
}, cancellationToken);
}, _ct);
await Task.WhenAll(task1, task2);
@@ -625,7 +724,7 @@ public class WireMockAssertionsTests : IDisposable
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrlUsingPost_ShouldChain()
public async Task HaveReceived1Call_AtAbsoluteUrlUsingPost_ShouldChain()
{
// Arrange
var server = WireMockServer.Start();

View File

@@ -735,11 +735,9 @@ message Other {
private static WireMockServer Given_When_ServerStarted_And_RunningOnHttpAndGrpc()
{
var ports = PortUtils.FindFreeTcpPorts(2);
var settings = new WireMockServerSettings
{
Urls = [$"http://*:{ports[0]}/", $"grpc://*:{ports[1]}/"],
Urls = [$"http://*:0", $"grpc://*:0/"],
StartAdminInterface = true
};
return WireMockServer.Start(settings);

View File

@@ -63,7 +63,7 @@ public class WireMockActivitySourceTests
// Assert
activity.GetTagItem(WireMockSemanticConventions.HttpStatusCode).Should().Be(200);
activity.GetTagItem("otel.status_code").Should().Be("OK");
activity.GetTagItem(WireMockSemanticConventions.OtelStatusCode).Should().Be("OK");
activity.GetTagItem(WireMockSemanticConventions.ResponseBody).Should().Be("ok");
}
@@ -82,7 +82,7 @@ public class WireMockActivitySourceTests
// Assert
activity.GetTagItem(WireMockSemanticConventions.HttpStatusCode).Should().Be(500);
activity.GetTagItem("otel.status_code").Should().Be("ERROR");
activity.GetTagItem(WireMockSemanticConventions.OtelStatusCode).Should().Be("ERROR");
}
[Fact]
@@ -182,7 +182,7 @@ public class WireMockActivitySourceTests
WireMockActivitySource.RecordException(activity, exception);
// Assert
activity.GetTagItem("otel.status_code").Should().Be("ERROR");
activity.GetTagItem(WireMockSemanticConventions.OtelStatusCode).Should().Be("ERROR");
activity.GetTagItem("otel.status_description").Should().Be("boom");
activity.GetTagItem("exception.type").Should().Be(typeof(InvalidOperationException).FullName);
activity.GetTagItem("exception.message").Should().Be("boom");

View File

@@ -50,6 +50,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\WireMock.Net.AwesomeAssertions\WireMock.Net.AwesomeAssertions.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net.Matchers.CSharpCode\WireMock.Net.Matchers.CSharpCode.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net.RestClient.AwesomeAssertions\WireMock.Net.RestClient.AwesomeAssertions.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net.RestClient\WireMock.Net.RestClient.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net.xUnit.v3\WireMock.Net.xUnit.v3.csproj" />
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />