向TestServer的HttpClient添加委派处理程序(传出请求中间件)

问题描述 投票:5回答:1

是否有可能在asp.net核心DelegationHandler构建的HttpClient中添加自定义TestServer(发出请求的中间件?

我正在尝试将使用customization possibilities来影响HttpClientFactoryHttpClients与内存中测试实用程序TestServer

TestServer

我找不到将我的public class ExternalProxySslDowngradeSimulator : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken token) { if (request.RequestUri.Scheme == "https") request.RequestUri = new Uri( request.RequestUri.OriginalString.Replace("https:", "http:")); return base.SendAsync(request, token); } } [TestFixture public class TestClass { [Test] public async Task CallApi() { var builder = new WebHostBuilder() .ConfigureAppConfiguration((ctx, config) => { }) .UseUrl("http://customDomain.com") .UseStartup<CustomApi.Startup>(); var testServer = new Microsoft.AspNetCore.TestHost.TestServer(builder); var httpClient = testServer.CreateClient(); var apiResult = await httpClient.GetAsync("https://customDomain"); } } 插入从DelegatingHandler返回的HttpClient中的方法>


到目前为止我尝试过的:

  1. testServer.CreateClient()(没用):
  2. Registering a custom HttpClientBuilder

浏览// did not work: var builder = new WebHostBuilder() .UseStartup<CustomApi.Startup>() .ConfigureServices(services => { services .AddHttpClient("") .AddHttpMessageHandler<ExternalProxySslDowngradeSimulator>(); 时,它没有连接到该管道。

  1. 自定义code for TestServer(无效)
  2. 我应该绑定自定义TestServer并将自定义操作添加到HttpClientFactoryOptions。但是TestServer并没有真正使用默认的Http Client Factory的这种方式。

  1. 自定义HttpClientFactoryOptions的设置(无效)
  2. [浏览文档/源代码没有发现任何地方可以挂入。虽然有HttpClientFactoryOptions.HttpMessageHandlerBuilderActions,但是我看不到如何将我带入TestServer使用的自定义HttpClientFactoryOptions.HttpMessageHandlerBuilderActions

是否有可能向asp.net核心TestServer构建的HttpClient添加自定义的委托处理程序(传出请求中间件)?我正在尝试结合使用...

c# testing asp.net-core .net-core dotnet-httpclient
1个回答
0
投票
TestServer
© www.soinside.com 2019 - 2024. All rights reserved.