Skip to content

Commit

Permalink
Merge pull request #150 from tmenier/dev
Browse files Browse the repository at this point in the history
Flurl.Http 1.1.1-pre
  • Loading branch information
tmenier authored Nov 24, 2016
2 parents a025bf2 + 53e4d51 commit f88af89
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Build/nuspec/Flurl.Http.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Flurl.Http</id>
<version>1.1.0-pre</version>
<version>1.1.1-pre</version>
<title>Flurl.Http</title>
<authors>Todd Menier</authors>
<projectUrl>http://tmenier.github.io/Flurl</projectUrl>
Expand All @@ -13,6 +13,7 @@
A fluent, portable, testable HTTP client library that extends Flurl's URL builder chain.
</description>
<releaseNotes>
1.1.1 - More query param assert variations (github #102), HttpCall.Url now a Flurl.Url instance
1.1.0 - Parallel testing (github #67), better DI/IoC/mocking support (github #146), assert query params (github #102)
1.0.3 - .NET Core 1.0.1, fixed assembly references (github #131)
1.0.2 - Updated Flurl dependency to 2.2.1 https://www.nuget.org/packages/Flurl/2.2.1
Expand Down
12 changes: 12 additions & 0 deletions Test/Flurl.Test.Shared/Http/RealHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
using Flurl.Http.Testing;
using NUnit.Framework;

namespace Flurl.Test.Http
Expand Down Expand Up @@ -208,5 +209,16 @@ public async Task can_handle_error() {
FlurlHttp.Configure(c => c.ResetDefaults());
}
}

[Test]
public async Task can_comingle_real_and_fake_tests() {
// do a fake call while a real call is running
var realTask = "https://www.google.com/".GetStringAsync();
using (new HttpTest()) {
var fake = await "https://www.google.com/".GetStringAsync();
Assert.AreEqual("", fake);
}
Assert.AreNotEqual("", await realTask);
}
}
}
59 changes: 49 additions & 10 deletions Test/Flurl.Test.Shared/Http/TestingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public async Task can_setup_multiple_responses() {
.RespondWith("two")
.RespondWith("three");

HttpTest.ShouldNotHaveMadeACall();

await "http://www.api.com/1".GetAsync();
await "http://www.api.com/2".GetAsync();
await "http://www.api.com/3".GetAsync();
Expand All @@ -61,27 +63,64 @@ public async Task can_setup_multiple_responses() {
Assert.AreEqual("two", await calls[1].Response.Content.ReadAsStringAsync());
Assert.AreEqual("three", await calls[2].Response.Content.ReadAsStringAsync());

HttpTest.ShouldHaveMadeACall();
HttpTest.ShouldHaveCalled("http://www.api.com/*").WithVerb(HttpMethod.Get).Times(3);
HttpTest.ShouldNotHaveCalled("http://www.otherapi.com/*");
}

[Test]
public async Task can_assert_query_params() {
await "http://www.api.com?x=111&y=222&z=333".GetAsync();

HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParam("x");
HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParam("y", 222);
HttpTest.ShouldHaveCalled("*").WithQueryParam("z", "*3");
HttpTest.ShouldHaveCalled("*").WithQueryParams(new { z = 333, y = 222 });
await "http://www.api.com?x=111&y=222&z=333#abcd".GetAsync();

HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParams();
HttpTest.ShouldHaveMadeACall().WithQueryParam("x");
HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParams("z", "y");
HttpTest.ShouldHaveMadeACall().WithQueryParamValue("y", 222);
HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParamValue("z", "*3");
HttpTest.ShouldHaveMadeACall().WithQueryParamValues(new { z = 333, y = 222 });

// without
HttpTest.ShouldHaveCalled("http://www.api.com*").WithoutQueryParam("w");
HttpTest.ShouldHaveMadeACall().WithoutQueryParams("t", "u", "v");
HttpTest.ShouldHaveCalled("http://www.api.com*").WithoutQueryParamValue("x", 112);
HttpTest.ShouldHaveMadeACall().WithoutQueryParamValues(new { x = 112, y = 223, z = 666 });

// failures
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithQueryParam("w"));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithQueryParamValue("y", 223));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithQueryParamValue("z", "*4"));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithQueryParamValues(new { x = 111, y = 666 }));

Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParam("w"));
HttpTest.ShouldHaveMadeACall().WithoutQueryParams());
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithoutQueryParam("x"));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithoutQueryParams("z", "y"));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveCalled("http://www.api.com*").WithQueryParam("y", 223));
HttpTest.ShouldHaveMadeACall().WithoutQueryParamValue("y", 222));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithoutQueryParamValue("z", "*3"));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveMadeACall().WithoutQueryParamValues(new { z = 333, y = 222 }));
}

