Beispiel #1
0
            public void Projections_Batch()
            {
                using (TestUtil.MetadataCacheCleaner())
                    using (ocs.PopulateData.CreateTableAndPopulateData())
                        using (TestWebRequest request = TestWebRequest.CreateForInProcess())
                        {
                            TestUtil.RunCombinations(
                                new Type[] { typeof(CustomDataContext), typeof(ocs.CustomObjectContext), typeof(CustomRowBasedContext), typeof(CustomRowBasedOpenTypesContext) },
                                CustomerSelects.Variations(2),
                                (dataServiceType, selects) =>
                            {
                                request.DataServiceType = dataServiceType;

                                BatchWebRequest batchRequest = new BatchWebRequest();
                                foreach (var select in selects)
                                {
                                    InMemoryWebRequest part = new InMemoryWebRequest();
                                    part.Accept             = "application/atom+xml,application/xml";
                                    part.RequestUriString   = "/Customers?$select=" + select.Select;
                                    batchRequest.Parts.Add(part);
                                }

                                batchRequest.SendRequest(request);

                                for (int i = 0; i < selects.Length; i++)
                                {
                                    UnitTestsUtil.VerifyXPathExists(UnitTestsUtil.GetResponseAsAtomXLinq(batchRequest.Parts[i]), selects[i].VerificationXPaths);
                                }
                            });
                        }
            }
Beispiel #2
0
            public void SendMoreThan100RequestsInBatch()
            {
                using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf())
                {
                    request.DataServiceType = typeof(CustomDataContext);
                    BatchWebRequest batchRequest = new BatchWebRequest();

                    for (int i = 0; i < 101; i++)
                    {
                        InMemoryWebRequest getRequest = new InMemoryWebRequest();
                        getRequest.RequestUriString = "Customers(1)";
                        batchRequest.Parts.Add(getRequest);
                    }

                    batchRequest.SendRequest(request);
                    Assert.IsFalse(batchRequest.Parts.Any(p => p.ResponseStatusCode != 200), "All the requests should succeed");
                }
            }
Beispiel #3
0
        private static void ExecuteUpdate(DSPResource resource, int id, TestWebRequest baseRequest, string httpMethod, string preferHeader, bool useBatch, DSPResourceSerializerFormat payloadFormat)
        {
            string payload = DSPResourceSerializer.WriteEntity(resource, payloadFormat);

            bool isPost = httpMethod == "POST";
            bool expectedReturnContent = preferHeader == "return=representation" || isPost && preferHeader == null;

            string uriSuffix = isPost ? String.Empty : String.Format("({0})", id);

            TestWebRequest request = useBatch ? new InMemoryWebRequest() : baseRequest;

            request.RequestUriString         = String.Format("/{0}s{1}", resource.ResourceType.Name, uriSuffix);
            request.HttpMethod               = httpMethod;
            request.RequestVersion           = "4.0;";
            request.RequestMaxVersion        = "4.0;";
            request.RequestHeaders["Prefer"] = preferHeader;
            request.Accept             = payloadFormat == DSPResourceSerializerFormat.Atom ? "application/atom+xml,application/xml" : UnitTestsUtil.JsonLightMimeType;
            request.RequestContentType = "application/atom+xml";
            request.SetRequestStreamAsText(payload);

            if (useBatch)
            {
                var batchRequest = new BatchWebRequest();
                var changeset    = new BatchWebRequest.Changeset();
                changeset.Parts.Add((InMemoryWebRequest)request);
                batchRequest.Changesets.Add(changeset);
                batchRequest.SendRequest(baseRequest);

                Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(baseRequest.ResponseStatusCode), "Unexpected error occurred on batch.");
            }
            else
            {
                request.SendRequest();
            }

            Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(request.ResponseStatusCode), "Unexpected error occurred when sending the request.");
            if (expectedReturnContent)
            {
                // If the request is expected to return content, verify there were no instream errors
                Exception e            = request.ParseResponseInStreamError();
                string    errorMessage = e != null ? e.Message : string.Empty;
                Assert.IsNull(e, "Expected no exception, but got the following error", errorMessage);
            }
        }
