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: diff --git a/readme.md b/readme.md index d4635b8..d4b79f2 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 @@ -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. 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":*