Esempio n. 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);
        }
        public void VerifyDifferentUserAgentStrings()
        {
            Dictionary <string, string> spChars = new Dictionary <string, string>()
            {
                { "p1", @"Linux4.4.0-93-generic11614.04.1-UbuntuSMPMonAug1416:07:05UTC2017" },
                { "p2", @"Linux4.4.0-93-generic;11614" },
                { "p3", @"Linux4.4.0-93-generic=11614" },
                { "p4", @"©Linux4" },
                { "p5", @"Linux4    generic" },
                { "p6", @"Linux4 " +
                  "generic" },
                { "p7", @"Linux4\r\ngeneric" },
                { "p8", @"Linux4\rgeneric" },
                { "p9", @"Linux4\ngeneric" },
                { "p10", @"Linux4\generic" },
                { "p11", @"Darwin17.7.0DarwinKernelVersion17.7.0ThuJun21225314PDT2018rootxnu-4570.71.21/RELEASE_X86_64" }
            };

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            foreach (KeyValuePair <string, string> kv in spChars)
            {
                fakeClient.SetUserAgent(kv.Key, kv.Value);
            }

            // If we get an exception, meaning user agent string is not compatible.
        }
        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);
        }
Esempio n. 4
0
        public void ClientAddHandlerToPipelineAddsHandler()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(), new BadResponseDelegatingHandler());
            var result2    = fakeClient.DoStuffSync();

            Assert.Equal(HttpStatusCode.InternalServerError, result2.StatusCode);
        }
        public void AddingSpCharsInUserAgent()
        {
            string sampleProd    = "SampleProdName";
            string newSampleProd = "NewSampleProdName";
            string spChars       = "*()!@#$%^&";
            string sampleVersion = "1.*.0.*";

            string concatProdName  = string.Concat(sampleProd, spChars);
            string cleanedProdName = string.Concat(sampleProd, ".");

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.SetUserAgent(concatProdName);
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            Assert.Equal(5, userAgentValueCollection.Count);

            var retrievedProdInfo = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(cleanedProdName)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.Equal(retrievedProdInfo?.Product?.Name, cleanedProdName);

            fakeClient.SetUserAgent(newSampleProd, sampleVersion);
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentVersion = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;
            var retrievedVersion = userAgentVersion.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(newSampleProd)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.Equal(retrievedVersion?.Product?.Version, "1.0.");
        }
        public void ClientDefaultHeaderValuesTest()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(), new BadResponseDelegatingHandler());
            var arr        = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Where(pihv => String.IsNullOrWhiteSpace(pihv.Product.Version)).ToArray();

            Assert.Empty(arr);
        }
Esempio n. 7
0
        public void AddUserAgentInfoWithVersion()
        {
            string  defaultProductName = "FxVersion";
            string  testProductName = "TestProduct";
            string  testProductVersion = "1.0.0.0";
            Version defaultProductVer, testProductVer;

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.SetUserAgent(testProductName, testProductVersion);
            HttpResponseMessage response = fakeClient.DoStuffSync();
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var defaultProduct = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(defaultProductName)).FirstOrDefault <ProductInfoHeaderValue>();
            var testProduct    = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(testProductName)).FirstOrDefault <ProductInfoHeaderValue>();

            Version.TryParse(defaultProduct.Product.Version, out defaultProductVer);
            Version.TryParse(testProduct.Product.Version, out testProductVer);

            Assert.True(defaultProduct.Product.Name.Equals(defaultProductName));
            Assert.NotNull(defaultProductVer);

            Assert.True(testProduct.Product.Name.Equals(testProductName));
            Assert.True(testProduct.Product.Version.Equals(testProductVersion));
        }
Esempio n. 8
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);
        }
        public void ClientEmptyProductHeaderValuesTest()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(), new BadResponseDelegatingHandler());

            fakeClient.SetUserAgent("MySpecialHeader", string.Empty);
            var arr = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Where(pihv => (pihv.Product.Name == "MySpecialHeader")).ToArray();

            Assert.Empty(arr);
        }
Esempio n. 10
0
        public void ClientAddHandlersToPipelineAddSingleHandler()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                new BadResponseDelegatingHandler()
                );

            var result2 = fakeClient.DoStuffSync();
            Assert.Equal(HttpStatusCode.InternalServerError, result2.StatusCode);
        }
        public void FakeSvcClientWithHttpClient()
        {
            HttpClient          hc              = new HttpClient(new ContosoMessageHandler());
            var                 fakeClient      = new FakeServiceClient(hc);
            HttpResponseMessage response        = fakeClient.DoStuffSync();
            string              responseContent = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Contoso Rocks", responseContent);
        }
