From a9c77e45aae04badab72c9b5ad85aec593096e12 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 17 Jul 2023 07:56:50 +0200 Subject: [PATCH] Sample logging handler. --- .../SampleLoggingHandler.cs | 28 ++++++ .../SquidexLoggingTests.cs | 94 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SampleLoggingHandler.cs create mode 100644 csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SquidexLoggingTests.cs diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SampleLoggingHandler.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SampleLoggingHandler.cs new file mode 100644 index 00000000..780e560e --- /dev/null +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SampleLoggingHandler.cs @@ -0,0 +1,28 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System.Collections.Concurrent; +using System.Net; + +namespace Squidex.ClientLibrary.Tests; + +internal sealed class SampleLoggingHandler : DelegatingHandler +{ + public ConcurrentBag<(string Url, bool IsAuthorized, HttpStatusCode StatusCode)> Log { get; } = new (); + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var response = await base.SendAsync(request, cancellationToken); + + Log.Add(( + request.RequestUri?.ToString() ?? string.Empty, + request.Headers.Contains("Authorization"), + response.StatusCode)); + + return response; + } +} diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SquidexLoggingTests.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SquidexLoggingTests.cs new file mode 100644 index 00000000..b8ebf1a4 --- /dev/null +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SquidexLoggingTests.cs @@ -0,0 +1,94 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Microsoft.Extensions.DependencyInjection; +using Squidex.ClientLibrary.Configuration; +using Xunit; + +namespace Squidex.ClientLibrary.Tests; + +public class SquidexLoggingTests +{ + [Fact] + public async Task Should_log_with_services() + { + var loggingHandler = new SampleLoggingHandler(); + + var sut = + new ServiceCollection() + .AddSquidexClient(options => + { + options.AppName = "invalid"; + options.ClientId = "invalid"; + options.ClientSecret = "invalid"; + }) + .AddSquidexHttpClient() + .AddHttpMessageHandler(() => loggingHandler) + .Services + .BuildServiceProvider() + .GetRequiredService(); + + try + { + await sut.Ping.GetAppPingAsync(); + } + catch + { + // Invalid Credentials + } + + Assert.NotEmpty(loggingHandler.Log); + Assert.Contains(loggingHandler.Log, x => x.Url.Contains("identity-server/connect/token", StringComparison.Ordinal)); + } + + [Fact] + public async Task Should_log_with_manual_client() + { + var loggingHandler = new SampleLoggingHandler(); + + var options = new SquidexOptions + { + AppName = "invalid", + ClientId = "invalid", + ClientSecret = "invalid" + }; + + options.ClientProvider = new ClientProvider(options, loggingHandler); + + var sut = new SquidexClient(options); + + try + { + await sut.Ping.GetAppPingAsync(); + } + catch + { + // Invalid Credentials + } + + Assert.NotEmpty(loggingHandler.Log); + Assert.Contains(loggingHandler.Log, x => x.Url.Contains("identity-server/connect/token", StringComparison.Ordinal)); + } + + private class ClientProvider : StaticHttpClientProvider + { + private readonly SampleLoggingHandler sampleLoggingHandler; + + public ClientProvider(SquidexOptions options, SampleLoggingHandler sampleLoggingHandler) + : base(options) + { + this.sampleLoggingHandler = sampleLoggingHandler; + } + + protected override HttpMessageHandler CreateMessageHandler(SquidexOptions options) + { + sampleLoggingHandler.InnerHandler = base.CreateMessageHandler(options); + + return sampleLoggingHandler; + } + } +}