public void InitializationTest() { // Arrange var pool = new TinyHttpClientPool(); // Act var a = pool.Fetch(); // Assert Assert.NotNull(a); }
public void NewInstanceTest() { // Arrange var pool = new TinyHttpClientPool(); // Act var a = pool.Fetch(); var b = pool.Fetch(); // Assert Assert.NotSame(a, b); }
public void ReuseTest() { // Arrange var pool = new TinyHttpClientPool(); // Act var a = pool.Fetch(); a.Dispose(); var b = pool.Fetch(); // Assert Assert.Same(a, b); }
public void BaseUrlThroughConfigurationTest() { // Arrange var pool = new TinyHttpClientPool( new TinyHttpClientPoolConfiguration() { BaseUrl = "https://www.ducksareawesome.net" }); // Act var a = pool.Fetch(); // Assert Assert.Equal("https://www.ducksareawesome.net/", a.BaseAddress.ToString()); }
public async Task <IEnumerable <TodoItem> > GetTodoItems(bool slow = false) { using (var client = TinyHttpClientPool.FetchClient()) { if (slow) { await Task.Delay(3000); } var url = "/todos"; var json = await client.GetStringAsync(url); return(Newtonsoft.Json.JsonConvert.DeserializeObject <List <TodoItem> >(json)); } }
public async System.Threading.Tasks.Task MessageHandlerThroughConfigurationTest() { // Arrange var pool = new TinyHttpClientPool( new TinyHttpClientPoolConfiguration() { BaseUrl = "https://www.ducksareawesome.net", MessageHandler = new TinyHttpClientPool.Tests.DummyMessageHandler() }); // Act var a = pool.Fetch(); var result = await a.GetAsync("http://test.nu/fake"); // Assert Assert.Equal(result.StatusCode, System.Net.HttpStatusCode.SeeOther); }
public App() { InitializeComponent(); // Initialize like this, or simply create an instance of // TinyHttpClientPool and keep track of it yourself. TinyHttpClientPool.Initialize(); // You can supply an action on what to do to initialize any new HttpClient // DO NOT keep a reference to the passed HttpClient since it must be controlled // by the pool. TinyHttpClientPool.Current.ClientInitializationOnCreation = (obj) => obj.BaseAddress = new Uri(BackendUrl); DependencyService.Register <TodoService>(); MainPage = new NavigationPage(new MainPage()); }
public void InitializeClientOnFetchTest() { // Arrange var i = 0; var pool = new TinyHttpClientPool(); pool.ClientInitializationOnFetch = (obj) => i++; // Act var a = pool.Fetch(); a.Dispose(); var b = pool.Fetch(); b.Dispose(); // Assert Assert.Equal(2, i); }
public void ResetHeadersOnReuseTest() { // Arrange var pool = new TinyHttpClientPool( new TinyHttpClientPoolConfiguration() { ResetHeadersOnReuse = true }); // Act var a = pool.Fetch(); a.DefaultRequestHeaders.Add("Ducks", "are awesome"); a.Dispose(); var b = pool.Fetch(); // Assert Assert.Same(a, b); Assert.Equal(0, b.DefaultRequestHeaders.Count()); }
public void FlushTest() { // Arrange var pool = new TinyHttpClientPool(); // Act var a = pool.Fetch(); var b = pool.Fetch(); // Assert and dispose Assert.Equal(0, pool.AvailableCount); Assert.Equal(2, pool.TotalPoolSize); a.Dispose(); Assert.Equal(1, pool.AvailableCount); Assert.Equal(2, pool.TotalPoolSize); pool.Flush(); // Only removes unused Assert.Equal(0, pool.AvailableCount); Assert.Equal(1, pool.TotalPoolSize); // There should be one left since it's still in use }