Example #1
0
        /// <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 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 = ODataConventionModelBuilderFactory.Create();
            builder.EntitySet<MyEtagCustomer>("Customers");
            IEdmModel model = builder.GetEdmModel();
            IEdmEntitySet customers = model.FindDeclaredEntitySet("Customers");
            ODataPath odataPath = new ODataPath(new EntitySetSegment(customers));

            HttpRequestMessage request = new HttpRequestMessage();
            request.EnableHttpDependencyInjectionSupport(model);
            request.ODataProperties().Path = odataPath;

            // 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);
            }
        }
Example #3
0
        public void GetETagTentity_RoundTrip_ETagInHeader()
        {
            // Arrange
            HttpRequestMessage request       = new HttpRequestMessage();
            HttpConfiguration  configuration = new HttpConfiguration();

            request.SetConfiguration(configuration);
            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "City", "Foo" }
            };
            EntityTagHeaderValue value = 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.GetEntitySet(null)).Returns((IEdmEntitySet)null);
            ODataPath odataPath = new ODataPath(new[] { mockSegment.Object });

            request.ODataProperties().Path = odataPath;

            // Act
            ETag <Customer> result        = request.GetETag <Customer>(value);
            dynamic         dynamicResult = result;

            // Assert
            Assert.Equal("Foo", result["City"]);
            Assert.Equal("Foo", dynamicResult.City);
        }
        public void GetETagTEntity_Returns_ETagInHeader()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            HttpRequestMessage request = new HttpRequestMessage();

            request.EnableHttpDependencyInjectionSupport(model.Model);

            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "City", "Foo" }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Customers));

            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);
        }
Example #5
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);
        }
        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 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.Single(values);
            Assert.Equal(value, values[0]);
        }
        public void GetIfMatchOrNoneMatch_ReturnsETag_SetETagHeaderValue(string header)
        {
            // Arrange
            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");

            var request = RequestFactory.CreateFromModel(model);

            EntitySetSegment entitySetSegment = new EntitySetSegment(customers);
            ODataPath        odataPath        = new ODataPath(new[] { entitySetSegment });

            request.ODataContext().Path = odataPath;

            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "Name", "Foo" }
            };
            EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties);

            if (header.Equals("IfMatch"))
            {
                request.Headers.AddIfMatch(etagHeaderValue);
            }
            else
            {
                request.Headers.AddIfNoneMatch(etagHeaderValue);
            }

            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);
        }
        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 = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <MyEtagOrder>("Orders");
            IEdmModel          model     = builder.GetEdmModel();
            IEdmEntitySet      orders    = model.FindDeclaredEntitySet("Orders");
            ODataPath          odataPath = new ODataPath(new EntitySetSegment(orders));
            HttpRequestMessage request   = new HttpRequestMessage();

            request.EnableHttpDependencyInjectionSupport(model);
            request.ODataProperties().Path = odataPath;

            // 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);
        }
        [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.Single(values);
            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.NotNull(notUsed);
            Assert.True(etagHeaderValue.IsWeak);
            Assert.Equal(values.Length, results.Count);
            for (int i = 0; i < values.Length; i++)
            {
                Assert.Equal(values[i], results[i]);
            }
        }
Example #13
0
        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);

            var builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <MyETagOrder>("Orders");
            IEdmModel     model     = builder.GetEdmModel();
            IEdmEntitySet orders    = model.FindDeclaredEntitySet("Orders");
            ODataPath     odataPath = new ODataPath(new[] { new EntitySetSegment(orders) });
            var           request   = RequestFactory.CreateFromModel(model);

            request.ODataContext().Path = odataPath;

            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(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " +
                    "AndAlso (Param_0.ShortVal == value(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty))",
                    methodCall.Arguments[1].ToString());
            }
            else
            {
                Assert.Equal(
                    "Param_0 => Not(((Param_0.ByteVal == value(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.SByte]).TypedProperty) " +
                    "AndAlso (Param_0.ShortVal == value(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Int16]).TypedProperty)))",
                    methodCall.Arguments[1].ToString());
            }
        }
Example #14
0
        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);

            var builder = ODataConventionModelBuilderFactory.Create();

            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) });
            var            request   = RequestFactory.CreateFromModel(model);

            request.ODataContext().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(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Double]).TypedProperty)",
                    methodCall.Arguments[1].ToString());
            }
            else
            {
                Assert.Equal(
                    "Param_0 => Not((Param_0.DoubleETag == value(Microsoft.AspNet.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.Double]).TypedProperty))",
                    methodCall.Arguments[1].ToString());
            }
        }