public void FormatFilter_ContextContainsFormat_DefaultFormat( string format, FormatSource place, string contentType) { // Arrange var mediaType = MediaTypeHeaderValue.Parse("application/json"); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); // Act filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_ContextContainsFormat_Custom( string format, FormatSource place, string contentType) { // Arrange var mediaType = MediaTypeHeaderValue.Parse(contentType); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( format, MediaTypeHeaderValue.Parse(contentType)); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_ContextContainsFormat_DefaultFormat( string format, FormatSource place, string contentType) { // Arrange var mediaType = MediaTypeHeaderValue.Parse("application/json"); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); // Act filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_IsActive( string format, FormatSource place, bool expected) { // Arrange var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var filterAttribute = new FormatFilterAttribute(); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act and Assert Assert.Equal(expected, filter.IsActive); }
public void FormatFilter_ContextDoesntContainFormat() { // Arrange var mockObjects = new MockObjects(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { }); var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextDoesntContainFormat() { // Arrange var mockObjects = new MockObjects(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsFormat_Invalid( string format, FormatSource place) { // Arrange var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsFormat_Invalid( string format, FormatSource place) { // Arrange var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { }); var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsFormat_ContainsProducesFilter_Matching( string format, FormatSource place, string contentType) { // Arrange var produces = new ProducesAttribute(contentType, new string[] { "application/foo", "text/bar" }); var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { produces }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsNonExistingFormat( string format, FormatSource place) { // Arrange var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var actionResult = resourceExecutingContext.Result; Assert.IsType <HttpNotFoundResult>(actionResult); }
public void FormatFilter_LessSpecificThan_Produces() { // Arrange var produces = new ProducesAttribute("application/xml;version=1", new string[] { }); var mockObjects = new MockObjects("xml", FormatSource.RouteData); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { produces }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml")); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsFormat_InRouteAndQueryData() { // If the format is present in both route and query data, the one in route data wins // Arrange var mediaType = MediaTypeHeaderValue.Parse("application/json"); var mockObjects = new MockObjects("json", FormatSource.RouteData); var httpContext = new Mock <HttpContext>(); // Query contains xml httpContext.Setup(c => c.Request.Query.ContainsKey("format")).Returns(true); httpContext.Setup(c => c.Request.Query.Get("format")).Returns("xml"); // Routedata contains json var data = new RouteData(); data.Values.Add("format", "json"); var ac = new ActionContext(httpContext.Object, data, new ActionDescriptor()); var resultExecutingContext = new ResultExecutingContext( ac, new IFilter[] { }, new ObjectResult("Hello!"), controller: new object()); var resourceExecutingContext = new ResourceExecutingContext( ac, new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType <ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_ContextContainsFormat_ContainsProducesFilter_Conflicting( string format, FormatSource place) { // Arrange var produces = new ProducesAttribute("application/xml", new string[] { "application/foo", "text/bar" }); var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { produces }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml")); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var result = Assert.IsType <HttpNotFoundResult>(resourceExecutingContext.Result); }
public void FormatFilter_MoreSpecificThan_Produces() { // Arrange var produces = new ProducesAttribute("application/xml", new string[] { }); var mockObjects = new MockObjects("xml", FormatSource.RouteData); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { produces }); mockObjects.Options.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml;version=1")); var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var actionResult = resourceExecutingContext.Result; Assert.IsType <HttpNotFoundResult>(actionResult); }
public void FormatFilter_ContextContainsFormat_ContainsProducesFilter_Conflicting( string format, FormatSource place) { // Arrange var produces = new ProducesAttribute("application/xml", new string[] { "application/foo", "text/bar" }); var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { produces }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml")); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var result = Assert.IsType<HttpNotFoundResult>(resourceExecutingContext.Result); }
public void FormatFilter_MoreSpecificThan_Produces() { // Arrange var produces = new ProducesAttribute("application/xml", new string[] { }); var mockObjects = new MockObjects("xml", FormatSource.RouteData); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { produces }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml;version=1")); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var actionResult = resourceExecutingContext.Result; Assert.IsType<HttpNotFoundResult>(actionResult); }
public void FormatFilter_ContextContainsNonExistingFormat( string format, FormatSource place) { // Arrange var mockObjects = new MockObjects(format, place); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert var actionResult = resourceExecutingContext.Result; Assert.IsType<HttpNotFoundResult>(actionResult); }
public void FormatFilter_ContextContainsFormat_Custom( string format, FormatSource place, string contentType) { // Arrange var mediaType = MediaTypeHeaderValue.Parse(contentType); var mockObjects = new MockObjects(format, place); var resultExecutingContext = mockObjects.CreateResultExecutingContext(); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilter[] { }); mockObjects.MockFormatterMappingOptions.FormatterMappings.SetMediaTypeMappingForFormat( format, MediaTypeHeaderValue.Parse(contentType)); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }
public void FormatFilter_LessSpecificThan_Produces() { // Arrange var produces = new ProducesAttribute("application/xml;version=1", new string[] { }); var mockObjects = new MockObjects("xml", FormatSource.RouteData); var resourceExecutingContext = mockObjects.CreateResourceExecutingContext(new IFilterMetadata[] { produces }); mockObjects.Options.FormatterMappings.SetMediaTypeMappingForFormat( "xml", MediaTypeHeaderValue.Parse("application/xml")); var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor); // Act filter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.Null(resourceExecutingContext.Result); }
public void FormatFilter_ContextContainsFormat_InRouteAndQueryData() { // If the format is present in both route and query data, the one in route data wins // Arrange var mediaType = MediaTypeHeaderValue.Parse("application/json"); var mockObjects = new MockObjects("json", FormatSource.RouteData); var httpContext = new Mock<HttpContext>(); // Query contains xml httpContext.Setup(c => c.Request.Query.ContainsKey("format")).Returns(true); httpContext.Setup(c => c.Request.Query.Get("format")).Returns("xml"); // Routedata contains json var data = new RouteData(); data.Values.Add("format", "json"); var ac = new ActionContext(httpContext.Object, data, new ActionDescriptor()); var resultExecutingContext = new ResultExecutingContext( ac, new IFilter[] { }, new ObjectResult("Hello!"), controller: new object()); var resourceExecutingContext = new ResourceExecutingContext( ac, new IFilter[] { }); var filter = new FormatFilter(mockObjects.IOptions, mockObjects.ScopedInstance); // Act filter.OnResourceExecuting(resourceExecutingContext); filter.OnResultExecuting(resultExecutingContext); // Assert var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result); Assert.Equal(1, objectResult.ContentTypes.Count); AssertMediaTypesEqual(mediaType, objectResult.ContentTypes[0]); }