public async void Retries_WithCustomResilencePolicyWithMaxRetrySet_PolicyUsedMaxRetryIgnored() { int policyRetryAttempts = 1; int expectedAttepts = policyRetryAttempts + 1; int ignoredRetryAttempt = 3; int actualHttpRequestCount = 0; _mockHttp .When($"{_baseUrl}/items") .Respond((request) => GetResponseAndLogRequest(HttpStatusCode.NotImplemented, ref actualHttpRequestCount)); var options = new DeliveryOptions { ProjectId = _guid.ToString(), MaxRetryAttempts = ignoredRetryAttempt }; var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(options, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true).RetryAsync(policyRetryAttempts)); await Assert.ThrowsAsync <DeliveryException>(async() => await client.GetItemsAsync()); A.CallTo(() => client.ResiliencePolicyProvider.Policy).MustHaveHappened(); Assert.Equal(expectedAttepts, actualHttpRequestCount); }
public async void Retries_WithMaxRetrySet_SettingReflected() { int retryAttempts = 3; int expectedAttempts = retryAttempts + 1; int actualHttpRequestCount = 0; _mockHttp .When($"{_baseUrl}/items") .Respond((request) => GetResponseAndLogRequest(HttpStatusCode.RequestTimeout, ref actualHttpRequestCount)); var options = new DeliveryOptions { ProjectId = _guid.ToString(), MaxRetryAttempts = retryAttempts }; var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(options, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true).RetryAsync(retryAttempts)); await Assert.ThrowsAsync <DeliveryException>(async() => await client.GetItemsAsync()); Assert.Equal(expectedAttempts, actualHttpRequestCount); }
public async void SecuredProductionAddCorrectHeader() { var securityKey = "someKey"; var options = new DeliveryOptions { ProjectId = _guid.ToString(), SecuredProductionApiKey = securityKey, UseSecuredProductionApi = true }; _mockHttp .Expect($"{_baseUrl}/items") .WithHeaders("Authorization", $"Bearer {securityKey}") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(options, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => false) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); await client.GetItemsAsync(); _mockHttp.VerifyNoOutstandingExpectation(); }
public async void GetItems_DeliveryOptionsWithIncludeTotalCount_IncludeTotalCountParameterAdded() { var responseJson = JsonConvert.SerializeObject(CreateItemsResponse()); MockHttp .Expect($"{BaseUrl}/items") .WithExactQueryString("includeTotalCount") .Respond("application/json", responseJson); var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(Options, MockHttp); await client.GetItemsAsync(); MockHttp.VerifyNoOutstandingExpectation(); }
public async void GetItemsTyped_DeliveryOptionsWithIncludeTotalCount_IncludeTotalCountParameterAdded() { var responseJson = JsonConvert.SerializeObject(CreateItemsResponse()); MockHttp .Expect($"{BaseUrl}/items") .WithExactQueryString("system.type=cafe&includeTotalCount") .Respond("application/json", responseJson); var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(Options, MockHttp); A.CallTo(() => client.TypeProvider.GetCodename(typeof(Cafe))).Returns("cafe"); await client.GetItemsAsync <Cafe>(); MockHttp.VerifyNoOutstandingExpectation(); }
public async void PreviewAndSecuredProductionThrowsWhenBothEnabled(bool usePreviewApi, bool useSecuredProduction) { if (usePreviewApi) { _mockHttp .When($@"https://preview-deliver.kenticocloud.com/{_guid}/items") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); } else { _mockHttp .When($"{_baseUrl}/items") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); } var options = new DeliveryOptions { ProjectId = _guid.ToString(), UsePreviewApi = usePreviewApi, UseSecuredProductionApi = useSecuredProduction, PreviewApiKey = "someKey", SecuredProductionApiKey = "someKey" }; var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(options, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); if (usePreviewApi && useSecuredProduction) { // Assert await Assert.ThrowsAsync <InvalidOperationException>(async() => await client.GetItemsAsync()); } else { var response = await client.GetItemsAsync(); // Assert Assert.NotNull(response); } }
public async void Retries_EnableResilienceLogicDisabled_DoesNotRetry() { var actualHttpRequestCount = 0; _mockHttp .When($"{_baseUrl}/items") .Respond((request) => GetResponseAndLogRequest(HttpStatusCode.RequestTimeout, ref actualHttpRequestCount)); var options = new DeliveryOptions { ProjectId = _guid.ToString(), EnableResilienceLogic = false }; var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(options, _mockHttp); await Assert.ThrowsAsync <DeliveryException>(async() => await client.GetItemsAsync()); Assert.Equal(1, actualHttpRequestCount); }