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

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

Closed
RoggerFabri opened this issue Apr 29, 2019 · 2 comments

Comments

@RoggerFabri
Copy link

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.

@RoggerFabri RoggerFabri changed the title .WithParamValue skips whole value when it's a string .WithParamValue skips whole value when it's a string leads to false-positive Apr 29, 2019
@RoggerFabri RoggerFabri changed the title .WithParamValue skips whole value when it's a string leads to false-positive .WithParamValue skips exact value when it's a string and leads to false-positive Apr 29, 2019
@tmenier
Copy link
Owner

tmenier commented Apr 29, 2019

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.

@tmenier
Copy link
Owner

tmenier commented Sep 12, 2020

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

@tmenier tmenier closed this as completed Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants