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); } }); } }
public void BatchSmokeTestWhereRequestUriIsNotRelativeToService() { Run(request => { request.HttpMethod = "POST"; request.RequestUriString = "NotBatchAtAll"; request.RequestHeaders["MyCustomRequestUri"] = "http://myservicebase/path1/path2/$batch"; request.RequestHeaders["MyCustomServiceUri"] = "http://myservicebase/path1/path2/"; var batch = new BatchWebRequest(); var innerRequest = new InMemoryWebRequest(); innerRequest.RequestUriString = "SomethingThatWillNotBeUsedAtAll"; innerRequest.RequestHeaders["MyCustomRequestUri"] = "http://myservicebase/path2/Customers(0)"; batch.Parts.Add(innerRequest); batch.SetContentTypeAndRequestStream(request); TestUtil.RunCatching(request.SendRequest); Assert.AreEqual(202, request.ResponseStatusCode); batch.ParseResponseFromRequest(request, true); Assert.AreEqual(400, innerRequest.ResponseStatusCode); var innerPayload = innerRequest.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPathExists(innerPayload, "adsm:error/adsm:message[text()=\"The URI 'http://myservicebase/path2/Customers(0)' is not valid because it is not based on 'http://myservicebase/path1/path2/'.\"]"); }); }
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"); } }
private static void RunBatchTest(TestWebRequest request, Action <TestWebRequest> configureBatchRequest, Action <TestWebRequest> configureInnerRequest, string innerRequestXPath) { request.HttpMethod = "POST"; var batch = new BatchWebRequest(); var innerRequest = new InMemoryWebRequest(); innerRequest.Accept = "application/atom+xml,application/xml"; configureBatchRequest(request); configureInnerRequest(innerRequest); batch.Parts.Add(innerRequest); batch.SetContentTypeAndRequestStream(request); TestUtil.RunCatching(request.SendRequest); Assert.AreEqual(202, request.ResponseStatusCode); batch.ParseResponseFromRequest(request, true); Assert.AreEqual(200, innerRequest.ResponseStatusCode); var innerPayload = innerRequest.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPathExists(innerPayload, innerRequestXPath); }
[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"); }); } }
[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"); }); } }
[Ignore] // Remove Atom // [TestCategory("Partition2"), TestMethod, Variation] public void BatchContentTypeTest() { var testCases = new BatchContentTypeTestCase[] { // Completely wrong content type new BatchContentTypeTestCase { ContentType = "text/plain", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = DataServicesClientResourceUtil.GetString("Batch_ExpectedContentType", "text/plain") }, // Just type is correct, subtype is wrong new BatchContentTypeTestCase { ContentType = "multipart/text", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = DataServicesClientResourceUtil.GetString("Batch_ExpectedContentType", "multipart/text") }, // No boundary - still wrong new BatchContentTypeTestCase { ContentType = "multipart/mixed", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = ODataLibResourceUtil.GetString("MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads", "multipart/mixed", "boundary") }, // Some other parameter but no boundary new BatchContentTypeTestCase { ContentType = "multipart/mixed;param=value", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = ODataLibResourceUtil.GetString("MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads", "multipart/mixed;param=value", "boundary") }, // Empty boundary - fails new BatchContentTypeTestCase { ContentType = "multipart/mixed;boundary=", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = ODataLibResourceUtil.GetString("ValidationUtils_InvalidBatchBoundaryDelimiterLength", string.Empty, "70") }, new BatchContentTypeTestCase { ContentType = "multipart/mixed;boundary=;param=value", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = ODataLibResourceUtil.GetString("ValidationUtils_InvalidBatchBoundaryDelimiterLength", string.Empty, "70") }, // Two boundary parameters - wrong new BatchContentTypeTestCase { ContentType = "multipart/mixed;boundary=one;boundary=two", ExpectedErrorStatusCode = 400, ExpectedClientErrorMessage = ODataLibResourceUtil.GetString("MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads", "multipart/mixed;boundary=one;boundary=two", "boundary") }, // Valid simple boundary new BatchContentTypeTestCase { ContentType = "multipart/mixed;boundary=batchboundary", PayloadBatchBoundary = "batchboundary" }, // Valid simple boundary - mimetype using different casing new BatchContentTypeTestCase { ContentType = "MultiPart/mIxed;boundary=batchboundary", PayloadBatchBoundary = "batchboundary" }, // Valid simple boundary - boundary parameter name different casing new BatchContentTypeTestCase { ContentType = "multipart/mixed;BounDary=batchboundary", PayloadBatchBoundary = "batchboundary" }, }; OpenWebDataServiceDefinition serverService = new OpenWebDataServiceDefinition() { DataServiceType = typeof(CustomDataContext) }; PlaybackServiceDefinition clientService = new PlaybackServiceDefinition(); TestUtil.RunCombinations( testCases, (testCase) => { using (TestWebRequest request = serverService.CreateForInProcess()) { request.RequestContentType = testCase.ContentType; request.SetRequestStreamAsText(string.Format( "--{0}\r\n" + "Content-Type: multipart/mixed; boundary=changesetresponse_00000001-0000-0000-0000-000000000000\r\n\r\n" + "--changesetresponse_00000001-0000-0000-0000-000000000000\r\n" + "--changesetresponse_00000001-0000-0000-0000-000000000000--\r\n" + "--{0}--\r\n", testCase.PayloadBatchBoundary)); request.RequestUriString = "/$batch"; request.HttpMethod = "POST"; Exception exception = TestUtil.RunCatching(request.SendRequest); int actualStatusCode = 0; if (exception != null) { actualStatusCode = request.ResponseStatusCode; } else { Assert.AreEqual(202, request.ResponseStatusCode, "Wrong response code for no-exception request."); BatchWebRequest batchResponse = BatchWebRequest.FromResponse(InMemoryWebRequest.FromResponse(request)); if (batchResponse.Parts.Count > 0) { actualStatusCode = batchResponse.Parts[0].ResponseStatusCode; if (actualStatusCode == 200) { actualStatusCode = 0; } } } Assert.AreEqual(testCase.ExpectedErrorStatusCode, actualStatusCode, "Wrong status code."); } using (TestWebRequest request = clientService.CreateForInProcessWcf()) { request.StartService(); clientService.ProcessRequestOverride = clientRequest => { var clientResponse = new InMemoryWebRequest(); clientResponse.SetResponseStatusCode(202); clientResponse.ResponseHeaders["Content-Type"] = testCase.ContentType; clientResponse.SetResponseStreamAsText(string.Format( "--{0}\r\n" + "Content-Type: application/http\r\n" + "Content-Transfer-Encoding: binary\r\n" + "\r\n" + "200 OK\r\n" + "<feed xmlns='http://www.w3.org/2005/Atom'/>\r\n" + "--{0}--\r\n", testCase.PayloadBatchBoundary)); return(clientResponse); }; DataServiceContext ctx = new DataServiceContext(request.ServiceRoot); Exception exception = TestUtil.RunCatching(() => ctx.ExecuteBatch(ctx.CreateQuery <Customer>("/Customers"))); if (exception != null) { exception = ((DataServiceRequestException)exception).InnerException; Assert.AreEqual(testCase.ExpectedClientErrorMessage, exception.Message, "Unexpected error message."); } else { Assert.IsNull(testCase.ExpectedClientErrorMessage, "Expected exception, but none was thrown."); } } }); }