From bd2462f2bc964839e9b64f167dfa47abb3f9afe6 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Nov 2020 17:08:20 +0800 Subject: ... --- .../Timeline.Tests/Helpers/ResponseAssertions.cs | 94 ++++++---------------- 1 file changed, 25 insertions(+), 69 deletions(-) (limited to 'BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs') diff --git a/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs b/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs index 024732f5..95df3eda 100644 --- a/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs +++ b/BackEnd/Timeline.Tests/Helpers/ResponseAssertions.cs @@ -6,9 +6,11 @@ using System; using System.Globalization; using System.Net; using System.Net.Http; +using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using System.Threading.Tasks; using Timeline.Models.Converters; using Timeline.Models.Http; @@ -27,27 +29,7 @@ namespace Timeline.Tests.Helpers string padding = new string('\t', context.Depth); var res = (HttpResponseMessage)value; - - var builder = new StringBuilder(); - builder.Append($"{newline}{padding} Status Code: {res.StatusCode} ; Body: "); - - try - { - var task = res.Content.ReadAsStringAsync(); - task.Wait(); - var body = task.Result; - if (body.Length > 40) - { - body = body[0..40] + " ..."; - } - builder.Append(body); - } - catch (AggregateException) - { - builder.Append("NOT A STRING."); - } - - return builder.ToString(); + return $"{newline}{padding} Status Code: {res.StatusCode}"; } } @@ -79,41 +61,17 @@ namespace Timeline.Tests.Helpers return new AndConstraint(this); } - public AndWhichConstraint HaveJsonBody(string because = "", params object[] becauseArgs) + public async Task HaveAndGetJsonBodyAsync(string because = "", params object[] becauseArgs) { var a = Execute.Assertion.BecauseOf(because, becauseArgs); - string body; - try - { - var task = Subject.Content.ReadAsStringAsync(); - task.Wait(); - body = task.Result; - } - catch (AggregateException e) - { - a.FailWith("Expected response body of {context:HttpResponseMessage} to be json string{reason}, but failed to read it or it was not a string. Exception is {0}.", e.InnerExceptions); - return new AndWhichConstraint(this, null); - } - - 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) + var body = await Subject.ReadBodyAsJsonAsync(); + if (body == null) { - 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); + a.FailWith("Expected response body of {context:HttpResponseMessage} to be json string of type {0}{reason}, but failed to read it or it was not a valid json string.", typeof(T).FullName); + return default!; } + return body; } } @@ -124,47 +82,45 @@ namespace Timeline.Tests.Helpers return new HttpResponseMessageAssertions(instance); } - public static AndWhichConstraint HaveCommonBody(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + public static Task HaveAndGetCommonBodyAsync(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - return assertions.HaveJsonBody(because, becauseArgs); + return assertions.HaveAndGetJsonBodyAsync(because, becauseArgs); } - public static void HaveCommonBody(this HttpResponseMessageAssertions assertions, int code, string message = null, params object[] messageArgs) + public static async Task HaveCommonBodyWithCodeAsync(this HttpResponseMessageAssertions assertions, int code, string? message = null, params object[] messageArgs) { message = string.IsNullOrEmpty(message) ? "" : ", " + string.Format(CultureInfo.CurrentCulture, message, messageArgs); - var body = assertions.HaveCommonBody("Response body should be CommonResponse{0}", message).Which; + var body = await assertions.HaveAndGetCommonBodyAsync("Response body should be CommonResponse{0}", message); body.Code.Should().Be(code, "Response body code is not the specified one{0}", message); } - public static AndWhichConstraint> HaveCommonDataBody(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + public static Task> HaveAndGetCommonDataBodyAsync(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - return assertions.HaveJsonBody>(because, becauseArgs); + return assertions.HaveAndGetJsonBodyAsync>(because, becauseArgs); } - public static void BePut(this HttpResponseMessageAssertions assertions, bool create, string because = "", params object[] becauseArgs) + public static async Task BePutAsync(this HttpResponseMessageAssertions assertions, bool create, string because = "", params object[] becauseArgs) { - var body = assertions.HaveStatusCode(create ? 201 : 200, because, becauseArgs) - .And.HaveJsonBody(because, becauseArgs) - .Which; + var body = await assertions.HaveStatusCode(create ? 201 : 200, because, becauseArgs) + .And.HaveAndGetJsonBodyAsync(because, becauseArgs); body.Code.Should().Be(0); body.Data.Create.Should().Be(create); } - public static void BeDelete(this HttpResponseMessageAssertions assertions, bool delete, string because = "", params object[] becauseArgs) + public static async Task BeDeleteAsync(this HttpResponseMessageAssertions assertions, bool delete, string because = "", params object[] becauseArgs) { - var body = assertions.HaveStatusCode(200, because, becauseArgs) - .And.HaveJsonBody(because, becauseArgs) - .Which; + var body = await assertions.HaveStatusCode(200, because, becauseArgs) + .And.HaveAndGetJsonBodyAsync(because, becauseArgs); body.Code.Should().Be(0); body.Data.Delete.Should().Be(delete); } - public static void BeInvalidModel(this HttpResponseMessageAssertions assertions, string message = null) + public static async Task BeInvalidModelAsync(this HttpResponseMessageAssertions assertions, string message = null) { message = string.IsNullOrEmpty(message) ? "" : ", " + message; - assertions.HaveStatusCode(400, "Invalid Model Error must have 400 status code{0}", message) - .And.HaveCommonBody("Invalid Model Error must have CommonResponse body{0}", message) - .Which.Code.Should().Be(ErrorCodes.Common.InvalidModel, + var body = await assertions.HaveStatusCode(400, "Invalid Model Error must have 400 status code{0}", message) + .And.HaveAndGetCommonBodyAsync("Invalid Model Error must have CommonResponse body{0}", message); + body.Code.Should().Be(ErrorCodes.Common.InvalidModel, "Invalid Model Error must have code {0} in body{1}", ErrorCodes.Common.InvalidModel, message); } -- cgit v1.2.3