Skip to content

Commit

Permalink
Make Http request/response formatter public
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiftode committed Feb 11, 2025
1 parent 4afaab7 commit aeb4980
Show file tree
Hide file tree
Showing 55 changed files with 180 additions and 122 deletions.
19 changes: 13 additions & 6 deletions FluentAssertions.Web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ 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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -104,10 +106,14 @@ 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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -125,7 +131,8 @@ 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}
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
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 MultipartProcessor : 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 abstract class ProcessorBase : IContentProcessor
{
Expand Down
Loading

0 comments on commit aeb4980

Please sign in to comment.