8000 Print the Content-Length header only if it actually exists by 0xced · Pull Request #122 · adrianiftode/FluentAssertions.Web · GitHub
[go: up one dir, main page]

Skip to content

Print the Content-Length header only if it actually exists #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/FluentAssertions.Web/HttpResponseMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ private static void AppendProtocolAndStatusCode(StringBuilder messageBuilder, Ht

private static void AppendContentLength(StringBuilder messageBuilder, HttpContent content)
{
content.TryGetContentLength(out var length);
messageBuilder.AppendLine($"Content-Length: {length}");
if (content.Headers.TryGetValues("Content-Length", out var values))
{
foreach (var value in values)
{
messageBuilder.AppendLine($"Content-Length: {value}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace FluentAssertions.Web.Tests;
public class HttpResponseMessageFormatterSpecs
{
[Fact]
public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero()
public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveNoContentLength()
{
// Arrange
var formattedGraph = new FormattedObjectGraph(maxLines: 100);
Expand All @@ -21,7 +21,6 @@ public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero
*
The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
The originating HTTP request was <null>.*
""");
}
Expand Down Expand Up @@ -59,7 +58,6 @@ public void GivenHeaders_ShouldPrintAllHeaders()
Date: Thu, 26 Sep 2019 22:33:34 GMT*
Connection: close*
Content-Type: text/html; charset=utf-8*
Content-Length: 0*
The originating HTTP request was <null>.*
""");
}
Expand Down Expand Up @@ -137,7 +135,6 @@ public void GivenResponseWithContent_ShouldPrintContent()
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: application/json; charset=utf-8*
Content-Length:*
{*
"glossary": {*
"title": "example glossary",*
Expand Down Expand Up @@ -214,7 +211,6 @@ public void GivenResponseWithMinifiedJson_ShouldPrintFormattedJson()
The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: application/json; charset=utf-8*
Content-Length:*
{*
"glossary": {*
"title": "example glossary",*
Expand Down Expand Up @@ -277,7 +273,6 @@ The content of the document......
The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: text/html; charset=utf-8*
Content-Length:*
<html>*
<head>*
<title>Title of the document</title>*
Expand Down Expand Up @@ -324,9 +319,10 @@ public void GivenRequest_ShouldPrintRequestDetails()
var formattedGraph = new FormattedObjectGraph(maxLines: 100);
using var subject = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("OK") { Headers = { ContentLength = 2 } },
RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5001/")
{
Content = new StringContent("Some content."),
Content = new StringContent("Some content.") { Headers = { ContentLength = 13 } },
Headers = { { "Authorization", "Bearer xyz" } }
}
};
Expand All @@ -341,12 +337,12 @@ public void GivenRequest_ShouldPrintRequestDetails()
"""
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
Content-Length: 2*
The originating HTTP request was:*
POST http://localhost:5001/ HTTP*
Authorization: Bearer xyz*
Content-Type: text/plain; charset=utf-8*
Content-Length: *
Content-Length: 13*
Some content.*
""");
}
Expand Down Expand Up @@ -377,12 +373,10 @@ public void GivenRequest_WhenRequestStreamAtTheEnd_ShouldPrintRequestDetails()
"""
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
The originating HTTP request was:*
POST http://localhost:5001/ HTTP*
Authorization: Bearer xyz*
Content-Type: text/plain; charset=utf-8*
Content-Length: *
Some content.*
""");
}
Expand All @@ -408,7 +402,7 @@ public void GivenResponseWithNoContentType_ShouldPrint()
"""
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*HTTP request*<null>*
*HTTP request*<null>*
""");
}

Expand Down Expand Up @@ -1434,7 +1428,6 @@ public void GivenLargeNonPrettifiedJson_ShouldPrintPrettified()
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: application/json; charset=utf-8*
Content-Length:*
*Content is too large to display and only a part is printed*
{*
"glossary": {*
Expand Down Expand Up @@ -1469,7 +1462,6 @@ public void GivenSyntacticallyMalformedNonPrettifiedJson_ShouldPrintPrettified()
*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: application/json; charset=utf-8*
Content-Length:*
{*
"glossary": {*
"title":*
Expand Down
0