Skip to content

Commit a9c77e4

Browse files
Sample logging handler.
1 parent 7c449d7 commit a9c77e4

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 System.Collections.Concurrent;
9+
using System.Net;
10+
11+
namespace Squidex.ClientLibrary.Tests;
12+
13+
internal sealed class SampleLoggingHandler : DelegatingHandler
14+
{
15+
public ConcurrentBag<(string Url, bool IsAuthorized, HttpStatusCode StatusCode)> Log { get; } = new ();
16+
17+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
18+
{
19+
var response = await base.SendAsync(request, cancellationToken);
20+
21+
Log.Add((
22+
request.RequestUri?.ToString() ?? string.Empty,
23+
request.Headers.Contains("Authorization"),
24+
response.StatusCode));
25+
26+
return response;
27+
}
28+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)