Skip to content

Commit

Permalink
Add more info to UnrecognizedMediaType (#43100)
Browse files Browse the repository at this point in the history
* Add more info to UnrecognizedMediaType

* Update HttpLoggingMiddleware.cs

* Update HttpLoggingMiddlewareTests.cs

* Update HttpLoggingExtensions.cs

* Update HttpLoggingMiddlewareTests.cs

* Update BufferingStream.cs

* Fixup

* Feedback, add test
  • Loading branch information
wtgodbe authored Aug 10, 2022
1 parent 579a254 commit 2f29a5e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Middleware/HttpLogging/src/BufferingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public string GetString(Encoding? encoding)

if (encoding == null)
{
_logger.UnrecognizedMediaType();
// This method is used only for the response body
_logger.UnrecognizedMediaType("response");
return "";
}

Expand Down
7 changes: 5 additions & 2 deletions src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public static void ResponseLog(this ILogger logger, HttpResponseLog responseLog)
[LoggerMessage(5, LogLevel.Debug, "Decode failure while converting body.", EventName = "DecodeFailure")]
public static partial void DecodeFailure(this ILogger logger, Exception ex);

[LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for body.", EventName = "UnrecognizedMediaType")]
public static partial void UnrecognizedMediaType(this ILogger logger);
[LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for {Name} body.", EventName = "UnrecognizedMediaType")]
public static partial void UnrecognizedMediaType(this ILogger logger, string name);

[LoggerMessage(7, LogLevel.Debug, "No Content-Type header for {Name} body.", EventName = "NoMediaType")]
public static partial void NoMediaType(this ILogger logger, string name);
}
8 changes: 6 additions & 2 deletions src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ private async Task InvokeInternal(HttpContext context)

if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestBody))
{
if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType,
if (request.ContentType is null)
{
_logger.NoMediaType("request");
}
else if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType,
options.MediaTypeOptions.MediaTypeStates,
out var encoding))
{
Expand All @@ -119,7 +123,7 @@ private async Task InvokeInternal(HttpContext context)
}
else
{
_logger.UnrecognizedMediaType();
_logger.UnrecognizedMediaType("request");
}
}

Expand Down
35 changes: 33 additions & 2 deletions src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public async Task RejectedContentTypes(string contentType)
await middleware.Invoke(httpContext);

Assert.DoesNotContain(TestSink.Writes, w => w.Message.Contains(expected));
Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for body."));
Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for request body."));
}

[Fact]
Expand Down Expand Up @@ -836,7 +836,38 @@ public async Task UnrecognizedMediaType()

await middleware.Invoke(httpContext);

Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for body."));
Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for response body."));
}

[Fact]
public async Task NoMediaType()
{
var options = CreateOptionsAccessor();
options.CurrentValue.LoggingFields = HttpLoggingFields.RequestBody;
var middleware = new HttpLoggingMiddleware(
async c =>
{
c.Request.ContentType = null;
var arr = new byte[4096];
while (true)
{
var res = await c.Request.Body.ReadAsync(arr);
if (res == 0)
{
break;
}
}
},
options,
LoggerFactory.CreateLogger<HttpLoggingMiddleware>());

var httpContext = new DefaultHttpContext();

httpContext.Request.Headers["foo"] = "bar";

await middleware.Invoke(httpContext);

Assert.Contains(TestSink.Writes, w => w.Message.Contains("No Content-Type header for request body."));
}

[Fact]
Expand Down

0 comments on commit 2f29a5e

Please sign in to comment.