public async Task GetResponseAsync_WithQueryParameterAndDefaultParameters_ShouldAppendDefaultParameters() { // Arrange var httpClient = A.Fake <IHttpClient>(); var url = "test/itemName/123?parameter=value"; var defaultParams = new NameValueCollection { { "APIKey", "keyvalue" } }; var expectedUrl = "/test/itemName/123?parameter=value&APIKey=keyvalue"; A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .Ignored)) .Returns(_basicResponse); A.CallTo( () => httpClient.Client) .Returns(_basicClient); var requester = new WebRequester(httpClient, TimeSpan.FromMilliseconds(1), defaultParams); // Act await requester.GetResponseAsync(url); // Assert A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .That.Matches(req => req.RequestUri.ToString().Equals(expectedUrl))) ).MustHaveHappened(); }
public async Task GetResponseAsync_WithTemplateAndDefaultParameters_ShouldBuildCorrectUrl() { // Arrange var httpClient = A.Fake <IHttpClient>(); var template = "test/{item}/{id}"; var defaultParams = new NameValueCollection { { "APIKey", "keyvalue" } }; var parameters = new NameValueCollection { { "item", "itemName" }, { "id", "123" }, { "isEnabled", "true" } }; var expectedUrl = "/test/itemName/123?isEnabled=true&APIKey=keyvalue"; A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .Ignored)) .Returns(_basicResponse); A.CallTo( () => httpClient.Client) .Returns(_basicClient); var requester = new WebRequester(httpClient, TimeSpan.FromMilliseconds(1), defaultParams); // Act await requester.GetResponseAsync(template, parameters); // Assert A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .That.Matches(req => req.RequestUri.ToString().Equals(expectedUrl))) ).MustHaveHappened(); }
public async Task GetResponseAsync_WithTemplate_ShouldBuildCorrectUrl() { // Arrange var httpClient = A.Fake <IHttpClient>(); var template = "test/{item}/{id}"; var parameters = new NameValueCollection { { "item", "itemName" }, { "id", "123" } }; var expectedUrl = "/test/itemName/123"; A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .Ignored)) .Returns(_basicResponse); A.CallTo( () => httpClient.Client) .Returns(_basicClient); var requester = new WebRequester(httpClient); // Act await requester.GetResponseAsync(template, parameters); // Assert A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .That.Matches(req => req.RequestUri.ToString().Equals(expectedUrl))) ).MustHaveHappened(); }
public async Task GetResponseAsync_NoParameters_ShouldThrowArgumentNullException() { // Arrange var httpClient = A.Fake <IHttpClient>(); var requester = new WebRequester(httpClient); // Act var ex = await Record.ExceptionAsync(async() => { await requester.GetResponseAsync("/test", null, 0); }); // Assert ex.ShouldBeOfType <ArgumentNullException>(); }
public async Task GetResponseAsync_ShouldTimeoutOnSlowResponse() { // Arrange var httpClient = WebRequestHttpClientFactory.Configure(new Uri(Url)).WithTimeout(TimeSpan.FromMilliseconds(100)).Create(); var webRequester = new WebRequester(httpClient); // Act var exception = await Record.ExceptionAsync(async() => await webRequester.GetResponseAsync("/delay/250")); // Assert exception.ShouldBeOfType(typeof(TaskCanceledException)); }
public async Task GetResponseAsync_WithRetries_ShouldOnlyRetryGivenAmountOfTimes() { // Arrange var httpClient = A.Fake <IHttpClient>(); A.CallTo(() => httpClient.SendAsync(A <HttpRequestMessage> .Ignored)).Throws <TaskCanceledException>(); var requester = new WebRequester(httpClient, TimeSpan.FromMilliseconds(1)); // Act var ex = await Record.ExceptionAsync(async() => { await requester.GetResponseAsync("/test", retries: 3); }); // Assert ex.ShouldBeOfType <TaskCanceledException>(); }
public async Task GetResponseAsync_WithRetries_ShouldRetrySendAsync() { // Arrange var httpClient = A.Fake <IHttpClient>(); A.CallTo(() => httpClient.SendAsync(A <HttpRequestMessage> .Ignored)).Throws <TaskCanceledException>().NumberOfTimes(1); var requester = new WebRequester(httpClient, TimeSpan.FromMilliseconds(1)); // Act await requester.GetResponseAsync("/test", retries : 3); // Assert A.CallTo(() => httpClient.SendAsync(A <HttpRequestMessage> .Ignored)).MustHaveHappened(Repeated.Exactly.Times(2)); }