public void ASimpleExceptionShouldSerializeToXmlUsingDataContractSerializer() { // Arrange var exception = new BasicApiProblemException( HttpStatusCode.Forbidden, "You do not have enough credit.", "http://example.com/probs/out-of-credit", "Your current balance is 30, but that costs 50."); const string expected = @" <problem xmlns=""urn:web-api-problem""> <problemType>http://example.com/probs/out-of-credit</problemType> <title>You do not have enough credit.</title> <detail>Your current balance is 30, but that costs 50.</detail> </problem>"; // Act var apiProblemType = exception.ApiProblem.GetType(); var dataContractSerializer = new DataContractSerializer(apiProblemType); string serialized; using (var memStream = new MemoryStream()) { dataContractSerializer.WriteObject(memStream, exception.ApiProblem); var data = memStream.ToArray(); serialized = Encoding.UTF8.GetString(data); } // Assert XmlDiffAssert.DiffXmlStrict(serialized, expected); }
public void TestContentNegotiation(string contentType, string expectedResponseContentType) { // Arrange var exception = new BasicApiProblemException(HttpStatusCode.BadRequest, "", ""); var filter = new ResponseExceptionFilterAttribute(ResponseExceptionFormat.Negotiate); var executedContext = CreateContext(exception, r => r.Headers.Add("Accept", contentType)); // Act filter.OnException(executedContext); // Assert executedContext.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest); executedContext.Response.Content.Headers.ContentType.ToString().Should().Be(expectedResponseContentType); }
private static HttpActionExecutedContext CreateContext(BasicApiProblemException exception, Action<HttpRequestMessage> setRequest = null) { var config = new HttpConfiguration(); var httpRouteData = new HttpRouteData(new HttpRoute()); var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com"); request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); if (setRequest != null) { setRequest(request); } var controllerContext = new HttpControllerContext(config, httpRouteData, request); var context = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor()); var executedContext = new HttpActionExecutedContext(context, exception); return executedContext; }
public void ASimpleExceptionShouldSerializeToJson() { // Arrange var exception = new BasicApiProblemException( HttpStatusCode.Forbidden, "You do not have enough credit.", "http://example.com/probs/out-of-credit", "Your current balance is 30, but that costs 50."); const string expected =@" { ""problemType"": ""http://example.com/probs/out-of-credit"", ""title"": ""You do not have enough credit."", ""detail"": ""Your current balance is 30, but that costs 50."", }"; // Act var serialized = exception.Serialize("application/json"); // Assert serialized.ShouldBeJson(expected); }
public void ASimpleExceptionShouldSerializeToXml() { // Arrange var exception = new BasicApiProblemException( HttpStatusCode.Forbidden, "You do not have enough credit.", "http://example.com/probs/out-of-credit", "Your current balance is 30, but that costs 50."); const string expected = @"<?xml version=""1.0"" encoding=""utf-8""?> <problem xmlns=""urn:web-api-problem""> <problemType>http://example.com/probs/out-of-credit</problemType> <title>You do not have enough credit.</title> <detail>Your current balance is 30, but that costs 50.</detail> </problem>"; // Act var serialized = exception.Serialize("application/xml"); // Assert XmlDiffAssert.DiffXmlStrict(serialized, expected); }