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 async Task TestRealSimpleGet() { // Simple usage (default client build strategy) var google = new Uri("https://google.com"); using (var googleClient = new EffectiveHttpClient(google)) { var result = await googleClient.GetStringAsync("https://google.com"); Assert.IsFalse(string.IsNullOrWhiteSpace(result)); } }
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 async Task TestHostChangeNotAllowed() { // Call the client with a different host ends up with exception await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => { var google = new Uri("https://google.com"); using (var googleClient = new EffectiveHttpClient(google)) { await googleClient.GetStringAsync("https://bing.com"); } }); // Call the client with a different scheme ends up with exception await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => { var google = new Uri("https://google.com"); using (var googleClient = new EffectiveHttpClient(google)) { await googleClient.GetStringAsync("http://google.com"); } }); }