-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test:
BodyLoggerMiddleware_Should_Redact_Password
.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
using Microsoft.ApplicationInsights.DataContracts; | ||
using Moq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Collections.Generic; | ||
|
||
namespace ApplicationInsightsRequestLoggingTests | ||
{ | ||
|
@@ -152,6 +153,48 @@ public async void BodyLoggerMiddleware_Should_Leave_Body_intact() | |
body.Should().Be("Hello from client"); | ||
} | ||
|
||
[Fact] | ||
public async void BodyLoggerMiddleware_Should_Redact_Password() | ||
{ | ||
// Arrange | ||
var telemetryWriter = new Mock<ITelemetryWriter>(); | ||
|
||
using var host = await new HostBuilder() | ||
.ConfigureWebHost(webBuilder => | ||
{ | ||
webBuilder | ||
.UseTestServer() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddTransient<IBodyReader, BodyReader>(); | ||
services.AddTransient<ISensitiveDataFilter>(provider => | ||
{ | ||
return new SensitiveDataFilter(new List<string>() { "password" }, new List<string>()); | ||
}); | ||
services.AddSingleton(telemetryWriter.Object); | ||
services.AddTransient<BodyLoggerMiddleware>(); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseMiddleware<BodyLoggerMiddleware>(); | ||
app.Run(async context => | ||
{ | ||
// Send request body back in response body | ||
context.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
await context.Request.Body.CopyToAsync(context.Response.Body); | ||
}); | ||
}); | ||
}) | ||
.StartAsync(); | ||
|
||
// Act | ||
var response = await host.GetTestClient().PostAsync("/", new StringContent("{\"email\":\"[email protected]\",\"password\":\"P@ssw0rd!\"}")); | ||
|
||
// Assert | ||
telemetryWriter.Verify(x => x.Write(It.IsAny<HttpContext>(), "RequestBody", "{\"email\":\"[email protected]\",\"password\":\"***MASKED***\"}"), Times.Once); | ||
telemetryWriter.Verify(x => x.Write(It.IsAny<HttpContext>(), "ResponseBody", "{\"email\":\"[email protected]\",\"password\":\"***MASKED***\"}"), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void BodyLoggerMiddleware_Should_Properly_Pass() | ||
{ | ||
|