From 37382ac85675bded43f800fec18fa8eb2ba22706 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 20 Nov 2019 19:59:06 +0800 Subject: Migrate to System.Text.Json . Format enum to string instead of number. Use ISO8601 format for datetime. --- Timeline.Tests/Helpers/ResponseAssertions.cs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'Timeline.Tests/Helpers/ResponseAssertions.cs') diff --git a/Timeline.Tests/Helpers/ResponseAssertions.cs b/Timeline.Tests/Helpers/ResponseAssertions.cs index 6d764c68..301ceef6 100644 --- a/Timeline.Tests/Helpers/ResponseAssertions.cs +++ b/Timeline.Tests/Helpers/ResponseAssertions.cs @@ -2,11 +2,13 @@ using FluentAssertions.Execution; using FluentAssertions.Formatting; using FluentAssertions.Primitives; -using Newtonsoft.Json; using System; using System.Net; using System.Net.Http; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Timeline.Models.Converters; using Timeline.Models.Http; namespace Timeline.Tests.Helpers @@ -88,8 +90,25 @@ namespace Timeline.Tests.Helpers return new AndWhichConstraint(this, null); } - var result = JsonConvert.DeserializeObject(body); // TODO! catch and throw on bad format - return new AndWhichConstraint(this, result); + + try + { + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + options.Converters.Add(new JsonStringEnumConverter()); + options.Converters.Add(new JsonDateTimeConverter()); + + var result = JsonSerializer.Deserialize(body, options); + + return new AndWhichConstraint(this, result); + } + catch (JsonException e) + { + a.FailWith("Expected response body of {context:HttpResponseMessage} to be json string{reason}, but failed to deserialize it. Exception is {0}.", e); + return new AndWhichConstraint(this, null); + } } } -- cgit v1.2.3