Skip to content

Make Http request/response formatter public #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="Shouldly " Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 20 additions & 6 deletions FluentAssertions.Web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Api.v8.Tests", "test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Web.v8.Tests", "test\FluentAssertions.Web.v8.Tests\FluentAssertions.Web.v8.Tests.csproj", "{056E517C-F644-4B8A-9494-09276E6E3C6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Web.Types", "src\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj", "{455E5F41-B678-4286-BB4D-FC232D08CAC0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ited.HttpFormatter", "src\Ited.HttpFormatter\Ited.HttpFormatter.csproj", "{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ited.HttpFormatter.Tests", "test\Ited.HttpFormatter.Tests\Ited.HttpFormatter.Tests.csproj", "{6BA3687F-995A-4726-96D6-366F277384F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shouldly.Web", "src\Shouldly.Web\Shouldly.Web.csproj", "{982D2645-AF8E-44E8-9E5D-D16B740913BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -104,10 +108,18 @@ Global
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Release|Any CPU.Build.0 = Release|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Release|Any CPU.Build.0 = Release|Any CPU
{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3}.Release|Any CPU.Build.0 = Release|Any CPU
{6BA3687F-995A-4726-96D6-366F277384F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BA3687F-995A-4726-96D6-366F277384F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BA3687F-995A-4726-96D6-366F277384F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BA3687F-995A-4726-96D6-366F277384F1}.Release|Any CPU.Build.0 = Release|Any CPU
{982D2645-AF8E-44E8-9E5D-D16B740913BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{982D2645-AF8E-44E8-9E5D-D16B740913BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{982D2645-AF8E-44E8-9E5D-D16B740913BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{982D2645-AF8E-44E8-9E5D-D16B740913BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -125,7 +137,9 @@ Global
{3872DF64-5DA9-4983-90D8-2802A0A26CFE} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{056E517C-F644-4B8A-9494-09276E6E3C6B} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{455E5F41-B678-4286-BB4D-FC232D08CAC0} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
{8C66744D-AC2B-4086-AC2C-5BD04D1BC0D3} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
{6BA3687F-995A-4726-96D6-366F277384F1} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{982D2645-AF8E-44E8-9E5D-D16B740913BC} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {81F41C75-2F8A-4E70-BA17-38146C4BB6E6}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageDescription>
Expand All @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
<ProjectReference Include="..\FluentAssertions.Web\FluentAssertions.Web.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using Newtonsoft.Json;
global using Ited.HttpFormatter;
global using Newtonsoft.Json;
global using System;
global using System.IO;
global using System.Text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable once CheckNamespace

namespace FluentAssertions;

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions src/FluentAssertions.Web.Types/GlobalUsings.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/FluentAssertions.Web.Types/Properties/AssemblyInfo.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/FluentAssertions.Web.v8/FluentAssertions.Web.v8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
<ProjectReference Include="..\Ited.HttpFormatter\Ited.HttpFormatter.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/FluentAssertions.Web/FluentAssertions.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
<ProjectReference Include="..\Ited.HttpFormatter\Ited.HttpFormatter.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// ReSharper disable once CheckNamespace
namespace FluentAssertions;
namespace FluentAssertions;

/// <summary>
/// Holder of the global <see cref="FluentAssertionsWebConfig"/>
Expand Down
3 changes: 1 addition & 2 deletions src/FluentAssertions.Web/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
global using FluentAssertions.Execution;
global using FluentAssertions.Web;
global using FluentAssertions.Web.Internal;
global using FluentAssertions.Web.Internal.Serializers;
global using Ited.HttpFormatter;
global using System;
global using System.Collections.Generic;
global using System.Diagnostics;
global using System.IO;
global using System.Linq;
global using System.Net;
global using System.Net.Http;
Expand Down
12 changes: 2 additions & 10 deletions src/FluentAssertions.Web/Internal/AssertionsFailuresFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentAssertions.Formatting;
using System.Text;

namespace FluentAssertions.Web.Internal;

Expand All @@ -14,15 +13,8 @@ public void Format(object value,
{
var assertionsFailures = (AssertionsFailures)value;

var messageBuilder = new StringBuilder();
messageBuilder.AppendLine();
messageBuilder.AppendLine();
var formatted = AssertionsFailuresFormatted.GetFormatted(assertionsFailures);

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

formattedGraph.AddFragment(messageBuilder.ToString());
formattedGraph.AddFragment(formatted);
}
}
19 changes: 19 additions & 0 deletions src/FluentAssertions.Web/Internal/HttpResponseMessageFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FluentAssertions.Formatting;

internal class HttpResponseMessageFormatter : IValueFormatter
{
public bool CanHandle(object value) => value is HttpResponseMessage;

/// <inheritdoc />
public void Format(object value,
FormattedObjectGraph formattedGraph,
FormattingContext context,
FormatChild formatChild)
{
var response = (HttpResponseMessage)value;

var formatted = HttpResponseMessageFormatted.GetFormatted(response);

formattedGraph.AddFragment(formatted);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentAssertions.Web.Internal;
namespace Ited.HttpFormatter;

internal class AssertionsFailures
public class AssertionsFailures
{
public AssertionsFailures(string[] failuresMessages)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Ited.HttpFormatter/AssertionsFailuresFormatted.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text;

namespace Ited.HttpFormatter;

public class AssertionsFailuresFormatted
{
public static string GetFormatted(AssertionsFailures assertionsFailures)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine();
messageBuilder.AppendLine();

foreach (var failure in assertionsFailures.FailuresMessages)
{
messageBuilder.AppendLine($" - {failure.ReplaceFirstWithLowercase()}");
}
return messageBuilder.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentAssertions.Web.Internal;
namespace Ited.HttpFormatter;

internal static class ContentFormatterOptions
public static class ContentFormatterOptions
{
public const int MaximumReadableBytes = 128 * 1024; // 1KB holds like 500 words
public const string WarningMessageWhenDisposed = "***** Content is disposed so it cannot be read. *****";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ReSharper disable once CheckNamespace
using System;
using System.Runtime.Serialization;

namespace FluentAssertions;
namespace Ited.HttpFormatter;

/// <summary>
/// Captures serialization exceptions.
Expand Down
10 changes: 10 additions & 0 deletions src/Ited.HttpFormatter/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global using Ited.HttpFormatter.Internal;
global using Ited.HttpFormatter;
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net;
global using System.Net.Http;
global using System.Text.Json;
global using System.Threading.Tasks;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentAssertions.Web.Internal;
namespace Ited.HttpFormatter;

internal static class Guard
public static class Guard
{
public static void ThrowIfArgumentIsNull<T>(T obj, string paramName)
where T : class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Text;

namespace FluentAssertions.Web.Internal;
namespace Ited.HttpFormatter;

internal static class HttpContentExtensions
public static class HttpContentExtensions
{
public static async Task<T?> ReadAsAsync<T>(this HttpContent content, ISerializer serializer)
{
var model = await ReadAsAsync(content, typeof(T), serializer);
var model = await content.ReadAsAsync(typeof(T), serializer);
return (T?)model;
}

Expand All @@ -19,7 +19,7 @@ internal static class HttpContentExtensions
return result;
}

public static Task<T?> ReadAsAsync<T>(this HttpContent content, T _, ISerializer serializer)
public static Task<T?> ReadAsAsync<T>(this HttpContent content, T _, ISerializer serializer)
=> content.ReadAsAsync<T>(serializer);

public static bool IsDisposed(this HttpContent? content)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentAssertions.Web.Internal;
namespace Ited.HttpFormatter;

internal static class HttpResponseMessageExtensions
public static class HttpResponseMessageExtensions
{
public static IEnumerable<string> GetHeaderValues(this HttpResponseMessage response, string header)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
using FluentAssertions.Formatting;
using FluentAssertions.Web.Internal.ContentProcessors;
using Ited.HttpFormatter.Internal.ContentProcessors;
using System.Text;

namespace FluentAssertions.Web;
namespace Ited.HttpFormatter;

internal class HttpResponseMessageFormatter : IValueFormatter
public class HttpResponseMessageFormatted
{
public bool CanHandle(object value) => value is HttpResponseMessage;

/// <inheritdoc />
public void Format(object value,
FormattedObjectGraph formattedGraph,
FormattingContext context,
FormatChild formatChild)
public static string GetFormatted(HttpResponseMessage response)
{
var response = (HttpResponseMessage)value;

var messageBuilder = new StringBuilder();
messageBuilder.AppendLine();
messageBuilder.AppendLine();
Expand All @@ -24,7 +15,8 @@ public void Format(object value,
Func<Task> contentResolver = async () => await AppendHttpResponseMessage(messageBuilder, response);
contentResolver.ExecuteInDefaultSynchronizationContext().GetAwaiter().GetResult();

formattedGraph.AddFragment(messageBuilder.ToString());
var formatted = messageBuilder.ToString();
return formatted;
}

private static async Task AppendHttpResponseMessage(StringBuilder messageBuilder, HttpResponseMessage response)
Expand Down Expand Up @@ -115,4 +107,4 @@ private static void AppendContentLength(StringBuilder messageBuilder, HttpConten
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// ReSharper disable once CheckNamespace
using System;
using System.IO;
using System.Threading.Tasks;

namespace FluentAssertions;
namespace Ited.HttpFormatter;

/// <summary>
/// Provides an abstraction to deserialize a Stream of binary data into a C# object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal static class Appender
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal class BinaryProcessor : ProcessorBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal class FallbackProcessor : ProcessorBase
{
Expand Down Expand Up @@ -28,7 +28,7 @@ protected override async Task Handle(StringBuilder contentBuilder)
}

// we might get here some StreamContent, let's try to print it
// but let's try not to get into this issue again https://github.com/adrianiftode/FluentAssertions.Web/issues/93
// but let's try not to get into this issue again https://github.com/adrianiftode/Ited.HttpFormatter/issues/93
var content = await _httpContent!.SafeReadAsStringAsync();
AppendContentWithinLimits(contentBuilder, content);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal interface IContentProcessor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal class InternalServerErrorProcessor : ProcessorBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace FluentAssertions.Web.Internal.ContentProcessors;
namespace Ited.HttpFormatter.Internal.ContentProcessors;

internal class JsonProcessor : ProcessorBase
{
Expand Down
Loading