Last active
March 6, 2018 12:55
-
-
Save tmenier/0d2221b01f902e26312bca8eca204c7d to your computer and use it in GitHub Desktop.
Flurl.Http + vcr-sharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. define a custom HttpClientFactory | |
public class VcrHttpClientFactory : DefaultHttpClientFactory | |
{ | |
private readonly string _cassette; | |
public VcrHttpClientFactory(string cassette) { | |
_cassette = cassette; | |
} | |
public override HttpMessageHandler CreateMessageHandler() { | |
var path = VcrSharp.Tests.HttpClientFactory.GetFixturePath(_cassette); | |
return new VcrSharp.Tests.ReplayingHandler(path); | |
} | |
} | |
// 2. define an extension method on IFlurlClient | |
public static class FlurlClientExtensions | |
{ | |
public static void WithCassette(this IFlurlClient cli, string cassette) { | |
cli.Settings.HttpClientFactory = new VcrHttpClientFactory(cassette); | |
} | |
} | |
// 3. create & use a VCR-enabled FlurlClient | |
var cli = new FlurlClient().WithCassette("my-test-scenario"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment