public async Task ExecuteAsync_ForProblemDetailsValue_UsesProblemDetailsContentType() { // Arrange var executor = CreateExecutor(); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext() { HttpContext = httpContext }; httpContext.Response.ContentType = "application/json"; // This will not be used var result = new ObjectResult(new ProblemDetails()); result.Formatters.Add(new TestXmlOutputFormatter()); // This will be chosen based on the problem details content type result.Formatters.Add(new TestJsonOutputFormatter()); result.Formatters.Add(new TestStringOutputFormatter()); // Act await executor.ExecuteAsync(actionContext, result); // Assert MediaTypeAssert.Equal("application/problem+xml; charset=utf-8", httpContext.Response.ContentType); }
public async Task ObjectResult_PerformsContentNegotiation_OnAllMediaRangeAcceptHeaderMediaType( string acceptHeader, string expectedContentType) { // Arrange var options = new MvcOptions(); options.RespectBrowserAcceptHeader = true; var executor = CreateExecutor(options: options); var result = new ObjectResult("input"); result.Formatters.Add(new TestJsonOutputFormatter()); result.Formatters.Add(new TestXmlOutputFormatter()); var actionContext = new ActionContext() { HttpContext = GetHttpContext(), }; actionContext.HttpContext.Request.Headers[HeaderNames.Accept] = acceptHeader; // Act await executor.ExecuteAsync(actionContext, result); // Assert var responseContentType = actionContext.HttpContext.Response.Headers[HeaderNames.ContentType]; MediaTypeAssert.Equal(expectedContentType, responseContentType); }
public async Task ExecuteAsync_UsesSpecifiedContentType() { // Arrange var executor = CreateExecutor(); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext() { HttpContext = httpContext }; httpContext.Request.Headers.Accept = "application/xml"; // This will not be used httpContext.Response.ContentType = "text/json"; var result = new ObjectResult("input") { ContentTypes = { "text/xml", }, }; result.Formatters.Add(new TestXmlOutputFormatter()); result.Formatters.Add(new TestJsonOutputFormatter()); result.Formatters.Add(new TestStringOutputFormatter()); // This will be chosen based on the content type // Act await executor.ExecuteAsync(actionContext, result); // Assert MediaTypeAssert.Equal("text/xml; charset=utf-8", httpContext.Response.ContentType); }
public void FormatFilter_ContextContainsFormat_DefaultFormat(string format, FormatSource place) { // Arrange var mediaType = new StringSegment("application/json"); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { }); var filter = new FormatFilter(mockObjects.OptionsManager, NullLoggerFactory.Instance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); // Act filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Single(objectResult.ContentTypes); MediaTypeAssert.Equal(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_ContextContainsFormat_Custom( string format, FormatSource place, string contentType) { // Arrange var mediaType = new StringSegment(contentType); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { }); mockObjects.MvcOptions.FormatterMappings.SetMediaTypeMappingForFormat( format, MediaTypeHeaderValue.Parse(contentType)); var filter = new FormatFilter(mockObjects.OptionsManager, NullLoggerFactory.Instance); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Single(objectResult.ContentTypes); MediaTypeAssert.Equal(mediaType, objectResult.ContentTypes[0]); }
public async Task ExecuteAsync_ContentTypeProvidedFromResponseAndObjectResult_UsesResponseContentType() { // Arrange var executor = CreateCustomObjectResultExecutor(); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext() { HttpContext = httpContext }; httpContext.Request.Headers[HeaderNames.Accept] = "application/xml"; // This will not be used httpContext.Response.ContentType = "text/plain"; var result = new ObjectResult("input"); result.Formatters.Add(new TestXmlOutputFormatter()); result.Formatters.Add(new TestJsonOutputFormatter()); result.Formatters.Add(new TestStringOutputFormatter()); // This will be chosen based on the content type // Act await executor.ExecuteAsync(actionContext, result); // Assert Assert.IsType <TestStringOutputFormatter>(executor.SelectedOutputFormatter); MediaTypeAssert.Equal("text/plain; charset=utf-8", httpContext.Response.ContentType); }
public void SelectFormatter_WithNoProvidedContentType_DoesConneg() { // Arrange var executor = CreateExecutor(); var formatters = new List <IOutputFormatter> { new TestXmlOutputFormatter(), new TestJsonOutputFormatter(), // This will be chosen based on the accept header }; var context = new OutputFormatterWriteContext( new DefaultHttpContext(), new TestHttpResponseStreamWriterFactory().CreateWriter, objectType: null, @object: null); context.HttpContext.Request.Headers[HeaderNames.Accept] = "application/json"; // Act var formatter = executor.SelectFormatter( context, new MediaTypeCollection { "application/json" }, formatters); // Assert Assert.Same(formatters[1], formatter); MediaTypeAssert.Equal("application/json", context.ContentType); }
public void FormatFilter_ExplicitContentType_SetOnObjectResult_TakesPrecedence() { // Arrange var mediaType = new StringSegment("application/foo"); var mockObjects = new MockObjects("json", FormatSource.QueryData); var httpContext = new Mock <HttpContext>(); httpContext.Setup(c => c.Response).Returns(new Mock <HttpResponse>().Object); httpContext.Setup(c => c.Request.Query["format"]).Returns("json"); var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()); var objectResult = new ObjectResult("Hello!"); objectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/foo")); var resultExecutingContext = new ResultExecutingContext( actionContext, new IFilterMetadata[] { }, objectResult, controller: new object()); var resourceExecutingContext = new ResourceExecutingContext( actionContext, new IFilterMetadata[] { }); var filter = new FormatFilter(mockObjects.OptionsManager); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var result = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, result.ContentTypes.Count); MediaTypeAssert.Equal(mediaType, result.ContentTypes[0]); }