Example #1
0
        public void RequestUriSpecialNumbersTest()
        {
            TypedCustomDataContext <AllTypes> .ClearHandlers();

            TypedCustomDataContext <AllTypes> .ClearValues();

            using (TestWebRequest request = TestWebRequest.CreateForInProcess())
            {
                request.DataServiceType = typeof(TypedCustomDataContext <AllTypes>);
                string[] uris = new string[]
                {
                    "/Values(1)",
                    "/Values(-1)",
                };
                foreach (string uri in uris)
                {
                    Trace.WriteLine("Requesting URI " + uri);
                    request.RequestUriString = uri;
                    Exception exception = TestUtil.RunCatching(request.SendRequest);
                    TestUtil.AssertExceptionExpected(exception, true);
                    TestUtil.AssertExceptionStatusCode(
                        exception,
                        404,
                        "Correctly parsed (but missing) entites should return 404.");
                }
            }
        }
Example #2
0
        public void RequestUriProcessorKeySpecialRealTest()
        {
            double[] doubleValues = new double[] { double.PositiveInfinity, double.NegativeInfinity, double.NaN };
            string[] findText     = new string[] { "INF", "-INF", "NaN" };
            int      textIndex    = 0; // Index corresponds to the findText array

            CombinatorialEngine engine = CombinatorialEngine.FromDimensions(
                new Dimension("doubleValue", doubleValues));

            using (TestWebRequest request = TestWebRequest.CreateForInProcess())
            {
                request.DataServiceType = typeof(TypedCustomDataContext <TypedEntity <double, string> >);
                TypedCustomDataContext <TypedEntity <double, string> > .ClearHandlers();

                TypedCustomDataContext <TypedEntity <double, string> > .ClearValues();

                TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values)
                {
                    double doubleValue = (double)values["doubleValue"];
                    TypedCustomDataContext <TypedEntity <double, string> > .ValuesRequested += (sender, args) =>
                    {
                        TypedCustomDataContext <TypedEntity <double, string> > s = (TypedCustomDataContext <TypedEntity <double, string> >)sender;
                        TypedEntity <double, string> entity = new TypedEntity <double, string>();
                        entity.ID = doubleValue;
                        s.SetValues(new object[] { entity });
                    };
                    try
                    {
                        Assert.IsTrue(textIndex < 3, "Out of bounds for test data array. Please check the doubleValues variable.");
                        string searchValue = findText[textIndex];

                        // Increment textIndex for next test case
                        ++textIndex;

                        request.RequestUriString = "/Values";
                        request.Accept           = "application/json";
                        request.SendRequest();

                        string responseText = request.GetResponseStreamAsText();
                        Assert.IsTrue(responseText.IndexOf(searchValue) > 0, String.Format("ID \"{0}\" expected in response.", searchValue));

                        Trace.WriteLine(String.Format("Found ID: {0}", searchValue));

                        // Finish the test suite after we've ran through all the strings. If we continue, the test suite continues and fails.
                        // Researching into that is a expensive at this time.
                        if (textIndex == 3)
                        {
                            return;
                        }
                    }
                    finally
                    {
                        TypedCustomDataContext <TypedEntity <double, string> > .ClearHandlers();
                        TypedCustomDataContext <TypedEntity <double, string> > .ClearValues();
                    }
                });
            }
        }
Example #3
0
        public void RequestQueryParserOperatorsPrimitives()
        {
            CombinatorialEngine engine = CombinatorialEngine.FromDimensions(
                new Dimension("Operator", OperatorData.Values),
                new Dimension("LeftType", TypeData.Values),
                new Dimension("RightType", TypeData.Values));

            TypedCustomDataContext <AllTypes> .ClearValues();

            TypedCustomDataContext <AllTypes> .ClearHandlers();

            try
            {
                TypedCustomDataContext <AllTypes> .ValuesRequested += (x, y) =>
                {
                    ((TypedCustomDataContext <AllTypes>)x).SetValues(new object[] { new AllTypes() });
                };
                using (TestWebRequest request = TestWebRequest.CreateForInProcess())
                {
                    request.DataServiceType = typeof(TypedCustomDataContext <AllTypes>);
                    TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values)
                    {
                        TypeData left  = (TypeData)values["LeftType"];
                        TypeData right = (TypeData)values["RightType"];

                        if (!left.IsTypeSupported || !right.IsTypeSupported)
                        {
                            return;
                        }

                        // Big matrix. Let's cut it down by assuming that left and right ordering does not matter.
                        if (Array.IndexOf(TypeData.Values, left) > Array.IndexOf(TypeData.Values, right))
                        {
                            return;
                        }

                        string leftName          = AllTypes.PropertyNameForType(left.ClrType);
                        string rightName         = AllTypes.PropertyNameForType(right.ClrType);
                        OperatorData o           = (OperatorData)values["Operator"];
                        string requestText       = "/Values?$filter=" + leftName + "%20" + o.Token + "%20" + rightName;
                        request.RequestUriString = requestText;
                        Exception exception      = TestUtil.RunCatching(request.SendRequest);
                        TestUtil.AssertExceptionExpected(exception,
                                                         o.IsEqualityComparison && !left.IsEqualityComparableTo(right),
                                                         o.IsOrderingComparison && !left.IsOrderComparableTo(right));
                    });
                }
            }
            finally
            {
                TypedCustomDataContext <AllTypes> .ClearValues();

                TypedCustomDataContext <AllTypes> .ClearHandlers();
            }
        }
