/// <summary> /// Gets the <see cref="IETagHandler"/> from the configuration. /// </summary> /// <param name="configuration">The server configuration.</param> /// <returns>The <see cref="IETagHandler"/> for the configuration.</returns> public static IETagHandler GetETagHandler(this HttpConfiguration configuration) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } object handler; if (!configuration.Properties.TryGetValue(ETagHandlerKey, out handler)) { IETagHandler defaultETagHandler = new DefaultODataETagHandler(); configuration.SetETagHandler(defaultETagHandler); return defaultETagHandler; } if (handler == null) { throw Error.InvalidOperation(SRResources.NullETagHandler); } IETagHandler etagHandler = handler as IETagHandler; if (etagHandler == null) { throw Error.InvalidOperation(SRResources.InvalidETagHandler, handler.GetType()); } return etagHandler; }
public void DefaultODataETagHandler_RoundTrips(object value) { // Arrange DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary<string, object> properties = new Dictionary<string, object> { { "Any", value } }; // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList<object> values = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(1, values.Count); Assert.Equal(value, values[0]); }
public void DefaultODataETagHandler_RoundTrips(object value) { // Arrange DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary <string, object> properties = new Dictionary <string, object> { { "Any", value } }; // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList <object> values = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(1, values.Count); Assert.Equal(value, values[0]); }
public void GetIfMatchOrNoneMatch_ReturnsETag_SetETagHeaderValue(string header) { // Arrange HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); Dictionary<string, object> properties = new Dictionary<string, object> { { "Name", "Foo" } }; EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties); if (header.Equals("IfMatch")) { request.Headers.IfMatch.Add(etagHeaderValue); } else { request.Headers.IfNoneMatch.Add(etagHeaderValue); } ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration<Customer> customer = builder.EntityType<Customer>(); customer.HasKey(c => c.Id); customer.Property(c => c.Id); customer.Property(c => c.Name).IsConcurrencyToken(); builder.EntitySet<Customer>("Customers"); IEdmModel model = builder.GetEdmModel(); var customers = model.FindDeclaredEntitySet("Customers"); Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(model.GetEdmType(typeof(Customer))); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns((IEdmNavigationSource)customers); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); request.ODataProperties().Path = odataPath; request.ODataProperties().Model = model; ODataQueryContext context = new ODataQueryContext(model, typeof(Customer)); // Act ODataQueryOptions<Customer> query = new ODataQueryOptions<Customer>(context, request); ETag result = header.Equals("IfMatch") ? query.IfMatch : query.IfNoneMatch; dynamic dynamicResult = result; // Assert Assert.Equal("Foo", result["Name"]); Assert.Equal("Foo", dynamicResult.Name); }
[InlineData("China Standard Time")] // +8:00 public void DefaultODataETagHandler_DateTime_RoundTrips(string timeZoneId) { // Arrange TimeZoneInfoHelper.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); DateTime value = new DateTime(2015, 2, 17, 1, 2, 3, DateTimeKind.Utc); DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary<string, object> properties = new Dictionary<string, object> { { "Any", value } }; // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList<object> values = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(1, values.Count); DateTimeOffset result = Assert.IsType<DateTimeOffset>(values[0]); Assert.Equal(TimeZoneInfo.ConvertTime(new DateTimeOffset(value), TimeZoneInfoHelper.TimeZone), result); }
public void CreateETag_ETagCreatedAndParsed_GivenValues(string notUsed, object[] values) { // Arrange DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary<string, object> properties = new Dictionary<string, object>(); for (int i = 0; i < values.Length; i++) { properties.Add("Prop" + i, values[i]); } // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList<object> results = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(values.Length, results.Count); for (int i = 0; i < values.Length; i++) { Assert.Equal(values[i], results[i]); } }
[InlineData("China Standard Time")] // +8:00 public void DefaultODataETagHandler_DateTime_RoundTrips(string timeZoneId) { // Arrange TimeZoneInfoHelper.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); DateTime value = new DateTime(2015, 2, 17, 1, 2, 3, DateTimeKind.Utc); DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary <string, object> properties = new Dictionary <string, object> { { "Any", value } }; // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList <object> values = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(1, values.Count); DateTimeOffset result = Assert.IsType <DateTimeOffset>(values[0]); Assert.Equal(TimeZoneInfo.ConvertTime(new DateTimeOffset(value), TimeZoneInfoHelper.TimeZone), result); }
public void CreateETag_ETagCreatedAndParsed_GivenValues(string notUsed, object[] values) { // Arrange DefaultODataETagHandler handler = new DefaultODataETagHandler(); Dictionary <string, object> properties = new Dictionary <string, object>(); for (int i = 0; i < values.Length; i++) { properties.Add("Prop" + i, values[i]); } // Act EntityTagHeaderValue etagHeaderValue = handler.CreateETag(properties); IList <object> results = handler.ParseETag(etagHeaderValue).Select(p => p.Value).ToList(); // Assert Assert.True(etagHeaderValue.IsWeak); Assert.Equal(values.Length, results.Count); for (int i = 0; i < values.Length; i++) { Assert.Equal(values[i], results[i]); } }
public void GetETagTEntity_Returns_ETagInHeader() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); Dictionary<string, object> properties = new Dictionary<string, object> { { "City", "Foo" } }; EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(model.Customer); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns((IEdmNavigationSource)null); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); request.ODataProperties().Path = odataPath; // Act ETag<Customer> result = request.GetETag<Customer>(etagHeaderValue); dynamic dynamicResult = result; // Assert Assert.Equal("Foo", result["City"]); Assert.Equal("Foo", dynamicResult.City); }
public void GetETag_Returns_ETagInHeader_ForInteger(byte byteValue, short shortValue, long longValue) { // Arrange Dictionary<string, object> properties = new Dictionary<string, object> { { "ByteVal", byteValue }, { "LongVal", longValue }, { "ShortVal", shortValue } }; EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties); var builder = new ODataConventionModelBuilder(); builder.EntitySet<MyEtagOrder>("Orders"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType order = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagOrder"); IEdmEntitySet orders = model.FindDeclaredEntitySet("Orders"); Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(order); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); request.ODataProperties().Path = odataPath; request.ODataProperties().Model = model; // Act ETag result = request.GetETag(etagHeaderValue); dynamic dynamicResult = result; // Assert byte actualByte = Assert.IsType<byte>(result["ByteVal"]); Assert.Equal(actualByte, dynamicResult.ByteVal); Assert.Equal(byteValue, actualByte); short actualShort = Assert.IsType<short>(result["ShortVal"]); Assert.Equal(actualShort, dynamicResult.ShortVal); Assert.Equal(shortValue, actualShort); long actualLong = Assert.IsType<long>(result["LongVal"]); Assert.Equal(actualLong, dynamicResult.LongVal); Assert.Equal(longValue, actualLong); }
public void GetETag_Returns_ETagInHeader_ForDouble(double value, bool isEqual) { // Arrange Dictionary<string, object> properties = new Dictionary<string, object> { { "Version", value } }; EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties); var builder = new ODataConventionModelBuilder(); builder.EntitySet<MyEtagCustomer>("Customers"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customer = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagCustomer"); IEdmEntitySet customers = model.FindDeclaredEntitySet("Customers"); Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(customer); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(customers); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); request.ODataProperties().Path = odataPath; request.ODataProperties().Model = model; // Act ETag result = request.GetETag(etagHeaderValue); dynamic dynamicResult = result; // Assert double actual = Assert.IsType<double>(result["Version"]); Assert.Equal(actual, dynamicResult.Version); if (isEqual) { Assert.Equal(value, actual); } else { Assert.NotEqual(value, actual); Assert.True(actual - value < 0.0000001); } }
public void ApplyTo_NewQueryReturned_ForInteger(sbyte byteVal, short shortVal, bool ifMatch, IList <int> expect) { // Arrange var mycustomers = new List <MyETagOrder> { new MyETagOrder { ID = 1, ByteVal = 7, ShortVal = 8 }, new MyETagOrder { ID = 2, ByteVal = SByte.MaxValue, ShortVal = Int16.MaxValue }, new MyETagOrder { ID = 3, ByteVal = SByte.MinValue, ShortVal = Int16.MinValue }, }; IETagHandler handerl = new DefaultODataETagHandler(); Dictionary <string, object> properties = new Dictionary <string, object> { { "ByteVal", byteVal }, { "ShortVal", shortVal } }; EntityTagHeaderValue etagHeaderValue = handerl.CreateETag(properties); HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); var builder = new ODataConventionModelBuilder(); builder.EntitySet <MyETagOrder>("Orders"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType order = model.SchemaElements.OfType <IEdmEntityType>().FirstOrDefault(e => e.Name == "MyETagOrder"); IEdmEntitySet orders = model.FindDeclaredEntitySet("Orders"); Mock <ODataPathSegment> mockSegment = new Mock <ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(order); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); request.ODataProperties().Path = odataPath; request.ODataProperties().Model = model; ETag etagCustomer = request.GetETag(etagHeaderValue); etagCustomer.EntityType = typeof(MyETagOrder); etagCustomer.IsIfNoneMatch = !ifMatch; // Act IQueryable queryable = etagCustomer.ApplyTo(mycustomers.AsQueryable()); // Assert Assert.NotNull(queryable); IEnumerable <MyETagOrder> actualOrders = Assert.IsAssignableFrom <IEnumerable <MyETagOrder> >(queryable); Assert.Equal(expect, actualOrders.Select(c => c.ID)); MethodCallExpression methodCall = queryable.Expression as MethodCallExpression; Assert.NotNull(methodCall); Assert.Equal(2, methodCall.Arguments.Count); if (ifMatch) { Assert.Equal( "Param_0 => ((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " + "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty))", methodCall.Arguments[1].ToString()); } else { Assert.Equal( "Param_0 => Not(((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " + "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty)))", methodCall.Arguments[1].ToString()); } }
public void ApplyTo_NewQueryReturned_ForDouble(double value, bool ifMatch, IList <int> expect) { // Arrange var myCustomers = new List <MyETagCustomer> { new MyETagCustomer { ID = 1, DoubleETag = 1.0, }, new MyETagCustomer { ID = 2, DoubleETag = 1.1, }, new MyETagCustomer { ID = 3, DoubleETag = 1.0, }, }; IETagHandler handerl = new DefaultODataETagHandler(); Dictionary <string, object> properties = new Dictionary <string, object> { { "DoubleETag", value } }; EntityTagHeaderValue etagHeaderValue = handerl.CreateETag(properties); HttpRequestMessage request = new HttpRequestMessage(); var builder = new ODataConventionModelBuilder(); builder.EntitySet <MyETagCustomer>("Customers"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customer = model.SchemaElements.OfType <IEdmEntityType>().FirstOrDefault(e => e.Name == "MyEtagCustomer"); IEdmEntitySet customers = model.FindDeclaredEntitySet("Customers"); ODataPath odataPath = new ODataPath(new[] { new EntitySetSegment(customers) }); request.EnableHttpDependencyInjectionSupport(model); request.ODataProperties().Path = odataPath; ETag etagCustomer = request.GetETag(etagHeaderValue); etagCustomer.EntityType = typeof(MyETagCustomer); etagCustomer.IsIfNoneMatch = !ifMatch; // Act IQueryable queryable = etagCustomer.ApplyTo(myCustomers.AsQueryable()); // Assert Assert.NotNull(queryable); IList <MyETagCustomer> actualCustomers = Assert.IsAssignableFrom <IEnumerable <MyETagCustomer> >(queryable).ToList(); if (expect != null) { Assert.Equal(expect, actualCustomers.Select(c => c.ID)); } MethodCallExpression methodCall = queryable.Expression as MethodCallExpression; Assert.NotNull(methodCall); Assert.Equal(2, methodCall.Arguments.Count); if (ifMatch) { Assert.Equal( "Param_0 => (Param_0.DoubleETag == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Double]).TypedProperty)", methodCall.Arguments[1].ToString()); } else { Assert.Equal( "Param_0 => Not((Param_0.DoubleETag == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Double]).TypedProperty))", methodCall.Arguments[1].ToString()); } }
public void ApplyTo_NewQueryReturned_ForInteger(sbyte byteVal, short shortVal, bool ifMatch, IList<int> expect) { // Arrange var mycustomers = new List<MyETagOrder> { new MyETagOrder { ID = 1, ByteVal = 7, ShortVal = 8 }, new MyETagOrder { ID = 2, ByteVal = SByte.MaxValue, ShortVal = Int16.MaxValue }, new MyETagOrder { ID = 3, ByteVal = SByte.MinValue, ShortVal = Int16.MinValue }, }; IETagHandler handerl = new DefaultODataETagHandler(); Dictionary<string, object> properties = new Dictionary<string, object> { { "ByteVal", byteVal }, { "ShortVal", shortVal } }; EntityTagHeaderValue etagHeaderValue = handerl.CreateETag(properties); HttpRequestMessage request = new HttpRequestMessage(); HttpConfiguration cofiguration = new HttpConfiguration(); request.SetConfiguration(cofiguration); var builder = new ODataConventionModelBuilder(); builder.EntitySet<MyETagOrder>("Orders"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType order = model.SchemaElements.OfType<IEdmEntityType>().FirstOrDefault(e => e.Name == "MyETagOrder"); IEdmEntitySet orders = model.FindDeclaredEntitySet("Orders"); Mock<ODataPathSegment> mockSegment = new Mock<ODataPathSegment> { CallBase = true }; mockSegment.Setup(s => s.GetEdmType(null)).Returns(order); mockSegment.Setup(s => s.GetNavigationSource(null)).Returns(orders); ODataPath odataPath = new ODataPath(new[] { mockSegment.Object }); request.ODataProperties().Path = odataPath; request.ODataProperties().Model = model; ETag etagCustomer = request.GetETag(etagHeaderValue); etagCustomer.EntityType = typeof(MyETagOrder); etagCustomer.IsIfNoneMatch = !ifMatch; // Act IQueryable queryable = etagCustomer.ApplyTo(mycustomers.AsQueryable()); // Assert Assert.NotNull(queryable); IEnumerable<MyETagOrder> actualOrders = Assert.IsAssignableFrom<IEnumerable<MyETagOrder>>(queryable); Assert.Equal(expect, actualOrders.Select(c => c.ID)); MethodCallExpression methodCall = queryable.Expression as MethodCallExpression; Assert.NotNull(methodCall); Assert.Equal(2, methodCall.Arguments.Count); if (ifMatch) { Assert.Equal( "Param_0 => ((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " + "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty))", methodCall.Arguments[1].ToString()); } else { Assert.Equal( "Param_0 => Not(((Param_0.ByteVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " + "AndAlso (Param_0.ShortVal == value(System.Web.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty)))", methodCall.Arguments[1].ToString()); } }