Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -143,6 +144,15 @@ public static IReverseProxyBuilder ConfigureHttpClient(this IReverseProxyBuilder
throw new ArgumentNullException(nameof(configure));
}

var service = builder.Services.FirstOrDefault(service => service.ServiceType == typeof(IForwarderHttpClientFactory));
if (service is not null)
{
if (service.ImplementationType != typeof(ForwarderHttpClientFactory))
{
throw new InvalidOperationException($"ConfigureHttpClient will override the custom IForwarderHttpClientFactory type.");
}
}

builder.Services.AddSingleton<IForwarderHttpClientFactory>(services =>
{
var logger = services.GetRequiredService<ILogger<ForwarderHttpClientFactory>>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Yarp.ReverseProxy.Forwarder;

public class ReverseProxyServiceCollectionTests
{

[Fact]
public void ConfigureHttpClient_Works()
{
new ServiceCollection()
.AddReverseProxy()
.ConfigureHttpClient((_, _) => { });
}

[Fact]
public void ConfigureHttpClient_ThrowIfCustomServiceAdded()
{
Assert.Throws<InvalidOperationException>(() =>
{
new ServiceCollection()
.AddSingleton<IForwarderHttpClientFactory, CustomForwarderHttpClientFactory>()
.AddReverseProxy()
.ConfigureHttpClient((_, _) => { });
});
}

private class CustomForwarderHttpClientFactory : IForwarderHttpClientFactory
{
public HttpMessageInvoker CreateClient(ForwarderHttpClientContext context)
{
throw new NotImplementedException();
}
}
}