是否可以在OWIN TestServer中使用Furl.Http? [英] Is it possible to use Furl.Http with the OWIN TestServer?

查看:92
本文介绍了是否可以在OWIN TestServer中使用Furl.Http?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OWIN TestServer,它为我提供了一个HttpClient来执行对测试服务器的内存调用.我想知道是否有一种方法可以传入现有的HttpClient供Flurl使用.

I'm using the OWIN TestServer which provides me an HttpClient to do my in memory calls to the test server. I'm wondering if there's a way of passing in the existing HttpClient for Flurl to use.

推荐答案

更新: 以下大部分信息在Flurl.Http 2.x中不再相关.具体来说,Flurr的大多数功能都包含在新的FlurlClient对象(用于包装HttpClient)中,而不包含在自定义消息处理程序中,因此,如果您提供不同的HttpClient,则不会失去功能.此外,从Flurl.Http 2.3.1开始,您不再需要自定义工厂来执行此操作.就像这样简单:

UPDATE: Much of the information below is no longer relevant in Flurl.Http 2.x. Specifically, most of Flurl's functionality is contained in the new FlurlClient object (which wraps HttpClient) and not in a custom message handler, so you're not losing functionality if you provide a different HttpClient. Further, as of Flurl.Http 2.3.1, you no longer need a custom factory to do this. It's as easy as:

var flurlClient = new FlurlClient(httpClient);


Flurl提供了一个IHttpClientFactory界面,允许您自定义HttpClient构造.但是,Flurl的许多功能是由自定义HttpMessageHandler提供的,该自定义HttpMessageHandler在构造时已添加到HttpClient中.您不希望为已实例化的HttpClient热插拔它,否则将有可能破坏Flurl.


Flurl provides an IHttpClientFactory interface that allows you to customize HttpClient construction. However, much of Flurl's functionality is provided by a custom HttpMessageHandler, which is added to HttpClient on construction. You wouldn't want to hot-swap it out for an already instantiated HttpClient or you'll risk breaking Flurl.

幸运的是,OWIN TestServer也是由HttpMessageHandler驱动的,创建HttpClient时可以流水线化多个对象.

Fortunately, OWIN TestServer is also driven by an HttpMessageHandler, and you can pipeline multiple when you create an HttpClient.

从允许您传入TestServer实例的自定义工厂开始:

Start with a custom factory that allows you to pass in the TestServer instance:

using Flurl.Http.Configuration;
using Microsoft.Owin.Testing;

public class OwinTestHttpClientFactory : DefaultHttpClientFactory
{
    private readonly TestServer _testServer;

    public OwinTestHttpClientFactory(TestServer server) {
        _testServer = server;
    }

    public override HttpMessageHandler CreateMessageHandler() {
        // TestServer's HttpMessageHandler will be added to the end of the pipeline
        return _testServer.Handler;
    }
}

可以在全球范围内注册工厂,但是由于每个测试都需要一个不同的TestServer实例,因此建议将其设置在FlurlClient实例上,该实例为

Factories can be registered globally, but since you need a different TestServer instance for each test, I'd recommend setting it on the FlurlClient instance, which is a new capability as of Flurl.Http 0.7. So your tests would look something like this:

using (var testServer = TestServer.Create(...)) {
    using (var flurlClient = new FlurlClient()) {
        flurlClient.Settings.HttpClientFactory = new OwinTestHttpClientFactory(testServer);

        // do your tests with the FlurlClient instance. 2 ways to do that:
        url.WithClient(flurlClient).PostJsonAsync(...);
        flurlClient.WithUrl(url).PostJsonAsync(...);
    }
}

这篇关于是否可以在OWIN TestServer中使用Furl.Http?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