mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 23:33:47 +01:00
Add overloads to AtUrl and AtAbsoluteUrl which can use a IStringMatcher (#1221)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
#pragma warning disable CS1591
|
||||
using System;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WireMock.FluentAssertions;
|
||||
@@ -11,7 +12,17 @@ public partial class WireMockAssertions
|
||||
[CustomAssertion]
|
||||
public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
|
||||
{
|
||||
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase));
|
||||
_ = AtAbsoluteUrl(new ExactMatcher(true, absoluteUrl), because, becauseArgs);
|
||||
|
||||
return new AndWhichConstraint<WireMockAssertions, string>(this, absoluteUrl);
|
||||
}
|
||||
|
||||
[CustomAssertion]
|
||||
public AndWhichConstraint<WireMockAssertions, IStringMatcher> AtAbsoluteUrl(IStringMatcher absoluteUrlMatcher, string because = "", params object[] becauseArgs)
|
||||
{
|
||||
var (filter, condition) = BuildFilterAndCondition(request => absoluteUrlMatcher.IsPerfectMatch(request.AbsoluteUrl));
|
||||
|
||||
var absoluteUrl = absoluteUrlMatcher.GetPatterns().FirstOrDefault().GetPattern();
|
||||
|
||||
Execute.Assertion
|
||||
.BecauseOf(because, becauseArgs)
|
||||
@@ -31,13 +42,23 @@ public partial class WireMockAssertions
|
||||
|
||||
FilterRequestMessages(filter);
|
||||
|
||||
return new AndWhichConstraint<WireMockAssertions, string>(this, absoluteUrl);
|
||||
return new AndWhichConstraint<WireMockAssertions, IStringMatcher>(this, absoluteUrlMatcher);
|
||||
}
|
||||
|
||||
[CustomAssertion]
|
||||
public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string because = "", params object[] becauseArgs)
|
||||
{
|
||||
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.Url, url, StringComparison.OrdinalIgnoreCase));
|
||||
_ = AtUrl(new ExactMatcher(true, url), because, becauseArgs);
|
||||
|
||||
return new AndWhichConstraint<WireMockAssertions, string>(this, url);
|
||||
}
|
||||
|
||||
[CustomAssertion]
|
||||
public AndWhichConstraint<WireMockAssertions, IStringMatcher> AtUrl(IStringMatcher urlMatcher, string because = "", params object[] becauseArgs)
|
||||
{
|
||||
var (filter, condition) = BuildFilterAndCondition(request => urlMatcher.IsPerfectMatch(request.Url));
|
||||
|
||||
var url = urlMatcher.GetPatterns().FirstOrDefault().GetPattern();
|
||||
|
||||
Execute.Assertion
|
||||
.BecauseOf(because, becauseArgs)
|
||||
@@ -57,6 +78,6 @@ public partial class WireMockAssertions
|
||||
|
||||
FilterRequestMessages(filter);
|
||||
|
||||
return new AndWhichConstraint<WireMockAssertions, string>(this, url);
|
||||
return new AndWhichConstraint<WireMockAssertions, IStringMatcher>(this, urlMatcher);
|
||||
}
|
||||
}
|
||||
20
src/WireMock.Net/Matchers/MatcherExtensions.cs
Normal file
20
src/WireMock.Net/Matchers/MatcherExtensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
namespace WireMock.Matchers;
|
||||
|
||||
/// <summary>
|
||||
/// Provides some extension methods for matchers.
|
||||
/// </summary>
|
||||
public static class MatcherExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if the match result is a perfect match.
|
||||
/// </summary>
|
||||
/// <param name="matcher">The string matcher.</param>
|
||||
/// <param name="input">The input string to match.</param>
|
||||
/// <returns><c>true</c>> if the match is perfect; otherwise, <c>false</c>>.</returns>
|
||||
public static bool IsPerfectMatch(this IStringMatcher matcher, string? input)
|
||||
{
|
||||
return matcher.IsMatch(input).IsPerfect();
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,16 @@ public class WireMockAssertionsTests : IDisposable
|
||||
{
|
||||
await _httpClient.GetAsync("anyurl").ConfigureAwait(false);
|
||||
|
||||
_server.Should()
|
||||
.HaveReceivedACall()
|
||||
.AtAbsoluteUrl(new WildcardMatcher($"http://localhost:{_portUsed}/any*"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HaveReceivedACall_AtAbsoluteUrlWilcardMAtcher_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
|
||||
{
|
||||
await _httpClient.GetAsync("anyurl").ConfigureAwait(false);
|
||||
|
||||
_server.Should()
|
||||
.HaveReceivedACall()
|
||||
.AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl");
|
||||
@@ -231,7 +241,7 @@ public class WireMockAssertionsTests : IDisposable
|
||||
using var client2 = server.CreateClient(handler);
|
||||
|
||||
// Act 1
|
||||
await client1.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
|
||||
var task1 = client1.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
@@ -240,7 +250,7 @@ public class WireMockAssertionsTests : IDisposable
|
||||
});
|
||||
|
||||
// Act 2
|
||||
await client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
|
||||
var task2 = client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
@@ -248,6 +258,8 @@ public class WireMockAssertionsTests : IDisposable
|
||||
}
|
||||
});
|
||||
|
||||
await Task.WhenAll(task1, task2);
|
||||
|
||||
// Assert
|
||||
server.Should()
|
||||
.HaveReceivedACall()
|
||||
@@ -268,6 +280,16 @@ public class WireMockAssertionsTests : IDisposable
|
||||
.AtUrl($"http://localhost:{_portUsed}/anyurl");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HaveReceivedACall_AtUrlWildcardMatcher_WhenACallWasMadeToUrl_Should_BeOK()
|
||||
{
|
||||
await _httpClient.GetAsync("anyurl").ConfigureAwait(false);
|
||||
|
||||
_server.Should()
|
||||
.HaveReceivedACall()
|
||||
.AtUrl(new WildcardMatcher($"http://localhost:{_portUsed}/AN*", true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HaveReceivedACall_AtUrl_Should_ThrowWhenNoCallsWereMade()
|
||||
{
|
||||
@@ -393,11 +415,14 @@ public class WireMockAssertionsTests : IDisposable
|
||||
[Fact]
|
||||
public async Task HaveReceived2Calls_UsingDelete_WhenACallWasMadeUsingDelete_Should_BeOK()
|
||||
{
|
||||
await _httpClient.DeleteAsync("anyurl").ConfigureAwait(false);
|
||||
var tasks = new[]
|
||||
{
|
||||
_httpClient.DeleteAsync("anyurl"),
|
||||
_httpClient.DeleteAsync("anyurl"),
|
||||
_httpClient.GetAsync("anyurl")
|
||||
};
|
||||
|
||||
await _httpClient.DeleteAsync("anyurl").ConfigureAwait(false);
|
||||
|
||||
await _httpClient.GetAsync("anyurl").ConfigureAwait(false);
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
_server.Should()
|
||||
.HaveReceived(2).Calls()
|
||||
@@ -521,11 +546,14 @@ public class WireMockAssertionsTests : IDisposable
|
||||
// Act
|
||||
var httpClient = new HttpClient();
|
||||
|
||||
await httpClient.GetAsync($"{server.Url}/a");
|
||||
var tasks = new[]
|
||||
{
|
||||
httpClient.GetAsync($"{server.Url}/a"),
|
||||
httpClient.PostAsync($"{server.Url}/b", new StringContent("B")),
|
||||
httpClient.PostAsync($"{server.Url}/c", new StringContent("C"))
|
||||
};
|
||||
|
||||
await httpClient.PostAsync($"{server.Url}/b", new StringContent("B"));
|
||||
|
||||
await httpClient.PostAsync($"{server.Url}/c", new StringContent("C"));
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
server
|
||||
|
||||
Reference in New Issue
Block a user