Пример #1
0
        public void GivenMultipartFormDataResponse_ShouldPrintAsSingleParts()
        {
            // Arrange
            var content = new MultipartFormDataContent("-----------------------------9051914041544843365972754266")
            {
                new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("key1", "value1"),
                    new KeyValuePair <string, string>("key2", "value2")
                }),
                { new ByteArrayContent(new byte[1]), "ByteArray" },
                { new ByteArrayContent(new byte[2]), "ByteArray", "a-file-name.jpg" },
                new StringContent("some string content", Encoding.UTF8, "plain/text")
            };

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = content
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should()
            .Match(@"*key1=value1&key2=value2*")
            .And.Match(@"*Content is of a binary data type having the length 1.*")
            .And.Match(@"*a-file-name.jpg*Content is of a binary data type having the length 2.*")
            .And.Match(@"*plain/text*some string content*")
            ;
        }
Пример #2
0
        public void GivenStreamResponse_ShouldPrintMessageInfo()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StreamContent(new MemoryStream(new byte[1]))
                      {
                          Headers =
                          {
                              ContentType = new MediaTypeHeaderValue("image/jpeg")
                          }
                      }
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*Content is of a stream type having the length 1.*");
        }
Пример #3
0
        public void GivenHeaders_ShouldPrintAllHeaders()
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent("", Encoding.UTF8, "text/html")
                  };
            subject.Headers.Add("Cache-Control", "no-store, no-cache, max-age=0");
            subject.Headers.Add("Pragma", "no-cache");
            subject.Headers.Add("Request-Context", "appId=cid-v1:2e15fa14-72b6-44b3-a9b2-560e7b3234e5");
            subject.Headers.Add("Strict-Transport-Security", "max-age=31536000");
            subject.Headers.Add("Date", "Thu, 26 Sep 2019 22:33:34 GMT");
            subject.Headers.Add("Connection", "close");
            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(
                @"*The HTTP response was:*
HTTP/1.1 200 OK*
Cache-Control: no-store, no-cache, max-age=0*
Pragma: no-cache*
Request-Context: appId=cid-v1:2e15fa14-72b6-44b3-a9b2-560e7b3234e5*
Strict-Transport-Security: max-age=31536000*
Date: Thu, 26 Sep 2019 22:33:34 GMT*
Connection: close*
Content-Type: text/html; charset=utf-8*
Content-Length: 0*
The originated HTTP request was <null>.*");
        }
Пример #4
0
        public void GivenDuplicatedHeaders_ShouldPrintOnNewLines()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent("", Encoding.UTF8, "text/html")
                  };
            subject.Headers.Add("Set-Cookie", "name1=value1");
            subject.Headers.Add("Set-Cookie", "name2=value2");
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*The HTTP response was:*
HTTP/1.1 200 OK*
Set-Cookie: name1=value1*
Set-Cookie: name2=value2*");
        }
Пример #5
0
        public void GivenFormUrlEncodedRequest_ShouldPrintFormUrlEncodedData()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      RequestMessage = new HttpRequestMessage
                      {
                          Content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("key1", "value1"),
                        new KeyValuePair <string, string>("key2", "value2")
                    })
                      }
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*key1=value1&key2=value2*");
        }
Пример #6
0
        public void GivenDisposedRequest_ShouldPrintAndShowWarning()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content        = new StringContent("", Encoding.UTF8),
                      RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://*****:*****@"*Content is disposed so it cannot be read*");
        }
Пример #7
0
        public void GivenStreamRequest_ShouldPrintMessageInfo()
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      RequestMessage = new HttpRequestMessage
                      {
                          Content = new StreamContent(new MemoryStream(new byte[1]))
                          {
                              Headers =
                              {
                                  ContentType = new MediaTypeHeaderValue("image/jpeg")
                              }
                          }
                      }
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(
                @"*Content is of a stream type having the length 1.*");
        }
Пример #8
0
        public void GivenRequest_WhenRequestStreamAtTheEnd_ShouldPrintRequestDetails()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://*****:*****@"*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
The originated HTTP request was:*
POST http://localhost:5001/ HTTP 1.1*
Authorization: Bearer xyz*
Content-Type: text/plain; charset=utf-8*
Content-Length: *
Some content.*");
        }
Пример #9
0
        public void GivenContentLengthInHeaders_ShouldNotPrintItTwice()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent("")
                  };
            subject.Content.Headers.Add("Content-Length", "0");
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: text/plain; charset=utf-8*
Content-Length: 0*
The originated HTTP request was <null>.*");
        }
Пример #10
0
        public void GivenRequest_ShouldPrintRequestDetails()
        {
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      RequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://*****:*****@"*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
The originated HTTP request was:*
POST http://localhost:5001/ HTTP 1.1*
Authorization: Bearer xyz*
Content-Type: text/plain; charset=utf-8*
Content-Length: *
Some content.*");
        }
