コード例 #1
0
        public async Task ExecuteAsync_Returns_CorrectResponse_WhenContentNegotiationFails()
        {
            // Arrange
            HttpStatusCode           statusCode        = HttpStatusCode.Conflict;
            object                   content           = CreateContent();
            ContentNegotiationResult negotiationResult = null;

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

                Mock <IContentNegotiator> spy = new Mock <IContentNegotiator>();
                spy.Setup(n => n.Negotiate(typeof(object), expectedRequest, expectedFormatters)).Returns(
                    negotiationResult);
                IContentNegotiator contentNegotiator = spy.Object;

                IHttpActionResult result = new NegotiatedContentResult <object>(statusCode, content, contentNegotiator,
                                                                                expectedRequest, expectedFormatters);

                // Act
                Task <HttpResponseMessage> task = result.ExecuteAsync(CancellationToken.None);

                // Assert
                Assert.NotNull(task);

                using (HttpResponseMessage response = await task)
                {
                    Assert.NotNull(response);
                    Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode);
                    Assert.Same(expectedRequest, response.RequestMessage);
                }
            }
        }
コード例 #2
0
        public void ExecuteAsync_ForApiController_ReturnsCorrectResponse_WhenContentNegotationSucceeds()
        {
            // Arrange
            HttpStatusCode           expectedStatusCode      = CreateStatusCode();
            object                   expectedContent         = CreateContent();
            ApiController            controller              = CreateController();
            MediaTypeFormatter       expectedInputFormatter  = CreateFormatter();
            MediaTypeFormatter       expectedOutputFormatter = CreateFormatter();
            MediaTypeHeaderValue     expectedMediaType       = CreateMediaType();
            ContentNegotiationResult negotiationResult       = new ContentNegotiationResult(expectedOutputFormatter,
                                                                                            expectedMediaType);

            Expression <Func <IEnumerable <MediaTypeFormatter>, bool> > formattersMatch = (f) =>
                                                                                          f != null && f.AsArray().Length == 1 && f.AsArray()[0] == expectedInputFormatter;

            using (HttpRequestMessage expectedRequest = CreateRequest())
            {
                Mock <IContentNegotiator> spy = new Mock <IContentNegotiator>();
                spy.Setup(n => n.Negotiate(typeof(object), expectedRequest, It.Is(formattersMatch))).Returns(
                    negotiationResult);
                IContentNegotiator contentNegotiator = spy.Object;

                using (HttpConfiguration configuration = CreateConfiguration(expectedInputFormatter,
                                                                             contentNegotiator))
                {
                    controller.Configuration = configuration;
                    controller.Request       = expectedRequest;

                    IHttpActionResult result = new NegotiatedContentResult <object>(expectedStatusCode, expectedContent,
                                                                                    controller);

                    // Act
                    Task <HttpResponseMessage> task = result.ExecuteAsync(CancellationToken.None);

                    // Assert
                    Assert.NotNull(task);
                    task.WaitUntilCompleted();

                    using (HttpResponseMessage response = task.Result)
                    {
                        Assert.NotNull(response);
                        Assert.Equal(expectedStatusCode, response.StatusCode);
                        HttpContent content = response.Content;
                        Assert.IsType <ObjectContent <object> >(content);
                        ObjectContent <object> typedContent = (ObjectContent <object>)content;
                        Assert.Same(expectedContent, typedContent.Value);
                        Assert.Same(expectedOutputFormatter, typedContent.Formatter);
                        Assert.NotNull(typedContent.Headers);
                        Assert.Equal(expectedMediaType, typedContent.Headers.ContentType);
                        Assert.Same(expectedRequest, response.RequestMessage);
                    }
                }
            }
        }
コード例 #3
0
        public async Task ExecuteAsync_Returns_CorrectResponse_WhenContentNegotiationSucceeds()
        {
            // Arrange
            HttpStatusCode           expectedStatusCode = CreateStatusCode();
            object                   expectedContent    = CreateContent();
            MediaTypeFormatter       expectedFormatter  = CreateFormatter();
            MediaTypeHeaderValue     expectedMediaType  = CreateMediaType();
            ContentNegotiationResult negotiationResult  = new ContentNegotiationResult(
                expectedFormatter,
                expectedMediaType
                );

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

                Mock <IContentNegotiator> spy = new Mock <IContentNegotiator>();
                spy.Setup(n => n.Negotiate(typeof(object), expectedRequest, expectedFormatters))
                .Returns(negotiationResult);
                IContentNegotiator contentNegotiator = spy.Object;

                IHttpActionResult result = new NegotiatedContentResult <object>(
                    expectedStatusCode,
                    expectedContent,
                    contentNegotiator,
                    expectedRequest,
                    expectedFormatters
                    );

                // Act
                Task <HttpResponseMessage> task = result.ExecuteAsync(CancellationToken.None);

                // Assert
                Assert.NotNull(task);

                using (HttpResponseMessage response = await task)
                {
                    Assert.NotNull(response);
                    Assert.Equal(expectedStatusCode, response.StatusCode);
                    HttpContent            content      = response.Content;
                    ObjectContent <object> typedContent = Assert.IsType <ObjectContent <object> >(
                        content
                        );
                    Assert.Same(expectedContent, typedContent.Value);
                    Assert.Same(expectedFormatter, typedContent.Formatter);
                    Assert.NotNull(typedContent.Headers);
                    Assert.Equal(expectedMediaType, typedContent.Headers.ContentType);
                    Assert.Same(expectedRequest, response.RequestMessage);
                }
            }
        }