public void TestRetry()
        {
            var mockHttp = new MockHttpMessageHandler();

            var counter = 0;

            mockHttp.When(HttpMethod.Post, "http://test/*")
            .Respond(msg =>
            {
                counter++;
                Assert.LessOrEqual(counter, 3);
                return(new HttpResponseMessage(HttpStatusCode.RequestTimeout));
            });
            var httpclient = mockHttp.ToHttpClient();

            var client = new RandomUrlHttpClient(
                httpclient,
                new[] { "http://test/1", "http://test/2" },
                null,
                3,
                null);

            Assert.ThrowsAsync <TransientHttpRequestException>(
                () => client.PostAsync("api/55", new StringContent("55")));
            Assert.AreEqual(3, counter);
        }
예제 #2
0
 public StackExchangeClient(HttpClient httpClient)
 {
     HttpClient = new RandomUrlHttpClient(httpClient, new[]
     {
         "https://api.stackexchange.com/2.2"
     });
 }
        public async Task TestPostAsync()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "http://test/*")
            .Respond(async(msg) =>
            {
                Assert.AreEqual("55", await msg.Content.ReadAsStringAsync());
                StringAssert.IsMatch("http://test/1|2/api/55", msg.RequestUri.AbsoluteUri);
                var resmsg = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("ok")
                };
                return(resmsg);
            });
            var httpclient = mockHttp.ToHttpClient();

            var client = new RandomUrlHttpClient(
                httpclient,
                new[] { "http://test/1", "http://test/2" },
                null,
                3,
                null);
            var res = await client.PostAsync("api/55", new StringContent("55"));

            var content = await res.Content.ReadAsStringAsync();

            Assert.AreEqual("ok", content);
        }
예제 #4
0
        public async Task TestPostJsonAsync()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "http://test/*")
            .With(arg =>
            {
                // Must provide content-type
                return(arg.Content.Headers.ContentType.MediaType == "application/json");
            })
            .Respond(async(msg) =>
            {
                Assert.AreEqual("{\"foo\":\"bar\"}", await msg.Content.ReadAsStringAsync());
                StringAssert.IsMatch("http://test/1|2/api/55", msg.RequestUri.AbsoluteUri);
                var resmsg = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("ok")
                };
                return(resmsg);
            });
            var httpclient = mockHttp.ToHttpClient();

            var client = new RandomUrlHttpClient(
                httpclient,
                new[] { "http://test/1", "http://test/2" },
                null,
                3,
                null);
            var res = await client.PostJsonAsync("api/55", "{\"foo\":\"bar\"}");

            var content = await res.Content.ReadAsStringAsync();

            Assert.AreEqual("ok", content);
        }
예제 #5
0
        public void UpdateBaseUrls()
        {
            var client = new RandomUrlHttpClient(new[] { "http://test/1", "http://test/2" });

            // Should not throw due to identical items in the list (Distinct before ToDictionary)
            client.UpdateBaseUrls(new[] { "http://test/3", "http://test/3" });
            Assert.AreEqual("http://test/3", client.UrlResourceManager.SelectRandomly());
        }
예제 #6
0
        public void CtorDuplicatedUrl()
        {
            var client = new RandomUrlHttpClient(
                new[] { "http://test/1", "http://test/1" });

            Assert.AreEqual(1, client.UrlResourceManager.Resources.Count);
            Assert.AreEqual("http://test/1", client.UrlResourceManager.Resources.Keys.First());
        }
예제 #7
0
 public GitHubClient(HttpClient httpClient)
 {
     HttpClient = new RandomUrlHttpClient(httpClient, new[]
     {
         "https://github.agodadev.io/api/v3"
     }, isErrorResponse: (msg, body) =>
     {
         // customize error predicate
         // 0 for non-error
         return(0);
     });
 }
예제 #8
0
 public StackExchangeClient(HttpClient httpClient)
 {
     HttpClient = new RandomUrlHttpClient(httpClient, new[]
     {
         "https://api.stackexchange.com/2.2"
     }, isErrorResponse: (msg, body) =>
     {
         // customize error predicate
         // 0 for non-error
         return(0);
     });
 }
예제 #9
0
 public RrCompatibleHttpClient(
     string[] baseUrls,
     TimeSpan?timeout,
     int retryCount,
     HttpMessageHandler handler)
 {
     _isGzip = handler is HttpClientHandler httpHandler &&
               (httpHandler?.AutomaticDecompression ?? 0) > 0;
     _httpClient = new RandomUrlHttpClient(
         new HttpClient(handler, true),
         baseUrls,
         timeout ?? TimeSpan.FromMilliseconds(1000),
         retryCount);
 }
예제 #10
0
        public async Task TestHttpRequestException()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "http://test/*")
            .Throw(new HttpRequestException());
            var httpclient = mockHttp.ToHttpClient();

            var client = new RandomUrlHttpClient(
                httpclient,
                new[] { "http://test/1", "http://test/2" },
                null,
                3,
                null);

            var result = await client.SendAsyncWithDiag("api/55", url => new HttpRequestMessage(HttpMethod.Post, url));

            Assert.IsAssignableFrom <ServiceUnavailableException>(result.Last().Exception);
            Assert.AreEqual(3, result.Count);
        }
예제 #11
0
        static void Main(string[] args)
        {
            var native = new HttpClient();

            Console.WriteLine("Native");
            RunTest(testCount, async url =>
            {
                return(await native.PostAsync(baseUrl, new StringContent("")));
            });

            var rr1 = RR1.CreateClient(baseUrl);

            Console.WriteLine("RR1");
            RunTest(testCount, url => RR1.Request(rr1, url));

            var newrrClient = new RandomUrlHttpClient(new[] { baseUrl });

            Console.WriteLine("RR3");
            RunTest(testCount, async url =>
            {
                return(await newrrClient.PostAsync(url, new StringContent("")));
            });
        }
        public async Task TestGetAsync()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Get, "http://test/*")
            .Respond(msg =>
            {
                StringAssert.IsMatch("http://test/1|2/api/55", msg.RequestUri.AbsoluteUri);
                return(new StringContent("ok"));
            });
            var httpclient = mockHttp.ToHttpClient();

            var client = new RandomUrlHttpClient(
                httpclient,
                new[] { "http://test/1", "http://test/2" },
                null,
                3,
                null);
            var res = await client.GetAsync("api/55");

            var content = await res.Content.ReadAsStringAsync();

            Assert.AreEqual("ok", content);
        }