Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Nov 14, 2024
1 parent 34742b5 commit b00fe7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
4 changes: 1 addition & 3 deletions OpenAI-DotNet-Tests/AbstractTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using NUnit.Framework;
using System;
using System.IO;
using System.Net.Http;
Expand Down Expand Up @@ -36,14 +35,13 @@ protected AbstractTestFixture()
});
var settings = new OpenAIClientSettings(domain: HttpClient.BaseAddress?.Authority);
var auth = new OpenAIAuthentication(TestUserToken);
HttpClient.Timeout = TimeSpan.FromMinutes(3);
OpenAIClient = new OpenAIClient(auth, settings, HttpClient)
{
EnableDebug = true
};
}

private Uri GetBaseAddressFromLaunchSettings()
private static Uri GetBaseAddressFromLaunchSettings()
{
var projectDir = Directory.GetCurrentDirectory();
var launchSettings = Path.Combine(projectDir, "Properties", "launchSettings.json");
Expand Down
39 changes: 30 additions & 9 deletions OpenAI-DotNet-Tests/TestFixture_00_01_Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using NUnit.Framework;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;

namespace OpenAI.Tests
Expand Down Expand Up @@ -68,28 +69,48 @@ public async Task Test_02_Client_Authenticated()
[Test]
public async Task Test_03_Client_Unauthenticated()
{
var webApplicationFactory = new TestProxyFactory();
var httpClient = webApplicationFactory.CreateClient(new WebApplicationFactoryClientOptions
{
BaseAddress = new("https://localhost:7777")
});
var settings = new OpenAIClientSettings(domain: httpClient.BaseAddress?.Authority);
var settings = new OpenAIClientSettings(domain: HttpClient.BaseAddress?.Authority);
var auth = new OpenAIAuthentication("sess-invalid-token");
var openAIClient = new OpenAIClient(auth, settings, httpClient);
var openAIClient = new OpenAIClient(auth, settings, HttpClient);

try
{
await openAIClient.ModelsEndpoint.GetModelsAsync();
}
catch (HttpRequestException httpRequestException)
{
Console.WriteLine(httpRequestException);
// System.Net.Http.HttpRequestException : GetModelsAsync Failed! HTTP status code: Unauthorized | Response body: User is not authorized
Assert.IsTrue(httpRequestException.StatusCode == HttpStatusCode.Unauthorized);
Assert.AreEqual(HttpStatusCode.Unauthorized, httpRequestException.StatusCode);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

[Test]
public async Task Test_04_Client_Websocket_Authentication()
{
using var websocket = new ClientWebSocket();

foreach (var (key, value) in OpenAIClient.WebsocketHeaders)
{
websocket.Options.SetRequestHeader(key, value);
}

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var realtimeUri = new Uri(string.Format(OpenAIClient.OpenAIClientSettings.BaseWebSocketUrlFormat, "/realtime"));
_ = websocket.ConnectAsync(realtimeUri, cts.Token);

while (websocket.State != WebSocketState.Open)
{
cts.Token.ThrowIfCancellationRequested();
await Task.Yield();
}

await Task.Delay(1000, cts.Token);
await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, cts.Token);
}
}
}

0 comments on commit b00fe7d

Please sign in to comment.