diff --git a/test/Common/Helpers/AssertHelper.cs b/test/Common/Helpers/AssertHelper.cs index 5e9c3a5aba..1a9ad8ee16 100644 --- a/test/Common/Helpers/AssertHelper.cs +++ b/test/Common/Helpers/AssertHelper.cs @@ -225,7 +225,37 @@ public static class AssertHelper public async static Task AssertResponseTypeIs(HttpContext context) { - return await JsonSerializer.DeserializeAsync(context.Response.Body); + try + { + if (context.Response.Body.CanSeek) + { + context.Response.Body.Position = 0; + } + + return await JsonSerializer.DeserializeAsync(context.Response.Body); + } + catch (JsonException ex) + { + string bodyText = ""; + try + { + if (context.Response.Body.CanSeek) + { + context.Response.Body.Position = 0; + } + + using var sr = new StreamReader(context.Response.Body, leaveOpen: true); + bodyText = await sr.ReadToEndAsync(); + } + catch + { + // ignore read errors + } + + throw new Xunit.Sdk.XunitException( + $"Failed to deserialize response to {typeof(T).Name}. " + + $"StatusCode: {context.Response.StatusCode}. Body:\n{bodyText}\nException: {ex}"); + } } public static TimeSpan AssertRecent(DateTime dateTime, int skewSeconds = 2)