Skip to content

Commit

Permalink
Sample logging handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jul 17, 2023
1 parent 7c449d7 commit a9c77e4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ISquidexClient>();

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;
}
}
}

0 comments on commit a9c77e4

Please sign in to comment.