Esempio n. 12
0
        public void ClientWithoutHandlerWorks()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler(),
                                                   new MirrorDelegatingHandler());

            var response = fakeClient.DoStuff("Text").Result.Content.ReadAsStringAsync().Result;

            Assert.Equal("Text", response);
        }
Esempio n. 13
0
        public void ClientWithoutHandlerWorks()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                                                   new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Text", response);
        }
        private async Task <FakeServiceClient> CreateClient(HttpClient hc, string userAgentProdName, string userAgentProdVersion, ManualResetEvent resetEvent)
        {
//            resetEvent.WaitOne();
            return(await Task.Run <FakeServiceClient>(() =>
            {
                FakeServiceClient fakeClient = new FakeServiceClient(hc);
                fakeClient.SetUserAgent(userAgentProdName, userAgentProdVersion);
                return fakeClient;
            }));
        }
Esempio n. 15
0
        public void ClientAddHandlersToPipelineAddMultipleHandler()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                new AddHeaderResponseDelegatingHandler("foo", "bar"),
                new BadResponseDelegatingHandler()
                );

            var result2 = fakeClient.DoStuffSync();
            Assert.Equal(result2.Headers.GetValues("foo").FirstOrDefault(), "bar");
            Assert.Equal(HttpStatusCode.InternalServerError, result2.StatusCode);
        }
        public void ClientAddHandlerToPipelineAddsHandler()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler());
            var result1 = fakeClient.DoStuff();
            Assert.Equal(HttpStatusCode.OK, result1.Result.StatusCode);

            fakeClient.AddHandlerToPipeline(new BadResponseDelegatingHandler());

            var result2 = fakeClient.DoStuff();
            Assert.Equal(HttpStatusCode.InternalServerError, result2.Result.StatusCode);
        }
Esempio n. 17
0
        public void ClientAddHandlersToPipelineAddMultipleHandler()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                                                   new AddHeaderResponseDelegatingHandler("foo", "bar"),
                                                   new BadResponseDelegatingHandler()
                                                   );

            var result2 = fakeClient.DoStuffSync();

            Assert.Equal(result2.Headers.GetValues("foo").FirstOrDefault(), "bar");
            Assert.Equal(HttpStatusCode.InternalServerError, result2.StatusCode);
        }
        public void VerifyOsInfoInUserAgent()
        {
            string osInfoProductName = "OSName";

            FakeServiceClient   fakeClient = new FakeServiceClient(new FakeHttpHandler());
            HttpResponseMessage response   = fakeClient.DoStuffSync();
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var osProduct = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(osInfoProductName)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.NotEmpty(osProduct.Product.Name);
            Assert.NotEmpty(osProduct.Product.Version);
        }
Esempio n. 19
0
        public void ClientAddHandlersToPipelineChainsEmptyHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");

            var fakeClient = new FakeServiceClient(new WebRequestHandler(),
                handlerA, handlerB, handlerC,
                new MirrorDelegatingHandler());

            var response = fakeClient.DoStuff("Text").Result.Content.ReadAsStringAsync().Result;
            Assert.Equal("Text+A+B+C", response);
        }
        public void ClientAddHandlerToPipelineAddsHandler()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler());
            var result1    = fakeClient.DoStuff();

            Assert.Equal(HttpStatusCode.OK, result1.Result.StatusCode);

            fakeClient.AddHandlerToPipeline(new BadResponseDelegatingHandler());

            var result2 = fakeClient.DoStuff();

            Assert.Equal(HttpStatusCode.InternalServerError, result2.Result.StatusCode);
        }
        public void RetryHandlerRetriesWith500Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler());
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy<DefaultHttpErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.GetHttpPipeline().OfType<RetryHandler>().FirstOrDefault();
            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuff();
            Assert.Equal(HttpStatusCode.InternalServerError, result.Result.StatusCode);
            Assert.Equal(2, attemptsFailed);
        }
Esempio n. 22
0
        public void InstantiateRetryDelegatingHandler()
        {
            RetryDelegatingHandler retryHandler = new RetryDelegatingHandler();
            FakeServiceClient      fakeClient   = new FakeServiceClient(new FakeHttpHandler(), retryHandler);

            var handlers = fakeClient.HttpMessageHandlers;

            if (handlers.Any <HttpMessageHandler>())
            {
                List <HttpMessageHandler> handlerList = handlers.ToList <HttpMessageHandler>();
                Assert.Equal(4, handlerList.Count);
            }
        }
