Skip to content

Commit 8dd989b

Browse files
authored
Merge pull request #122 from 0xced/ContentLength
Print the Content-Length header only if it actually exists
2 parents a5423d4 + f4cfe55 commit 8dd989b

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/FluentAssertions.Web/HttpResponseMessageFormatter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ private static void AppendProtocolAndStatusCode(StringBuilder messageBuilder, Ht
107107

108108
private static void AppendContentLength(StringBuilder messageBuilder, HttpContent content)
109109
{
110-
content.TryGetContentLength(out var length);
111-
messageBuilder.AppendLine($"Content-Length: {length}");
110+
if (content.Headers.TryGetValues("Content-Length", out var values))
111+
{
112+
foreach (var value in values)
113+
{
114+
messageBuilder.AppendLine($"Content-Length: {value}");
115+
}
116+
}
112117
}
113118
}

test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace FluentAssertions.Web.Tests;
55
public class HttpResponseMessageFormatterSpecs
66
{
77
[Fact]
8-
public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero()
8+
public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveNoContentLength()
99
{
1010
// Arrange
1111
var formattedGraph = new FormattedObjectGraph(maxLines: 100);
@@ -21,7 +21,6 @@ public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero
2121
*
2222
The HTTP response was:*
2323
HTTP/1.1 200 OK*
24-
Content-Length: 0*
2524
The originating HTTP request was <null>.*
2625
""");
2726
}
@@ -59,7 +58,6 @@ public void GivenHeaders_ShouldPrintAllHeaders()
5958
Date: Thu, 26 Sep 2019 22:33:34 GMT*
6059
Connection: close*
6160
Content-Type: text/html; charset=utf-8*
62-
Content-Length: 0*
6361
The originating HTTP request was <null>.*
6462
""");
6563
}
@@ -137,7 +135,6 @@ public void GivenResponseWithContent_ShouldPrintContent()
137135
*The HTTP response was:*
138136
HTTP/1.1 200 OK*
139137
Content-Type: application/json; charset=utf-8*
140-
Content-Length:*
141138
{*
142139
"glossary": {*
143140
"title": "example glossary",*
@@ -214,7 +211,6 @@ public void GivenResponseWithMinifiedJson_ShouldPrintFormattedJson()
214211
The HTTP response was:*
215212
HTTP/1.1 200 OK*
216213
Content-Type: application/json; charset=utf-8*
217-
Content-Length:*
218214
{*
219215
"glossary": {*
220216
"title": "example glossary",*
@@ -277,7 +273,6 @@ The content of the document......
277273
The HTTP response was:*
278274
HTTP/1.1 200 OK*
279275
Content-Type: text/html; charset=utf-8*
280-
Content-Length:*
281276
<html>*
282277
<head>*
283278
<title>Title of the document</title>*
@@ -324,9 +319,10 @@ public void GivenRequest_ShouldPrintRequestDetails()
324319
var formattedGraph = new FormattedObjectGraph(maxLines: 100);
325320
using var subject = new HttpResponseMessage(HttpStatusCode.OK)
326321
{
322+
Content = new StringContent("OK") { Headers = { ContentLength = 2 } },
327323
RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:5001/")
328324
{
329-
Content = new StringContent("Some content."),
325+
Content = new StringContent("Some content.") { Headers = { ContentLength = 13 } },
330326
Headers = { { "Authorization", "Bearer xyz" } }
331327
}
332328
};
@@ -341,12 +337,12 @@ public void GivenRequest_ShouldPrintRequestDetails()
341337
"""
342338
*The HTTP response was:*
343339
HTTP/1.1 200 OK*
344-
Content-Length: 0*
340+
Content-Length: 2*
345341
The originating HTTP request was:*
346342
POST http://localhost:5001/ HTTP*
347343
Authorization: Bearer xyz*
348344
Content-Type: text/plain; charset=utf-8*
349-
Content-Length: *
345+
Content-Length: 13*
350346
Some content.*
351347
""");
352348
}
@@ -377,12 +373,10 @@ public void GivenRequest_WhenRequestStreamAtTheEnd_ShouldPrintRequestDetails()
377373
"""
378374
*The HTTP response was:*
379375
HTTP/1.1 200 OK*
380-
Content-Length: 0*
381376
The originating HTTP request was:*
382377
POST http://localhost:5001/ HTTP*
383378
Authorization: Bearer xyz*
384379
Content-Type: text/plain; charset=utf-8*
385-
Content-Length: *
386380
Some content.*
387381
""");
388382
}
@@ -408,7 +402,7 @@ public void GivenResponseWithNoContentType_ShouldPrint()
408402
"""
409403
*The HTTP response was:*
410404
HTTP/1.1 200 OK*
411-
Content-Length: 0*HTTP request*<null>*
405+
*HTTP request*<null>*
412406
""");
413407
}
414408

@@ -1434,7 +1428,6 @@ public void GivenLargeNonPrettifiedJson_ShouldPrintPrettified()
14341428
*The HTTP response was:*
14351429
HTTP/1.1 200 OK*
14361430
Content-Type: application/json; charset=utf-8*
1437-
Content-Length:*
14381431
*Content is too large to display and only a part is printed*
14391432
{*
14401433
"glossary": {*
@@ -1469,7 +1462,6 @@ public void GivenSyntacticallyMalformedNonPrettifiedJson_ShouldPrintPrettified()
14691462
*The HTTP response was:*
14701463
HTTP/1.1 200 OK*
14711464
Content-Type: application/json; charset=utf-8*
1472-
Content-Length:*
14731465
{*
14741466
"glossary": {*
14751467
"title":*

0 commit comments

Comments
 (0)