[Test]
public async Task can_assert_multiple_occurances_of_query_param() {
await "http://www.api.com?x=1&x=2&x=3&y=4#abcd".GetAsync();

HttpTest.ShouldHaveMadeACall().WithQueryParam("x");
HttpTest.ShouldHaveMadeACall().WithQueryParamValue("x", new[] { 2, 1 }); // order shouldn't matter
HttpTest.ShouldHaveMadeACall().WithQueryParamValues(new { x = new[] { 3, 2, 1 } }); // order shouldn't matter

Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveCalled("*").WithQueryParam("z", "*4"));
HttpTest.ShouldHaveMadeACall().WithQueryParamValue("x", new[] { 1, 2, 4 }));
Assert.Throws<HttpCallAssertException>(() =>
HttpTest.ShouldHaveCalled("*").WithQueryParams(new { x = 111, y = 666 }));
HttpTest.ShouldHaveMadeACall().WithQueryParamValues(new { x = new[] { 1, 2, 4 } }));
}

[Test]
Expand Down
9 changes: 5 additions & 4 deletions src/Flurl.Http.Shared/HttpCall.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using Flurl.Http.Configuration;
Expand All @@ -13,9 +11,12 @@ namespace Flurl.Http
/// </summary>
public class HttpCall
{
private readonly Lazy<Url> _url;

internal HttpCall(HttpRequestMessage request, FlurlHttpSettings settings) {
Request = request;
Settings = settings;
_url = new Lazy<Url>(() => new Url(Request.RequestUri.AbsoluteUri));
}

/// <summary>
Expand Down Expand Up @@ -65,9 +66,9 @@ internal HttpCall(HttpRequestMessage request, FlurlHttpSettings settings) {
public TimeSpan? Duration => EndedUtc - StartedUtc;

/// <summary>
/// Absolute URI being called.
/// The URL being called.
/// </summary>
public string Url => Request.RequestUri.AbsoluteUri;
public Url Url => _url.Value;

/// <summary>
/// True if a response was received, regardless of whether it is an error status.
Expand Down
36 changes: 21 additions & 15 deletions src/Flurl.Http.Shared/Testing/HttpCallAssertException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

namespace Flurl.Http.Testing
{
Expand All @@ -11,21 +12,26 @@ public class HttpCallAssertException : Exception
/// <summary>
/// Initializes a new instance of the <see cref="HttpCallAssertException"/> class.
/// </summary>
/// <param name="urlPattern">The URL pattern.</param>
/// <param name="expectedCalls">The expected calls.</param>
/// <param name="actualCalls">The actual calls.</param>
public HttpCallAssertException(string urlPattern, int? expectedCalls, int actualCalls) : base(BuildMessage(urlPattern, expectedCalls, actualCalls)) { }
/// <param name="conditions">The expected call conditions.</param>
/// <param name="expectedCalls">The expected number of calls.</param>
/// <param name="actualCalls">The actual number calls.</param>
public HttpCallAssertException(IList<string> conditions, int? expectedCalls, int actualCalls) : base(BuildMessage(conditions, expectedCalls, actualCalls)) { }

private static string BuildMessage(string urlPattern, int? expectedCalls, int actualCalls) {
if (expectedCalls == null)
return $"Expected call to {urlPattern} was not made.";

return new StringBuilder()
.Append("Expected ").Append(expectedCalls.Value)
.Append(expectedCalls == 1 ? " call" : " calls")
.Append(" to ").Append(urlPattern).Append(" but ").Append(actualCalls)
.Append(actualCalls == 1 ? " call was made." : " calls were made.")
.ToString();
private static string BuildMessage(IList<string> conditions, int? expectedCalls, int actualCalls) {
var expected =
(expectedCalls == null) ? "any calls to be made" :
(expectedCalls == 0) ? "no calls to be made" :
(expectedCalls == 1) ? "1 call to be made" :
expectedCalls + " calls to be made";
var actual =
(actualCalls == 0) ? "no matching calls were made" :
(actualCalls == 1) ? "1 matching call was made" :
actualCalls + " matching calls were made";
if (conditions.Any())
expected += " with " + string.Join(" and ", conditions);
else
actual = actual.Replace(" matching", "");
return $"Expected {expected}, but {actual}.";
}
}
}
Loading

0 comments on commit f88af89

Please sign in to comment.