Пример #1
0
        public Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var errorContract = new ErrorContract
            {
                Message = Message,
                Errors  = Errors
            };

            var problemHeader = new MediaTypeHeaderValue("application/api-problem+json");

            var result = new FormattedContentResult <ErrorContract>(
                Status,
                errorContract,
                _configuration.Formatters.JsonFormatter,
                problemHeader,
                _controller);

            //var contentResult = _controller != null
            //    ? new FormattedContentResult<ErrorContract>(
            //        Status, errorContract, _configuration.Formatters.JsonFormatter, problemHeader, _controller)
            //    : new FormattedContentResult<ErrorContract>(
            //        Status, errorContract, _configuration.Formatters.JsonFormatter, problemHeader, _request);

            return(result.ExecuteAsync(cancellationToken));
        }
Пример #2
0
        /// <summary>Creates an <see cref="HttpResponseMessage"/> asynchronously.</summary>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A task that, when completed, contains the <see cref="HttpResponseMessage"/>.</returns>
        public Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var mediaTypeHeader = new MediaTypeHeaderValue(MediaTypeHeader.MediaType);

            var result = new FormattedContentResult <TResponse>(StatusCode, Response,
                                                                _dependencies.Formatter, mediaTypeHeader, _dependencies.RequestMessage);

            return(result.ExecuteAsync(cancellationToken));
        }
        public void ExecuteAsync_ForApiController_ReturnsCorrectResponse()
        {
            // Arrange
            HttpStatusCode expectedStatusCode = CreateStatusCode();
            object expectedContent = CreateContent();
            MediaTypeFormatter expectedFormatter = CreateFormatter();
            MediaTypeHeaderValue expectedMediaType = CreateMediaType();
            ApiController controller = CreateController();

            using (HttpRequestMessage expectedRequest = CreateRequest())
            {
                controller.Request = expectedRequest;
                IHttpActionResult result = new FormattedContentResult<object>(expectedStatusCode, expectedContent,
                    expectedFormatter, expectedMediaType, 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(expectedFormatter, typedContent.Formatter);
                    Assert.NotNull(typedContent.Headers);
                    Assert.Equal(expectedMediaType, typedContent.Headers.ContentType);
                    Assert.Equal(expectedRequest, response.RequestMessage);
                }
            }
        }