Пример #11
0
        public void GivenResponseWithMinifiedJson_ShouldPrintFormattedJson()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent(
                          @"{""glossary"":{""title"":""example glossary"",""GlossDiv"":{""title"":""S"",""GlossList"":{""GlossEntry"":{""ID"":""SGML"",""SortAs"":""SGML"",""GlossTerm"":""Standard Generalized Markup Language"",""Acronym"":""SGML"",""Abbrev"":""ISO 8879:1986"",""GlossDef"":{""para"":""A meta-markup language, used to create markup languages such as DocBook."",""GlossSeeAlso"":[""GML"",""XML""]},""GlossSee"":""markup""}}}}}",
                          Encoding.UTF8, "application/json")
                  };
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(@"*
The HTTP response was:*
HTTP/1.1 200 OK*
Content-Type: application/json; charset=utf-8*
Content-Length:*
{*
  ""glossary"": {*
    ""title"": ""example glossary"",*
    ""GlossDiv"": {*
      ""title"": ""S"",*
      ""GlossList"": {*
        ""GlossEntry"": {*
          ""ID"": ""SGML"",*
          ""SortAs"": ""SGML"",*
          ""GlossTerm"": ""Standard Generalized Markup Language"",*
          ""Acronym"": ""SGML"",*
          ""Abbrev"": ""ISO 8879:1986"",*
          ""GlossDef"": {*
            ""para"": ""A meta-markup language, used to create markup languages such as DocBook."",*
            ""GlossSeeAlso"": [*
              ""GML"",*
              ""XML""*
            ]*
          },*
          ""GlossSee"": ""markup""*
        }*
      }*
    }*
  }*
}*
The originated HTTP request was <null>.*");
        }
Пример #12
0
        public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero()
        {
            // Arrange
            using var subject = new HttpResponseMessage();
            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(@"*
The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*
The originated HTTP request was <null>.*");
        }
Пример #13
0
        public void GivenByteArrayResponse_ShouldPrintMessageInfo()
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new ByteArrayContent(new byte[1])
                  };
            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(
                @"*Content is of a binary data type having the length 1.*");
        }
Пример #14
0
        public void GivenLargeStringContent_ShouldNotPrintEverything()
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent(new string ('-', ContentFormatterOptions.MaximumReadableBytes + 1) + "end")
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match("*Content is too large to display*")
            .And.Contain(new string('-', 500));
        }
Пример #15
0
        public void GivenInternalServerError_ShouldPrintExceptionDetails(string content, string expected,
                                                                         string unexpected)
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                  {
                      Content = new StringContent(content)
                  };
            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(expected);
            formatted.Should().NotContain(unexpected);
        }
Пример #16
0
        public void GivenResponseWithNoContentType_ShouldPrint()
        {
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent("", Encoding.UTF8)
                  };
            subject.Content.Headers.ContentType = null;
            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(
                @"*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*HTTP request*<null>*");
        }
Пример #17
0
        public void GivenReadOnlyMemoryRequest_ShouldPrintMessageInfo()
        {
            // Arrange
            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      RequestMessage = new HttpRequestMessage
                      {
                          Content = new ReadOnlyMemoryContent(new ReadOnlyMemory <byte>(new byte[1]))
                      }
                  };

            var sut = new HttpResponseMessageFormatter();

            // Act
            var formatted = sut.Format(subject, null !, null !);

            // Assert
            formatted.Should().Match(
                @"*Content is of a binary data type having the length 1.*");
        }
Пример #18
0
        public void GivenByteArrayResponse_ShouldPrintMessageInfo()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new ByteArrayContent(new byte[1])
                  };
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*Content is of a binary data type having the length 1.*");
        }
Пример #19
0
        public void GivenHtmlResponse_ShouldPrintAsItIs()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent(@"<html>
<head>
<title>Title of the document</title>
</head>

<body>
The content of the document......
</body>

</html>", Encoding.UTF8, "text/html")
                  };
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(@"*
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>*
</head>*
<body>*
The content of the document......*
</body>*
</html>*
The originated HTTP request was <null>.*");
        }
Пример #20
0
        public void GivenResponseWithNoContentType_ShouldPrint()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent("", Encoding.UTF8)
                  };
            subject.Content.Headers.ContentType = null;
            var sut = new HttpResponseMessageFormatter();

            // Act
            sut.Format(subject, formattedGraph, null !, null !);

            // Assert
            var formatted = formattedGraph.ToString();

            formatted.Should().Match(
                @"*The HTTP response was:*
HTTP/1.1 200 OK*
Content-Length: 0*HTTP request*<null>*");
        }