-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathServiceCollectionInjectorTests.cs
146 lines (126 loc) · 5.34 KB
/
ServiceCollectionInjectorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Net.Http.Headers;
using Cnblogs.DashScope.Core;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Cnblogs.DashScope.Sdk.UnitTests;
public class ServiceCollectionInjectorTests
{
private const string ApiKey = "api key";
private const string ProxyApi = "https://www.gateway.com/dashScope/v1/";
[Fact]
public void Parameter_Normal_Inject()
{
// Arrange
var services = new ServiceCollection();
// Act
services.AddDashScopeClient(ApiKey);
var provider = services.BuildServiceProvider();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(IDashScopeClient));
// Assert
provider.GetRequiredService<IDashScopeClient>().Should().NotBeNull().And
.BeOfType<DashScopeClientCore>();
httpClient.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", ApiKey));
}
[Fact]
public void Parameter_HasProxy_Inject()
{
// Arrange
var services = new ServiceCollection();
// Act
services.AddDashScopeClient(ApiKey, ProxyApi);
var provider = services.BuildServiceProvider();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(IDashScopeClient));
// Assert
provider.GetRequiredService<IDashScopeClient>().Should().NotBeNull().And
.BeOfType<DashScopeClientCore>();
httpClient.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", ApiKey));
httpClient.BaseAddress.Should().BeEquivalentTo(new Uri(ProxyApi));
}
[Fact]
public void Configuration_Normal_Inject()
{
// Arrange
var services = new ServiceCollection();
var config = new Dictionary<string, string?>
{
{ "irrelevant", "irr" },
{ "dashScope:apiKey", ApiKey },
{ "dashScope:baseAddress", ProxyApi }
};
var configuration = new ConfigurationBuilder().AddInMemoryCollection(config).Build();
// Act
services.AddDashScopeClient(configuration);
var provider = services.BuildServiceProvider();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(IDashScopeClient));
// Assert
provider.GetRequiredService<IDashScopeClient>().Should().NotBeNull().And
.BeOfType<DashScopeClientCore>();
httpClient.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", ApiKey));
httpClient.BaseAddress.Should().BeEquivalentTo(new Uri(ProxyApi));
}
[Fact]
public void Configuration_CustomSectionName_Inject()
{
// Arrange
var services = new ServiceCollection();
var config = new Dictionary<string, string?>
{
{ "irrelevant", "irr" },
{ "dashScopeCustom:apiKey", ApiKey },
{ "dashScopeCustom:baseAddress", ProxyApi }
};
var configuration = new ConfigurationBuilder().AddInMemoryCollection(config).Build();
// Act
services.AddDashScopeClient(configuration, "dashScopeCustom");
var provider = services.BuildServiceProvider();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(IDashScopeClient));
// Assert
provider.GetRequiredService<IDashScopeClient>().Should().NotBeNull().And
.BeOfType<DashScopeClientCore>();
httpClient.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", ApiKey));
httpClient.BaseAddress.Should().BeEquivalentTo(new Uri(ProxyApi));
}
[Fact]
public void Configuration_AddMultipleTime_Replace()
{
// Arrange
var services = new ServiceCollection();
// Act
services.AddDashScopeClient(ApiKey, ProxyApi);
services.AddDashScopeClient(ApiKey, ProxyApi);
var provider = services.BuildServiceProvider();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(IDashScopeClient));
// Assert
provider.GetRequiredService<IDashScopeClient>().Should().NotBeNull().And
.BeOfType<DashScopeClientCore>();
httpClient.Should().NotBeNull();
httpClient.DefaultRequestHeaders.Authorization.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", ApiKey));
httpClient.BaseAddress.Should().BeEquivalentTo(new Uri(ProxyApi));
}
[Fact]
public void Configuration_NoApiKey_Throw()
{
// Arrange
var services = new ServiceCollection();
var config = new Dictionary<string, string?>
{
{ "irrelevant", "irr" },
{ "dashScope:baseAddress", ProxyApi }
};
var configuration = new ConfigurationBuilder().AddInMemoryCollection(config).Build();
// Act
var act = () => services.AddDashScopeClient(configuration);
// Assert
act.Should().Throw<InvalidOperationException>();
}
}