Beispiel #4
0
        [Ignore] // Remove Atom
        // [TestMethod]
        public void CreateODataWriterDelegateTestForOpenProvider()
        {
            var testInfo = new[] {
                new { Query = "/Customers?$format=atom", CustomerCount = 3, NavLinkCount = 3, OrderCount = 0 },
                new { Query = "/Customers?$format=atom&$expand=Orders", CustomerCount = 3, NavLinkCount = 3, OrderCount = 6 },
                new { Query = "/Customers(1)?$format=atom", CustomerCount = 1, NavLinkCount = 1, OrderCount = 0 },
                new { Query = "/Customers(1)?$format=atom&$expand=Orders", CustomerCount = 1, NavLinkCount = 1, OrderCount = 2 },
                new { Query = "/Customers(1)/Orders?$format=atom", CustomerCount = 0, NavLinkCount = 0, OrderCount = 2 },
            };

            using (OpenWebDataServiceHelper.CreateODataWriterDelegate.Restore())
                using (MyODataWriter.WriteEntryStart.Restore())
                    using (MyODataWriter.WriteLinkStart.Restore())
                        using (var request = TestWebRequest.CreateForInProcess())
                        {
                            request.HttpMethod      = "GET";
                            request.DataServiceType = typeof(CustomRowBasedOpenTypesContext);
                            OpenWebDataServiceHelper.CreateODataWriterDelegate.Value = (odataWriter) =>
                            {
                                return(new MyODataWriter(odataWriter));
                            };

                            test.TestUtil.RunCombinations(testInfo, UnitTestsUtil.BooleanValues, (ti, batchMode) =>
                            {
                                int CustomerCount  = 0;
                                int NavigationLink = 0;
                                int OrderCount     = 0;

                                MyODataWriter.WriteEntryStart.Value = (args) =>
                                {
                                    Assert.IsTrue(args.Instance.GetType() == typeof(RowComplexType), "Making sure the right provider type is exposed");
                                    var instance = (RowComplexType)args.Instance;
                                    if (args.Entry.TypeName.Contains("Customer"))
                                    {
                                        Assert.IsTrue(instance.TypeName.Contains("Customer"), "Make sure the instance is customer or customerwithbirthday");
                                        CustomerCount++;
                                    }
                                    else if (args.Entry.TypeName.Contains("Order"))
                                    {
                                        Assert.IsTrue(instance.TypeName.Contains("Order"), "Make sure the instance is order");
                                        OrderCount++;
                                    }

                                    return(false);
                                };

                                MyODataWriter.WriteLinkStart.Value = (args) =>
                                {
                                    if (args.NavigationLink.Name == "Orders")
                                    {
                                        Assert.IsTrue(args.NavigationLink.IsCollection.Value, "orders must be collection");
                                        NavigationLink++;
                                    }

                                    return(false);
                                };

                                if (!batchMode)
                                {
                                    request.RequestUriString = ti.Query;
                                    request.SendRequest();
                                }
                                else
                                {
                                    BatchWebRequest batchRequest  = new BatchWebRequest();
                                    InMemoryWebRequest getRequest = new InMemoryWebRequest();
                                    getRequest.RequestUriString   = ti.Query;
                                    batchRequest.Parts.Add(getRequest);
                                    batchRequest.SendRequest(request);
                                }

                                Assert.AreEqual(CustomerCount, ti.CustomerCount, "CustomerCount should match");
                                Assert.AreEqual(OrderCount, ti.OrderCount, "OrderCount should match");
                                Assert.AreEqual(NavigationLink, ti.NavLinkCount, "NavigationCount should match");
                            });
                        }
        }
