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

HttpTest: Set up responses depending on the call being made #481

Closed
tmenier opened this issue Dec 6, 2019 · 5 comments
Closed

HttpTest: Set up responses depending on the call being made #481

tmenier opened this issue Dec 6, 2019 · 5 comments

Comments

@tmenier
Copy link
Owner

tmenier commented Dec 6, 2019

Flurl's current testing features allow you to fluently build up a queue of fake responses like this:

httpTest
    .RespondWith(...)
    .RespondWith(...)
    .RespondWith(...);

These responses are guaranteed to return to the test subject in same order that the Flurl requests are sent, but that's not always determinate, and there have been requests to allow configuring responses based on the call made, particularly the URL.

3.0 introduces filtered test setups. The example above will still work for general cases where filtering isn't needed, or when no filters match. Here is a fairly comprehensive example demonstrating just about all of the new possibilities:

httpTest
    .ForCallsTo("*.api.com*", "*.test-api.com*") // multiple allowed, wildcard supported
    .WithVerb("put", "PATCH") // or HttpMethod.Put, HttpMethod.Patch
    .WithQueryParam("x", "a*") // value optional, wildcard supported
    .WithQueryParams(new { y = 2, z = 3 })
    .WithAnyQueryParam("a", "b", "c")
    .WithoutQueryParam("d")
    .WithHeader("h1", "f*o") // value optional, wildcard supported
    .WithoutHeader("h2")
    .WithRequestBody("*something*") // wildcard supported
    .WithRequestJson(new { a = "*", b = "hi" }) // wildcard supported in sting values
    .With(call => true) // check anything on FlurlCall
    .Without(call => false) // check anything on FlurlCall
    .RespondWith("all conditions met!", 200);

You can add as many filtered setups to a test as you want, each beginning with a new call to httpTest.ForCallsTo(...).

Closely related is #225, the much-requested ability to selectively allow real HTTP calls to be made within a test. The example is a little buried in that issue so I'll repeat it here:

httpTest.ForCallsTo("*.api.com*").AllowRealHttp();
@tmenier
Copy link
Owner Author

tmenier commented Dec 15, 2019

Now available to test: https://www.nuget.org/packages/Flurl.Http/3.0.0-pre2

@michbushi
Copy link

that looks awesome! Any chance to add an "invisible proxy" mode; such as all the HttpTest-matched responses would return the mocked responses as configured, but for all unmatched calls, would actually call the underlying api and return the real response?

@tmenier
Copy link
Owner Author

tmenier commented Dec 17, 2019

@michbushi Yep, that's covered by #225, which is so closely related that I added an example to this issue's description.

@APIWT
Copy link

APIWT commented Feb 18, 2020

Thank you so much for this enhancement! Do you have a pulse on when 3.0 will be released?

@tmenier
Copy link
Owner Author

tmenier commented Feb 19, 2020

@APIWT I don't have an estimate yet. There's a fair amount more I want to do before I commit to no more breaking changes, and my available time is limited. What I've been telling people is that I consider these prereleases to be as stable and production-ready as any other release, the only difference being that there can and will be additional breaking changes. But I track them carefully in the release notes, you're control of when you upgrade, and I don't anticipate more than maybe 2 or 3 more before I strip the -pre label and call it official.

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

3 participants