private InvalidModelStateResult(ModelStateDictionary modelState, ExceptionResult.IDependencyProvider dependencies) { if (modelState == null) { throw new ArgumentNullException("modelState"); } Contract.Assert(dependencies != null); _modelState = modelState; _dependencies = dependencies; }
public void ContentNegotiator_ForApiController_Throws_WhenConfigurationIsNull() { // Arrange Exception exception = CreateException(); ApiController controller = CreateController(); HttpControllerContext context = new HttpControllerContext(); using (HttpRequestMessage request = CreateRequest()) { controller.ControllerContext = context; ExceptionResult result = CreateProductUnderTest(exception, controller); // Act & Assert Assert.Throws <InvalidOperationException>( () => { IContentNegotiator ignore = result.ContentNegotiator; }, "HttpControllerContext.Configuration must not be null."); } }
public void IncludeErrorDetail_ForApiController_Throws_WhenControllerRequestIsNull() { // Arrange Exception exception = CreateException(); ApiController controller = CreateController(); Assert.Null(controller.Request); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) { controller.Configuration = configuration; ExceptionResult result = CreateProductUnderTest(exception, controller); // Act & Assert Assert.Throws <InvalidOperationException>(() => { bool ignore = result.IncludeErrorDetail; }, "ApiController.Request must not be null."); } }
public void Formatters_ForApiController_EvaluatesOnce() { // Arrange Exception exception = CreateException(); ApiController controller = CreateController(); MediaTypeFormatter expectedFormatter = CreateFormatter(); IContentNegotiator contentNegotiator = CreateDummyContentNegotiator(); using ( HttpConfiguration earlyConfiguration = CreateConfiguration( expectedFormatter, contentNegotiator ) ) using (HttpRequestMessage request = CreateRequest()) { controller.Configuration = earlyConfiguration; controller.Request = request; ExceptionResult result = CreateProductUnderTest(exception, controller); IEnumerable <MediaTypeFormatter> ignore = result.Formatters; using ( HttpConfiguration lateConfiguration = CreateConfiguration( CreateFormatter(), contentNegotiator ) ) { controller.Configuration = lateConfiguration; // Act IEnumerable <MediaTypeFormatter> formatters = result.Formatters; // Assert Assert.NotNull(formatters); MediaTypeFormatter formatter = Assert.Single(formatters); Assert.Same(expectedFormatter, formatter); } } }
public void Exception_ReturnsInstanceProvided() { // Arrange Exception expectedException = CreateException(); bool includeErrorDetail = true; IContentNegotiator contentNegotiator = CreateDummyContentNegotiator(); using (HttpRequestMessage request = CreateRequest()) { IEnumerable <MediaTypeFormatter> formatters = CreateFormatters(); ExceptionResult result = CreateProductUnderTest(expectedException, includeErrorDetail, contentNegotiator, request, formatters); // Act Exception exception = result.Exception; // Assert Assert.Same(expectedException, exception); } }
public void ContentNegotiator_ForApiController_Throws_WhenServiceIsNull() { // Arrange Exception exception = CreateException(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), null)) using (HttpRequestMessage request = CreateRequest()) { controller.Request = request; controller.Configuration = configuration; ExceptionResult result = CreateProductUnderTest(exception, controller); // Act & Assert Assert.Throws <InvalidOperationException>( () => { IContentNegotiator ignore = result.ContentNegotiator; }, "The provided configuration does not have an instance of the " + "'System.Net.Http.Formatting.IContentNegotiator' service registered."); } }
public void ApiControllerInternalServerError_WithException_CreatesCorrectResult() { // Arrange Exception expectedException = CreateException(); ApiController controller = CreateController(); // Act ExceptionResult result = controller.InternalServerError(expectedException); // Assert Assert.NotNull(result); Assert.Same(expectedException, result.Exception); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Configuration = configuration; controller.Request = expectedRequest; Assert.Same(expectedRequest, result.Request); } }