Ejemplo n.º 1
0
        public async Task ObjectResult_WithMultipleContentTypesAndAcceptHeaders_PerformsContentNegotiation(
            IEnumerable <string> contentTypes, string acceptHeader, string expectedHeader)
        {
            // Arrange
            var expectedContentType = expectedHeader;
            var input  = "testInput";
            var stream = new MemoryStream();

            var httpResponse    = new Mock <HttpResponse>();
            var tempContentType = string.Empty;

            httpResponse.SetupProperty <string>(o => o.ContentType);
            httpResponse.SetupGet(r => r.Body).Returns(stream);

            var actionContext = CreateMockActionContext(httpResponse.Object, acceptHeader);
            var result        = new ObjectResult(input);

            // Set the content type property explicitly.
            result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList();
            result.Formatters   = new List <IOutputFormatter>
            {
                new CannotWriteFormatter(),
                new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // should always select the Json Output formatter even though it is second in the list.
            httpResponse.VerifySet(r => r.ContentType = expectedContentType);
        }
Ejemplo n.º 2
0
        ObjectResult_NoContentTypeSetWithNoAcceptHeadersAndNoRequestContentType_PicksFirstFormatterWhichCanWrite()
        {
            // Arrange
            var stream = new MemoryStream();
            var expectedContentType = "application/json;charset=utf-8";
            var httpResponse        = new Mock <HttpResponse>();

            httpResponse.SetupProperty <string>(o => o.ContentType);
            httpResponse.SetupGet(r => r.Body).Returns(stream);

            var actionContext = CreateMockActionContext(httpResponse.Object,
                                                        requestAcceptHeader: null,
                                                        requestContentType: null);
            var input  = "testInput";
            var result = new ObjectResult(input);

            // Set more than one formatters. The test output formatter throws on write.
            result.Formatters = new List <IOutputFormatter>
            {
                new CannotWriteFormatter(),
                new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
            };
            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Asserts that content type is not text/custom.
            httpResponse.VerifySet(r => r.ContentType = expectedContentType);
        }
Ejemplo n.º 3
0
        public async Task ObjectResult_NoContentTypeSetWithAcceptHeaders_PicksFormatterOnAcceptHeaders()
        {
            // Arrange
            var expectedContentType = "application/json;charset=utf-8";
            var input  = "testInput";
            var stream = new MemoryStream();

            var httpResponse  = GetMockHttpResponse();
            var actionContext =
                CreateMockActionContext(httpResponse.Object,
                                        requestAcceptHeader: "text/custom;q=0.1,application/json;q=0.9",
                                        requestContentType: "application/custom");
            var result = new ObjectResult(input);

            // Set more than one formatters. The test output formatter throws on write.
            result.Formatters = new List <IOutputFormatter>
            {
                new CannotWriteFormatter(),
                new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
            };

            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Asserts that content type is not text/custom. i.e the formatter is not TestOutputFormatter.
            httpResponse.VerifySet(r => r.ContentType = expectedContentType);
        }
Ejemplo n.º 4
0
        public async Task ObjectResult_MultipleFormattersSupportingTheSameContentType_SelectsTheFirstFormatterInList()
        {
            // Arrange
            var input  = "testInput";
            var stream = new MemoryStream();

            var httpResponse  = GetMockHttpResponse();
            var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null);
            var result        = new ObjectResult(input);

            // It should select the mock formatter as that is the first one in the list.
            var contentTypes         = new[] { "application/json", "text/custom" };
            var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse("text/custom");

            // Get a  mock formatter which supports everything.
            var mockFormatter = GetMockFormatter();

            result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList();
            result.Formatters   = new List <IOutputFormatter>
            {
                mockFormatter.Object,
                new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
                new CannotWriteFormatter()
            };
            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Verify that mock formatter was chosen.
            mockFormatter.Verify(o => o.WriteAsync(It.IsAny <OutputFormatterContext>()));
        }
