Skip to content

Commit 5036af7

Browse files
authored
Merge pull request #123 from 0xced/Fix-NullReferenceException
Fix NullReferenceException
2 parents 9fe220f + 1872bf4 commit 5036af7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/FluentAssertions.Web/HttpResponseMessageFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ private static void AppendProtocolAndStatusCode(StringBuilder messageBuilder, Ht
105105
messageBuilder.AppendLine($@"HTTP/{response.Version} {(int)response.StatusCode} {response.StatusCode}");
106106
}
107107

108-
private static void AppendContentLength(StringBuilder messageBuilder, HttpContent content)
108+
private static void AppendContentLength(StringBuilder messageBuilder, HttpContent? content)
109109
{
110-
if (content.Headers.TryGetValues("Content-Length", out var values))
110+
if (content != null && content.Headers.TryGetValues("Content-Length", out var values))
111111
{
112112
foreach (var value in values)
113113
{

test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,25 @@ The originating HTTP request was <null>.*
312312
""");
313313
}
314314

315+
[Fact]
316+
public void GivenRequestWithoutContent_ShouldNotThrowAnyException()
317+
{
318+
// Arrange
319+
var formattedGraph = new FormattedObjectGraph(maxLines: 100);
320+
using var subject = new HttpResponseMessage(HttpStatusCode.OK)
321+
{
322+
RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5001/"),
323+
};
324+
var sut = new HttpResponseMessageFormatter();
325+
326+
// Act
327+
sut.Format(subject, formattedGraph, null!, null!);
328+
329+
// Assert
330+
var formatted = formattedGraph.ToString();
331+
formatted.Should().NotContain("An exception occurred");
332+
}
333+
315334
[Fact]
316335
public void GivenRequest_ShouldPrintRequestDetails()
317336
{

0 commit comments

Comments
 (0)