Esempio n. 23
0
        public void ClientAddHandlersToPipelineChainsEmptyHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");

            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                handlerA, handlerB, handlerC,
                new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
            Assert.Equal("Text+A+B+C", response);
        }
        public void RetryHandlerDoesntRetryFor400Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler() { StatusCodeToReturn = HttpStatusCode.Conflict });
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy<DefaultHttpErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.GetHttpPipeline().OfType<RetryHandler>().FirstOrDefault();
            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuff();
            Assert.Equal(HttpStatusCode.Conflict, result.Result.StatusCode);
            Assert.Equal(0, attemptsFailed);
        }
Esempio n. 25
0
        public void ClientAddHandlersToPipelineChainsEmptyHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");

            var fakeClient = new FakeServiceClient(new WebRequestHandler(),
                                                   handlerA, handlerB, handlerC,
                                                   new MirrorDelegatingHandler());

            var response = fakeClient.DoStuff("Text").Result.Content.ReadAsStringAsync().Result;

            Assert.Equal("Text+A+B+C", response);
        }
Esempio n. 26
0
        public void ClientAddHandlersToPipelineChainsEmptyHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");

            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                                                   handlerA, handlerB, handlerC,
                                                   new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Text+A+B+C", response);
        }
        public void ClientAddHandlersToPipelineAddMultipleHandler()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler());
            var result1 = fakeClient.DoStuff();
            Assert.Equal(HttpStatusCode.OK, result1.Result.StatusCode);

            fakeClient = fakeClient.WithHandlers(new DelegatingHandler[] {
                new BadResponseDelegatingHandler(),
                new AddHeaderResponseDelegatingHandler("foo", "bar"),
            });

            var result2 = fakeClient.DoStuff();
            Assert.Equal(result2.Result.Headers.GetValues("foo").FirstOrDefault(), "bar");
            Assert.Equal(HttpStatusCode.InternalServerError, result2.Result.StatusCode);
        }
        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);
        }
        public void AddDuplicateUserAgentInfo()
        {
            // FullNetFx -- Default (3) + 1 (TestClient) + 1 added below = 5
            string defaultProductName = "FxVersion";
            string testProductName    = "TestProduct";
            string testProductVersion = "1.0.0.0";

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.SetUserAgent(testProductName, testProductVersion);

            Assert.Equal(5, fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Count);
            fakeClient.SetUserAgent(testProductName, testProductVersion);
            Assert.Equal(5, fakeClient.HttpClient.DefaultRequestHeaders.UserAgent.Count);
        }
        public void RetryHandlerRetriesWith500Errors()
        {
            var fakeClient     = new FakeServiceClient(new FakeHttpHandler());
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy <DefaultHttpErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.GetHttpPipeline().OfType <RetryHandler>().FirstOrDefault();

            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuff();

            Assert.Equal(HttpStatusCode.InternalServerError, result.Result.StatusCode);
            Assert.Equal(2, attemptsFailed);
        }
        public void CheckIfHttpClientIsDisposed()
        {
            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.DoStuffSync();

            fakeClient.Dispose();
            try
            {
                fakeClient.DoStuffSync();
            }
            catch (NullReferenceException nEx)
            {
                Assert.True(true);
            }
        }
Esempio n. 32
0
        public void MeasureSvcClientMem()
        {
            AddHeaderResponseDelegatingHandler addHeader = new AddHeaderResponseDelegatingHandler("Hello", "World");
            var fakeClient = new FakeServiceClient(addHeader);

            RetryDelegatingHandler retryDelHandler = fakeClient.HttpMessageHandlers.OfType <RetryDelegatingHandler>().FirstOrDefault();

            Assert.Equal(0, retryDelHandler.EventCallbackCount);

            fakeClient.DoStuffSync();
            Assert.Equal(0, retryDelHandler.EventCallbackCount);
            Assert.Equal(0, retryDelHandler.RetryPolicy.EventCallbackCount);

            fakeClient.DoStuffSync();
            Assert.Equal(0, retryDelHandler.RetryPolicy.EventCallbackCount);
        }
Esempio n. 33
0
        public void ClientAddHandlersToPipelineAddMultipleHandler()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler());
            var result1    = fakeClient.DoStuff();

            Assert.Equal(HttpStatusCode.OK, result1.Result.StatusCode);

            fakeClient = fakeClient.WithHandlers(new DelegatingHandler[] {
                new BadResponseDelegatingHandler(),
                new AddHeaderResponseDelegatingHandler("foo", "bar"),
            });

            var result2 = fakeClient.DoStuff();

            Assert.Equal(result2.Result.Headers.GetValues("foo").FirstOrDefault(), "bar");
            Assert.Equal(HttpStatusCode.InternalServerError, result2.Result.StatusCode);
        }
Esempio n. 34
0
        public void RetryHandlerRetriesWith500Errors()
        {
            var fakeClient     = new FakeServiceClient(new FakeHttpHandler());
            int attemptsFailed = 0;

            var retryPolicy = new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(2);

            fakeClient.SetRetryPolicy(retryPolicy);
            var retryHandler = fakeClient.HttpMessageHandlers.OfType <RetryDelegatingHandler>().FirstOrDefault();

            retryPolicy.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();

            Assert.Equal(HttpStatusCode.InternalServerError, result.StatusCode);
            Assert.Equal(2, attemptsFailed);
        }
Esempio n. 35
0
        public void RetryHandlerRetriesWith500ErrorsAndSucceeds()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler()
            {
                NumberOfTimesToFail = 1
            });
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.HttpMessageHandlers.OfType <RetryDelegatingHandler>().FirstOrDefault();

            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();

            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            Assert.Equal(1, attemptsFailed);
        }
        public void RetryHandlerDoesntRetryFor400Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler()
            {
                StatusCodeToReturn = HttpStatusCode.Conflict
            });
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy <DefaultHttpErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.GetHttpPipeline().OfType <RetryHandler>().FirstOrDefault();

            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuff();

            Assert.Equal(HttpStatusCode.Conflict, result.Result.StatusCode);
            Assert.Equal(0, attemptsFailed);
        }
Esempio n. 37
0
        public void ClientAddHandlersToPipelineChainsNestedHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");
            handlerA.InnerHandler = handlerB;
            handlerB.InnerHandler = handlerC;
            var handlerD = new AppenderDelegatingHandler("D");
            var handlerE = new AppenderDelegatingHandler("E");
            handlerD.InnerHandler = handlerE;
            handlerE.InnerHandler = new MirrorMessageHandler("F");

            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                handlerA, handlerD,
                new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
            Assert.Equal("Text+A+B+C+D+E", response);
        }
Esempio n. 38
0
        public void RetryHandlerDoesntRetryFor400Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler()
            {
                StatusCodeToReturn = HttpStatusCode.Conflict
            });
            int attemptsFailed = 0;

            var retryPolicy = new RetryPolicy <HttpStatusCodeErrorDetectionStrategy>(2);

            fakeClient.SetRetryPolicy(retryPolicy);
            var retryHandler = fakeClient.HttpMessageHandlers.OfType <RetryDelegatingHandler>().FirstOrDefault();

            retryPolicy.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();

            Assert.Equal(HttpStatusCode.Conflict, result.StatusCode);
            Assert.Equal(0, attemptsFailed);
        }
Esempio n. 39
0
        public void ClientAddHandlersToPipelineChainsNestedHandler()
        {
            var handlerA = new AppenderDelegatingHandler("A");
            var handlerB = new AppenderDelegatingHandler("B");
            var handlerC = new AppenderDelegatingHandler("C");

            handlerA.InnerHandler = handlerB;
            handlerB.InnerHandler = handlerC;
            var handlerD = new AppenderDelegatingHandler("D");
            var handlerE = new AppenderDelegatingHandler("E");

            handlerD.InnerHandler = handlerE;
            handlerE.InnerHandler = new MirrorMessageHandler("F");

            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                                                   handlerA, handlerD,
                                                   new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Text+A+B+C+D+E", response);
        }
Esempio n. 40
0
        public void ClientWithoutHandlerWorks()
        {
            var fakeClient = new FakeServiceClient(new WebRequestHandler(),
                new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().Result;
            Assert.Equal("Text", response);
        }
        public void RetryHandlerRetriesWith500Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler());
            int attemptsFailed = 0;

            var retryPolicy = new RetryPolicy<HttpStatusCodeErrorDetectionStrategy>(2);
            fakeClient.SetRetryPolicy(retryPolicy);
            var retryHandler = fakeClient.HttpMessageHandlers.OfType<RetryDelegatingHandler>().FirstOrDefault();
            retryPolicy.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();
            Assert.Equal(HttpStatusCode.InternalServerError, result.StatusCode);
            Assert.Equal(2, attemptsFailed);
        }
