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 - Mock specific url calls #225

Closed
gregoks opened this issue Oct 1, 2017 · 12 comments
Closed

HttpTest - Mock specific url calls #225

gregoks opened this issue Oct 1, 2017 · 12 comments

Comments

@gregoks
Copy link

gregoks commented Oct 1, 2017

Hi,

Is it possible to decide which url calls will be mocked and which wont? For example, if my method that I test has several HTTP calls to different Urls. I only want to mock one of the calls and the other calls should get to the actual API url.

Thanks,

Greg

@tmenier
Copy link
Owner

tmenier commented Oct 1, 2017

Not currently, no. Test mode is sort of all or nothing. If it were 2 method calls, you could always fire up an HttpTest for one and not the other, but it sounds like that's not the case here.

I'll keep this open for consideration. I have thought about enhancing RespondWith so you can specify which URL or URL pattern it applies to, but hadn't really considered this scenario.

@gregoks
Copy link
Author

gregoks commented Oct 1, 2017

Thanks for the response. Actually my scenario is that I'm testing a service of mine which calls a 3rd party web api inside one of its methods. I want to mock that 3rd party web api to control which response it returns.

@felipe-rochac
Copy link

I have the some case tests like yours @gregoks and I want to be able to test some complexes scenarios by using Flurl, is it too complicated to add ?

@tmenier
Copy link
Owner

tmenier commented May 12, 2018

@felipe-rochac I wouldn't say it's "too complicated", but it is a major enhancement. I definitely want to do it though, and it could be the centerpiece of the next minor version upgrade. Spending most of my available time on overhauling the docs right now, but hopefully this will be the next big thing.

@gregoks
Copy link
Author

gregoks commented May 13, 2018

@tmenier thanks! Any idea when will it be released?

@TomBruyneel
Copy link

I'll keep this open for consideration. I have thought about enhancing RespondWith so you can specify which URL or URL pattern it applies to

I was just about to suggest this as a feature ;). I want to test a function that executes several calls concurrently, which is impossible with the current httptest

@laurieathey
Copy link

As TomBruyneel said, could really use this :)

@tmenier tmenier added this to the Flurl.Http 3.0 milestone Feb 15, 2019
@tmenier tmenier added 3.0 and removed reviewed labels Mar 24, 2019
@tmenier
Copy link
Owner

tmenier commented Apr 6, 2019

I'm actively gathering feedback to help prioritize issues for 3.0. If this one is important to you, please vote for it here!

@juchom
Copy link

juchom commented Nov 27, 2019

I'm just hitting this issue with current testing model.

If we have this code, is there a way to test the GetRiskForTicker method with doing a fake call with FlurlStockClient and do a real call with FlurlRiskClient?

public class RiskManagementService
{
    private FlurlStockClient _stockClient;
    private FlurlRiskClient _riskClient;

    RiskManagementService(FlurlStockClient stockClient, FlurlRiskClient riskClient)
    {
        _stockClient = stockClient;
        _riskClient = riskClient;
    }

    public double GetRiskForTicker(string ticker)
    {
        double tickerPrice = _stockClient.GetTickerPrice(ticker);
        return _riskClient.GetRisk(tickerPrice);
    }
}

public FlurlStockClient(IFlurlClientFactory flurlClientFac)
{
    private readonly IFlurlClient _client;

        public FlurlStockClient(IFlurlClientFactory flurlClientFac)
        {
            _client = flurlClientFac.Get("https://stock-service.com");
        }
}

public FlurlRiskClient(IFlurlClientFactory flurlClientFac)
{
    private readonly IFlurlClient _client;

        public FlurlRiskClient(IFlurlClientFactory flurlClientFac)
        {
            _client = flurlClientFac.Get("https://risk-service.com");
        }
}

Thanks

@tmenier
Copy link
Owner

tmenier commented Nov 28, 2019

@juchom I would normally answer no and link to the open issue but I see you already found it. ;)

@tmenier
Copy link
Owner

tmenier commented Dec 12, 2019

Changed my mind about combining this with #481. I'll track it a separate issue.

Here's how this will look in 3.0:

httpTest
    .ForCallsTo("*.api.com*", "*.test-api.com*") // multiple allowed, wildcard supported
    .AllowRealHttp();

That's it. By default, all Flurl calls made during the lifetime of an HttpTest object are still faked unless you explicitly opt in as shown above. Along with the other features introduced in #481, you could even enable real HTTP calls only when additional conditions are met. For example, do real GETs to an API but fake POSTs.

@tmenier tmenier reopened this Dec 12, 2019
@tmenier
Copy link
Owner

tmenier commented Dec 15, 2019

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

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

6 participants