Flurl: .WithParamValue skips exact value when it's a string and leads to false-positive

Created on 29 Apr 2019  路  2Comments  路  Source: tmenier/Flurl

I've been using the assertions .WithQueryParamValue in my UnitTests but I noticed that it's not comparing the whole value for the "value", instead it's using a regex pattern to match if the value is present on the Request Uri, however this can leads to false-positives as it's not checking the exact value.

I'm showing it with two unit tests:

[Test]
public async Task ShouldFail()
{
    using (var httpFake = new HttpTest())
    {
        var request = await "http://www.uol.com.br/?code=UNIT_TEST".GetStringAsync();

        httpFake
            .ShouldHaveCalled("*http://www.uol.com.br*")
            .WithQueryParamValue("code", "UNIT");
    }
}

[Test]
public async Task ShouldPass()
{
    using (var httpFake = new HttpTest())
    {
        var request = await "http://www.uol.com.br/?code=UNIT_TEST".GetStringAsync();

        httpFake
            .ShouldHaveCalled("*http://www.uol.com.br*")
            .WithQueryParamValue("code", "UNIT_TEST");
    }
}

First one should Fail as it's not matching the exact value.

I didn't check it further but I believe the issue is in:

private bool QueryParamMatches(QueryParameter qp, string name, object value)
{
      if (qp.Name != name)
            return false;
      if (value is string pattern)
            return this.MatchesPattern(qp.Value?.ToString(), pattern);
      return qp.Value?.ToString() == value?.ToString();
}

Because you're validating using a Regex pattern before validating if the value is exactly the same.

3.0 breaking

Most helpful comment

Thanks for reporting. Closely related to #323. I'm not calling either a bug but I am convinced that the behavior should change. I'll get this done in 3.0.

All 2 comments

Thanks for reporting. Closely related to #323. I'm not calling either a bug but I am convinced that the behavior should change. I'll get this done in 3.0.

Sorry, lost sight of this issue but it was fixed in a recent 3.0 prerelease.

Was this page helpful?
0 / 5 - 0 ratings