public static async Task MakeHttpGet(Random random) { var baseAddress = new Uri(@"https://bing.com"); var url = @"?toWww=1&redig=test"; // 5 minute timeout on all calls for debugging var buildStrategy = new HttpClientBuildStrategy(baseAddress) .UseTimeOut(new TimeSpan(0, 5, 0)); // 5 minute age then it'll retire var renewStrategy = new RenewStrategy() .UseAgeStrategy(new TimeSpan(1, 5, 0)); using (var client = new EffectiveHttpClient(buildStrategy, renewStrategy)) { // Sleep random number of milliseconds (20 to 500) beforehand to allow connection reuse // This simulates real time concurrency scenarios Thread.Sleep(random.Next(100, 5000)); // Make the real call var originatingThread = Thread.CurrentThread.ManagedThreadId; Console.WriteLine($"{originatingThread}: started..."); var ret = await client.GetStringAsync(url).ConfigureAwait(false); var endingThread = Thread.CurrentThread.ManagedThreadId; Console.WriteLine($"{originatingThread} -> {endingThread}: downloaded {ret.Length} characters. {client.ToString()}"); } }
public async Task TestRealComplexPost() { // More complex usage with http post var httpbin = new Uri("http://httpbin.org"); var buildStrategy = new HttpClientBuildStrategy(httpbin) .UseDefaultHeaders(x => { x.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); }); // Using is not needed, but you can still use it without having to worry about anything using (var httpbinClient = new EffectiveHttpClient(buildStrategy, new RenewStrategy())) { var payload = "testdata"; var content = new StringContent(payload, Encoding.UTF8); using (var response = await httpbinClient.PostAsync("http://httpbin.org/post", content)) { Assert.IsTrue(response.IsSuccessStatusCode); var result = await response.Content.ReadAsStringAsync(); Assert.IsTrue(result.Contains("testdata")); } } }
public void TestHttpClientNotDisposedWhenDisposeCalled() { var baseAddress = "http://google.com"; // since we cannot mock HttpClient.Dispose(non virtual), we mock HttpClient.Dispose(bool) instead. var httpClientMock = new Mock <HttpClient>(); httpClientMock.Protected().Setup("Dispose", It.IsAny <bool>()); var buildStrategy = new HttpClientBuildStrategy(new Uri(baseAddress), () => httpClientMock.Object); var renewStrategy = new RenewStrategy(); var client = new EffectiveHttpClient(buildStrategy, renewStrategy); // Call dispose, and make sure HttpClient is not really disposed client.Dispose(); httpClientMock.Protected().Verify("Dispose", Times.Never(), It.IsAny <bool>()); client.Dispose(); httpClientMock.Protected().Verify("Dispose", Times.Never(), It.IsAny <bool>()); }
public void TestClientBuildStrategyConstruction() { var uri = new Uri("https://test.com"); // Test strategy with no creation factory var strategy = new HttpClientBuildStrategy(uri); Assert.IsTrue(strategy.BaseAddress == uri); var client = strategy.Build(); Assert.IsTrue(client is Renewable <HttpClient>); Assert.IsTrue(client.Client.BaseAddress == uri); // Test strategy with creation factory var dummyclient = new HttpClient(); dummyclient.BaseAddress = new Uri("https://unknown.com"); var strategy2 = new HttpClientBuildStrategy(uri, () => dummyclient); var client2 = strategy2.Build(); Assert.IsTrue(client2.Client.BaseAddress == uri); // Test strategy with UseDefaultHeaders dummyclient.BaseAddress = new Uri("https://unknown.com"); var strategy3 = new HttpClientBuildStrategy(uri, () => dummyclient).UseDefaultHeaders(x => { x.Add("x-custom", "x-custom"); x.Authorization = new AuthenticationHeaderValue("Bearer", "token"); x.From = "*****@*****.**"; }); var client3 = strategy3.Build(); Assert.IsTrue(client3.Client.BaseAddress == uri); var customHeader = client3.Client.DefaultRequestHeaders.GetValues("x-custom"); Assert.IsTrue(customHeader.Count() == 1); Assert.IsTrue(customHeader.FirstOrDefault() == "x-custom"); Assert.IsTrue(client3.Client.DefaultRequestHeaders.Authorization.Scheme == "Bearer"); Assert.IsTrue(client3.Client.DefaultRequestHeaders.Authorization.Parameter == "token"); Assert.IsTrue(client3.Client.DefaultRequestHeaders.From == "*****@*****.**"); }