|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.ClientModel.Primitives; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.AI.OpenAI; |
| 9 | +using Azure.Core; |
| 10 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 11 | +using Microsoft.SemanticKernel.Connectors.AzureOpenAI; |
| 12 | +using SemanticKernel.Connectors.AzureOpenAI.Core; |
| 13 | + |
| 14 | +namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Core; |
| 15 | + |
| 16 | +public sealed class ClientCoreTests : IDisposable |
| 17 | +{ |
| 18 | + private readonly MultipleHttpMessageHandlerStub _multiHttpMessageHandlerStub; |
| 19 | + private readonly HttpClient _httpClient; |
| 20 | + |
| 21 | + public ClientCoreTests() |
| 22 | + { |
| 23 | + this._multiHttpMessageHandlerStub = new MultipleHttpMessageHandlerStub(); |
| 24 | + this._httpClient = new HttpClient(this._multiHttpMessageHandlerStub); |
| 25 | + } |
| 26 | + |
| 27 | + public void Dispose() |
| 28 | + { |
| 29 | + this._httpClient.Dispose(); |
| 30 | + this._multiHttpMessageHandlerStub.Dispose(); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task AuthenticationHeaderShouldBeProvidedOnlyOnce() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + using var firstResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests); |
| 38 | + using var secondResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests); |
| 39 | + using var thirdResponse = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests); |
| 40 | + |
| 41 | + this._multiHttpMessageHandlerStub.ResponsesToReturn.AddRange([firstResponse, secondResponse, thirdResponse]); |
| 42 | + var options = new AzureOpenAIClientOptions() |
| 43 | + { |
| 44 | + Transport = new HttpClientPipelineTransport(this._httpClient), |
| 45 | + RetryPolicy = new ClientRetryPolicy(2), |
| 46 | + NetworkTimeout = TimeSpan.FromSeconds(10), |
| 47 | + }; |
| 48 | + |
| 49 | + // Bug fix workaround |
| 50 | + options.AddPolicy(new SingleAuthorizationHeaderPolicy(), PipelinePosition.PerTry); |
| 51 | + |
| 52 | + var azureClient = new AzureOpenAIClient( |
| 53 | + endpoint: new Uri("http://any"), |
| 54 | + credential: new TestJWTBearerTokenCredential(), |
| 55 | + options: options); |
| 56 | + |
| 57 | + var clientCore = new AzureClientCore("deployment-name", azureClient); |
| 58 | + |
| 59 | + ChatHistory chatHistory = []; |
| 60 | + chatHistory.AddUserMessage("User test"); |
| 61 | + |
| 62 | + // Act |
| 63 | + var exception = await Record.ExceptionAsync(() => clientCore.GetChatMessageContentsAsync("model-id", chatHistory, null, null, CancellationToken.None)); |
| 64 | + |
| 65 | + // Assert |
| 66 | + Assert.NotNull(exception); |
| 67 | + Assert.Equal(3, this._multiHttpMessageHandlerStub.RequestHeaders.Count); |
| 68 | + |
| 69 | + foreach (var requestHeaders in this._multiHttpMessageHandlerStub.RequestHeaders) |
| 70 | + { |
| 71 | + this._multiHttpMessageHandlerStub.RequestHeaders[2]!.TryGetValues("Authorization", out var authHeaders); |
| 72 | + Assert.NotNull(authHeaders); |
| 73 | + Assert.Single(authHeaders); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private sealed class TestJWTBearerTokenCredential : TokenCredential |
| 78 | + { |
| 79 | + public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) |
| 80 | + { |
| 81 | + return new AccessToken("JWT", DateTimeOffset.Now.AddHours(1)); |
| 82 | + } |
| 83 | + |
| 84 | + public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) |
| 85 | + { |
| 86 | + return ValueTask.FromResult(new AccessToken("JWT", DateTimeOffset.Now.AddHours(1))); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments