public void Formatters_ForApiController_EvaluatesOnce()
        {
            // Arrange
            Uri                location          = CreateLocation();
            object             content           = CreateContent();
            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;

                    CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(location, 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 ContentNegotiator_ForApiController_EvaluatesOnce()
        {
            // Arrange
            Uri                location = CreateLocation();
            object             content  = CreateContent();
            IContentNegotiator expectedContentNegotiator = CreateDummyContentNegotiator();
            ApiController      controller = CreateController();

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

                    CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(location, content, 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_EvaluatesOnce()
        {
            // Arrange
            Uri           location   = CreateLocation();
            object        content    = CreateContent();
            ApiController controller = CreateController();

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

                CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(location, 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);
                    }
                }
            }
        }
        [InlineData("http://example.com")] // Absolute URL with host
        public void ApiControllerCreated_WithStringAndContent_CreatesCorrectResult(string location)
        {
            // Arrange
            object        expectedContent = CreateContent();
            ApiController controller      = CreateController();

            // Act
            CreatedNegotiatedContentResult <object> result = controller.Created(
                location,
                expectedContent
                );

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

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

            Assert.Null(controller.Request);

            using (
                HttpConfiguration configuration = CreateConfiguration(
                    CreateFormatter(),
                    CreateDummyContentNegotiator()
                    )
                )
            {
                controller.Configuration = configuration;
                CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(
                    location,
                    content,
                    controller
                    );

                // Act & Assert
                InvalidOperationException exception = Assert.Throws <InvalidOperationException>(
                    () =>
                {
                    HttpRequestMessage ignore = result.Request;
                },
                    "ApiController.Request must not be null."
                    );
            }
        }
        public void ContentNegotiator_ForApiController_Throws_WhenServiceIsNull()
        {
            // Arrange
            Uri           location   = CreateLocation();
            object        content    = CreateContent();
            ApiController controller = CreateController();

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

                    CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(
                        location,
                        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 ContentNegotiator_ForApiController_Throws_WhenConfigurationIsNull()
        {
            // Arrange
            Uri                   location   = CreateLocation();
            object                content    = CreateContent();
            ApiController         controller = CreateController();
            HttpControllerContext context    = new HttpControllerContext();

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

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

                // Act & Assert
                Assert.Throws <InvalidOperationException>(
                    () =>
                {
                    IContentNegotiator ignore = result.ContentNegotiator;
                },
                    "HttpControllerContext.Configuration must not be null."
                    );
            }
        }
        public void Formatters_Returns_InstanceProvided()
        {
            // Arrange
            Uri                location          = CreateLocation();
            object             content           = CreateContent();
            IContentNegotiator contentNegotiator = CreateDummyContentNegotiator();

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

                CreatedNegotiatedContentResult <object> result = CreateProductUnderTest(
                    location,
                    content,
                    contentNegotiator,
                    request,
                    expectedFormatters
                    );

                // Act
                IEnumerable <MediaTypeFormatter> formatters = result.Formatters;

                // Assert
                Assert.Same(expectedFormatters, formatters);
            }
        }