public void Formatters_ForApiController_EvaluatesOnce() { // Arrange string message = CreateMessage(); 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; ErrorMessageResult result = CreateProductUnderTest(message, 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 Request_ForApiController_EvaluatesOnce() { // Arrange string message = CreateMessage(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) { controller.Configuration = configuration; ErrorMessageResult result = CreateProductUnderTest(message, 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 ContentNegotiator_ForApiController_EvaluatesOnce() { // Arrange string message = CreateMessage(); IContentNegotiator expectedContentNegotiator = CreateDummyContentNegotiator(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), expectedContentNegotiator)) using (HttpRequestMessage request = CreateRequest()) { controller.Configuration = configuration; controller.Request = request; ErrorMessageResult result = CreateProductUnderTest(message, controller); IContentNegotiator ignore = result.ContentNegotiator; configuration.Services.Replace(typeof(IContentNegotiator), CreateDummyContentNegotiator()); // Act IContentNegotiator contentNegotiator = result.ContentNegotiator; // Assert Assert.Same(expectedContentNegotiator, contentNegotiator); } }
public void Request_ForApiController_Throws_WhenControllerRequestIsNull() { // Arrange string message = CreateMessage(); ApiController controller = CreateController(); Assert.Null(controller.Request); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) { controller.Configuration = configuration; ErrorMessageResult result = CreateProductUnderTest(message, controller); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => { HttpRequestMessage ignore = result.Request; }, "ApiController.Request must not be null."); } }
public void ContentNegotiator_ForApiController_Throws_WhenConfigurationIsNull() { // Arrange string message = CreateMessage(); ApiController controller = CreateController(); HttpControllerContext context = new HttpControllerContext(); using (HttpRequestMessage request = CreateRequest()) { controller.ControllerContext = context; ErrorMessageResult result = CreateProductUnderTest(message, controller); // Act & Assert Assert.Throws <InvalidOperationException>( () => { IContentNegotiator ignore = result.ContentNegotiator; }, "HttpControllerContext.Configuration must not be null."); } }
public void Message_ReturnsInstanceProvided() { // Arrange string expectedMessage = CreateMessage(); IContentNegotiator contentNegotiator = CreateDummyContentNegotiator(); using (HttpRequestMessage request = CreateRequest()) { IEnumerable <MediaTypeFormatter> formatters = CreateFormatters(); ErrorMessageResult result = CreateProductUnderTest(expectedMessage, contentNegotiator, request, formatters); // Act string message = result.Message; // Assert Assert.Same(expectedMessage, message); } }
public void ContentNegotiator_ForApiController_Throws_WhenServiceIsNull() { // Arrange string message = CreateMessage(); ApiController controller = CreateController(); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), null)) using (HttpRequestMessage request = CreateRequest()) { controller.Request = request; controller.Configuration = configuration; ErrorMessageResult result = CreateProductUnderTest(message, 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 ApiControllerBadRequest_WithString_CreatesCorrectResult() { // Arrange string expectedMessage = CreateMessage(); ApiController controller = CreateController(); // Act ErrorMessageResult result = controller.BadRequest(expectedMessage); // Assert Assert.NotNull(result); Assert.Same(expectedMessage, result.Message); using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateDummyContentNegotiator())) using (HttpRequestMessage expectedRequest = CreateRequest()) { controller.Configuration = configuration; controller.Request = expectedRequest; Assert.Same(expectedRequest, result.Request); } }