-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c449d7
commit a9c77e4
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SampleLoggingHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SquidexLoggingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |