private void AssertInStreamErrorThrown(string uri, string contentType, string resourceIdentifier, params object[] arguments) { var response = CustomInstanceAnnotationsReader.GetResponseString(new Uri(this.ServiceUri + uri), contentType); // In-stream errors cannot be parsed using ODL reader, so we have to check for a match in the response stream StringResourceUtil.VerifyODataLibString(response, resourceIdentifier, false /* isExactMatch */, arguments); }
public void QueryEntityNavigationWithImplicitKeys() { // this test is to baseline the WCF Data Service behavior that is not modified to support implicit keys Dictionary <string, bool> testCases = new Dictionary <string, bool>() { { "Login('1')/SentMessages(FromUsername='******',MessageId=-10)", false /*expect error*/ }, { "Login('1')/SentMessages(MessageId=-10)", true /*expect error*/ }, { "Login('1')/SentMessages(-10)", true /*expect error*/ }, }; var contextWrapper = this.CreateContext(); foreach (var testCase in testCases) { try { var message = contextWrapper.Execute <Message>(new Uri(this.ServiceUri.OriginalString.TrimEnd('/') + "/" + testCase.Key)).Single(); Assert.False(testCase.Value); Assert.Equal(-10, message.MessageId); } catch (DataServiceQueryException ex) { Assert.True(testCase.Value); Assert.Equal(400, ex.Response.StatusCode); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "BadRequest_KeyCountMismatch", "Microsoft.Test.OData.Services.AstoriaDefaultService.Message"); //InnerException for DataServiceClientException must be set with the exception response from the server. ODataErrorException oDataErrorException = ex.InnerException.InnerException as ODataErrorException; Assert.True(oDataErrorException != null, "InnerException for DataServiceClientException has not been set."); Assert.Equal("An error was read from the payload. See the 'Error' property for more details.", oDataErrorException.Message); } } }
public void AddRemoveNullEntity() { var ctxwrap = this.CreateWrappedContext <DefaultContainer>(); var context = ctxwrap.Context; DataServiceCollection <Customer> DSC = new DataServiceCollection <Customer>(context); try { DSC.Add(null); Assert.True(false, "Expected error not thrown"); } catch (InvalidOperationException e) { StringResourceUtil.VerifyDataServicesClientString(e.Message, "DataBinding_BindingOperation_ArrayItemNull", "Add"); } try { DSC.Remove(null); Assert.True(false, "Expected error not thrown"); } catch (InvalidOperationException e) { StringResourceUtil.VerifyDataServicesClientString(e.Message, "DataBinding_BindingOperation_ArrayItemNull", "Remove"); } this.EnqueueTestComplete(); }
public void QueryEntityNavigationWithImplicitKeys( string endpoint, bool expectSuccess, /* only used on success */ int messageId, /* only used on error */ string errorStringIdentifier) { // this test is to baseline the WCF Data Service behavior that is not modified to support implicit keys var contextWrapper = this.CreateContext(); if (expectSuccess) { var message = contextWrapper.Execute <Message>(new Uri(this.ServiceUri.OriginalString.TrimEnd('/') + "/" + endpoint)).Single(); Assert.Equal(messageId, message.MessageId); } else { try { var message = contextWrapper.Execute <Message>(new Uri(this.ServiceUri.OriginalString.TrimEnd('/') + "/" + endpoint)).Single(); Assert.False(true, "This statement should not have executed because the request should have thrown an exception."); } catch (DataServiceQueryException ex) { Assert.Equal(400, ex.Response.StatusCode); StringResourceUtil.VerifyODataLibString(ClientExceptionUtil.ExtractServerErrorMessage(ex), errorStringIdentifier, true, "Microsoft.Test.OData.Services.AstoriaDefaultService.Message"); //InnerException for DataServiceClientException must be set with the exception response from the server. ODataErrorException oDataErrorException = ex.InnerException.InnerException as ODataErrorException; Assert.True(oDataErrorException != null, "InnerException for DataServiceClientException has not been set."); Assert.Equal("An error was read from the payload. See the 'Error' property for more details.", oDataErrorException.Message); } } }
public void QueryEntityNavigationWithImplicitKeys() { // this test is to baseline the WCF Data Service behavior that is not modified to support implicit keys Dictionary <string, bool> testCases = new Dictionary <string, bool>() { { "Login('1')/SentMessages(FromUsername='******',MessageId=-10)", false /*expect error*/ }, { "Login('1')/SentMessages(MessageId=-10)", true /*expect error*/ }, { "Login('1')/SentMessages(-10)", true /*expect error*/ }, }; var contextWrapper = this.CreateContext(); foreach (var testCase in testCases) { try { var message = contextWrapper.Execute <Message>(new Uri(this.ServiceUri.OriginalString.TrimEnd('/') + "/" + testCase.Key)).Single(); Assert.IsFalse(testCase.Value); Assert.AreEqual(-10, message.MessageId); } catch (DataServiceQueryException ex) { Assert.IsTrue(testCase.Value); Assert.AreEqual(400, ex.Response.StatusCode); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "BadRequest_KeyCountMismatch", "Microsoft.Test.OData.Services.AstoriaDefaultService.Message"); } } }
private static void AssertThrows <T>(Action action, string expectedError) where T : Exception { try { action(); Assert.IsNull(expectedError, "Expected exception not thrown"); } catch (T e) { Assert.IsNotNull(expectedError, "Unexpected expection " + e.Message); StringResourceUtil.VerifyODataLibString(e.Message, expectedError, false /* isExactMatch */); } }
private void VerifyErrorString(DataServiceContext context, string errorUrl, string errorString, params object[] arguments) { try { context.Execute <Computer>(new Uri(this.ServiceUri.OriginalString + errorUrl)); Assert.Fail("Expected Exception not thrown for " + errorUrl); } catch (DataServiceQueryException ex) { Assert.IsNotNull(ex.InnerException, "No inner exception found"); Assert.IsInstanceOfType(ex.InnerException, typeof(DataServiceClientException), "Unexpected inner exception type"); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), errorString, arguments); } }
private void VerifyErrorString(DataServiceContext context, string errorUrl, string errorString, params object[] arguments) { try { context.Execute <Computer>(new Uri(this.ServiceUri.OriginalString + errorUrl)); Assert.True(false, "Expected Exception not thrown for " + errorUrl); } catch (DataServiceQueryException ex) { Assert.NotNull(ex.InnerException); Assert.IsType <DataServiceClientException>(ex.InnerException); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), errorString, arguments); } }
public void ClientWithKeyAsSegmentSendsRequestsToServerWithoutKeyAsSegment() { try { var contextWrapper = this.CreateWrappedContext <InMemoryEntities>(); contextWrapper.UrlKeyDelimiter = DataServiceUrlKeyDelimiter.Slash; contextWrapper.Context.Orders.Where(c => c.OrderID == 0).ToArray(); Assert.True(false, "Expected DataServiceException was not thrown."); } catch (DataServiceQueryException ex) { Assert.NotNull(ex.InnerException); Assert.IsType <DataServiceClientException>(ex.InnerException); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "RequestUriProcessor_CannotQueryCollections", "Orders"); } }
public void ClientWithKeyAsSegmentSendsRequestsToServerWithoutKeyAsSegment() { try { var contextWrapper = this.CreateWrappedContext <DefaultContainer>(); contextWrapper.UrlConventions = DataServiceUrlConventions.KeyAsSegment; contextWrapper.Context.Customer.Where(c => c.CustomerId == 0).ToArray(); Assert.Fail("Expected DataServiceException was not thrown."); } catch (DataServiceQueryException ex) { Assert.IsNotNull(ex.InnerException, "No inner exception found"); Assert.IsInstanceOfType(ex.InnerException, typeof(DataServiceClientException), "Unexpected inner exception type"); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "RequestUriProcessor_CannotQueryCollections", "Customer"); } }
public void HttpMergeIsNoLongerSupported() { Func <DataServiceClientRequestMessageArgs, DataServiceClientRequestMessage> addHeader = (args) => { if (args.Method == "PATCH") { // use Merge var newArgs = new DataServiceClientRequestMessageArgs("MERGE", args.RequestUri, true, args.UsePostTunneling, args.Headers); // use V4 since Merge is removed only in V4 newArgs.Headers["DataServiceVersion"] = "4.0"; return(new HttpWebRequestMessage(newArgs)); } return(new HttpWebRequestMessage(args)); }; var ctx = this.CreateContext(addHeader); Product product = null; var query = ctx.Context.Product.Take(1) as DataServiceQuery <Product>; var ar = query.BeginExecute(null, null).EnqueueWait(this); product = query.EndExecute(ar).Single(); product.Description = "New Description " + Guid.NewGuid().ToString(); ctx.UpdateObject(product); try { var ar2 = ctx.BeginSaveChanges(null, null).EnqueueWait(this); ctx.EndSaveChanges(ar2); Assert.Fail("Expected error not thrown"); } catch (DataServiceRequestException e) { #if !PORTABLELIB && !SILVERLIGHT StringResourceUtil.VerifyDataServicesClientString(e.Message, "DataServiceException_GeneralError"); #else Assert.IsNotNull(e); #endif } }
public void ModifyQueryOptions() { var context = this.CreateWrappedContext <DefaultContainer>(); var personQuery = context.CreateQuery <Person>("Person"); try { personQuery.Execute(); Assert.Fail("ModifyingQueryOptionsShouldFail"); } catch (DataServiceQueryException ex) { Assert.IsNotNull(ex.InnerException, "No inner exception found"); Assert.IsInstanceOfType(ex.InnerException, typeof(DataServiceClientException), "Unexpected inner exception type"); StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "AstoriaRequestMessage_CannotChangeQueryString"); } }
public void BatchRequestBaseUriDifferentBetweenBatchAndRequest() { var context = this.CreateWrappedContext <DefaultContainer>(); //Setup queries DataServiceRequest[] reqs = new DataServiceRequest[] { context.CreateQuery <Customer>("BatchRequest3"), }; var response = context.ExecuteBatch(reqs); Assert.IsNotNull(response); Assert.IsTrue(response.IsBatchResponse); foreach (QueryOperationResponse item in response) { Assert.IsNotNull(item.Error); Assert.IsInstanceOfType(item.Error, typeof(DataServiceClientException), "Unexpected inner exception type"); var ex = item.Error as DataServiceClientException; StringResourceUtil.VerifyDataServicesString(ClientExceptionUtil.ExtractServerErrorMessage(item), "DataServiceOperationContext_CannotModifyServiceUriInsideBatch"); } }
public void BasesDontMatchFail() { var context = this.CreateWrappedContext <DefaultContainer>(); DataServiceQuery customQuery = context.CreateQuery <Customer>("BasesDontMatchFail"); //Path should remap to the customers set var expectedServiceUrl = "http://potato:9090/FailMeService/"; var expectedRequestUrl = "http://potato:9090/DontFailMeService/Customer"; try { customQuery.Execute(); Assert.Fail("Different service bases between service uri and request uri should fail"); } catch (DataServiceQueryException ex) { Assert.IsNotNull(ex.InnerException, "No inner exception found"); Assert.IsInstanceOfType(ex.InnerException, typeof(DataServiceClientException), "Unexpected inner exception type"); StringResourceUtil.VerifyODataLibString(ClientExceptionUtil.ExtractServerErrorMessage(ex), "UriQueryPathParser_RequestUriDoesNotHaveTheCorrectBaseUri", true, expectedRequestUrl, expectedServiceUrl); } }