// Verify that for 429 exceptions, session token is not updated public async Task GatewayStoreModel_Exception_NotUpdateSessionTokenOnKnownExceptions() { INameValueCollection headers = new StoreRequestNameValueCollection(); headers.Set(HttpConstants.HttpHeaders.SessionToken, "0:1#100#1=20#2=5#3=30"); headers.Set(WFConstants.BackendHeaders.LocalLSN, "10"); await this.GatewayStoreModel_Exception_NotUpdateSessionTokenOnKnownException(new RequestRateTooLargeException("429", headers, new Uri("http://one.com"))); }
private async Task GatewayStoreModel_Exception_UpdateSessionTokenOnKnownException(Exception ex) { const string originalSessionToken = "0:1#100#1=20#2=5#3=30"; const string updatedSessionToken = "0:1#100#1=20#2=5#3=31"; Func <HttpRequestMessage, Task <HttpResponseMessage> > sendFunc = request => { throw ex; }; Mock <IDocumentClientInternal> mockDocumentClient = new Mock <IDocumentClientInternal>(); mockDocumentClient.Setup(client => client.ServiceEndpoint).Returns(new Uri("https://foo")); using GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, new ConnectionPolicy()); SessionContainer sessionContainer = new SessionContainer(string.Empty); DocumentClientEventSource eventSource = DocumentClientEventSource.Instance; HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc); using GatewayStoreModel storeModel = new GatewayStoreModel( endpointManager, sessionContainer, ConsistencyLevel.Eventual, eventSource, null, MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler))); INameValueCollection headers = new StoreRequestNameValueCollection(); headers.Set(HttpConstants.HttpHeaders.ConsistencyLevel, ConsistencyLevel.Session.ToString()); headers.Set(HttpConstants.HttpHeaders.SessionToken, originalSessionToken); headers.Set(WFConstants.BackendHeaders.PartitionKeyRangeId, "0"); using (new ActivityScope(Guid.NewGuid())) { using (DocumentServiceRequest request = DocumentServiceRequest.Create( OperationType.Read, ResourceType.Document, "dbs/OVJwAA==/colls/OVJwAOcMtA0=/docs/OVJwAOcMtA0BAAAAAAAAAA==/", AuthorizationTokenType.PrimaryMasterKey, headers)) { request.UseStatusCodeFor429 = true; request.UseStatusCodeForFailures = true; try { DocumentServiceResponse response = await storeModel.ProcessMessageAsync(request); Assert.Fail("Should had thrown exception"); } catch (Exception) { // Expecting exception } Assert.AreEqual(updatedSessionToken, sessionContainer.GetSessionToken("dbs/OVJwAA==/colls/OVJwAOcMtA0=")); } } }
// Verify that for known exceptions, session token is updated public async Task GatewayStoreModel_Exception_UpdateSessionTokenOnKnownException() { INameValueCollection headers = new StoreRequestNameValueCollection(); headers.Set(HttpConstants.HttpHeaders.SessionToken, "0:1#100#1=20#2=5#3=31"); headers.Set(WFConstants.BackendHeaders.LocalLSN, "10"); await this.GatewayStoreModel_Exception_UpdateSessionTokenOnKnownException(new ConflictException("test", headers, new Uri("http://one.com"))); await this.GatewayStoreModel_Exception_UpdateSessionTokenOnKnownException(new NotFoundException("test", headers, new Uri("http://one.com"))); await this.GatewayStoreModel_Exception_UpdateSessionTokenOnKnownException(new PreconditionFailedException("test", headers, new Uri("http://one.com"))); }
private async Task <AccountProperties> GetDatabaseAccountAsync(Uri serviceEndpoint) { INameValueCollection headers = new StoreRequestNameValueCollection(); await this.cosmosAuthorization.AddAuthorizationHeaderAsync( headersCollection : headers, serviceEndpoint, HttpConstants.HttpMethods.Get, AuthorizationTokenType.PrimaryMasterKey); using (HttpResponseMessage responseMessage = await this.httpClient.GetAsync( uri: serviceEndpoint, additionalHeaders: headers, resourceType: ResourceType.DatabaseAccount, diagnosticsContext: null, cancellationToken: default))
private async Task <AccountProperties> GetDatabaseAccountAsync(Uri serviceEndpoint) { INameValueCollection headers = new StoreRequestNameValueCollection(); await this.cosmosAuthorization.AddAuthorizationHeaderAsync( headersCollection : headers, serviceEndpoint, HttpConstants.HttpMethods.Get, AuthorizationTokenType.PrimaryMasterKey); using (HttpResponseMessage responseMessage = await this.httpClient.GetAsync( uri: serviceEndpoint, additionalHeaders: headers, resourceType: ResourceType.DatabaseAccount, timeoutPolicy: HttpTimeoutPolicyControlPlaneRead.Instance, trace: NoOpTrace.Singleton, cancellationToken: default))
public override INameValueCollection Clone() { Lazy <Dictionary <string, string> > cloneNotCommonHeaders = new Lazy <Dictionary <string, string> >(() => new Dictionary <string, string>(StoreRequestNameValueCollection.DefaultStringComparer)); if (this.lazyNotCommonHeaders.IsValueCreated) { foreach (KeyValuePair <string, string> notCommonHeader in this.lazyNotCommonHeaders.Value) { cloneNotCommonHeaders.Value[notCommonHeader.Key] = notCommonHeader.Value; } } StoreRequestNameValueCollection cloneHeaders = new StoreRequestNameValueCollection(cloneNotCommonHeaders) { Authorization = this.Authorization, ClientRetryAttemptCount = this.ClientRetryAttemptCount, CollectionRid = this.CollectionRid, ConsistencyLevel = this.ConsistencyLevel, Continuation = this.Continuation, EffectivePartitionKey = this.EffectivePartitionKey, ExcludeSystemProperties = this.ExcludeSystemProperties, HttpDate = this.HttpDate, IsBatchAtomic = this.IsBatchAtomic, IsBatchOrdered = this.IsBatchOrdered, IsUpsert = this.IsUpsert, PartitionKey = this.PartitionKey, Prefer = this.Prefer, RemainingTimeInMsOnClientRequest = this.RemainingTimeInMsOnClientRequest, ResourceTokenExpiry = this.ResourceTokenExpiry, ResourceTypes = this.ResourceTypes, SessionToken = this.SessionToken, ShouldBatchContinueOnError = this.ShouldBatchContinueOnError, TargetGlobalCommittedLsn = this.TargetGlobalCommittedLsn, TargetLsn = this.TargetLsn, TimeToLiveInSeconds = this.TimeToLiveInSeconds, TransactionCommit = this.TransactionCommit, TransactionId = this.TransactionId, TransportRequestID = this.TransportRequestID, Version = this.Version, XDate = this.XDate, }; return(cloneHeaders); }