Example #4
0
        public void RequestUriProcessorKeySpecialRealTest()
        {
            double[]            doubleValues = new double[] { double.PositiveInfinity, double.NegativeInfinity, double.NaN };
            CombinatorialEngine engine       = CombinatorialEngine.FromDimensions(
                new Dimension("doubleValue", doubleValues));

            using (TestWebRequest request = TestWebRequest.CreateForInProcess())
            {
                request.DataServiceType = typeof(TypedCustomDataContext <TypedEntity <double, string> >);
                TypedCustomDataContext <TypedEntity <double, string> > .ClearHandlers();

                TypedCustomDataContext <TypedEntity <double, string> > .ClearValues();

                TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values)
                {
                    double doubleValue = (double)values["doubleValue"];
                    TypedCustomDataContext <TypedEntity <double, string> > .ValuesRequested += (sender, args) =>
                    {
                        TypedCustomDataContext <TypedEntity <double, string> > s = (TypedCustomDataContext <TypedEntity <double, string> >)sender;
                        TypedEntity <double, string> entity = new TypedEntity <double, string>();
                        entity.ID = doubleValue;
                        s.SetValues(new object[] { entity });
                    };
                    try
                    {
                        request.RequestUriString = "/Values";
                        request.Accept           = "application/atom+xml,application/xml";
                        request.SendRequest();
                        XmlDocument document = request.GetResponseStreamAsXmlDocument();
                        XmlElement element   = TestUtil.AssertSelectSingleElement(document, "/atom:feed/atom:entry/atom:id");

                        Trace.WriteLine("Found ID: " + element.InnerText);
                        request.FullRequestUriString = element.InnerText;
                        Exception exception          = TestUtil.RunCatching(request.SendRequest);

                        // One NaN value won't match another except throug the use of the .IsNaN
                        // method. It's probably OK to not support this.
                        TestUtil.AssertExceptionExpected(exception, double.IsNaN(doubleValue));

                        string responseText = request.GetResponseStreamAsText();
                        Trace.WriteLine(responseText);
                    }
                    finally
                    {
                        TypedCustomDataContext <TypedEntity <double, string> > .ClearHandlers();
                        TypedCustomDataContext <TypedEntity <double, string> > .ClearValues();
                    }
                });
            }
        }
Example #5
0
        public void RequestQueryParserIdentifiersAmbiguous()
        {
            string[] predicates = new string[]
            {
                "ID eq 0",
                "length(length) eq 1",
                "length('length') gt 1",
                "cast(cast, 'Edm.Byte') lt 100",
            };
            var contextType = typeof(TypedCustomDataContext <AllTypes>);

            TypedCustomDataContext <AmbiguousNameType> .ClearHandlers();

            TypedCustomDataContext <AmbiguousNameType> .ClearValues();

            try
            {
                TypedCustomDataContext <AmbiguousNameType> .ValuesRequested += (sender, e) =>
                {
                    var s = (TypedCustomDataContext <AmbiguousNameType>)sender;
                    s.SetValues(new object[] { new AmbiguousNameType()
                                               {
                                                   ID = 1, cast = 1, length = "l"
                                               } });
                };
                using (TestWebRequest request = TestWebRequest.CreateForInProcess())
                {
                    request.DataServiceType = typeof(TypedCustomDataContext <AmbiguousNameType>);
                    foreach (string predicate in predicates)
                    {
                        request.RequestUriString = "/Values?$filter=" + predicate.Replace(" ", "%20");
                        request.SendRequest();
                        //Trace.WriteLine("Running query for " + predicate);
                        //TestUtil.TraceXml(request.GetResponseStreamAsXmlDocument());
                    }
                }
            }
            finally
            {
                TypedCustomDataContext <AmbiguousNameType> .ClearHandlers();

                TypedCustomDataContext <AmbiguousNameType> .ClearValues();
            }
        }
