public async Task TestGatewayModelSession() { ContainerProperties containerProperties = await this.Container.GetCachedContainerPropertiesAsync( false, Trace.GetRootTrace("Test"), CancellationToken.None); ISessionContainer sessionContainer = this.cosmosClient.DocumentClient.sessionContainer; string docLink = "dbs/" + this.database.Id + "/colls/" + containerProperties.Id + "/docs/3"; Documents.Collections.INameValueCollection headers = new StoreRequestNameValueCollection(); headers.Set(HttpConstants.HttpHeaders.PartitionKey, "[\"Status3\"]"); DocumentServiceRequest request = DocumentServiceRequest.Create(OperationType.Read, ResourceType.Document, docLink, AuthorizationTokenType.PrimaryMasterKey, headers); string globalSessionToken = sessionContainer.ResolveGlobalSessionToken(request); Assert.IsTrue(globalSessionToken.Split(',').Length > 1); await GatewayStoreModel.ApplySessionTokenAsync(request, Cosmos.ConsistencyLevel.Session, sessionContainer, await this.cosmosClient.DocumentClient.GetPartitionKeyRangeCacheAsync(NoOpTrace.Singleton), await this.cosmosClient.DocumentClient.GetCollectionCacheAsync(NoOpTrace.Singleton)); string sessionToken = request.Headers[HttpConstants.HttpHeaders.SessionToken]; Assert.IsTrue(!string.IsNullOrEmpty(sessionToken) && sessionToken.Split(',').Length == 1); }
/// <summary> /// Sends a request with a particular response status code through the GatewayStoreModel /// </summary> private static async Task GatewayStoreClientRunScenario( DocumentServiceRequest request, int responseStatusCode) { Func <HttpRequestMessage, Task <HttpResponseMessage> > sendFunc = async httpRequest => await Task.FromResult(new HttpResponseMessage((HttpStatusCode)responseStatusCode)); GatewayStoreModel storeModel = MockGatewayStoreModel(sendFunc); using (new ActivityScope(Guid.NewGuid())) { await storeModel.ProcessMessageAsync(request); } }
public async Task GatewaySameSessionTokenTest() { string createSessionToken = null; GatewaySessionTokenTests.HttpClientHandlerHelper httpClientHandler = new HttpClientHandlerHelper { ResponseCallBack = (result) => { HttpResponseMessage response = result.Result; if (response.StatusCode != HttpStatusCode.Created) { return(response); } response.Headers.TryGetValues("x-ms-session-token", out IEnumerable <string> sessionTokens); foreach (string singleToken in sessionTokens) { createSessionToken = singleToken; break; } return(response); } }; using (CosmosClient client = TestCommon.CreateCosmosClient(builder => builder .WithConnectionModeGateway() .WithConsistencyLevel(Cosmos.ConsistencyLevel.Session) .WithHttpClientFactory(() => new HttpClient(httpClientHandler)))) { Container container = client.GetContainer(this.database.Id, this.Container.Id); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity("Status1001", "1001"); ItemResponse <ToDoActivity> itemResponse = await container.CreateItemAsync(item); // Read back the created Item and check if the session token is identical. string docLink = "dbs/" + this.database.Id + "/colls/" + this.Container.Id + "/docs/1001"; Documents.Collections.INameValueCollection headers = new StoreRequestNameValueCollection(); headers.Set(HttpConstants.HttpHeaders.PartitionKey, "[\"Status1001\"]"); DocumentServiceRequest request = DocumentServiceRequest.Create(OperationType.Read, ResourceType.Document, docLink, AuthorizationTokenType.PrimaryMasterKey, headers); await GatewayStoreModel.ApplySessionTokenAsync(request, Cosmos.ConsistencyLevel.Session, client.DocumentClient.sessionContainer, await client.DocumentClient.GetPartitionKeyRangeCacheAsync(NoOpTrace.Singleton), await client.DocumentClient.GetCollectionCacheAsync(NoOpTrace.Singleton)); string readSessionToken = request.Headers[HttpConstants.HttpHeaders.SessionToken]; Assert.AreEqual(readSessionToken, createSessionToken); } }