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)); }
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_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 Retries_WithCustomResilencePolicy_PolicyUsed() { int retryAttempts = 1; int expectedAttepts = retryAttempts + 1; int actualHttpRequestCount = 0; _mockHttp .When($"{_baseUrl}/items") .Respond((request) => GetResponseAndLogRequest(HttpStatusCode.NotImplemented, 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()); A.CallTo(() => client.ResiliencePolicyProvider.Policy).MustHaveHappened(); Assert.Equal(expectedAttepts, 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); }