Skip to content

Commit

Permalink
#102 asserting query params
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Nov 20, 2016
1 parent f0cfb70 commit 90198cb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Test/Flurl.Test.Shared/Http/TestingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public async Task can_setup_multiple_responses() {
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 });
}

[Test]
public async Task can_simulate_timeout() {
HttpTest.SimulateTimeout();
Expand Down
36 changes: 35 additions & 1 deletion src/Flurl.Http.Shared/Testing/HttpCallAssertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using Flurl.Util;

namespace Flurl.Http.Testing
{
Expand Down Expand Up @@ -45,7 +46,40 @@ public void Times(int expectedCount) {
/// <param name="urlPattern">Can contain * wildcard.</param>
public HttpCallAssertion WithUrlPattern(string urlPattern) {
_urlPattern = urlPattern; // this will land in the exception message when we assert, which is the only reason for capturing it
return With(c => MatchesPattern(c.Request.RequestUri.AbsoluteUri, urlPattern));
return With(c => MatchesPattern(c.Url, urlPattern));
}

/// <summary>
/// Asserts whether calls were made containing the given query parameter (regardless of its value).
/// </summary>
/// <param name="name">The query parameter name.</param>
/// <returns></returns>
public HttpCallAssertion WithQueryParam(string name) {
return With(c => new Url(c.Url).QueryParams.Any(q => q.Name == name));
}

/// <summary>
/// Asserts whether calls were made containing the given query parameter name/value.
/// </summary>
/// <param name="name">The query parameter name.</param>
/// <param name="value">The query parameter value. Can contain * wildcard.</param>
/// <returns></returns>
public HttpCallAssertion WithQueryParam(string name, object value) {
return With(c => new Url(c.Url).QueryParams.Any(q => q.Name == name && MatchesPattern(q.Value.ToString(), value.ToString())));
}

/// <summary>
/// Asserts whether calls were made containing all of the given query parameters.
/// </summary>
/// <param name="values">Object (usually anonymous) or dictionary that is parsed to name/value query parameters to check for.</param>
/// <returns></returns>
public HttpCallAssertion WithQueryParams(object values) {
return With(c => {
var expected = values.ToKeyValuePairs().Select(kv => $"{kv.Key}={kv.Value}");
var actual = new Url(c.Url).QueryParams.Select(q => $"{q.Name}={q.Value}");
//http://stackoverflow.com/a/333034/62600
return !expected.Except(actual).Any();
});
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Flurl.Http.Shared/Testing/HttpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public HttpCallAssertion ShouldHaveCalled(string urlPattern) {
/// Throws an HttpCallAssertException if a URL matching the given pattern was called.
/// </summary>
/// <param name="urlPattern">URL that should not have been called. Can include * wildcard character.</param>
public HttpCallAssertion ShouldNotHaveCalled(string urlPattern) {
return new HttpCallAssertion(CallLog, true).WithUrlPattern(urlPattern);
public void ShouldNotHaveCalled(string urlPattern) {
new HttpCallAssertion(CallLog, true).WithUrlPattern(urlPattern);
}

/// <summary>
Expand Down

0 comments on commit 90198cb

Please sign in to comment.