示例#1
0
        public virtual async Task <DocumentServiceResponse> ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            GatewayStoreModel.ApplySessionToken(
                request,
                this.defaultConsistencyLevel,
                this.sessionContainer);

            DocumentServiceResponse response;

            try
            {
                Uri physicalAddress = GatewayStoreClient.IsFeedRequest(request.OperationType) ? this.GetFeedUri(request) : this.GetEntityUri(request);
                response = await this.gatewayStoreClient.InvokeAsync(request, request.ResourceType, physicalAddress, cancellationToken);
            }
            catch (DocumentClientException exception)
            {
                if ((!ReplicatedResourceClient.IsMasterResource(request.ResourceType)) &&
                    (exception.StatusCode == HttpStatusCode.PreconditionFailed || exception.StatusCode == HttpStatusCode.Conflict ||
                     (exception.StatusCode == HttpStatusCode.NotFound && exception.GetSubStatus() != SubStatusCodes.ReadSessionNotAvailable)))
                {
                    this.CaptureSessionToken(exception.StatusCode, exception.GetSubStatus(), request, exception.Headers);
                }

                throw;
            }

            this.CaptureSessionToken(response.StatusCode, response.SubStatusCode, request, response.Headers);
            return(response);
        }
示例#2
0
        public void TestApplySessionForDataOperation()
        {
            List <ResourceType> resourceTypes = new List <ResourceType>()
            {
                ResourceType.Document,
                ResourceType.Conflict,
                ResourceType.Batch
            };

            List <OperationType> operationTypes = new List <OperationType>()
            {
                OperationType.Create,
                OperationType.Delete,
                OperationType.Read,
                OperationType.Upsert,
                OperationType.Replace
            };

            foreach (ResourceType resourceType in resourceTypes)
            {
                foreach (OperationType operationType in operationTypes)
                {
                    Assert.IsFalse(GatewayStoreModel.IsMasterOperation(
                                       resourceType,
                                       operationType),
                                   $"{resourceType}, {operationType}");

                    // Verify when user does set session token
                    DocumentServiceRequest dsr = DocumentServiceRequest.CreateFromName(
                        operationType,
                        "Test",
                        resourceType,
                        AuthorizationTokenType.PrimaryMasterKey);

                    string dsrSessionToken = Guid.NewGuid().ToString();
                    dsr.Headers.Add(HttpConstants.HttpHeaders.SessionToken, dsrSessionToken);

                    GatewayStoreModel.ApplySessionToken(
                        dsr,
                        ConsistencyLevel.Session,
                        new Mock <ISessionContainer>().Object);

                    Assert.AreEqual(dsrSessionToken, dsr.Headers[HttpConstants.HttpHeaders.SessionToken]);

                    // Verify when user does not set session token
                    DocumentServiceRequest dsrNoSessionToken = DocumentServiceRequest.CreateFromName(
                        operationType,
                        "Test",
                        resourceType,
                        AuthorizationTokenType.PrimaryMasterKey);

                    Mock <ISessionContainer> sMock = new Mock <ISessionContainer>();
                    sMock.Setup(x => x.ResolveGlobalSessionToken(dsrNoSessionToken)).Returns(dsrSessionToken);

                    GatewayStoreModel.ApplySessionToken(
                        dsrNoSessionToken,
                        ConsistencyLevel.Session,
                        sMock.Object);

                    Assert.AreEqual(dsrSessionToken, dsrNoSessionToken.Headers[HttpConstants.HttpHeaders.SessionToken]);
                }
            }

            // Verify stored procedure execute
            Assert.IsFalse(GatewayStoreModel.IsMasterOperation(
                               ResourceType.StoredProcedure,
                               OperationType.ExecuteJavaScript));

            DocumentServiceRequest dsrSprocExecute = DocumentServiceRequest.CreateFromName(
                OperationType.ExecuteJavaScript,
                "Test",
                ResourceType.StoredProcedure,
                AuthorizationTokenType.PrimaryMasterKey);

            string sessionToken = Guid.NewGuid().ToString();

            dsrSprocExecute.Headers.Add(HttpConstants.HttpHeaders.SessionToken, sessionToken);

            GatewayStoreModel.ApplySessionToken(
                dsrSprocExecute,
                ConsistencyLevel.Session,
                new Mock <ISessionContainer>().Object);

            Assert.AreEqual(sessionToken, dsrSprocExecute.Headers[HttpConstants.HttpHeaders.SessionToken]);
        }
示例#3
0
        public void TestApplySessionForMasterOperation()
        {
            List <ResourceType> resourceTypes = new List <ResourceType>()
            {
                ResourceType.Database,
                ResourceType.Collection,
                ResourceType.User,
                ResourceType.Permission,
                ResourceType.StoredProcedure,
                ResourceType.Trigger,
                ResourceType.UserDefinedFunction,
                ResourceType.Offer,
                ResourceType.DatabaseAccount,
                ResourceType.PartitionKeyRange,
                ResourceType.UserDefinedType,
            };

            List <OperationType> operationTypes = new List <OperationType>()
            {
                OperationType.Create,
                OperationType.Delete,
                OperationType.Read,
                OperationType.Upsert,
                OperationType.Replace
            };

            foreach (ResourceType resourceType in resourceTypes)
            {
                foreach (OperationType operationType in operationTypes)
                {
                    Assert.IsTrue(GatewayStoreModel.IsMasterOperation(
                                      resourceType,
                                      operationType),
                                  $"{resourceType}, {operationType}");

                    DocumentServiceRequest dsr = DocumentServiceRequest.CreateFromName(
                        operationType,
                        "Test",
                        resourceType,
                        AuthorizationTokenType.PrimaryMasterKey);

                    dsr.Headers.Add(HttpConstants.HttpHeaders.SessionToken, Guid.NewGuid().ToString());

                    GatewayStoreModel.ApplySessionToken(
                        dsr,
                        ConsistencyLevel.Session,
                        new Mock <ISessionContainer>().Object);

                    Assert.IsNull(dsr.Headers[HttpConstants.HttpHeaders.SessionToken]);
                }
            }

            Assert.IsTrue(GatewayStoreModel.IsMasterOperation(
                              ResourceType.Document,
                              OperationType.QueryPlan));

            DocumentServiceRequest dsrQueryPlan = DocumentServiceRequest.CreateFromName(
                OperationType.QueryPlan,
                "Test",
                ResourceType.Document,
                AuthorizationTokenType.PrimaryMasterKey);

            dsrQueryPlan.Headers.Add(HttpConstants.HttpHeaders.SessionToken, Guid.NewGuid().ToString());

            GatewayStoreModel.ApplySessionToken(
                dsrQueryPlan,
                ConsistencyLevel.Session,
                new Mock <ISessionContainer>().Object);

            Assert.IsNull(dsrQueryPlan.Headers[HttpConstants.HttpHeaders.SessionToken]);
        }