Skip to content

Commit 2388620

Browse files
authored
Merge pull request #26 from cnblogs/fix-authentication-regression
fix: authentication regression
2 parents 43963af + 30c8599 commit 2388620

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Cnblogs.DashScope.Core/DashScopeClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Cnblogs.DashScope.Core.Internals;
1+
using System.Net.Http.Headers;
2+
using Cnblogs.DashScope.Core.Internals;
23

34
namespace Cnblogs.DashScope.Core;
45

@@ -33,6 +34,8 @@ private static HttpClient GetConfiguredClient(string apiKey, TimeSpan? timeout)
3334
BaseAddress = new Uri(DashScopeDefaults.DashScopeApiBaseAddress),
3435
Timeout = timeout ?? TimeSpan.FromMinutes(2)
3536
};
37+
38+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
3639
ClientPools.Add(GetCacheKey(), client);
3740
}
3841

test/Cnblogs.DashScope.Sdk.UnitTests/DashScopeClientTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Net.Http.Headers;
2+
using System.Reflection;
23
using Cnblogs.DashScope.Core;
34
using FluentAssertions;
45

@@ -59,6 +60,21 @@ public void DashScopeClient_Constructor_CacheableParams(
5960
value.Should().Be(value2);
6061
}
6162

63+
[Fact]
64+
public void DashScopeClient_Constructor_WithApiKeyHeader()
65+
{
66+
// Arrange
67+
const string apiKey = "key";
68+
var client = new DashScopeClient(apiKey);
69+
70+
// Act
71+
var value = HttpClientAccessor.GetValue(client) as HttpClient;
72+
73+
// Assert
74+
value?.DefaultRequestHeaders.Authorization?.Should()
75+
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", apiKey));
76+
}
77+
6278
public static TheoryData<string, string, TimeSpan?, TimeSpan?> ParamsShouldNotCache
6379
=> new()
6480
{

0 commit comments

Comments
 (0)