public void ReturnInsufficientCash(string contentType, string expectedResponseContentType) { var exception = new InsufficientCashException(new InsufficientCashProblem { Type = new Uri("https://example.com/probs/out-of-credit").ToString(), Title = "You do not have enough credit.", Status = (int)HttpStatusCode.Forbidden, Detail = "Your current balance is 30, but that costs 50.", Instance = new Uri("/account/12345/msgs/abc", UriKind.Relative).ToString() }); var filter = new HttpProblemDetailsExceptionFilterAttribute(); var actionExecutedContext = CreateActionExecutedContext("http://host:1234/payment/12345", exception, request => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType))); filter.OnException(actionExecutedContext); Assert.Equal(HttpStatusCode.Forbidden, actionExecutedContext.Response.StatusCode); Assert.Equal(expectedResponseContentType, actionExecutedContext.Response.Content.Headers.ContentType.MediaType); string body = actionExecutedContext.Response.Content.ReadAsStringAsync().Result; dynamic problemDetail = JsonConvert.DeserializeObject(body); Assert.NotNull(problemDetail); Assert.Equal("https://example.com/probs/out-of-credit", problemDetail.Type.ToString()); Assert.Equal("You do not have enough credit.", problemDetail.Title.ToString()); Assert.Equal("403", problemDetail.Status.ToString()); Assert.Equal("Your current balance is 30, but that costs 50.", problemDetail.Detail.ToString()); Assert.Equal("/account/12345/msgs/abc", problemDetail.Instance.ToString()); }
public void ReturnSuccess(string contentType, string expectedResponseContentType) { var filter = new HttpProblemDetailsExceptionFilterAttribute(); var actionExecutedContext = CreateActionExecutedContext("http://host:1234/payment/67890", null, request => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType))); filter.OnException(actionExecutedContext); Assert.Equal(HttpStatusCode.OK, actionExecutedContext.Response.StatusCode); Assert.Equal("OK", actionExecutedContext.Response.Content.ReadAsStringAsync().Result); Assert.Equal(expectedResponseContentType, actionExecutedContext.Response.Content.Headers.ContentType.MediaType); }