From ee8d7147229bc0f456eaaa8a43ae670707ff60d4 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Mon, 15 Jul 2024 03:04:32 +0100 Subject: [PATCH] Fixed tests, as we were failing many --- .../ServiceCollectionExtensions.cs | 7 +++-- src/PinguApps.Appwrite.Playground/Program.cs | 4 +-- .../ServiceCollectionExtensionsTests.cs | 28 +++++++++---------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs b/src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs index fa6b34e2..d83ece5b 100644 --- a/src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs +++ b/src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs @@ -52,9 +52,12 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect services.AddRefitClient(refitSettings) .ConfigureHttpClient(x => x.BaseAddress = new Uri(endpoint)) .AddHttpMessageHandler() - .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + .ConfigurePrimaryHttpMessageHandler((handler, sp) => { - UseCookies = false + if (handler is HttpClientHandler clientHandler) + { + clientHandler.UseCookies = false; + } }); services.AddSingleton(); diff --git a/src/PinguApps.Appwrite.Playground/Program.cs b/src/PinguApps.Appwrite.Playground/Program.cs index 2f280220..07824411 100644 --- a/src/PinguApps.Appwrite.Playground/Program.cs +++ b/src/PinguApps.Appwrite.Playground/Program.cs @@ -7,8 +7,8 @@ var builder = Host.CreateApplicationBuilder(args); -builder.Services.AddAppwriteClient(builder.Configuration.GetValue("ProjectId")!); -//builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue("ProjectId")!); +//builder.Services.AddAppwriteClient(builder.Configuration.GetValue("ProjectId")!); +builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue("ProjectId")!); builder.Services.AddAppwriteServer(builder.Configuration.GetValue("ProjectId")!, builder.Configuration.GetValue("ApiKey")!); builder.Services.AddSingleton(); diff --git a/tests/PinguApps.Appwrite.Client.Tests/ServiceCollectionExtensionsTests.cs b/tests/PinguApps.Appwrite.Client.Tests/ServiceCollectionExtensionsTests.cs index 1f93b2cb..f1d65270 100644 --- a/tests/PinguApps.Appwrite.Client.Tests/ServiceCollectionExtensionsTests.cs +++ b/tests/PinguApps.Appwrite.Client.Tests/ServiceCollectionExtensionsTests.cs @@ -18,21 +18,26 @@ public void AddAppwriteClient_RegistersExpectedServices() // Assert var provider = services.BuildServiceProvider(); - // Assert HeaderHandler is registered var headerHandler = provider.GetService(); Assert.NotNull(headerHandler); - // Assert IAccountApi is registered and configured + var clientCookieSessionHandler = provider.GetService(); + Assert.NotNull(clientCookieSessionHandler); + var accountApi = provider.GetService(); Assert.NotNull(accountApi); - // Assert services are registered Assert.NotNull(provider.GetService()); Assert.NotNull(provider.GetService()); + + var lazyClient = provider.GetService>(); + Assert.NotNull(lazyClient); + var client = lazyClient.Value; + Assert.NotNull(client); } [Fact] - public void AddAppwriteClientForServer_RegistersExpectedServicesWithTransientLifetime() + public void AddAppwriteClientForServer_RegistersExpectedServices() { // Arrange var services = new ServiceCollection(); @@ -43,21 +48,16 @@ public void AddAppwriteClientForServer_RegistersExpectedServicesWithTransientLif // Assert var provider = services.BuildServiceProvider(); - // Assert HeaderHandler is registered var headerHandler = provider.GetService(); Assert.NotNull(headerHandler); - // Assert IAccountApi is registered and configured + var clientCookieSessionHandler = provider.GetService(); + Assert.Null(clientCookieSessionHandler); + var accountApi = provider.GetService(); Assert.NotNull(accountApi); - // Assert services are registered with Transient lifetime - var accountClientServiceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IAccountClient)); - Assert.NotNull(accountClientServiceDescriptor); - Assert.Equal(ServiceLifetime.Transient, accountClientServiceDescriptor.Lifetime); - - var appwriteClientServiceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IAppwriteClient)); - Assert.NotNull(appwriteClientServiceDescriptor); - Assert.Equal(ServiceLifetime.Transient, appwriteClientServiceDescriptor.Lifetime); + Assert.NotNull(provider.GetService()); + Assert.NotNull(provider.GetService()); } }