diff options
author | crupest <crupest@outlook.com> | 2019-12-10 13:55:44 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-12-10 13:55:44 +0800 |
commit | e95030ec056577143f37c06de8351ba511e7ff75 (patch) | |
tree | 584876517cb343d207d2b613706c69d748613370 /Timeline.Tests/Helpers/ResponseAssertions.cs | |
parent | b89ecc4e9eecec7bd22b0f35db60dd26b7098799 (diff) | |
parent | 1bb9a2a622955e7c0a0b75e36fb1673cb9f3aca5 (diff) | |
download | timeline-e95030ec056577143f37c06de8351ba511e7ff75.tar.gz timeline-e95030ec056577143f37c06de8351ba511e7ff75.tar.bz2 timeline-e95030ec056577143f37c06de8351ba511e7ff75.zip |
Merge branch 'master' of github.com:crupest/Timeline
Diffstat (limited to 'Timeline.Tests/Helpers/ResponseAssertions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/ResponseAssertions.cs | 25 |
1 files changed, 22 insertions, 3 deletions
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<HttpResponseMessageAssertions, T>(this, null);
}
- var result = JsonConvert.DeserializeObject<T>(body); // TODO! catch and throw on bad format
- return new AndWhichConstraint<HttpResponseMessageAssertions, T>(this, result);
+
+ try
+ {
+ var options = new JsonSerializerOptions
+ {
+ PropertyNamingPolicy = JsonNamingPolicy.CamelCase
+ };
+ options.Converters.Add(new JsonStringEnumConverter());
+ options.Converters.Add(new JsonDateTimeConverter());
+
+ var result = JsonSerializer.Deserialize<T>(body, options);
+
+ return new AndWhichConstraint<HttpResponseMessageAssertions, T>(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<HttpResponseMessageAssertions, T>(this, null);
+ }
}
}
|