Example #6
0
        [Ignore] // Remove Atom
        // [TestMethod]
        public void RequestQueryParserReproTests()
        {
            using (TestWebRequest request = TestWebRequest.CreateForInProcess())
            {
                request.DataServiceType = typeof(TypedCustomDataContext <AllTypes>);
                string[] filters = new string[]
                {
                    // Protocol: Can't compare decimal value to literal greater than maxint
                    "DecimalType eq 100000000000000000000000M",

                    // Protocol: Can't use large Decimal in filter expression
                    "79228162514264337593543950335M gt 0",

                    // Protocol: Filter does not match large float values
                    "DoubleType eq 3.4E%2B38",

                    //(double)(3.4E+38f) will be 3.3999999521443642E+38

                    // Protocol: filter can't compare two byte properties
                    "ByteType eq ByteType",
                    "NullableByteType eq NullableByteType",
                    "ByteType eq (1 add 1 sub 2)",

                    // Protocol: contains in filter expression can not find single extended char
                    "null ne StringType and contains(StringType,'\x00A9 \x0040')",
                    "null ne StringType and contains(StringType,'\x00A9')"
                };

                TypedCustomDataContext <AllTypes> .ClearValues();

                TypedCustomDataContext <AllTypes> .ValuesRequested += (sender, y) =>
                {
                    AllTypes[] values = new AllTypes[]
                    {
                        new AllTypes()
                        {
                            ID = 1, DecimalType = 100000000000000000000000m
                        },
                        new AllTypes()
                        {
                            ID = 2, DoubleType = 3.4E+38
                        },
                        new AllTypes()
                        {
                            ID = 3, StringType = "\x00A9 \x0040"
                        },
                    };
                    TypedCustomDataContext <AllTypes> c = (TypedCustomDataContext <AllTypes>)sender;
                    c.SetValues(values);
                };

                foreach (string filter in filters)
                {
                    request.RequestUriString = "/Values?$format=atom&$filter=" + filter;
                    Trace.WriteLine("Sending request for " + request.RequestUriString);
                    Exception exception = TestUtil.RunCatching(request.SendRequest);
                    TestUtil.AssertExceptionExpected(exception, false);
                    XmlDocument document = request.GetResponseStreamAsXmlDocument();
                    TestUtil.AssertSelectNodes(document, "/atom:feed/atom:entry");
                }
            }
        }
Example #7
0
        public void RequestQueryParserNullableTest()
        {
            // Reproes:
            //  - Round doe not work for properties (only constants)
            //  - Protocol: Filter cannot be used with nullable properties
            //  - Can't call year() in filter expression
            //  - get cast exception on server when trying to use a Byte property in a filter option
            string[] predicates = new string[]
            {
                "BoolType eq false",
                "not (BoolType eq null)",
                "NullableBoolType eq null",
                "ByteType lt 100",
                "NullableByteType lt 100",
                "cast(NullableSingleType, 'Edm.Int32') eq null",
                "cast(NullableSingleType, 'Edm.Single') eq null",
                "year(NullableDateTimeOffsetType) eq 2008",
                "round(NullableDoubleType) eq 2008",
                "round(NullableSingleType) eq 2008",
                "NullableInt32Type eq 20",
                "NullableByteType eq 20",
            };

            var contextType = typeof(TypedCustomDataContext <AllTypes>);

            TypedCustomDataContext <AllTypes> .ClearHandlers();

            TypedCustomDataContext <AllTypes> .ClearValues();

            try
            {
                TypedCustomDataContext <AllTypes> .ValuesRequested += (sender, args) =>
                {
                    var      t         = (TypedCustomDataContext <AllTypes>)sender;
                    object[] newValues = new object[]
                    {
                        new AllTypes()
                        {
                            ID = 1,
                            NullableDateTimeOffsetType = new DateTime(2008, 1, 20),
                            NullableDoubleType         = 2008d,
                            NullableSingleType         = 2008,
                            NullableInt32Type          = 20,
                            NullableByteType           = 20,
                        }
                    };
                    t.SetValues(newValues);
                };
                using (TestWebRequest request = TestWebRequest.CreateForInProcess())
                {
                    request.DataServiceType = contextType;
                    foreach (string predicate in predicates)
                    {
                        Trace.WriteLine("Requesting " + predicate);
                        request.RequestUriString = "/Values?$filter=" + predicate.Replace(" ", "%20");
                        request.SendRequest();
                    }
                }
            }
            finally
            {
                TypedCustomDataContext <AllTypes> .ClearHandlers();

                TypedCustomDataContext <AllTypes> .ClearValues();
            }
        }
