|
| 1 | +using System.Net; |
| 2 | +using System.Net.WebSockets; |
| 3 | +using System.Reflection; |
| 4 | +using Cnblogs.DashScope.Core; |
| 5 | +using Cnblogs.DashScope.Core.Internals; |
| 6 | + |
| 7 | +namespace Cnblogs.DashScope.Sdk.UnitTests; |
| 8 | + |
| 9 | +public class DashScopeClientWebSocketFactoryTests |
| 10 | +{ |
| 11 | + private static readonly FieldInfo ClientWebSocketWrapperGetter = |
| 12 | + typeof(DashScopeClientWebSocket).GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 13 | + |
| 14 | + private static readonly FieldInfo ClientWebSocketGetter = |
| 15 | + typeof(ClientWebSocketWrapper).GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 16 | + |
| 17 | + private static readonly PropertyInfo RequestHeaderGetter = typeof(ClientWebSocketOptions).GetProperty( |
| 18 | + "RequestHeaders", |
| 19 | + BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void CreateSocket_WithWorkspaceId_SetKeyAndSpaceIdProperly() |
| 23 | + { |
| 24 | + // Arrange |
| 25 | + const string apiKey = "apikey"; |
| 26 | + const string workspaceId = "some-space"; |
| 27 | + var factory = new DashScopeClientWebSocketFactory(); |
| 28 | + |
| 29 | + // Act |
| 30 | + var socket = factory.GetClientWebSocket(apiKey, workspaceId); |
| 31 | + var socketWrapper = ClientWebSocketWrapperGetter.GetValue(socket) as ClientWebSocketWrapper; |
| 32 | + var clientWebSocket = ClientWebSocketGetter.GetValue(socketWrapper) as ClientWebSocket; |
| 33 | + var headers = RequestHeaderGetter.GetValue(clientWebSocket?.Options) as WebHeaderCollection; |
| 34 | + |
| 35 | + // Assert |
| 36 | + Assert.NotNull(socketWrapper); |
| 37 | + Assert.NotNull(headers); |
| 38 | + Assert.Equal("bearer " + apiKey, headers.Get("Authorization")); |
| 39 | + Assert.Equal(workspaceId, headers.Get("X-DashScope-WorkspaceId")); |
| 40 | + } |
| 41 | +} |
0 commit comments