Skip to content

Commit

Permalink
#18: Synergy.Web.Api.Testing: Sample extended
Browse files Browse the repository at this point in the history
Fixed DateTime serialization - removed time zone from date which caused markdown docs change when generating around the world.
  • Loading branch information
MarcinCelej committed May 16, 2024
1 parent 21e9ca0 commit 9062e89
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using Synergy.Sample.Web.API.Extensions;
using Synergy.Web.Api.Testing;

namespace Synergy.Sample.Web.API.Tests.Infrastructure
{
public class SampleTestServer : TestServer
{
/// <inheritdoc />
public override JsonSerializerSettings SerializationSettings => new()
{
DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
};

private WebApplicationFactory<Startup>? webApplicationFactory;

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"login": {
"value": "[email protected]"
},
"created": "2024-05-13T13:19:15.8420304+02:00"
"created": "2022-12-15T19:09:56+05:30"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Content-Length: 182
"login": {
"value": "[email protected]"
},
"created": "2024-05-13T13:19:15.8420304+02:00"
"created": "2022-12-15T14:39:56+01:00"
}
}
```
Expand Down
19 changes: 11 additions & 8 deletions Web/Synergy.Web.Api.Testing/HttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ public static string ToHttpLook(this HttpRequestMessage request, HttpOperation o
var requestBody = request.Content.ReadJson();
if (requestBody != null)
{
report.Append(requestBody.ToString(
Formatting.Indented,
operation.TestServer.Converters()
)
report.Append(
Serialize(requestBody, operation.TestServer.SerializationSettings)
);
}
return report.ToString().Trim();
Expand All @@ -121,15 +119,20 @@ public static string ToHttpLook(this HttpResponseMessage response, HttpOperation
InsertHeaders(report, response.GetAllHeaders());
var responseBody = response.Content.ReadJson();
if (responseBody != null)
report.Append(responseBody.ToString(
Formatting.Indented,
operation.TestServer.Converters()
)
report.Append(
Serialize(responseBody, operation.TestServer.SerializationSettings)
);

return report.ToString().Trim();
}

private static string Serialize(JToken json, JsonSerializerSettings settings)
=> JsonConvert.SerializeObject(
json,
Formatting.Indented,
settings
);

private static void InsertHeaders(StringBuilder report, IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
{
foreach (var header in headers)
Expand Down

0 comments on commit 9062e89

Please sign in to comment.