public void PreferHeader_CrossFeature() { DSPMetadata metadata = PreferHeader_CreateMetadata(); DSPSelfmodifyingResource existingItem = new DSPSelfmodifyingResource(metadata.GetResourceType("Item")); existingItem.SetRawValue("ID", 0); existingItem.SetRawValue("ETagProperty", 1); DSPServiceDefinition service = PreferHeader_CreateService(metadata, existingItem, null, null, null); ProcessingPipelineCallCount callCount = new ProcessingPipelineCallCount(); service.ProcessingPipeline.ProcessingRequest += (sender, args) => { callCount.ProcessingRequestCallCount++; }; service.ProcessingPipeline.ProcessedRequest += (sender, args) => { callCount.ProcessedRequestCallCount++; }; service.ProcessingPipeline.ProcessingChangeset += (sender, args) => { callCount.ProcessingChangesetCallCount++; }; service.ProcessingPipeline.ProcessedChangeset += (sender, args) => { callCount.ProcessedChangesetCallCount++; }; service.DataServiceType = typeof(PreferHeader_InterceptorsService); ProcessingPipelineCallCount expectedCallCount = new ProcessingPipelineCallCount() { ProcessingRequestCallCount = 1, ProcessedRequestCallCount = 1, ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 }; // Can't get the payload from the service since we sometimes don't allow read rights XDocument existingItemAtom = XDocument.Parse("<entry xmlns:d='http://docs.oasis-open.org/odata/ns/data' xmlns:m='http://docs.oasis-open.org/odata/ns/metadata' xmlns='http://www.w3.org/2005/Atom'>" + "<id>http://host/Items(0)</id>" + "<title type='text'></title>" + "<updated>2010-07-19T14:20:32Z</updated>" + "<author><name /></author>" + "<category term='TestNS.Item' scheme='http://docs.oasis-open.org/odata/ns/scheme' />" + "<content type='application/xml'><m:properties>" + "<d:ID m:type='Edm.Int32'>0</d:ID>" + "<d:ETagProperty m:type='Edm.Int32'>1</d:ETagProperty>" + "</m:properties></content></entry>"); TestUtil.RunCombinations( UnitTestsUtil.BooleanValues, UnitTestsUtil.BooleanValues, (includeRelationshipLinks, allowReadAccess) => { service.DataServiceBehavior.IncludeRelationshipLinksInResponse = includeRelationshipLinks; service.EntitySetAccessRule = new Dictionary<string,EntitySetRights>() { {"Items", EntitySetRights.AllWrite | (allowReadAccess ? EntitySetRights.AllRead : EntitySetRights.None)} }; using (TestWebRequest request = service.CreateForInProcess()) { request.RegisterForDispose(PreferHeader_InterceptorsService.InterceptItems.Restore()); TestUtil.RunCombinations( new string[] { "POST", "PUT", "PATCH" }, new string[] { null, "return=representation", "return=minimal" }, new string[] { UnitTestsUtil.AtomFormat }, UnitTestsUtil.BooleanValues, UnitTestsUtil.BooleanValues, (httpMethod, preferHeader, format, batch, interceptItems) => { PreferHeader_InterceptorsService.InterceptItems.Value = interceptItems; PreferHeader_InterceptorsService.LastItemsUpdateOperation = UpdateOperations.None; PreferHeader_InterceptorsService.ItemsQueryInterceptorInvoked = false; service.ClearChanges(); callCount.Clear(); TestWebRequest r = request; if (batch) { r = new InMemoryWebRequest(); } r.RequestVersion = "4.0;"; r.RequestMaxVersion = "4.0;"; r.RequestHeaders["Prefer"] = preferHeader; PreferHeader_SetupRequest(r, httpMethod, format, existingItemAtom, existingItem, null); if (batch) { var batchRequest = new BatchWebRequest(); var changeset = new BatchWebRequest.Changeset(); changeset.Parts.Add((InMemoryWebRequest)r); batchRequest.Changesets.Add(changeset); batchRequest.SendRequest(request); } else { TestUtil.RunCatching(request.SendRequest); } Assert.AreEqual(httpMethod != "POST", PreferHeader_InterceptorsService.ItemsQueryInterceptorInvoked, "The query interceptor fire or didn't fire when it should have."); if (!allowReadAccess && httpMethod != "POST") { Assert.AreEqual(403, r.ResponseStatusCode, "The request should have failed due to access rights."); } else if (interceptItems && httpMethod != "POST") { Assert.AreEqual(404, r.ResponseStatusCode, "The request should not have found any resource."); } else { PreferHeader_VerifyResponse(r, request.ServiceRoot.ToString(), true, (response) => { var relationshipLinks = response.Root.Elements(UnitTestsUtil.AtomNamespace + "link") .Where(e => e.Attribute("rel").Value.Contains("http://docs.oasis-open.org/odata/ns/relatedlinks/")); if (includeRelationshipLinks) { Assert.AreEqual(1, relationshipLinks.Count(), "The response should contain a relationship link."); } else { Assert.AreEqual(0, relationshipLinks.Count(), "The resounrce should not contain any relationship links."); } }); callCount.AssertEquals(expectedCallCount); Assert.AreEqual(httpMethod == "POST" ? UpdateOperations.Add : UpdateOperations.Change, PreferHeader_InterceptorsService.LastItemsUpdateOperation, "The change interceptor didn't fire correctly."); } }); } }); }
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); } }
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"); } }
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"); }); } }
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"); }); } }
public void Collection_ProcessingPipeline() { var metadata = CreateMetadataForXFeatureEntity(); Func<int, DSPResource> CreateNewXFeatureEntityResource = (id) => { DSPResourceWithCollectionProperty newResource = new DSPResourceWithCollectionProperty(metadata.GetResourceType("XFeatureTestsEntity")); newResource.SetRawValue("ID", id); newResource.SetRawValue("Description", "Second"); newResource.SetRawValue("Strings", new List<string>() { "One", "Two" }); newResource.SetRawValue("Structs", new List<DSPResource>()); return newResource; }; var actualCallCount = new ProcessingPipelineCallCount(); DSPServiceDefinition service = new DSPServiceDefinition() { Metadata = metadata, CreateDataSource = (m) => { DSPContext context = new DSPContext(); context.GetResourceSetEntities("Entities").Add(CreateNewXFeatureEntityResource(0)); return context; }, Writable = true, }; service.ProcessingPipeline.ProcessingRequest = (sender, args) => { actualCallCount.ProcessingRequestCallCount++; }; service.ProcessingPipeline.ProcessedRequest = (sender, args) => { actualCallCount.ProcessedRequestCallCount++; }; service.ProcessingPipeline.ProcessingChangeset = (sender, args) => { actualCallCount.ProcessingChangesetCallCount++; }; service.ProcessingPipeline.ProcessedChangeset = (sender, args) => { actualCallCount.ProcessedChangesetCallCount++; }; var testCases = new ProcessingPipelineTestCase[] { new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "GET"; r.RequestUriString = "/Entities"; r.Accept = format; }, }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "GET"; r.RequestUriString = "/Entities(0)/Strings"; r.Accept = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; }, }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "GET"; r.RequestUriString = "/Entities(0)/Structs"; r.Accept = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; }, }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "POST"; r.RequestUriString = "/Entities"; r.Accept = format; r.RequestContentType = format; r.SetRequestStreamAsText(DSPResourceSerializer.WriteEntity( CreateNewXFeatureEntityResource(1), DSPResourceSerializer.SerializerFormatFromMimeType(format))); }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "PUT"; r.RequestUriString = "/Entities(0)"; r.Accept = format; r.RequestContentType = format; r.SetRequestStreamAsText(DSPResourceSerializer.WriteEntity( CreateNewXFeatureEntityResource(0), DSPResourceSerializer.SerializerFormatFromMimeType(format))); }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "PATCH"; r.RequestUriString = "/Entities(0)"; r.Accept = format; r.RequestContentType = format; r.SetRequestStreamAsText(DSPResourceSerializer.WriteEntity( CreateNewXFeatureEntityResource(0), DSPResourceSerializer.SerializerFormatFromMimeType(format))); }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "DELETE"; r.RequestUriString = "/Entities(0)"; r.Accept = format; }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "PUT"; r.RequestUriString = "/Entities(0)/Strings"; r.Accept = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; r.RequestContentType = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; r.SetRequestStreamAsText(DSPResourceSerializer.WriteProperty( metadata.GetResourceType("XFeatureTestsEntity").Properties.First(p => p.Name == "Strings"), new List<string> { "Foo", "Bar" }, DSPResourceSerializer.SerializerFormatFromMimeType(format))); }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, new ProcessingPipelineTestCase() { SetupRequest = (r, format) => { r.HttpMethod = "PUT"; r.RequestUriString = "/Entities(0)/Structs"; r.Accept = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; r.RequestContentType = format == UnitTestsUtil.AtomFormat ? UnitTestsUtil.MimeApplicationXml : format; r.SetRequestStreamAsText(DSPResourceSerializer.WriteProperty( metadata.GetResourceType("XFeatureTestsEntity").Properties.First(p => p.Name == "Structs"), new List<DSPResource>(), DSPResourceSerializer.SerializerFormatFromMimeType(format))); }, ExpectedCallCount = new ProcessingPipelineCallCount() { ProcessingChangesetCallCount = 1, ProcessedChangesetCallCount = 1 } }, }; using (DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Restore()) using (TestWebRequest request = service.CreateForInProcess()) { DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Value = true; TestUtil.RunCombinations(testCases, UnitTestsUtil.BooleanValues, UnitTestsUtil.ResponseFormats, (testCase, batch, format) => { service.ClearChanges(); actualCallCount.Clear(); var expectedCallCount = new ProcessingPipelineCallCount(testCase.ExpectedCallCount); // Each request must fire at least one ProcessingRequest and ProcessedRequest expectedCallCount.ProcessingRequestCallCount++; expectedCallCount.ProcessedRequestCallCount++; if (batch) { InMemoryWebRequest batchPart = new InMemoryWebRequest(); testCase.SetupRequest(batchPart, format); BatchWebRequest batchRequest = new BatchWebRequest(); if (batchPart.HttpMethod == "GET") { batchRequest.Parts.Add(batchPart); } else { var changeset = new BatchWebRequest.Changeset(); changeset.Parts.Add(batchPart); batchRequest.Changesets.Add(changeset); } batchRequest.SendRequest(request); } else { request.RequestContentType = null; request.RequestStream = null; testCase.SetupRequest(request, format); request.SendRequest(); } actualCallCount.AssertEquals(expectedCallCount); }); } }
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); } }); } }