/// <summary> /// Serialize the HTTP content to ProblemDetails as an asynchronous operation. /// </summary> /// <param name="content">The HTTP content.</param> public static ProblemDetails ReadAsProblemDetails(this HttpContent content) { _ = content ?? throw new ArgumentNullException(nameof(content)); if (!content.Headers.ContentType.MediaType.Equals("application/problem+json", StringComparison.OrdinalIgnoreCase)) { throw new SerializationException("HttpContent is not of type \"application/problem+json\"."); } var str = content.ReadAsStringAsync().Result; var converter = new ProblemDetailsJsonConverter(); var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(str)); var problemDetails = converter.Read(ref reader, typeof(ProblemDetails), new JsonSerializerOptions()); return(problemDetails); }
public static ProblemDetails ShouldBeProblemDetails(this HttpResponseMessage response, HttpStatusCode statusCode) { response.StatusCode.Should().Be(statusCode); response.Content.Headers.ContentType.MediaType.Should().Be("application/problem+json"); var str = response.Content.ReadAsStringAsync().Result; var converter = new ProblemDetailsJsonConverter(); var reader = new System.Text.Json.Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(str)); var problemDetails = converter.Read(ref reader, typeof(ProblemDetails), new JsonOptions().JsonSerializerOptions); problemDetails.Should().NotBeNull(); problemDetails.Status.Should().Be((int)statusCode); problemDetails.Title.Should().NotBeNull(); problemDetails.Type.Should().NotBeNull(); problemDetails.Detail.Should().NotBeNull(); problemDetails.Instance.Should().Be(response.RequestMessage.RequestUri.AbsolutePath); return(problemDetails); }