コード例 #1
0
        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 Documents.Collections.RequestNameValueCollection();
            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),
                                                           this.cosmosClient.DocumentClient.GlobalEndpointManager);

            string sessionToken = request.Headers[HttpConstants.HttpHeaders.SessionToken];

            Assert.IsTrue(!string.IsNullOrEmpty(sessionToken) && sessionToken.Split(',').Length == 1);
        }
コード例 #2
0
        public async Task GatewaySameSessionTokenTest()
        {
            string createSessionToken = null;
            HttpHandlerMetaDataValidator httpClientHandler = new HttpHandlerMetaDataValidator
            {
                RequestCallBack = (request, response) =>
                {
                    if (response.StatusCode != HttpStatusCode.Created)
                    {
                        return;
                    }

                    response.Headers.TryGetValues("x-ms-session-token", out IEnumerable <string> sessionTokens);
                    foreach (string singleToken in sessionTokens)
                    {
                        createSessionToken = singleToken;
                        break;
                    }
                }
            };

            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 Documents.Collections.RequestNameValueCollection();
                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),
                                                               client.DocumentClient.GlobalEndpointManager);

                string readSessionToken = request.Headers[HttpConstants.HttpHeaders.SessionToken];
                Assert.AreEqual(readSessionToken, createSessionToken);
            }
        }