public void Formatters_ForApiController_EvaluatesOnce()
        {
            // Arrange
            object             content           = CreateContent();
            ApiController      controller        = CreateController();
            MediaTypeFormatter expectedFormatter = CreateFormatter();
            IContentNegotiator contentNegotiator = CreateContentNegotiator();

            using (
                HttpConfiguration earlyConfiguration = CreateConfiguration(
                    expectedFormatter,
                    contentNegotiator
                    )
                )
                using (HttpRequestMessage request = CreateRequest())
                {
                    controller.Configuration = earlyConfiguration;
                    controller.Request       = request;

                    OkNegotiatedContentResult <object> result = CreateProductUnderTest(
                        content,
                        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 Request_ForApiController_Throws_WhenControllerRequestIsNull()
        {
            // Arrange
            object        content    = CreateContent();
            ApiController controller = CreateController();

            Assert.Null(controller.Request);

            using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateContentNegotiator()))
            {
                controller.Configuration = configuration;
                OkNegotiatedContentResult <object> result = CreateProductUnderTest(content, 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
            object                content    = CreateContent();
            ApiController         controller = CreateController();
            HttpControllerContext context    = new HttpControllerContext();

            using (HttpRequestMessage request = CreateRequest())
            {
                controller.ControllerContext = context;

                OkNegotiatedContentResult <object> result = CreateProductUnderTest(content, controller);

                // Act & Assert
                Assert.Throws <InvalidOperationException>(
                    () => { IContentNegotiator ignore = result.ContentNegotiator; },
                    "HttpControllerContext.Configuration must not be null.");
            }
        }
        public void ContentNegotiator_Returns_InstanceProvided()
        {
            // Arrange
            object             content = CreateContent();
            IContentNegotiator expectedContentNegotiator = CreateContentNegotiator();

            using (HttpRequestMessage request = CreateRequest())
            {
                IEnumerable <MediaTypeFormatter> formatters = CreateFormatters();

                OkNegotiatedContentResult <object> result = CreateProductUnderTest(content, expectedContentNegotiator,
                                                                                   request, formatters);

                // Act
                IContentNegotiator contentNegotiator = result.ContentNegotiator;

                // Assert
                Assert.Same(expectedContentNegotiator, contentNegotiator);
            }
        }
        public void ContentNegotiator_ForApiController_Throws_WhenServiceIsNull()
        {
            // Arrange
            object        content    = CreateContent();
            ApiController controller = CreateController();

            using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), null))
                using (HttpRequestMessage request = CreateRequest())
                {
                    controller.Configuration = configuration;

                    OkNegotiatedContentResult <object> result = CreateProductUnderTest(content, 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 ApiControllerOk_WithContent_CreatesCorrectResult()
        {
            // Arrange
            object        expectedContent = CreateContent();
            ApiController controller      = CreateController();

            // Act
            OkNegotiatedContentResult <object> result = controller.Ok(expectedContent);

            // Assert
            Assert.NotNull(result);
            Assert.Same(expectedContent, result.Content);

            using (HttpConfiguration configuration = CreateConfiguration(CreateFormatter(), CreateContentNegotiator()))
                using (HttpRequestMessage expectedRequest = CreateRequest())
                {
                    controller.Configuration = configuration;
                    controller.Request       = expectedRequest;
                    Assert.Same(expectedRequest, result.Request);
                }
        }
        public void Request_ForApiController_EvaluatesOnce()
        {
            // Arrange
            object        content    = CreateContent();
            ApiController controller = CreateController();

            using (
                HttpConfiguration configuration = CreateConfiguration(
                    CreateFormatter(),
                    CreateContentNegotiator()
                    )
                )
            {
                controller.Configuration = configuration;

                OkNegotiatedContentResult <object> result = CreateProductUnderTest(
                    content,
                    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_EvaluatesLazily()
        {
            // Arrange
            object        content    = CreateContent();
            ApiController controller = CreateController();

            using (
                HttpConfiguration configuration = CreateConfiguration(
                    CreateFormatter(),
                    CreateContentNegotiator()
                    )
                )
            {
                controller.Configuration = configuration;

                using (HttpRequestMessage request = CreateRequest())
                {
                    controller.Request = request;

                    OkNegotiatedContentResult <object> result = CreateProductUnderTest(
                        content,
                        controller
                        );

                    IContentNegotiator expectedContentNegotiator = CreateContentNegotiator();
                    configuration.Services.Replace(
                        typeof(IContentNegotiator),
                        expectedContentNegotiator
                        );

                    // Act
                    IContentNegotiator contentNegotiator = result.ContentNegotiator;

                    // Assert
                    Assert.Same(expectedContentNegotiator, contentNegotiator);
                }
            }
        }