예제 #1
0
        public void LimitsRetryCount()
        {
            // Setup to always return 429.
            var fakeHttpHandler = new FakeHttpHandler
            {
                StatusCodeToReturn = (HttpStatusCode)429,
                TweakResponse      = (response) =>
                {
                    response.Headers.Add("Retry-After", "1");
                }
            };

            var retryHandler = new RetryAfterDelegatingHandler(fakeHttpHandler)
            {
                MaxRetries = 3
            };

            var httpClient = new HttpClient(retryHandler, false);

            // Make a request using the HttpClient.
            var fakeClient      = new FakeServiceClient(httpClient);
            var responseMessage = fakeClient.DoStuffSync();

            Assert.NotNull(responseMessage);
            Assert.Equal(3, fakeHttpHandler.NumberOfTimesFailedSoFar);
            Assert.Equal((HttpStatusCode)429, responseMessage.StatusCode);
        }
예제 #2
0
        public void HttpClientTimeoutReturnsErrorResponse(string content)
        {
            // Setup to always return 429.
            var fakeHttpHandler = new FakeHttpHandler
            {
                StatusCodeToReturn = (HttpStatusCode)429,
                TweakResponse      = (response) =>
                {
                    response.Content = content == null ? null : new StringContent(String.Empty);
                    response.Headers.Add("Retry-After", "1");
                }
            };

            var retryHandler = new RetryAfterDelegatingHandler(fakeHttpHandler);

            // Setup HttpClient to timeout after 5 seconds.
            var httpClient = new HttpClient(retryHandler, false)
            {
                Timeout = TimeSpan.FromSeconds(5)
            };

            // Make a request using the HttpClient.
            var fakeClient      = new FakeServiceClient(httpClient);
            var responseMessage = fakeClient.DoStuffSync();

            Assert.NotNull(responseMessage);
            Assert.True(fakeHttpHandler.NumberOfTimesFailedSoFar > 1);
            Assert.Equal((HttpStatusCode)429, responseMessage.StatusCode);
        }
예제 #3
0
        public void Initialize()
        {
            HttpMessageHandler myHttpMessageHandler = new FakeHttpHandler();

            httpClient = new HttpClient(myHttpMessageHandler);
            htmlReader = new HtmlReader(httpClient);
        }
예제 #4
0
        // Creates an httpClient that will respond with a space (this.Space or passed in space)
        public static (HttpClient, FakeHttpHandler) CreateWithSpace(
            IEnumerable <Guid> postResponseGuids,
            IEnumerable <HttpResponseMessage> getResponses   = null,
            IEnumerable <HttpResponseMessage> patchResponses = null,
            Models.Space space = null)
        {
            postResponseGuids = postResponseGuids ?? Array.Empty <Guid>();
            getResponses      = getResponses ?? Array.Empty <HttpResponseMessage>();
            patchResponses    = patchResponses ?? Array.Empty <HttpResponseMessage>();
            space             = space ?? Space;

            var getRootSpaceResponse = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new [] { space })),
            };

            var getSensorsForResultsResponse = new [] { Responses.NotFound };

            return(FakeHttpHandler.CreateHttpClient(
                       postResponses: CreateGuidResponses(postResponseGuids),
                       getResponses: new [] { getRootSpaceResponse }
                       .Concat(getResponses)
                       .Concat(getSensorsForResultsResponse),
                       patchResponses: patchResponses));
        }
        public async Task HttpClientTimeoutReturnsErrorResponse(string content)
        {
            // Setup the RetryDelegatingHandler to retry very quickly.
            var retryStrategy = new FixedIntervalRetryStrategy(Int32.MaxValue, TimeSpan.FromTicks(1));
            var retryPolicy   = new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), retryStrategy);

            var fakeHttpHandler = new FakeHttpHandler
            {
                TweakResponse = (response) =>
                {
                    response.Content = content == null ? null : new StringContent(String.Empty);
                }
            };

            var retryHandler = new RetryDelegatingHandler(retryPolicy, fakeHttpHandler);

            // Setup HttpClient to timeout after 500 milliseconds.
            var httpClient = new HttpClient(retryHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(500)
            };

            // Make a request using the HttpClient.
            var fakeClient      = new FakeServiceClient(httpClient);
            var responseMessage = fakeClient.DoStuffSync();

            Assert.NotNull(responseMessage);
            Assert.True(fakeHttpHandler.NumberOfTimesFailedSoFar > 1);
            Assert.Equal(HttpStatusCode.InternalServerError, responseMessage.StatusCode);
        }
예제 #6
0
 public static (HttpClient, FakeHttpHandler) Create(
     IEnumerable <Guid> postResponseGuids           = null,
     IEnumerable <HttpResponseMessage> getResponses = null)
 {
     return(FakeHttpHandler.CreateHttpClient(
                postResponses: postResponseGuids != null ? CreateGuidResponses(postResponseGuids) : null,
                getResponses: getResponses));
 }
예제 #7
0
        public static (FakeHttpHandler handler, S3Client client) CreateFakeClient()
        {
            S3Config config = new S3Config(new StringAccessKey("ExampleKeyId00000000", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"), AwsRegion.USEast1);

            FakeHttpHandler fakeHandler = new FakeHttpHandler();

            S3Client client = new S3Client(config, fakeHandler);

            return(fakeHandler, client);
        }
        private async Task GetApiInfoInvalidResponse(Func <HttpRequestMessage, HttpResponseMessage> send, string code, string?message)
        {
            var handler    = new FakeHttpHandler(send);
            var httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://example.com/")
            };
            var clientApi = new HttpClientConformanceApi(new HttpClientServiceSettings {
                HttpClient = httpClient
            });
            var result = await clientApi.GetApiInfoAsync(new GetApiInfoRequestDto());

            result.Should().BeFailure(code);
            result.Error !.Message.Should().StartWith(message);
        }
        public void RetryAfterHandleTest()
        {
            var http = new FakeHttpHandler();

            http.NumberOfTimesToFail = 2;
            http.StatusCodeToReturn  = (HttpStatusCode)429;
            http.TweakResponse       = (response) => { response.Headers.Add("Retry-After", "10"); };

            var fakeClient = new FakeServiceClient(http, new RetryAfterDelegatingHandler());

            var result = fakeClient.DoStuffSync();

            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            Assert.Equal(2, http.NumberOfTimesFailedSoFar);
        }
예제 #10
0
        public void TaskCanceledCallingBaseSendAsyncCausesErrorResponse()
        {
            // Setup to always return 429.
            var fakeHttpHandler = new FakeHttpHandler();

            fakeHttpHandler.StatusCodeToReturn = (HttpStatusCode)429;

            int callCount = 0;

            fakeHttpHandler.TweakResponse = (response) =>
            {
                // After 3 calls returning 429, cause TaskCanceledException to be thrown.
                if (callCount++ > 3)
                {
                    throw new TaskCanceledException();
                }

                response.Content = new StringContent(String.Empty);
                response.Headers.Add("Retry-After", "1");
            };

            var retryHandler = new RetryAfterDelegatingHandler(fakeHttpHandler);

            // Setup HttpClient to timeout after 5 seconds.
            var httpClient = new HttpClient(retryHandler, false)
            {
                Timeout = TimeSpan.FromSeconds(5)
            };

            // Make a request using the HttpClient.
            var fakeClient      = new FakeServiceClient(httpClient);
            var responseMessage = fakeClient.DoStuffSync();

            Assert.NotNull(responseMessage);
            Assert.True(fakeHttpHandler.NumberOfTimesFailedSoFar > 1);
            Assert.Equal((HttpStatusCode)429, responseMessage.StatusCode);
        }