|
1 |
| -using Microsoft.Extensions.Options; |
2 |
| - |
3 |
| -namespace Refit.Tests; |
4 |
| - |
| 1 | +using System.Globalization; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Reflection; |
5 | 4 | using System.Text.Json;
|
| 5 | + |
6 | 6 | using Microsoft.Extensions.DependencyInjection;
|
| 7 | +using Microsoft.Extensions.Options; |
| 8 | + |
| 9 | +using Refit.Implementation; |
| 10 | + |
7 | 11 | using Xunit;
|
8 | 12 |
|
| 13 | +namespace Refit.Tests; |
| 14 | + |
9 | 15 | public class HttpClientFactoryExtensionsTests
|
10 | 16 | {
|
11 | 17 | class User { }
|
@@ -154,6 +160,37 @@ public void HttpClientSettingsCanBeProvidedStaticallyGivenTypeArgument()
|
154 | 160 | );
|
155 | 161 | }
|
156 | 162 |
|
| 163 | + [Fact] |
| 164 | + public void ProvidedHttpClientIsUsedAsNamedClient() |
| 165 | + { |
| 166 | + var baseUri = new Uri("https://0:1337"); |
| 167 | + var services = new ServiceCollection(); |
| 168 | + |
| 169 | + services.AddHttpClient("MyHttpClient", client => { |
| 170 | + client.BaseAddress = baseUri; |
| 171 | + client.DefaultRequestHeaders.Add("X-Powered-By", Environment.OSVersion.VersionString); |
| 172 | + }); |
| 173 | + services.AddRefitClient<IGitHubApi>(null, "MyHttpClient"); |
| 174 | + |
| 175 | + var sp = services.BuildServiceProvider(); |
| 176 | + var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>(); |
| 177 | + var httpClient = httpClientFactory.CreateClient("MyHttpClient"); |
| 178 | + |
| 179 | + var gitHubApi = sp.GetRequiredService<IGitHubApi>(); |
| 180 | + |
| 181 | + var memberInfos = typeof(Generated).GetMember("RefitTestsIGitHubApi", BindingFlags.NonPublic); |
| 182 | + var genApi = Convert.ChangeType(gitHubApi, (Type)memberInfos[0], CultureInfo.InvariantCulture); |
| 183 | + var genApiProperty = genApi.GetType().GetProperty("Client")!; |
| 184 | + var genApiClient = (HttpClient)genApiProperty.GetValue(genApi)!; |
| 185 | + |
| 186 | + Assert.NotSame(httpClient, genApiClient); |
| 187 | + Assert.Equal(httpClient.BaseAddress, genApiClient.BaseAddress); |
| 188 | + Assert.Equal(baseUri, genApiClient.BaseAddress); |
| 189 | + Assert.Contains( |
| 190 | + new KeyValuePair<string, IEnumerable<string>>("X-Powered-By", |
| 191 | + new[] { Environment.OSVersion.VersionString }), genApiClient.DefaultRequestHeaders); |
| 192 | + } |
| 193 | + |
157 | 194 | class ClientOptions
|
158 | 195 | {
|
159 | 196 | public SystemTextJsonContentSerializer Serializer { get; set; }
|
|
0 commit comments