Skip to content

Commit

Permalink
#596 testing - don't require trailing * to match URL with query
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Apr 28, 2021
1 parent e24ff98 commit b089fb8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Test/Flurl.Test/Http/TestingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ public async Task can_respond_based_on_query_params() {
Assert.AreEqual("query param conditions met!", await "http://api.com?x=1&y=2&z=3&c=yes".GetStringAsync());
}

[Test] // #596
public async Task url_patterns_ignore_query_when_not_specified() {
HttpTest.ForCallsTo("http://api.com/1").RespondWith("one");
HttpTest.ForCallsTo("http://api.com/2").WithAnyQueryParam().RespondWith("two");
HttpTest.ForCallsTo("http://api.com/3").WithoutQueryParams().RespondWith("three");

Assert.AreEqual("one", await "http://api.com/1".GetStringAsync());
Assert.AreEqual("one", await "http://api.com/1?x=foo&y=bar".GetStringAsync());

Assert.AreEqual("", await "http://api.com/2".GetStringAsync());
Assert.AreEqual("two", await "http://api.com/2?x=foo&y=bar".GetStringAsync());

Assert.AreEqual("three", await "http://api.com/3".GetStringAsync());
Assert.AreEqual("", await "http://api.com/3?x=foo&y=bar".GetStringAsync());

HttpTest.ShouldHaveCalled("http://api.com/1").Times(2);
HttpTest.ShouldHaveCalled("http://api.com/1").WithAnyQueryParam().Times(1);
HttpTest.ShouldHaveCalled("http://api.com/1").WithoutQueryParams().Times(1);
HttpTest.ShouldHaveCalled("http://api.com/1?x=foo").Times(1);
HttpTest.ShouldHaveCalled("http://api.com/1?x=foo").WithQueryParam("y").Times(1);
HttpTest.ShouldHaveCalled("http://api.com/1?x=foo").WithQueryParam("y", "bar").Times(1);
}

[Test]
public async Task can_respond_based_on_headers() {
HttpTest
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/Testing/FilteredHttpTestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class FilteredHttpTestSetup : HttpTestSetup
/// <param name="urlPatterns">URL(s) or URL pattern(s) that this HttpTestSetup applies to. Can contain * wildcard.</param>
public FilteredHttpTestSetup(TestFlurlHttpSettings settings, params string[] urlPatterns) : base(settings) {
if (urlPatterns.Any())
With(call => urlPatterns.Any(p => Util.MatchesPattern(call.Request.Url, p)));
With(call => urlPatterns.Any(p => Util.MatchesUrlPattern(call.Request.Url, p)));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/Testing/HttpCallAssertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public HttpCallAssertion Without(Func<FlurlCall, bool> match, string descrip = n
/// </summary>
/// <param name="urlPattern">Can contain * wildcard.</param>
public HttpCallAssertion WithUrlPattern(string urlPattern) {
return With(c => Util.MatchesPattern(c.Request.Url, urlPattern), $"URL pattern {urlPattern}");
return With(c => Util.MatchesUrlPattern(c.Request.Url, urlPattern), $"URL pattern {urlPattern}");
}

/// <summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Flurl.Http/Testing/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ private static bool MatchesValueOrPattern(object valueToMatch, object value) {
return valueToMatch?.ToInvariantString() == value?.ToInvariantString();
}

/// <summary>
/// same as MatchesPattern, but doesn't require trailing * to ignore query string
/// </summary>
internal static bool MatchesUrlPattern(string url, string pattern) {
if (MatchesPattern(url, pattern))
return true;
if (pattern.OrdinalEndsWith("*"))
return false;
if (pattern.OrdinalContains("?"))
return MatchesPattern(url, pattern + "&*");
else
return MatchesPattern(url, pattern + "?*");
}

/// <summary>
/// match simple patterns with * wildcard
/// </summary>
internal static bool MatchesPattern(string textToCheck, string pattern) {
// avoid regex'ing in simple cases
if (string.IsNullOrEmpty(pattern) || pattern == "*") return true;
Expand Down

0 comments on commit b089fb8

Please sign in to comment.