예제 #1
0
        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();
        }
예제 #2
0
        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();
        }
예제 #3
0
        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();
        }
예제 #4
0
        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));
        }
예제 #6
0
        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>();
        }
예제 #7
0
        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));
        }