Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions for ClientIP, Url and ProxyUrl #529

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public WireMockAssertions(IWireMockServer subject, int? callsCount)
}

[CustomAssertion]
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "",
params object[] becauseArgs)
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand All @@ -36,13 +35,11 @@ public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, strin
}

[CustomAssertion]
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value,
string because = "", params object[] becauseArgs)
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value, string because = "", params object[] becauseArgs)
=> WithHeader(expectedKey, new[] {value}, because, becauseArgs);

[CustomAssertion]
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[] expectedValues,
string because = "", params object[] becauseArgs)
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[] expectedValues, string because = "", params object[] becauseArgs)
{
var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers)
.ToDictionary(x => x.Key, x => x.Value);
Expand Down Expand Up @@ -72,5 +69,62 @@ public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[]

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);
}
}
}
133 changes: 131 additions & 2 deletions test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Xunit;
using WireMock.FluentAssertions;
using System.Threading.Tasks;
using WireMock.Settings;
using static System.Environment;

namespace WireMock.Net.Tests.FluentAssertions
Expand All @@ -23,10 +24,10 @@ public WireMockAssertionsTests()
{
_server = WireMockServer.Start();
_server.Given(Request.Create().UsingAnyMethod())
.RespondWith(Response.Create().WithStatusCode(200));
.RespondWith(Response.Create().WithSuccess());
_portUsed = _server.Ports.First();

_httpClient = new HttpClient { BaseAddress = new Uri($"http://localhost:{_portUsed}") };
_httpClient = new HttpClient { BaseAddress = new Uri(_server.Urls[0]) };
}

[Fact]
Expand Down Expand Up @@ -155,6 +156,134 @@ public async Task WithHeader_Should_ThrowWhenNoCallsMatchingTheHeaderWithMultipl
.And.Message.Should()
.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()
{
Expand Down