|
| 1 | +// ========================================================================== |
| 2 | +// Squidex Headless CMS |
| 3 | +// ========================================================================== |
| 4 | +// Copyright (c) Squidex UG (haftungsbeschraenkt) |
| 5 | +// All rights reserved. Licensed under the MIT license. |
| 6 | +// ========================================================================== |
| 7 | + |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Squidex.ClientLibrary.Configuration; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Squidex.ClientLibrary.Tests; |
| 13 | + |
| 14 | +public class SquidexLoggingTests |
| 15 | +{ |
| 16 | + [Fact] |
| 17 | + public async Task Should_log_with_services() |
| 18 | + { |
| 19 | + var loggingHandler = new SampleLoggingHandler(); |
| 20 | + |
| 21 | + var sut = |
| 22 | + new ServiceCollection() |
| 23 | + .AddSquidexClient(options => |
| 24 | + { |
| 25 | + options.AppName = "invalid"; |
| 26 | + options.ClientId = "invalid"; |
| 27 | + options.ClientSecret = "invalid"; |
| 28 | + }) |
| 29 | + .AddSquidexHttpClient() |
| 30 | + .AddHttpMessageHandler(() => loggingHandler) |
| 31 | + .Services |
| 32 | + .BuildServiceProvider() |
| 33 | + .GetRequiredService<ISquidexClient>(); |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + await sut.Ping.GetAppPingAsync(); |
| 38 | + } |
| 39 | + catch |
| 40 | + { |
| 41 | + // Invalid Credentials |
| 42 | + } |
| 43 | + |
| 44 | + Assert.NotEmpty(loggingHandler.Log); |
| 45 | + Assert.Contains(loggingHandler.Log, x => x.Url.Contains("identity-server/connect/token", StringComparison.Ordinal)); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public async Task Should_log_with_manual_client() |
| 50 | + { |
| 51 | + var loggingHandler = new SampleLoggingHandler(); |
| 52 | + |
| 53 | + var options = new SquidexOptions |
| 54 | + { |
| 55 | + AppName = "invalid", |
| 56 | + ClientId = "invalid", |
| 57 | + ClientSecret = "invalid" |
| 58 | + }; |
| 59 | + |
| 60 | + options.ClientProvider = new ClientProvider(options, loggingHandler); |
| 61 | + |
| 62 | + var sut = new SquidexClient(options); |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + await sut.Ping.GetAppPingAsync(); |
| 67 | + } |
| 68 | + catch |
| 69 | + { |
| 70 | + // Invalid Credentials |
| 71 | + } |
| 72 | + |
| 73 | + Assert.NotEmpty(loggingHandler.Log); |
| 74 | + Assert.Contains(loggingHandler.Log, x => x.Url.Contains("identity-server/connect/token", StringComparison.Ordinal)); |
| 75 | + } |
| 76 | + |
| 77 | + private class ClientProvider : StaticHttpClientProvider |
| 78 | + { |
| 79 | + private readonly SampleLoggingHandler sampleLoggingHandler; |
| 80 | + |
| 81 | + public ClientProvider(SquidexOptions options, SampleLoggingHandler sampleLoggingHandler) |
| 82 | + : base(options) |
| 83 | + { |
| 84 | + this.sampleLoggingHandler = sampleLoggingHandler; |
| 85 | + } |
| 86 | + |
| 87 | + protected override HttpMessageHandler CreateMessageHandler(SquidexOptions options) |
| 88 | + { |
| 89 | + sampleLoggingHandler.InnerHandler = base.CreateMessageHandler(options); |
| 90 | + |
| 91 | + return sampleLoggingHandler; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments