From cff5c8bdf5c9fe45e88668ec3b30aa0d523b44e4 Mon Sep 17 00:00:00 2001 From: Adrian Iftode Date: Wed, 17 Apr 2024 22:38:07 +0100 Subject: [PATCH 1/4] Version ci to 1.4.0 --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index c30d259..e99f9cb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.3.0-ci-{build} +version: 1.4.0-ci-{build} image: Visual Studio 2022 branches: only: From 08b64c9b531d6adcd3badc6c096bea05dac50ef2 Mon Sep 17 00:00:00 2001 From: Adrian Iftode Date: Wed, 17 Apr 2024 22:38:54 +0100 Subject: [PATCH 2/4] Fix sonar links --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index d4635b8..a3d199a 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ This is a [*FluentAssertions*](https://fluentassertions.com/) extension over the It provides assertions specific to HTTP responses and outputs rich erros messages when the tests fail, so less time with debugging is spent. [![Build status](https://ci.appveyor.com/api/projects/status/93qtbyftww0snl4x/branch/master?svg=true)](https://ci.appveyor.com/project/adrianiftode/fluentassertions-web/branch/master) -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=FluentAssertions.Web&metric=alert_status)](https://sonarcloud.io/dashboard?id=FluentAssertions.Web) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=adrianiftode_FluentAssertions.Web&metric=alert_status)](https://sonarcloud.io/project/overview?id=adrianiftode_FluentAssertions.Web) [![NuGet](https://img.shields.io/nuget/v/FluentAssertions.Web.svg)](https://www.nuget.org/packages/FluentAssertions.Web) ```csharp From a5423d4ec8d9375e9883464ed75c100b43fa3c7b Mon Sep 17 00:00:00 2001 From: Adrian Iftode Date: Thu, 18 Apr 2024 20:38:04 +0100 Subject: [PATCH 3/4] Update readme.md --- readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index a3d199a..d4b79f2 100644 --- a/readme.md +++ b/readme.md @@ -323,14 +323,13 @@ NewtonsoftJsonSerializerConfig.Options.Converters.Add(new YesNoBooleanJsonConver ### The HttpResponsesMessage assertions from FluentAssertions vs. FluentAssertions.Web -In the [6.4.0](https://fluentassertions.com/releases/#640) release the main library introduced a set of related assertions: *BeSuccessful, BeRedirection, HaveClientError, HaveServerError, HaveError, HaveStatusCode, NotHaveStatusCode*. +In the [6.4.0](https://fluentassertions.com/releases/#640) release FluentAssertions introduced a set of related assertions: *BeSuccessful, BeRedirection, HaveClientError, HaveServerError, HaveError, HaveStatusCode, NotHaveStatusCode*. This library can still be used with FluentAssertions and it did not become obsoleted, not only because of the rich set of assertions, but also for the comprehensive output messages that are displayed when the test fails, feature that is not present in the main library, but in FluentAssertions.Web one. ### FluentAssertions.Web vs FluentAssertions.Mvc vs FluentAssertions.Http - -**FluentAssertions.Web** does not extend the ASP.NET Core Controllers assertions, if you are looking for that, then consider [FluentAssertions.Mvc](https://github.com/fluentassertions/fluentassertions.mvc). +**FluentAssertions.Web** does not extend the assertions for the ASP.NET Core *Controllers*, if you are looking for that, then consider [FluentAssertions.Mvc](https://github.com/fluentassertions/fluentassertions.mvc). When FluentAssertions.Web was created, [FluentAssertions.Http](https://github.com/balanikas/FluentAssertions.Http) also existed at the time, solving the same problem when considering the asserting language. -Besides the extra assertions added by FluentAssertions.Web, an important effort is put by this library on what happens when a test fails. \ No newline at end of file +Besides the extra assertions added by FluentAssertions.Web, an important effort is put by this library on what happens when a test fails. From f4cfe558651f4e34fde0e009a86d2d9146af3d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Fri, 24 May 2024 18:09:09 +0200 Subject: [PATCH 4/4] Print the Content-Length header only if it actually exists It is unsettling to see the "Content-Length" header printed in the diagnosis messages even though it is not actually present in the request and/or response headers. Note that we can't just test whether the `ContentLength` property is null or not because the [HttpContentHeaders.ContentLength implementation][1] automatically computes a value even if the Content-Length is not present. This is why using `content.Headers.TryGetValues` is mandatory. [1]: https://github.com/dotnet/runtime/blob/v8.0.5/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpContentHeaders.cs#L41-L55 --- .../HttpResponseMessageFormatter.cs | 9 +++++++-- .../HttpResponseMessageFormatterSpecs.cs | 20 ++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/FluentAssertions.Web/HttpResponseMessageFormatter.cs b/src/FluentAssertions.Web/HttpResponseMessageFormatter.cs index fc14ab3..7e2d1ea 100644 --- a/src/FluentAssertions.Web/HttpResponseMessageFormatter.cs +++ b/src/FluentAssertions.Web/HttpResponseMessageFormatter.cs @@ -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}"); + } + } } } \ No newline at end of file diff --git a/test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs b/test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs index a43878a..e3aa4a9 100644 --- a/test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs +++ b/test/FluentAssertions.Web.Tests/HttpResponseMessageFormatterSpecs.cs @@ -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); @@ -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 .* """); } @@ -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 .* """); } @@ -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",* @@ -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",* @@ -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:* * * Title of the document* @@ -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" } } } }; @@ -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.* """); } @@ -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.* """); } @@ -408,7 +402,7 @@ public void GivenResponseWithNoContentType_ShouldPrint() """ *The HTTP response was:* HTTP/1.1 200 OK* - Content-Length: 0*HTTP request** + *HTTP request** """); } @@ -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": {* @@ -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":*