Esempio n. 42
0
        public void RetryHandlerRetriesWith500ErrorsAndSucceeds()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler() {NumberOfTimesToFail = 1});
            int attemptsFailed = 0;

            fakeClient.SetRetryPolicy(new RetryPolicy<HttpStatusCodeErrorDetectionStrategy>(2));
            var retryHandler = fakeClient.HttpMessageHandlers.OfType<RetryDelegatingHandler>().FirstOrDefault();
            retryHandler.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            Assert.Equal(1, attemptsFailed);
        }
 public void HeadersAndPayloadAreNotDisposed()
 {
     FakeServiceClient fakeClient = null;
     try
     {
         fakeClient = new FakeServiceClient(new HttpClientHandler(),
              new MirrorDelegatingHandler());
         var response = fakeClient.DoStuffAndThrowSync("Text");
         Assert.True(false);
     }
     catch (HttpOperationException ex)
     {
         fakeClient.Dispose();
         fakeClient = null;
         GC.Collect();
         Assert.NotNull(ex.Request);
         Assert.NotNull(ex.Response);
         Assert.Equal("2013-11-01", ex.Request.Headers["x-ms-version"].First());
         Assert.Equal("Text", ex.Response.Content);
     }
 }
Esempio n. 44
0
        public void AddUserAgentInfoWithVersion()
        {
            string defaultProductName = "FxVersion";
            string testProductName = "TestProduct";
            string testProductVersion = "1.0.0.0";
            Version defaultProductVer, testProductVer;

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());
            fakeClient.SetUserAgent(testProductName, testProductVersion);
            HttpResponseMessage response = fakeClient.DoStuffSync();
            HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var defaultProduct = userAgentValueCollection.Where<ProductInfoHeaderValue>((p) => p.Product.Name.Equals(defaultProductName)).FirstOrDefault<ProductInfoHeaderValue>();
            var testProduct = userAgentValueCollection.Where<ProductInfoHeaderValue>((p) => p.Product.Name.Equals(testProductName)).FirstOrDefault<ProductInfoHeaderValue>();

            Version.TryParse(defaultProduct.Product.Version, out defaultProductVer);
            Version.TryParse(testProduct.Product.Version, out testProductVer);

            Assert.True(defaultProduct.Product.Name.Equals(defaultProductName));
            Assert.NotNull(defaultProductVer);

            Assert.True(testProduct.Product.Name.Equals(testProductName));
            Assert.True(testProduct.Product.Version.Equals(testProductVersion));
        }
Esempio n. 45
0
        public void VerifyOsInfoInUserAgent()
        {

            string osInfoProductName = "OSName";

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());
            HttpResponseMessage response = fakeClient.DoStuffSync();
            HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var osProduct = userAgentValueCollection.Where<ProductInfoHeaderValue>((p) => p.Product.Name.Equals(osInfoProductName)).FirstOrDefault<ProductInfoHeaderValue>();

            Assert.NotEmpty(osProduct.Product.Name);
            Assert.NotEmpty(osProduct.Product.Version);
        }
Esempio n. 46
0
        public void ClientWithoutHandlerWorks()
        {
            var fakeClient = new FakeServiceClient(new HttpClientHandler(),
                new MirrorDelegatingHandler());

            var response = fakeClient.DoStuffSync("Text").Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
            Assert.Equal("Text", response);
        }
        public void RetryHandlerDoesntRetryFor400Errors()
        {
            var fakeClient = new FakeServiceClient(new FakeHttpHandler() {StatusCodeToReturn = HttpStatusCode.Conflict});
            int attemptsFailed = 0;

            var retryPolicy = new RetryPolicy<HttpStatusCodeErrorDetectionStrategy>(2);
            fakeClient.SetRetryPolicy(retryPolicy);
            var retryHandler = fakeClient.HttpMessageHandlers.OfType<RetryDelegatingHandler>().FirstOrDefault();
            retryPolicy.Retrying += (sender, args) => { attemptsFailed++; };

            var result = fakeClient.DoStuffSync();
            Assert.Equal(HttpStatusCode.Conflict, result.StatusCode);
            Assert.Equal(0, attemptsFailed);
        }