Skip to content

Commit 28aa3eb

Browse files
committed
Fix a bug where HttpClientFactory named instances didn't work
Fixes #67
1 parent 4eb13d0 commit 28aa3eb

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Genbox.SimpleS3.GenericS3.Extensions;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Genbox.SimpleS3.Core.Tests.OfflineTests;
5+
6+
public class BugTests
7+
{
8+
[Fact]
9+
public void GitHubIssue67()
10+
{
11+
// https://github.com/Genbox/SimpleS3/issues/67
12+
// This tests if using the DI system interferes with existing HttpClients
13+
ServiceCollection collection = new ServiceCollection();
14+
collection.AddHttpClient();
15+
collection.AddGenericS3();
16+
17+
using ServiceProvider provider = collection.BuildServiceProvider();
18+
IEnumerable<HttpClient> clients = provider.GetServices<HttpClient>();
19+
HttpClient client = Assert.Single(clients); //We should only have the HttpClient we added ourselves. SimpleS3 should only register HttpActions for a named HttpClientFactory.
20+
21+
Assert.Empty(client.DefaultRequestHeaders); //SimpleS3 sets a few headers. They must not be present on the user's HttpClient.
22+
}
23+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
<ItemGroup>
3+
<ProjectReference Include="..\SimpleS3.GenericS3\SimpleS3.GenericS3.csproj" />
4+
</ItemGroup>
35
</Project>

Src/SimpleS3.Core/Extensions/SimpleS3CoreServices.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static class SimpleS3CoreServices
3838
/// </summary>
3939
/// <param name="collection">The service collection</param>
4040
/// <param name="configure">Use this to configure the configuration used by SimpleS3</param>
41-
public static ICoreBuilder AddSimpleS3Core(IServiceCollection collection, Action<SimpleS3Config>? configure = null)
41+
/// <param name="name">The name to use for the builder. Used for named dependency isolation.</param>
42+
public static ICoreBuilder AddSimpleS3Core(IServiceCollection collection, Action<SimpleS3Config>? configure = null, string name = "SimpleS3")
4243
{
4344
//We don't use the microsoft extension here as we only want a subset of services.
4445

@@ -102,8 +103,7 @@ public static ICoreBuilder AddSimpleS3Core(IServiceCollection collection, Action
102103
collection.TryAddEnumerable(RegisterAsActual(typeof(IValidator<>), assembly)); //We register IValidator twice to support IValidator<T> as well
103104
collection.TryAddEnumerable(RegisterAsActual(typeof(IValidateOptions<>), assembly)); //Make sure that the options system validators are added too
104105

105-
//NOTE: I have not given the user a chance to set the name (named configs) here yet. It will eventually be used for config isolation between instances.
106-
return new CoreBuilder(collection);
106+
return new CoreBuilder(collection, name);
107107
}
108108

109109
/// <summary>Register services as the interface type given</summary>

0 commit comments

Comments
 (0)