2012-08-14 02:03:41 +04:00
|
|
|
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
|
|
|
|
|
2012-10-12 00:58:28 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
2012-10-19 04:48:18 +04:00
|
|
|
|
using System.Net.Http.Formatting;
|
2012-10-12 00:58:28 +04:00
|
|
|
|
using System.Web.Http.Controllers;
|
|
|
|
|
using System.Web.Http.Filters;
|
2012-08-14 02:03:41 +04:00
|
|
|
|
using System.Web.Http.OData.Builder;
|
2012-09-07 00:27:11 +04:00
|
|
|
|
using System.Web.Http.OData.Formatter;
|
2012-10-12 00:58:28 +04:00
|
|
|
|
using System.Web.Http.OData.Query;
|
2012-11-14 05:40:58 +04:00
|
|
|
|
using System.Web.Http.OData.Routing;
|
2012-08-14 02:03:41 +04:00
|
|
|
|
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;
|
2012-08-14 02:03:41 +04:00
|
|
|
|
using Microsoft.Data.Edm;
|
2012-11-28 02:02:12 +04:00
|
|
|
|
using Microsoft.Data.Edm.Library;
|
2012-11-30 04:18:13 +04:00
|
|
|
|
using Microsoft.Data.OData;
|
2012-08-22 22:38:00 +04:00
|
|
|
|
using Microsoft.TestCommon;
|
2012-09-07 00:27:11 +04:00
|
|
|
|
using Moq;
|
2012-08-14 02:03:41 +04:00
|
|
|
|
|
|
|
|
|
namespace System.Web.Http.OData
|
|
|
|
|
{
|
2012-12-20 06:09:27 +04:00
|
|
|
|
public class ODataHttpConfigurationExtensionTest
|
2012-08-14 02:03:41 +04:00
|
|
|
|
{
|
2012-09-07 00:27:11 +04:00
|
|
|
|
[Fact]
|
|
|
|
|
public void SetODataFormatter_AddsFormatterToTheFormatterCollection()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
HttpConfiguration configuration = new HttpConfiguration();
|
2012-11-30 04:18:13 +04:00
|
|
|
|
ODataMediaTypeFormatter formatter = CreateODataFormatter();
|
2012-09-07 00:27:11 +04:00
|
|
|
|
|
|
|
|
|
// Act
|
2012-10-19 04:48:18 +04:00
|
|
|
|
configuration.Formatters.Insert(0, formatter);
|
2012-09-07 00:27:11 +04:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.Contains(formatter, configuration.Formatters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2013-01-05 02:09:45 +04:00
|
|
|
|
public void IsODataFormatter_ReturnsTrue_ForODataFormatters()
|
2012-09-07 00:27:11 +04:00
|
|
|
|
{
|
|
|
|
|
// 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);
|
2012-09-07 00:27:11 +04:00
|
|
|
|
|
|
|
|
|
// 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);
|
2012-09-07 00:27:11 +04:00
|
|
|
|
|
|
|
|
|
// Assert
|
2012-11-30 04:18:13 +04:00
|
|
|
|
IEnumerable<MediaTypeFormatter> expectedFormatters = new MediaTypeFormatter[]
|
|
|
|
|
{
|
|
|
|
|
formatter1, formatter2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Assert.True(expectedFormatters.SequenceEqual(result));
|
2012-09-07 00:27:11 +04:00
|
|
|
|
}
|
2012-10-12 00:58:28 +04:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddQuerySupport_AddsQueryableFilterProvider()
|
|
|
|
|
{
|
|
|
|
|
HttpConfiguration configuration = new HttpConfiguration();
|
|
|
|
|
|
|
|
|
|
configuration.EnableQuerySupport();
|
|
|
|
|
|
2012-10-17 04:26:33 +04:00
|
|
|
|
var queryFilterProviders = configuration.Services.GetFilterProviders().OfType<QueryFilterProvider>();
|
|
|
|
|
Assert.Equal(1, queryFilterProviders.Count());
|
|
|
|
|
var queryAttribute = Assert.IsType<QueryableAttribute>(queryFilterProviders.First().QueryFilter);
|
2012-10-12 22:13:53 +04:00
|
|
|
|
}
|
2012-10-12 00:58:28 +04:00
|
|
|
|
|
2012-10-17 04:26:33 +04:00
|
|
|
|
[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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-12 00:58:28 +04:00
|
|
|
|
[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-10-12 00:58:28 +04:00
|
|
|
|
}
|
2012-11-30 04:18:13 +04:00
|
|
|
|
|
|
|
|
|
private static ODataMediaTypeFormatter CreateODataFormatter()
|
|
|
|
|
{
|
2013-01-05 02:09:45 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-14 02:03:41 +04:00
|
|
|
|
}
|
|
|
|
|
}
|