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 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 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); }
private IDeliveryClient Build(DeliveryOptions options, string name) { return(DeliveryClientFactory.Create( new DeliveryOptionsMonitor(options, name), GetNamedServiceOrDefault <IModelProvider>(name), GetNamedServiceOrDefault <IRetryPolicyProvider>(name), GetNamedServiceOrDefault <ITypeProvider>(name), GetNamedServiceOrDefault <IDeliveryHttpClient>(name), GetNamedServiceOrDefault <JsonSerializer>(name))); }
private DeliveryClient InitializeDeliveryClientWithCustomModelProvider(MockHttpMessageHandler handler, IPropertyMapper propertyMapper = null) { var mapper = propertyMapper ?? A.Fake <IPropertyMapper>(); var modelProvider = new ModelProvider(null, null, _mockTypeProvider, mapper); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId(_guid, handler, modelProvider); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); return(client); }
public async void TooLongUrlThrows() { _mockHttp .When($"{_baseUrl}/items") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId(_guid, _mockHttp); var elements = new ElementsParameter(Enumerable.Range(0, 1000000).Select(i => "test").ToArray()); // Act / Assert await Assert.ThrowsAsync <UriFormatException>(async() => await client.GetItemsAsync(elements)); }
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); } }
private DeliveryClient InitializeDeliveryClientWithACustomTypeProvider(MockHttpMessageHandler handler) { var customTypeProvider = new CustomTypeProvider(); var modelProvider = new ModelProvider( _mockContentLinkUrlResolver, null, customTypeProvider, new PropertyMapper()); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId( _guid, handler, modelProvider, typeProvider: customTypeProvider); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); return(client); }
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); }
public async void Retries_WithDefaultSettings_Retries() { var actualHttpRequestCount = 0; var retryAttempts = 4; var expectedRetryAttempts = retryAttempts + 1; _mockHttp .When($"{_baseUrl}/items") .Respond((request) => GetResponseAndLogRequest(HttpStatusCode.RequestTimeout, ref actualHttpRequestCount)); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId(_guid, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true).RetryAsync(retryAttempts)); await Assert.ThrowsAsync <DeliveryException>(async() => await client.GetItemsAsync()); Assert.Equal(expectedRetryAttempts, actualHttpRequestCount); }
public async void CorrectSdkVersionHeaderAdded() { var assembly = typeof(DeliveryClient).Assembly; var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); var sdkVersion = fileVersionInfo.ProductVersion; var sdkPackageId = assembly.GetName().Name; _mockHttp .Expect($"{_baseUrl}/items") .WithHeaders("X-KC-SDKID", $"nuget.org;{sdkPackageId};{sdkVersion}") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId(_guid, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => false) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); await client.GetItemsAsync(); _mockHttp.VerifyNoOutstandingExpectation(); }
public void LongUrl() { _mockHttp .When($"{_baseUrl}/items") .Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}items.json"))); var client = DeliveryClientFactory.GetMockedDeliveryClientWithProjectId(_guid, _mockHttp); A.CallTo(() => client.ResiliencePolicyProvider.Policy) .Returns(Policy.HandleResult <HttpResponseMessage>(result => true) .RetryAsync(client.DeliveryOptions.MaxRetryAttempts)); var elements = new ElementsParameter(Enumerable.Range(0, 1000).Select(i => "test").ToArray()); var inFilter = new InFilter("test", Enumerable.Range(0, 1000).Select(i => "test").ToArray()); var allFilter = new AllFilter("test", Enumerable.Range(0, 1000).Select(i => "test").ToArray()); var anyFilter = new AnyFilter("test", Enumerable.Range(0, 1000).Select(i => "test").ToArray()); // Act var response = client.GetItemsAsync(elements, inFilter, allFilter, anyFilter).Result; // Assert Assert.NotNull(response); }