Example #8
0
        public void FilterCollectionWithAnyAll()
        {
            using (OpenWebDataServiceHelper.AcceptAnyAllRequests.Restore())
                using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf())
                    using (TestUtil.RestoreStaticValueOnDispose(typeof(TypedCustomDataContext <EntityForQuery>), "PreserveChanges"))
                    {
                        OpenWebDataServiceHelper.AcceptAnyAllRequests.Value = true;
                        request.Accept          = UnitTestsUtil.AtomFormat;
                        request.DataServiceType = typeof(TypedCustomDataContext <EntityForQuery>);

                        TypedCustomDataContext <EntityForQuery> .ClearHandlers();

                        TypedCustomDataContext <EntityForQuery> .ClearValues();

                        TypedCustomDataContext <EntityForQuery> .PreserveChanges  = true;
                        TypedCustomDataContext <EntityForQuery> .ValuesRequested += (sender, args) =>
                        {
                            TypedCustomDataContext <EntityForQuery> typedContext = (TypedCustomDataContext <EntityForQuery>)sender;
                            typedContext.SetValues(new EntityForQuery[] {
                                new EntityForQuery {
                                    ID = 0,
                                    CollectionOfInt = new List <int>()
                                    {
                                        1, 2, 3
                                    },
                                    CollectionOfString = new List <string>()
                                    {
                                        "a", "b", "c"
                                    },
                                    CollectionOfComplexType = new List <MVComplexType>()
                                },
                                new EntityForQuery {
                                    ID = 1,
                                    CollectionOfInt = new List <int>()
                                    {
                                        4, 5, 6
                                    },
                                    CollectionOfString = new List <string>()
                                    {
                                        "d", "e", "f"
                                    },
                                    CollectionOfComplexType = new List <MVComplexType>()
                                },
                                new EntityForQuery {
                                    ID = 2,
                                    CollectionOfInt = new List <int>()
                                    {
                                        7, 8, 9
                                    },
                                    CollectionOfString = new List <string>()
                                    {
                                        "x", "y", "z"
                                    },
                                    CollectionOfComplexType = new List <MVComplexType>()
                                    {
                                        new MVComplexType {
                                            Name = "mvcomplextype", Numbers = new List <int>()
                                            {
                                                100, 101
                                            }
                                        }
                                    }
                                },
                            });
                        };

                        request.StartService();

                        string[] requestStrings = new string[]
                        {
                            "/Values?$filter=CollectionOfInt/any() and (ID eq 2)",
                            "/Values?$filter=CollectionOfString/any(s: s eq 'y')",
                            "/Values?$filter=CollectionOfInt/any(i: cast(i, 'Edm.Int64') eq 7)",
                            "/Values?$filter=CollectionOfInt/all(i: (i ge 7) and (i le 9))",
                            "/Values?$filter=CollectionOfInt/all(i: isof(i, 'Edm.Int32') and ($it/ID eq 2))",
                            "/Values?$filter=CollectionOfComplexType/any(ct: (ct/Name eq 'mvcomplextype') and ct/Numbers/all(n: (n ge 100) and (ct/Name eq 'mvcomplextype')))"
                        };

                        foreach (var uri in requestStrings)
                        {
                            request.RequestUriString   = uri;
                            request.ForceVerboseErrors = true;
                            Exception e = TestUtil.RunCatching(request.SendRequest);
                            Assert.IsNull(e, "Not expecting exception.");

                            var xdoc = request.GetResponseStreamAsXDocument();
                            UnitTestsUtil.VerifyXPaths(xdoc,
                                                       "count(//atom:entry)=1",
                                                       "boolean(//atom:entry/atom:id[contains(.,'Values(2)')])");
                        }
                    }
        }