Ejemplo n.º 5
0
        public async Task ObjectResult_MultipleContentTypes_PicksFirstFormatterWhichSupportsAnyOfTheContentTypes()
        {
            // Arrange
            var expectedContentType = "application/json;charset=utf-8";
            var input         = "testInput";
            var httpResponse  = GetMockHttpResponse();
            var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null);
            var result        = new ObjectResult(input);

            // It should not select TestOutputFormatter,
            // This is because it should accept the first formatter which supports any of the two contentTypes.
            var contentTypes = new[] { "application/custom", "application/json" };

            // Set the content type and the formatters property explicitly.
            result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType))
                                  .ToList();
            result.Formatters = new List <IOutputFormatter>
            {
                new CannotWriteFormatter(),
                new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
            };
            // Act
            await result.ExecuteResultAsync(actionContext);

            // Assert
            // Asserts that content type is not text/custom.
            httpResponse.VerifySet(r => r.ContentType = expectedContentType);
        }
Ejemplo n.º 6
0
        public IActionResult OverrideTheFallback_UsingCustomFormatters(int input)
        {
            var objectResult = new ObjectResult(input);

            objectResult.Formatters.Add(new HttpNotAcceptableOutputFormatter());
            objectResult.Formatters.Add(new PlainTextFormatter());
            objectResult.Formatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false));
            return(objectResult);
        }
Ejemplo n.º 7
0
        public IActionResult UseTheFallback_UsingCustomFormatters(int input)
        {
            var objectResult = new ObjectResult(input);

            // Request content type is application/custom.
            // PlainTextFormatter cannot write because it does not support the type.
            // JsonOutputFormatter cannot write in the first attempt because it does not support the
            // request content type.
            objectResult.Formatters.Add(new PlainTextFormatter());
            objectResult.Formatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false));

            return(objectResult);
        }
Ejemplo n.º 8
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            var result = context.Result as ObjectResult;

            if (result != null)
            {
                result.Formatters.Add(new PlainTextFormatter());
                result.Formatters.Add(new CustomFormatter("application/custom"));
                result.Formatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(),
                                                              indent: true));
            }

            base.OnActionExecuted(context);
        }
Ejemplo n.º 9
0
        public async Task ObjectResult_Execute_CallsJsonResult_SetsContent()
        {
            // Arrange
            var expectedContentType = "application/json;charset=utf-8";
            var nonStringValue      = new { x1 = 10, y1 = "Hello" };
            var httpResponse        = Mock.Of <HttpResponse>();

            httpResponse.Body = new MemoryStream();
            var actionContext    = CreateMockActionContext(httpResponse);
            var tempStream       = new MemoryStream();
            var tempHttpContext  = new Mock <HttpContext>();
            var tempHttpResponse = new Mock <HttpResponse>();

            tempHttpResponse.SetupGet(o => o.Body).Returns(tempStream);
            tempHttpResponse.SetupProperty <string>(o => o.ContentType);
            tempHttpContext.SetupGet(o => o.Response).Returns(tempHttpResponse.Object);
            tempHttpContext.SetupGet(o => o.Request.AcceptCharset).Returns(string.Empty);
            var tempActionContext = new ActionContext(tempHttpContext.Object,
                                                      new RouteData(),
                                                      new ActionDescriptor());
            var formatterContext = new OutputFormatterContext()
            {
                ActionContext = tempActionContext,
                Object        = nonStringValue,
                DeclaredType  = nonStringValue.GetType()
            };
            var formatter = new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false);

            formatter.WriteResponseContentHeaders(formatterContext);
            await formatter.WriteAsync(formatterContext);

            // Act
            var result = new ObjectResult(nonStringValue);
            await result.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal(expectedContentType, httpResponse.ContentType);
            Assert.Equal(tempStream.ToArray(), ((MemoryStream)actionContext.HttpContext.Response.Body).ToArray());
        }