Skip to content

Commit 2f29a5e

Browse files
authored
Add more info to UnrecognizedMediaType (#43100)
* Add more info to UnrecognizedMediaType * Update HttpLoggingMiddleware.cs * Update HttpLoggingMiddlewareTests.cs * Update HttpLoggingExtensions.cs * Update HttpLoggingMiddlewareTests.cs * Update BufferingStream.cs * Fixup * Feedback, add test
1 parent 579a254 commit 2f29a5e

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/Middleware/HttpLogging/src/BufferingStream.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public string GetString(Encoding? encoding)
5959

6060
if (encoding == null)
6161
{
62-
_logger.UnrecognizedMediaType();
62+
// This method is used only for the response body
63+
_logger.UnrecognizedMediaType("response");
6364
return "";
6465
}
6566

src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public static void ResponseLog(this ILogger logger, HttpResponseLog responseLog)
2929
[LoggerMessage(5, LogLevel.Debug, "Decode failure while converting body.", EventName = "DecodeFailure")]
3030
public static partial void DecodeFailure(this ILogger logger, Exception ex);
3131

32-
[LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for body.", EventName = "UnrecognizedMediaType")]
33-
public static partial void UnrecognizedMediaType(this ILogger logger);
32+
[LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for {Name} body.", EventName = "UnrecognizedMediaType")]
33+
public static partial void UnrecognizedMediaType(this ILogger logger, string name);
34+
35+
[LoggerMessage(7, LogLevel.Debug, "No Content-Type header for {Name} body.", EventName = "NoMediaType")]
36+
public static partial void NoMediaType(this ILogger logger, string name);
3437
}

src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ private async Task InvokeInternal(HttpContext context)
105105

106106
if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestBody))
107107
{
108-
if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType,
108+
if (request.ContentType is null)
109+
{
110+
_logger.NoMediaType("request");
111+
}
112+
else if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType,
109113
options.MediaTypeOptions.MediaTypeStates,
110114
out var encoding))
111115
{
@@ -119,7 +123,7 @@ private async Task InvokeInternal(HttpContext context)
119123
}
120124
else
121125
{
122-
_logger.UnrecognizedMediaType();
126+
_logger.UnrecognizedMediaType("request");
123127
}
124128
}
125129

src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public async Task RejectedContentTypes(string contentType)
511511
await middleware.Invoke(httpContext);
512512

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

517517
[Fact]
@@ -836,7 +836,38 @@ public async Task UnrecognizedMediaType()
836836

837837
await middleware.Invoke(httpContext);
838838

839-
Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for body."));
839+
Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for response body."));
840+
}
841+
842+
[Fact]
843+
public async Task NoMediaType()
844+
{
845+
var options = CreateOptionsAccessor();
846+
options.CurrentValue.LoggingFields = HttpLoggingFields.RequestBody;
847+
var middleware = new HttpLoggingMiddleware(
848+
async c =>
849+
{
850+
c.Request.ContentType = null;
851+
var arr = new byte[4096];
852+
while (true)
853+
{
854+
var res = await c.Request.Body.ReadAsync(arr);
855+
if (res == 0)
856+
{
857+
break;
858+
}
859+
}
860+
},
861+
options,
862+
LoggerFactory.CreateLogger<HttpLoggingMiddleware>());
863+
864+
var httpContext = new DefaultHttpContext();
865+
866+
httpContext.Request.Headers["foo"] = "bar";
867+
868+
await middleware.Invoke(httpContext);
869+
870+
Assert.Contains(TestSink.Writes, w => w.Message.Contains("No Content-Type header for request body."));
840871
}
841872

842873
[Fact]

0 commit comments

Comments
 (0)