public static CosmosClient CreateMockCosmosClient(CosmosRequestHandler preProcessingHandler = null, CosmosConfiguration configuration = null) { DocumentClient documentClient = new MockDocumentClient(); CosmosConfiguration cosmosConfiguration = configuration?.AddCustomHandlers(preProcessingHandler) ?? new CosmosConfiguration("http://localhost", Guid.NewGuid().ToString()) .AddCustomHandlers(preProcessingHandler); return(new CosmosClient( cosmosConfiguration, documentClient)); }
public override Task <CosmosResponseMessage> SendAsync( CosmosRequestMessage request, CancellationToken cancellationToken) { CosmosRequestHandler targetHandler = null; if (request.IsPartitionedFeedOperation) { targetHandler = documentFeedHandler; } else { targetHandler = pointOperationHandler; } return(targetHandler.SendAsync(request, cancellationToken)); }
public RouterHandler( CosmosRequestHandler documentFeedHandler, CosmosRequestHandler pointOperationHandler) { if (documentFeedHandler == null) { throw new ArgumentNullException(nameof(documentFeedHandler)); } if (pointOperationHandler == null) { throw new ArgumentNullException(nameof(pointOperationHandler)); } this.documentFeedHandler = documentFeedHandler; this.pointOperationHandler = pointOperationHandler; }
public override Task <CosmosResponseMessage> SendAsync( CosmosRequestMessage request, CancellationToken cancellationToken) { CosmosRequestHandler targetHandler = null; if (request.OperationType == OperationType.ReadFeed && request.ResourceType == ResourceType.Document) { targetHandler = doucumentFeedHandler; } else { targetHandler = pointOperationHandler; } return(targetHandler.SendAsync(request, cancellationToken)); }
public void HandlerOrder() { CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(); Type[] types = new Type[] { typeof(RequestInvokerHandler), typeof(RetryHandler), typeof(RouterHandler) }; CosmosRequestHandler handler = client.RequestHandler; foreach (Type type in types) { Assert.IsTrue(type.Equals(handler.GetType())); handler = (CosmosRequestHandler)handler.InnerHandler; } Assert.IsNull(handler); }
private static CosmosClientContext GetMockedClientContext( Func <CosmosRequestMessage, CancellationToken, Task <CosmosResponseMessage> > handlerFunc) { CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(); Mock <PartitionRoutingHelper> partitionRoutingHelperMock = MockCosmosUtil.GetPartitionRoutingHelperMock("0"); PartitionKeyRangeHandler partitionKeyRangeHandler = new PartitionKeyRangeHandler(client, partitionRoutingHelperMock.Object); TestHandler testHandler = new TestHandler(handlerFunc); partitionKeyRangeHandler.InnerHandler = testHandler; CosmosRequestHandler handler = client.RequestHandler.InnerHandler; while (handler != null) { if (handler.InnerHandler is RouterHandler) { handler.InnerHandler = new RouterHandler(partitionKeyRangeHandler, testHandler); break; } handler = handler.InnerHandler; } CosmosJsonSerializer cosmosJsonSerializer = new CosmosJsonSerializerCore(); CosmosResponseFactory responseFactory = new CosmosResponseFactory(cosmosJsonSerializer, cosmosJsonSerializer); return(new CosmosClientContextCore( client: client, clientOptions: null, userJsonSerializer: cosmosJsonSerializer, defaultJsonSerializer: cosmosJsonSerializer, cosmosResponseFactory: responseFactory, requestHandler: client.RequestHandler, documentClient: new MockDocumentClient(), documentQueryClient: new Mock <Query.IDocumentQueryClient>().Object)); }