Skip to content

Commit

Permalink
Add test for logging from custom request action
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Oct 16, 2024
1 parent 5137ec9 commit 84b055c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion test/KubeClient.Tests/Logging/TestLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Net.Http;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -104,5 +105,5 @@ public void Log<TState>(LogLevel level, EventId eventId, TState state, Exception
/// An <see cref="IDisposable"/> that ends the logical operation scope when disposed.
/// </returns>
public IDisposable BeginScope<TState>(TState state) => Disposable.Empty;
}
}
}
56 changes: 53 additions & 3 deletions test/KubeClient.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
using HTTPlease.Diagnostics;
using HTTPlease.Formatters;
using HTTPlease.Testability;
using HTTPlease.Testability.Mocks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Xunit;
using Newtonsoft.Json;

namespace KubeClient.Tests
{
using Logging;
using Models;
using Utilities;

/// <summary>
/// Tests for <see cref="KubeApiClient"/> logging.
Expand Down Expand Up @@ -50,6 +50,7 @@ public async Task PodsV1_GetByName_NotFound()
return request.CreateResponse(HttpStatusCode.NotFound,
responseBody: JsonConvert.SerializeObject(new StatusV1
{
Status = "Failure",
Reason = "NotFound"
}),
WellKnownMediaTypes.Json
Expand Down Expand Up @@ -165,5 +166,54 @@ public async Task PodsV1_GetByName_OK()
logEntry2.Properties["StatusCode"]
);
}

/// <summary>
/// Verify that the client's logger emits the correct log entry from a custom request action.
/// </summary>
[Fact(DisplayName = "Emit log entry from custom request action")]
public async Task Custom_Request_Action()
{
var logEntries = new List<LogEntry>();

TestLogger logger = new TestLogger(LogLevel.Information);
logger.LogEntries.Subscribe(
logEntry => logEntries.Add(logEntry)
);

ClientBuilder clientBuilder = new ClientBuilder()
.WithLogging(logger);

HttpClient httpClient = clientBuilder.CreateClient("http://localhost:1234/api", TestHandlers.RespondWith(request =>
{
return request.CreateResponse(HttpStatusCode.OK,
responseBody: JsonConvert.SerializeObject(new StatusV1
{
Status = "Success",
}),
WellKnownMediaTypes.Json
);
}));

HttpRequest request = HttpRequest.Create("{Foo}/{Bar}?Baz={Baz}")
.WithRequestAction((HttpRequestMessage requestMessage) =>
{
logger.LogDebug("Start streaming {RequestMethod} request for {RequestUri}...", HttpMethod.Get, requestMessage.RequestUri.SafeGetPathAndQuery());
}).WithTemplateParameters(new
{
Foo = "foo",
Bar = "bAr",
Baz = "b4z",
});

using (HttpResponseMessage response = await httpClient.GetAsync(request))
{
// [0] = Custom message, [1] = Begin request, [2] End request
Assert.Equal(3, logEntries.Count);

Assert.Equal("Start streaming GET request for /api/foo/bAr?Baz=b4z...", logEntries[0].Message);

response.EnsureSuccessStatusCode();
}
}
}
}

0 comments on commit 84b055c

Please sign in to comment.