Skip to content

Instantly share code, notes, and snippets.

@tmenier
Last active March 6, 2018 12:55
Show Gist options
  • Save tmenier/0d2221b01f902e26312bca8eca204c7d to your computer and use it in GitHub Desktop.
Save tmenier/0d2221b01f902e26312bca8eca204c7d to your computer and use it in GitHub Desktop.
Flurl.Http + vcr-sharp
// 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