public void RequestUriProcessorEmptySegments() { // This test reproes: extra / are not ignored, http://host/service//$metadata string[] uris = new string[] { "/", "/$metadata", "//$metadata", "//", "///", "//Values//", }; using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = typeof(TypedCustomDataContext <TypedEntity <int, string> >); foreach (string uri in uris) { Trace.WriteLine("Requesting " + uri); request.RequestUriString = uri; request.Accept = "application/atom+xml,application/xml"; request.SendRequest(); var doc = request.GetResponseStreamAsXmlDocument(); if (uri == "/$metadata") { TestUtil.AssertSelectNodes(doc, "/edmx:Edmx/edmx:DataServices[0 = count(@adsm:DataServiceVersion)]"); } } } }
public void RequestUriResourceKeyTest() { CombinatorialEngine engine = CombinatorialEngine.FromDimensions( new Dimension("TypeData", TypeData.Values), new Dimension("UseSmallCasing", new bool[] { true, false }), new Dimension("UseDoublePostfix", new bool[] { true, false })); TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values) { TypeData typeData = (TypeData)values["TypeData"]; if (!typeData.IsTypeSupportedAsKey) { return; } // TODO: when System.Uri handles '/' correctly, re-enable. if (typeData.ClrType == typeof(System.Xml.Linq.XElement)) { return; } Type entityType = typeof(TypedEntity <,>).MakeGenericType(typeData.ClrType, typeof(int)); CustomDataContextSetup setup = new CustomDataContextSetup(entityType); object idValue = typeData.NonNullValue; if (idValue is byte[]) { // idValue = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; return; } bool useSmallCasing = (bool)values["UseSmallCasing"]; bool useDoublePostFix = (bool)values["UseDoublePostfix"]; if (!(idValue is double) && useDoublePostFix) { return; } string valueAsString = TypeData.FormatForKey(idValue, useSmallCasing, useDoublePostFix); using (TestWebRequest request = TestWebRequest.CreateForLocation(WebServerLocation.InProcess)) { Trace.WriteLine("Running with value: [" + valueAsString + "] for [" + typeData.ToString() + "]"); setup.Id = idValue; setup.MemberValue = 1; request.DataServiceType = setup.DataServiceType; request.Accept = "application/atom+xml,application/xml"; request.RequestUriString = "/Values(" + valueAsString + ")"; Trace.WriteLine("RequestUriString: " + request.RequestUriString); request.SendRequest(); request.GetResponseStreamAsXmlDocument(); } setup.Cleanup(); }); }
static void SendRequestAndAssertExistence(TestWebRequest request, string requestUri, params string[] xpaths) { request.RequestUriString = requestUri; request.SendRequest(); XmlDocument responseXml = request.GetResponseStreamAsXmlDocument(); foreach (string xpath in xpaths) { TestUtil.AssertSelectSingleElement(responseXml, xpath); } }
static void InitializeServiceAndDatabase(SqlConnection connection) { // Set up the service and issue a request - Code First will create a database based on the model using (TestWebRequest request = CreateTestWebRequest("/")) { request.Accept = "application/atom+xml,application/xml"; request.SendRequest(); XmlDocument response = request.GetResponseStreamAsXmlDocument(); TestUtil.AssertSelectNodes(response, "//app:collection[@href='Elevators']"); TestUtil.AssertSelectNodes(response, "//app:collection[@href='FloorCalls']"); TestUtil.AssertSelectNodes(response, "//app:collection[@href='ElevatorControlSystems']"); } }
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(); } }); } }
public void SecurityCallbacksFilterTest() { CombinatorialEngine engine = CombinatorialEngine.FromDimensions( new Dimension("option", "$filter,$orderby".Split(','))); TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values) { string option = (string)values["option"]; int callCount = 0; using (AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.InitializationCallbackManager.RegisterStatic((s, e) => { e.Configuration.SetEntitySetAccessRule("*", EntitySetRights.All); })) using (StaticCallbackManager <AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.ComposeQueryEventArgs> .RegisterStatic((s, e) => { System.Linq.Expressions.Expression <Func <Customer, bool> > notZero = c => c.ID != 0; e.Filter = notZero; callCount++; })) using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.ServiceType = typeof(AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.WebDataServiceA); request.RequestUriString = "/Customers?" + ((option == "$filter") ? "$filter=BestFriend/ID%20gt%200" : "$orderby=BestFriend/ID%20desc"); request.Accept = "application/atom+xml,application/xml"; request.SendRequest(); var document = request.GetResponseStreamAsXmlDocument(); Assert.AreEqual(2, callCount, "Callback is called twice (once for URI, once for best friend."); // Customer with ID #2 has best friend with ID #1 and thus it's returned. TestUtil.AssertSelectSingleElement(document, "/atom:feed/atom:entry/atom:id[text()='http://host/Customers(2)']"); if (option == "$filter") { // Customer #0 is not returned because of the filter (on the segment), and // customer #1 is not returned because of the filter (on the navigation property). TestUtil.AssertSelectSingleElement(document, "/atom:feed[0 = count(//atom:id[text()='http://host/Customers(0)'])]"); TestUtil.AssertSelectSingleElement(document, "/atom:feed[0 = count(//atom:id[text()='http://host/Customers(1)'])]"); } else { // Customer #0 is not returned because of the filter (on the segment). TestUtil.AssertSelectSingleElement(document, "/atom:feed[0 = count(//atom:id[text()='http://host/Customers(0)'])]"); TestUtil.AssertSelectSingleElement(document, "/atom:feed[1 = count(//atom:id[text()='http://host/Customers(1)'])]"); } } }); }
private static void CompareIds(string uri, string uri1) { string xpath = "/atom:feed/atom:id"; using (CustomDataContext.CreateChangeScope()) using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = typeof(CustomDataContext); request.RequestUriString = uri; request.Accept = "application/atom+xml,application/xml"; request.SendRequest(); XmlDocument document = request.GetResponseStreamAsXmlDocument(); string id = document.SelectSingleNode(xpath, TestUtil.TestNamespaceManager).InnerText; request.DataServiceType = typeof(CustomDataContext); request.RequestUriString = uri1; request.SendRequest(); document = request.GetResponseStreamAsXmlDocument(); string id1 = document.SelectSingleNode(xpath, TestUtil.TestNamespaceManager).InnerText; Assert.AreEqual(id, id1); Assert.IsFalse(id.Contains("()")); } }
public void WebDataServiceReflectionMimeTypes() { // Request metadata for an attributed data context. using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = typeof(CustomDataContext); request.RequestUriString = "/$metadata"; request.SendRequest(); XmlDocument document = request.GetResponseStreamAsXmlDocument(); // Verify that mime types are included in the payload. XmlNodeList list = document.SelectNodes("//@adsm:MimeType", TestUtil.TestNamespaceManager); Assert.IsTrue(list.Count > 0, "MimeType attributes present in attributed custom data context."); } }
public void WebDataServiceDocumentTest() { CombinatorialEngine engine = CombinatorialEngine.FromDimensions( new Dimension("Location", new object[] { WebServerLocation.InProcess, WebServerLocation.InProcessWcf }), new Dimension("ServiceModelData", ServiceModelData.Values), new Dimension("Accept", new string[] { "application/atomsvc+xml", "application/atomsvc+xml;q=0.8", "application/xml", "application/xml;q=0.5" })); TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values) { string accept = (string)values["Accept"]; WebServerLocation location = (WebServerLocation)values["Location"]; ServiceModelData model = (ServiceModelData)values["ServiceModelData"]; if (!model.IsValid) { return; } using (TestWebRequest request = TestWebRequest.CreateForLocation(location)) { request.Accept = accept; request.DataServiceType = model.ServiceModelType; request.RequestUriString = "/"; request.SendRequest(); XmlDocument document = request.GetResponseStreamAsXmlDocument(); string responseType = TestUtil.GetMediaType(request.ResponseContentType); if (accept.Contains("application/atomsvc+xml")) { Assert.AreEqual("application/atomsvc+xml", responseType); } else { Assert.AreEqual("application/xml", responseType); } Trace.WriteLine(document.OuterXml); CheckServiceDocument(document); } }); }
[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"); } } }
public void WebDataServiceBaseTypeContainer() { AdHocEntityType customersBase = new AdHocEntityType("CustomerBase"); AdHocEntityType customersDerived = new AdHocEntityType(customersBase) { Name = "CustomerDerived" }; AdHocEntityType referencingType = new AdHocEntityType("ReferencingType"); AdHocEntitySet customerSet = new AdHocEntitySet() { Name = "Customers", Type = customersBase }; AdHocAssociationType associationType = new AdHocAssociationType() { Name = "ReferenceToDerivedCustomerType", Ends = new List <AdHocAssociationTypeEnd>() { new AdHocAssociationTypeEnd() { Multiplicity = "1", RoleName = "Reference", Type = referencingType }, new AdHocAssociationTypeEnd() { Multiplicity = "*", RoleName = "Customer", Type = customersDerived }, } }; AdHocEntitySet referencingSet = new AdHocEntitySet() { Name = "ReferenceHolder", Type = referencingType }; AdHocContainer container = new AdHocContainer() { AssociationSets = new List <AdHocAssociationSet>() { new AdHocAssociationSet() { Name = "ReferenceToDerivedCustomer", Type = associationType, Ends = new List <AdHocAssociationSetEnd>() { new AdHocAssociationSetEnd() { EndType = associationType.Ends[0], EntitySet = referencingSet }, new AdHocAssociationSetEnd() { EndType = associationType.Ends[1], EntitySet = customerSet } }, } }, EntitySets = new List <AdHocEntitySet>() { customerSet, referencingSet }, ExtraEntityTypes = new List <AdHocEntityType>() { customersDerived }, }; associationType.AddNavigationProperties(); AdHocModel model = new AdHocModel(container) { ConceptualNs = TestXmlConstants.EdmV1Namespace }; Assembly assembly = model.GenerateModelsAndAssembly("WebDataServiceBaseTypeContainer", false /* isReflectionProviderBased */); using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = TestUtil.LoadDerivedTypeFromAssembly(assembly, typeof(System.Data.Objects.ObjectContext)); request.RequestUriString = "/$metadata"; request.SendRequest(); using (var s = new StreamReader(request.GetResponseStream())) Trace.WriteLine(s.ReadToEnd()); } using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = typeof(ContextWithBaseType); request.RequestUriString = "/$metadata"; request.SendRequest(); XmlDocument document = request.GetResponseStreamAsXmlDocument(); UnitTestsUtil.VerifyXPaths(document, "//csdl:Schema/csdl:EntityType[@Name='WebDataServiceTest_SimpleBaseType']", "//csdl:Schema/csdl:EntityType[@Name='WebDataServiceTest_ReferencingType']", "//csdl:Schema/csdl:EntityType[@Name='WebDataServiceTest_DerivedType' and @BaseType='AstoriaUnitTests.Tests.WebDataServiceTest_SimpleBaseType']", "//csdl:Schema/csdl:EntityContainer/csdl:EntitySet[@Name='B']", "//csdl:Schema/csdl:EntityContainer/csdl:EntitySet[@Name='R']/csdl:NavigationPropertyBinding[@Path='DerivedReference' and @Target='B']"); } }
public void SecurityCallbacksFilterEdmTest() { ServiceModelData.Northwind.EnsureDependenciesAvailable(); CombinatorialEngine engine = CombinatorialEngine.FromDimensions( new Dimension("option", "$filter,$orderby".Split(','))); TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values) { string option = (string)values["option"]; int callCount = 0; using (AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.InitializationCallbackManager.RegisterStatic((s, e) => { e.Configuration.SetEntitySetAccessRule("*", EntitySetRights.All); })) using (StaticCallbackManager <AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.ComposeQueryEventArgs> .RegisterStatic((s, e) => { System.Linq.Expressions.Expression <Func <NorthwindModel.Customers, bool> > notAnatr = c => c.CustomerID != "ANATR"; e.Filter = notAnatr; callCount++; })) using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { // In Northwind, the two first customers are 'ALFKI' and 'ANATR'. // 'ALFKI' has these orders: 10643, 10692, 10702, 10835, 10952, 11011. // 'ANATR' has these orders: 10308, 10625, 10759, 10926. request.ServiceType = typeof(AstoriaUnitTests.Tests.UnitTestModule.AuthorizationTest.WebDataServiceEdmCustomerCallback); request.RequestUriString = "/Orders?" + ((option == "$filter") ? "$filter=startswith(Customers/CustomerID, 'A')" : "$filter=startswith(Customers/CustomerID,%20'A')%20or%20Customers%20eq%20null&$orderby=Customers/CustomerID"); request.Accept = "application/atom+xml,application/xml"; request.SendRequest(); var document = request.GetResponseStreamAsXmlDocument(); if (option == "$filter") { Assert.AreEqual(1, callCount, "Callback is called a single time (for the navigation property."); // The orders for 'ANATR' should not be returned TestUtil.AssertSelectSingleElement(document, "/atom:feed[0 = count(atom:entry/atom:id[text()='http://host/Orders(10308)'])]"); TestUtil.AssertSelectSingleElement(document, "/atom:feed[0 = count(atom:entry/atom:id[text()='http://host/Orders(10625)'])]"); // ALFKI is OK. TestUtil.AssertSelectSingleElement(document, "/atom:feed[1 = count(atom:entry/atom:id[text()='http://host/Orders(10643)'])]"); } else { Assert.AreEqual(3, callCount, "Callback is called a three tims (for the navigation property)"); // ANATR will come before ALFKI System.Xml.XmlElement alfkiOrder = TestUtil.AssertSelectSingleElement(document, "/atom:feed/atom:entry[atom:id/text()='http://host/Orders(10643)']"); System.Xml.XmlElement anatrOrder = TestUtil.AssertSelectSingleElement(document, "/atom:feed/atom:entry[atom:id/text()='http://host/Orders(10308)']"); bool found = false; System.Xml.XmlNode node = alfkiOrder; while (node != null) { if (node == anatrOrder) { found = true; break; } else { node = node.PreviousSibling; } } Assert.IsTrue(found, "ANATR orders sort after ALFKI"); } } }); }
static void SendRequestAndAssertExistence(TestWebRequest request, string requestUri, params string[] xpaths) { request.RequestUriString = requestUri; request.SendRequest(); XmlDocument responseXml = request.GetResponseStreamAsXmlDocument(); foreach (string xpath in xpaths) { TestUtil.AssertSelectSingleElement(responseXml, xpath); } }