Ejemplo n.º 1
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*");
        }
Ejemplo n.º 2
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*");
        }
Ejemplo n.º 3
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var list = ((HashSet <(Type, Type)>)value).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                (Type, Type)obj = list[i];

                if (i == 0)
                {
                    formattedGraph.AddLine(":");
                }

                if (i != list.Count - 1)
                {
                    formattedGraph.AddLine(" - " + obj);
                }
                else
                {
                    // Last line. Fluent Assertions will append a period character at this point, so let's just use
                    // AddFragment to avoid a superfluous newline in the output.
                    formattedGraph.AddFragment(" - " + obj);
                }
            }

            if (list.Count == 0)
            {
                formattedGraph.AddFragment("{empty}");
            }
        }
Ejemplo n.º 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*");
        }
            public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
            {
                var prefix     = context.UseLineBreaks ? Environment.NewLine : string.Empty;
                var diagnostic = (Diagnostic)value;

                formattedGraph.AddFragment($"{prefix}\"[{diagnostic.Code} ({diagnostic.Level})] {diagnostic.Message}\"");
            }
Ejemplo n.º 6
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>.*");
        }
Ejemplo n.º 7
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.*");
        }
Ejemplo n.º 8
0
        public void GivenStreamRequest_ShouldPrintMessageInfo()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            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
            sut.Format(subject, formattedGraph, null !, null !);

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

            formatted.Should().Match(
                @"*Content is of a stream type having the length 1.*");
        }
        private static string Format(JToken value, bool useLineBreaks = false)
        {
            var output = new FormattedObjectGraph(100);

            new JTokenFormatter().Format(value, output, new FormattingContext
            {
                UseLineBreaks = useLineBreaks
            }, null);

            return(output.ToString());
        }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            string result = "\"" + value + "\"";

            if (context.UseLineBreaks)
            {
                formattedGraph.AddFragmentOnNewLine(result);
            }
            else
            {
                formattedGraph.AddFragment(result);
            }
        }
Ejemplo n.º 11
0
#pragma warning disable CA1822 // Making this method static is a breaking chan
        public string Format(JToken value, bool useLineBreaks = false)
        {
            // SMELL: Why is this method necessary at all?
            // SMELL: Why aren't we using the Formatter class directly?
            var output = new FormattedObjectGraph(maxLines: 100);

            new JTokenFormatter().Format(value, output, new FormattingContext
            {
                UseLineBreaks = useLineBreaks
            }, null);

            return(output.ToString());
        }
Ejemplo n.º 12
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            string outerXml = ((XmlNode)value).OuterXml;

            const int maxLength = 20;

            if (outerXml.Length > maxLength)
            {
                outerXml = outerXml.Substring(0, maxLength).TrimEnd() + "…";
            }

            formattedGraph.AddLine(outerXml.EscapePlaceholders());
        }
Ejemplo n.º 13
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>.*");
        }
Ejemplo n.º 14
0
        /// <inheritdoc />
        public void Format(object value,
                           FormattedObjectGraph formattedGraph,
                           FormattingContext context,
                           FormatChild formatChild)
        {
            var response = (HttpResponseMessage)value;

            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine();
            messageBuilder.AppendLine();
            messageBuilder.AppendLine("The HTTP response was:");

            Func <Task> contentResolver = async() => await AppendHttpResponseMessage(messageBuilder, response);

            contentResolver.ExecuteInDefaultSynchronizationContext().GetAwaiter().GetResult();

            formattedGraph.AddFragment(messageBuilder.ToString());
        }
        public void Format(object value,
                           FormattedObjectGraph formattedGraph,
                           FormattingContext context,
                           FormatChild formatChild)
        {
            var assertionsFailures = (AssertionsFailures)value;

            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine();
            messageBuilder.AppendLine();

            foreach (var failure in assertionsFailures.FailuresMessages)
            {
                messageBuilder.AppendLine($"    - { failure.ReplaceFirstWithLowercase() }");
            }

            formattedGraph.AddFragment(messageBuilder.ToString());
        }
Ejemplo n.º 16
0
        public void GivenUnspecifiedResponse_ShouldPrintProtocolAndHaveContentLengthZero()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage();
            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*
The originated HTTP request was <null>.*");
        }
Ejemplo n.º 17
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.*");
        }
Ejemplo n.º 18
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>.*");
        }
Ejemplo n.º 19
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var jToken = value as JToken;

            if (context.UseLineBreaks)
            {
                var result = jToken?.ToString(Newtonsoft.Json.Formatting.Indented);
                if (result is not null)
                {
                    formattedGraph.AddFragmentOnNewLine(result);
                }
                else
                {
                    formattedGraph.AddFragment("<null>");
                }
            }
            else
            {
                formattedGraph.AddFragment(jToken?.ToString().RemoveNewLines() ?? "<null>");
            }
        }
Ejemplo n.º 20
0
        public void GivenLargeStringContent_ShouldNotPrintEverything()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.OK)
                  {
                      Content = new StringContent(new string ('-', ContentFormatterOptions.MaximumReadableBytes + 1) + "end")
                  };

            var sut = new HttpResponseMessageFormatter();

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

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

            formatted.Should().Match("*Content is too large to display*")
            .And.Contain(new string('-', 500));
        }
Ejemplo n.º 21
0
        public void GivenInternalServerError_ShouldPrintExceptionDetails(string content, string expected,
                                                                         string unexpected)
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            using var subject = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                  {
                      Content = new StringContent(content)
                  };
            var sut = new HttpResponseMessageFormatter();

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

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

            formatted.Should().Match(expected);
            formatted.Should().NotContain(unexpected);
        }
Ejemplo n.º 22
0
        public void GivenMultipartFormDataRequest_ShouldPrintAsSingleParts()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);
            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)
                  {
                      RequestMessage = new HttpRequestMessage
                      {
                          Content = content
                      }
                  };

            var sut = new HttpResponseMessageFormatter();

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

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

            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*")
            ;
        }
Ejemplo n.º 23
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>*");
        }
Ejemplo n.º 24
0
        public void GivenHeaders_ShouldPrintAllHeaders()
        {
            // Arrange
            var formattedGraph = new FormattedObjectGraph(maxLines: 100);

            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
            sut.Format(subject, formattedGraph, null !, null !);

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

            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>.*");
        }
Ejemplo n.º 25
0
 public static void Foo2(SomeClassInheritedFromClassWithCustomFormatterLvl2 value, FormattedObjectGraph output)
 {
     output.AddFragment("Property is " + value.Property);
 }
Ejemplo n.º 26
0
 public static void Foo(SomeOtherClassWithCustomFormatter _, FormattedObjectGraph output)
 {
     throw new XunitException("Should never be called");
 }
Ejemplo n.º 27
0
 public static void Foo(SomeClassWithCustomFormatter value, FormattedObjectGraph output)
 {
     output.AddFragment("Property = " + value.Property);
 }
Ejemplo n.º 28
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment("<null>");
 }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var errors = (IEnumerable <IError>)value;

            formattedGraph.AddFragment(string.Join("; ", errors.Select(error => error.Message)));
        }