public void Factory_CreateClient_WithoutName_UsesDefaultOptions() { // Arrange var count = 0; Options.CurrentValue.HttpClientActions.Add(b => { count++; }); var factory = new TestHttpClientFactory(Services, LoggerFactory, Options, EmptyFilters); // Act var client = factory.CreateClient(); // Assert Assert.Equal(1, count); }
public void Factory_CreateClient_WithName_UsesNamedOptions() { // Arrange var count = 0; Options.Get("github").HttpClientActions.Add(b => { count++; }); var factory = new TestHttpClientFactory(Services, ScopeFactory, LoggerFactory, Options, EmptyFilters); // Act var client = factory.CreateClient("github"); // Assert Assert.Equal(1, count); }
private async Task <ExpiredHandlerTrackingEntry> SimulateClientUse( TestHttpClientFactory factory, DisposeTrackingHandler disposeHandler) { // Create a handler and move it to the expired state var client1 = factory.CreateClient("github"); var kvp = Assert.Single(factory.ActiveEntryState); kvp.Value.Item1.SetResult(kvp.Key); await kvp.Value.Item2; // Our handler is now in the cleanup state. var cleanupEntry = Assert.Single(factory._expiredHandlers); Assert.True(factory.CleanupTimerStarted.IsSet, "Cleanup timer started"); // Nulling out the references to the internal state of the factory since they wouldn't exist in the non-test // scenario. We're holding on the client to prevent disposal - like a real use case. lock (this) { // Prevent reordering kvp = default; } // Act - 1 factory.CleanupTimer_Tick(null); // Assert Assert.Same(cleanupEntry, Assert.Single(factory._expiredHandlers)); Assert.Equal(0, disposeHandler.DisposeCount); Assert.True(factory.CleanupTimerStarted.IsSet, "Cleanup timer started"); // We need to make sure that the outer handler actually gets GCed, so drop our references to it. // This is important because the factory relies on this possibility for correctness. We need to ensure that // the factory isn't keeping any references. lock (this) { // Prevent reordering client1 = null; } return(cleanupEntry); }
public void Factory_DisposeClient_DoesNotDisposeHandler() { // Arrange Options.CurrentValue.HttpMessageHandlerBuilderActions.Add(b => { var mockHandler = new Mock <HttpMessageHandler>(); mockHandler .Protected() .Setup("Dispose", true) .Throws(new Exception("Dispose should not be called")); b.PrimaryHandler = mockHandler.Object; }); var factory = new TestHttpClientFactory(Services, ScopeFactory, LoggerFactory, Options, EmptyFilters); // Act using (factory.CreateClient()) { } // Assert (does not throw) }
public void Execute_WhenNoFilterArguments_ShouldReturnOk() { //---------------Arrange------------------- var requestUri = "todo/fetch"; var args = new TodoFilterInput() { IncludedCompleted = false }; var useCase = CreateFetchTodoCollectionUseCase(); var testServer = new TestServerBuilder <FetchFilteredTodo>() .WithInstanceRegistration <IFetchFilteredTodoUseCase>(useCase) .Build(); using (testServer) { var client = TestHttpClientFactory.CreateClient(testServer); //---------------Act------------------- var response = client.PostAsJsonAsync(requestUri, args).Result; //---------------Assert------------------- Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } }
private async Task <ExpiredHandlerTrackingEntry> SimulateClientUse_Factory_CleanupCycle_DisposesEligibleHandler(TestHttpClientFactory factory) { // Create a handler and move it to the expired state var client1 = factory.CreateClient("github"); var kvp = Assert.Single(factory.ActiveEntryState); kvp.Value.Item1.SetResult(kvp.Key); await kvp.Value.Item2; // Our handler is now in the cleanup state. var cleanupEntry = Assert.Single(factory._expiredHandlers); Assert.True(factory.CleanupTimerStarted.IsSet, "Cleanup timer started"); // We need to make sure that the outer handler actually gets GCed, so drop our references to it. // This is important because the factory relies on this possibility for correctness. We need to ensure that // the factory isn't keeping any references. kvp = default; client1 = null; return(cleanupEntry); }