public void Request_ForApiController_EvaluatesOnce() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) { controller.Configuration = configuration; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Request = expectedRequest; HttpRequestMessage ignore = result.Request; using (HttpRequestMessage otherRequest = CreateRequest()) { controller.Request = otherRequest; // Act HttpRequestMessage request = result.Request; // Assert Assert.Same(expectedRequest, request); } } } }
public void Formatters_ForApiController_EvaluatesOnce() { // Arrange ModelStateDictionary modelState = CreateModelState(); 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; InvalidModelStateResult result = CreateProductUnderTest(modelState, 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); Assert.Equal(1, formatters.Count()); Assert.Same(expectedFormatter, formatters.Single()); } } }
public void IncludeErrorDetail_ForApiController_EvaluatesOnce() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) using (HttpRequestMessage request = CreateRequest()) { HttpRequestContext requestContext = new HttpRequestContext { Configuration = configuration, IncludeErrorDetail = true }; controller.RequestContext = requestContext; controller.Request = request; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); bool ignore = result.IncludeErrorDetail; requestContext.IncludeErrorDetail = false; // Act bool includeErrorDetail = result.IncludeErrorDetail; // Assert Assert.Equal(true, includeErrorDetail); } }
public void ContentNegotiator_ForApiController_EvaluatesOnce() { // Arrange ModelStateDictionary modelState = CreateModelState(); IContentNegotiator expectedContentNegotiator = CreateDummyContentNegotiator(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), expectedContentNegotiator)) using (HttpRequestMessage request = CreateRequest()) { controller.Configuration = configuration; controller.Request = request; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); IContentNegotiator ignore = result.ContentNegotiator; configuration.Services.Replace(typeof(IContentNegotiator), CreateDummyContentNegotiator()); // Act IContentNegotiator contentNegotiator = result.ContentNegotiator; // Assert Assert.Same(expectedContentNegotiator, contentNegotiator); } }
public void IncludeErrorDetail_ForApiController_EvaluatesLazily() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) { configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; controller.Configuration = configuration; using (HttpRequestMessage request = CreateRequest()) { controller.Request = request; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never; // Act IContentNegotiator contentNegotiator = result.ContentNegotiator; // Assert Assert.Equal(false, result.IncludeErrorDetail); } } }
public void ApiControllerBadRequest_WithModelStateDictionary_CreatesCorrectResult() { // Arrange ModelStateDictionary expectedModelState = CreateModelState(); ApiController controller = CreateController(); // Act InvalidModelStateResult result = controller.BadRequest(expectedModelState); // Assert Assert.NotNull(result); Assert.Same(expectedModelState, result.ModelState); using ( HttpConfiguration configuration = CreateConfiguration( CreateFormatter(), CreateDummyContentNegotiator() ) ) using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Configuration = configuration; controller.Request = expectedRequest; Assert.Same(expectedRequest, result.Request); } }
public void ContentNegotiator_ForApiController_Throws_WhenServiceIsNull() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), null)) using (HttpRequestMessage request = CreateRequest()) { controller.Request = request; controller.Configuration = configuration; InvalidModelStateResult result = CreateProductUnderTest(modelState, 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 IncludeErrorDetail_ForApiController_Throws_WhenControllerRequestIsNull() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); Assert.Null(controller.Request); using ( HttpConfiguration configuration = CreateConfiguration( CreateFormatter(), CreateDummyContentNegotiator() ) ) { controller.Configuration = configuration; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>( () => { bool ignore = result.IncludeErrorDetail; }, "ApiController.Request must not be null." ); } }
public void Formatters_ReturnsInstanceProvided() { // Arrange ModelStateDictionary modelState = CreateModelState(); bool includeErrorDetail = true; IContentNegotiator contentNegotiator = CreateDummyContentNegotiator(); using (HttpRequestMessage request = CreateRequest()) { IEnumerable <MediaTypeFormatter> expectedFormatters = CreateFormatters(); InvalidModelStateResult result = CreateProductUnderTest( modelState, includeErrorDetail, contentNegotiator, request, expectedFormatters ); // Act IEnumerable <MediaTypeFormatter> formatters = result.Formatters; // Assert Assert.Same(expectedFormatters, formatters); } }
public void ContentNegotiator_ForApiController_Throws_WhenConfigurationIsNull() { // Arrange ModelStateDictionary modelState = CreateModelState(); ApiController controller = CreateController(); HttpControllerContext context = new HttpControllerContext(); using (HttpRequestMessage request = CreateRequest()) { controller.ControllerContext = context; InvalidModelStateResult result = CreateProductUnderTest(modelState, controller); // Act & Assert Assert.Throws <InvalidOperationException>( () => { IContentNegotiator ignore = result.ContentNegotiator; }, "HttpControllerContext.Configuration must not be null."); } }