AspNetWebStack/test/System.Web.Http.OData.Test/ODataHttpConfigurationExten...

137 строки
5.2 KiB
C#
Исходник Обычный вид История

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Formatter;
using System.Web.Http.OData.Query;
using System.Web.Http.OData.Routing;
using System.Web.Http.OData.TestCommon.Models;
2013-03-23 05:13:41 +04:00
using System.Web.Http.Services;
using System.Web.Http.Tracing;
using Microsoft.Data.Edm;
using Microsoft.Data.Edm.Library;
2012-11-30 04:18:13 +04:00
using Microsoft.Data.OData;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.OData
{
public class ODataHttpConfigurationExtensionTest
{
[Fact]
public void SetODataFormatter_AddsFormatterToTheFormatterCollection()
{
// Arrange
HttpConfiguration configuration = new HttpConfiguration();
2012-11-30 04:18:13 +04:00
ODataMediaTypeFormatter formatter = CreateODataFormatter();
// Act
configuration.Formatters.Insert(0, formatter);
// Assert
Assert.Contains(formatter, configuration.Formatters);
}
[Fact]
public void IsODataFormatter_ReturnsTrue_ForODataFormatters()
{
// Arrange
HttpConfiguration configuration = new HttpConfiguration();
2012-11-30 04:18:13 +04:00
ODataMediaTypeFormatter formatter1 = CreateODataFormatter();
ODataMediaTypeFormatter formatter2 = CreateODataFormatter();
configuration.Formatters.Add(formatter1);
configuration.Formatters.Add(formatter2);
// Act
2013-03-23 05:13:41 +04:00
IEnumerable<MediaTypeFormatter> result = configuration.Formatters.Where(f => f != null && Decorator.GetInner(f) is ODataMediaTypeFormatter);
// Assert
IEnumerable<MediaTypeFormatter> expectedFormatters = new MediaTypeFormatter[]
{
formatter1, formatter2
};
Assert.True(expectedFormatters.SequenceEqual(result));
}
[Fact]
public void IsODataFormatter_ReturnsTrue_For_Derived_ODataFormatters()
{
// Arrange
HttpConfiguration configuration = new HttpConfiguration();
ODataMediaTypeFormatter formatter1 = CreateODataFormatter();
DerivedODataMediaTypeFormatter formatter2 = new DerivedODataMediaTypeFormatter(new ODataPayloadKind[0]);
configuration.Formatters.Add(formatter1);
configuration.Formatters.Add(formatter2);
// Act
IEnumerable<MediaTypeFormatter> result = configuration.Formatters.Where(f => f != null && Decorator.GetInner(f) is ODataMediaTypeFormatter);
// Assert
2012-11-30 04:18:13 +04:00
IEnumerable<MediaTypeFormatter> expectedFormatters = new MediaTypeFormatter[]
{
formatter1, formatter2
};
Assert.True(expectedFormatters.SequenceEqual(result));
}
[Fact]
public void AddQuerySupport_AddsQueryableFilterProvider()
{
HttpConfiguration configuration = new HttpConfiguration();
configuration.EnableQuerySupport();
var queryFilterProviders = configuration.Services.GetFilterProviders().OfType<QueryFilterProvider>();
Assert.Equal(1, queryFilterProviders.Count());
var queryAttribute = Assert.IsType<QueryableAttribute>(queryFilterProviders.First().QueryFilter);
}
[Fact]
public void AddQuerySupport_AddsFilterProviderForQueryFilter()
{
HttpConfiguration configuration = new HttpConfiguration();
Mock<IActionFilter> myQueryFilter = new Mock<IActionFilter>();
configuration.EnableQuerySupport(myQueryFilter.Object);
var queryFilterProviders = configuration.Services.GetFilterProviders().OfType<QueryFilterProvider>();
Assert.Equal(1, queryFilterProviders.Count());
Assert.Same(myQueryFilter.Object, queryFilterProviders.First().QueryFilter);
}
[Fact]
public void AddQuerySupport_ActionFilters_TakePrecedence()
{
HttpConfiguration config = new HttpConfiguration();
config.EnableQuerySupport();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(config, "FilterProviderTest", typeof(FilterProviderTestController));
HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(FilterProviderTestController).GetMethod("GetQueryableWithFilterAttribute"));
Collection<FilterInfo> filters = actionDescriptor.GetFilterPipeline();
Assert.Equal(1, filters.Count);
2012-12-17 23:38:55 +04:00
Assert.Equal(100, ((QueryableAttribute)filters[0].Instance).PageSize);
}
2012-11-30 04:18:13 +04:00
private static ODataMediaTypeFormatter CreateODataFormatter()
{
return new ODataMediaTypeFormatter(new ODataPayloadKind[0]);
2012-11-30 04:18:13 +04:00
}
2013-03-23 05:13:41 +04:00
private class DerivedODataMediaTypeFormatter: ODataMediaTypeFormatter
{
public DerivedODataMediaTypeFormatter(IEnumerable<ODataPayloadKind> payloadKinds)
: base(payloadKinds)
{
}
}
}
}