Beispiel #5
0
        [Ignore] // Remove Atom
        // [TestMethod]
        public void CreateODataWriterDelegateTest()
        {
            int createODataWriterDelegateCount = 0;
            var testInfo = new[] {
                new { Query = "/Customers?$format=atom", CustomerCount = 3, NavLinkCount = 6, OrderCount = 0 },
                new { Query = "/Customers?$format=atom&$expand=BestFriend", CustomerCount = 5, NavLinkCount = 10, OrderCount = 0 },
                new { Query = "/Customers?$format=atom&$expand=Orders", CustomerCount = 3, NavLinkCount = 18, OrderCount = 6 },
                new { Query = "/Customers(1)?$format=atom", CustomerCount = 1, NavLinkCount = 2, OrderCount = 0 },
                new { Query = "/Customers(1)?$format=atom&$expand=Orders", CustomerCount = 1, NavLinkCount = 6, OrderCount = 2 },
                new { Query = "/Customers(1)/Orders?$format=atom", CustomerCount = 0, NavLinkCount = 4, OrderCount = 2 },
            };

            using (OpenWebDataServiceHelper.CreateODataWriterDelegate.Restore())
                using (MyODataWriter.WriteEntryStart.Restore())
                    using (MyODataWriter.WriteLinkStart.Restore())
                        using (var request = TestWebRequest.CreateForInProcess())
                        {
                            createODataWriterDelegateCount = 0;
                            request.HttpMethod             = "GET";
                            request.DataServiceType        = typeof(CustomDataContext);
                            OpenWebDataServiceHelper.CreateODataWriterDelegate.Value = (odataWriter) =>
                            {
                                createODataWriterDelegateCount++;
                                return(new MyODataWriter(odataWriter));
                            };

                            test.TestUtil.RunCombinations(testInfo, UnitTestsUtil.BooleanValues, (ti, batchMode) =>
                            {
                                createODataWriterDelegateCount = 0;
                                int CustomerCount  = 0;
                                int NavigationLink = 0;
                                int OrderCount     = 0;

                                MyODataWriter.WriteEntryStart.Value = (args) =>
                                {
                                    if (args.Entry != null)
                                    {
                                        if (args.Entry.TypeName.Contains("Customer"))
                                        {
                                            Assert.IsTrue(typeof(Customer).IsAssignableFrom(args.Instance.GetType()), "Make sure the instance is customer or customerwithbirthday");
                                            CustomerCount++;
                                        }
                                        else if (args.Entry.TypeName.Contains("Order"))
                                        {
                                            Assert.IsTrue(typeof(Order).IsAssignableFrom(args.Instance.GetType()), "Make sure the instance is order");
                                            OrderCount++;
                                        }
                                    }

                                    return(false);
                                };

                                MyODataWriter.WriteLinkStart.Value = (args) =>
                                {
                                    NavigationLink++;
                                    return(false);
                                };

                                if (!batchMode)
                                {
                                    request.RequestUriString = ti.Query;
                                    request.SendRequest();
                                    Assert.AreEqual(1, createODataWriterDelegateCount, "NavigationCount should match");
                                }
                                else
                                {
                                    BatchWebRequest batchRequest  = new BatchWebRequest();
                                    InMemoryWebRequest getRequest = new InMemoryWebRequest();
                                    getRequest.RequestUriString   = ti.Query;
                                    batchRequest.Parts.Add(getRequest);
                                    batchRequest.SendRequest(request);
                                    Assert.AreEqual(batchRequest.Parts.Count, createODataWriterDelegateCount, "NavigationCount should match");
                                }

                                Assert.AreEqual(ti.CustomerCount, CustomerCount, "CustomerCount should match");
                                Assert.AreEqual(ti.OrderCount, OrderCount, "OrderCount should match");
                                Assert.AreEqual(ti.NavLinkCount, NavigationLink, "NavigationCount should match");
                            });
                        }
        }