mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 15:10:17 +02:00
Add assertions for ClientIP, Url and ProxyUrl (#529)
* Add assertions for ClientIP Url and ProxyUrl * Requested changes
This commit is contained in:
@@ -16,8 +16,7 @@ namespace WireMock.FluentAssertions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "",
|
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
|
||||||
params object[] becauseArgs)
|
|
||||||
{
|
{
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
@@ -36,13 +35,11 @@ namespace WireMock.FluentAssertions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value,
|
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value, string because = "", params object[] becauseArgs)
|
||||||
string because = "", params object[] becauseArgs)
|
|
||||||
=> WithHeader(expectedKey, new[] {value}, because, becauseArgs);
|
=> WithHeader(expectedKey, new[] {value}, because, becauseArgs);
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[] expectedValues,
|
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[] expectedValues, string because = "", params object[] becauseArgs)
|
||||||
string because = "", params object[] becauseArgs)
|
|
||||||
{
|
{
|
||||||
var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers)
|
var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers)
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
.ToDictionary(x => x.Key, x => x.Value);
|
||||||
@@ -72,5 +69,62 @@ namespace WireMock.FluentAssertions
|
|||||||
|
|
||||||
return new AndConstraint<WireMockAssertions>(this);
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[CustomAssertion]
|
||||||
|
public AndConstraint<WireMockAssertions> AtUrl(string url, string because = "", params object[] becauseArgs)
|
||||||
|
{
|
||||||
|
Execute.Assertion
|
||||||
|
.BecauseOf(because, becauseArgs)
|
||||||
|
.Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList())
|
||||||
|
.ForCondition(requests => requests.Any())
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.",
|
||||||
|
url)
|
||||||
|
.Then
|
||||||
|
.ForCondition(x => x.Any(y => y.Url == url))
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but didn't find it among the calls to {1}.",
|
||||||
|
_ => url, requests => requests.Select(request => request.Url));
|
||||||
|
|
||||||
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[CustomAssertion]
|
||||||
|
public AndConstraint<WireMockAssertions> WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs)
|
||||||
|
{
|
||||||
|
Execute.Assertion
|
||||||
|
.BecauseOf(because, becauseArgs)
|
||||||
|
.Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList())
|
||||||
|
.ForCondition(requests => requests.Any())
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.",
|
||||||
|
proxyUrl)
|
||||||
|
.Then
|
||||||
|
.ForCondition(x => x.Any(y => y.ProxyUrl == proxyUrl))
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.",
|
||||||
|
_ => proxyUrl, requests => requests.Select(request => request.ProxyUrl));
|
||||||
|
|
||||||
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[CustomAssertion]
|
||||||
|
public AndConstraint<WireMockAssertions> FromClientIP(string clientIP, string because = "", params object[] becauseArgs)
|
||||||
|
{
|
||||||
|
Execute.Assertion
|
||||||
|
.BecauseOf(because, becauseArgs)
|
||||||
|
.Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList())
|
||||||
|
.ForCondition(requests => requests.Any())
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.",
|
||||||
|
clientIP)
|
||||||
|
.Then
|
||||||
|
.ForCondition(x => x.Any(y => y.ClientIP == clientIP))
|
||||||
|
.FailWith(
|
||||||
|
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but didn't find it among the calls from IP(s) {1}.",
|
||||||
|
_ => clientIP, requests => requests.Select(request => request.ClientIP));
|
||||||
|
|
||||||
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ using WireMock.Server;
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
using WireMock.FluentAssertions;
|
using WireMock.FluentAssertions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using WireMock.Settings;
|
||||||
using static System.Environment;
|
using static System.Environment;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.FluentAssertions
|
namespace WireMock.Net.Tests.FluentAssertions
|
||||||
@@ -23,10 +24,10 @@ namespace WireMock.Net.Tests.FluentAssertions
|
|||||||
{
|
{
|
||||||
_server = WireMockServer.Start();
|
_server = WireMockServer.Start();
|
||||||
_server.Given(Request.Create().UsingAnyMethod())
|
_server.Given(Request.Create().UsingAnyMethod())
|
||||||
.RespondWith(Response.Create().WithStatusCode(200));
|
.RespondWith(Response.Create().WithSuccess());
|
||||||
_portUsed = _server.Ports.First();
|
_portUsed = _server.Ports.First();
|
||||||
|
|
||||||
_httpClient = new HttpClient { BaseAddress = new Uri($"http://localhost:{_portUsed}") };
|
_httpClient = new HttpClient { BaseAddress = new Uri(_server.Urls[0]) };
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -155,6 +156,134 @@ namespace WireMock.Net.Tests.FluentAssertions
|
|||||||
.And.Message.Should()
|
.And.Message.Should()
|
||||||
.Be($"{string.Join(NewLine, missingValue1Message, missingValue2Message)}{NewLine}");
|
.Be($"{string.Join(NewLine, missingValue1Message, missingValue2Message)}{NewLine}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AtUrl_WhenACallWasMadeToUrl_Should_BeOK()
|
||||||
|
{
|
||||||
|
await _httpClient.GetAsync("anyurl");
|
||||||
|
|
||||||
|
_server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.AtUrl($"http://localhost:{_portUsed}/anyurl");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AtUrl_Should_ThrowWhenNoCallsWereMade()
|
||||||
|
{
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.AtUrl("anyurl");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
"Expected _server to have been called at address matching the url \"anyurl\", but no calls were made.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AtUrl_Should_ThrowWhenNoCallsMatchingTheUrlWereMade()
|
||||||
|
{
|
||||||
|
await _httpClient.GetAsync("");
|
||||||
|
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.AtUrl("anyurl");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
$"Expected _server to have been called at address matching the url \"anyurl\", but didn't find it among the calls to {{\"http://localhost:{_portUsed}/\"}}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WithProxyUrl_WhenACallWasMadeWithProxyUrl_Should_BeOK()
|
||||||
|
{
|
||||||
|
_server.ResetMappings();
|
||||||
|
_server.Given(Request.Create().UsingAnyMethod())
|
||||||
|
.RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings {Url = "http://localhost:9999"}));
|
||||||
|
|
||||||
|
await _httpClient.GetAsync("");
|
||||||
|
|
||||||
|
_server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.WithProxyUrl($"http://localhost:9999");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WithProxyUrl_Should_ThrowWhenNoCallsWereMade()
|
||||||
|
{
|
||||||
|
_server.ResetMappings();
|
||||||
|
_server.Given(Request.Create().UsingAnyMethod())
|
||||||
|
.RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings {Url = "http://localhost:9999"}));
|
||||||
|
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.WithProxyUrl("anyurl");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
"Expected _server to have been called with proxy url \"anyurl\", but no calls were made.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WithProxyUrl_Should_ThrowWhenNoCallsWithTheProxyUrlWereMade()
|
||||||
|
{
|
||||||
|
_server.ResetMappings();
|
||||||
|
_server.Given(Request.Create().UsingAnyMethod())
|
||||||
|
.RespondWith(Response.Create().WithProxy(new ProxyAndRecordSettings {Url = "http://localhost:9999"}));
|
||||||
|
|
||||||
|
await _httpClient.GetAsync("");
|
||||||
|
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.WithProxyUrl("anyurl");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
$"Expected _server to have been called with proxy url \"anyurl\", but didn't find it among the calls with {{\"http://localhost:9999\"}}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FromClientIP_whenACallWasMadeFromClientIP_Should_BeOK()
|
||||||
|
{
|
||||||
|
await _httpClient.GetAsync("");
|
||||||
|
var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP;
|
||||||
|
|
||||||
|
_server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.FromClientIP(clientIP);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FromClientIP_Should_ThrowWhenNoCallsWereMade()
|
||||||
|
{
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.FromClientIP("different-ip");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
"Expected _server to have been called from client IP \"different-ip\", but no calls were made.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FromClientIP_Should_ThrowWhenNoCallsFromClientIPWereMade()
|
||||||
|
{
|
||||||
|
await _httpClient.GetAsync("");
|
||||||
|
var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP;
|
||||||
|
|
||||||
|
Action act = () => _server.Should()
|
||||||
|
.HaveReceivedACall()
|
||||||
|
.FromClientIP("different-ip");
|
||||||
|
|
||||||
|
act.Should().Throw<Exception>()
|
||||||
|
.And.Message.Should()
|
||||||
|
.Be(
|
||||||
|
$"Expected _server to have been called from client IP \"different-ip\", but didn't find it among the calls from IP(s) {{\"{clientIP}\"}}.");
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user