diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs index f1edf8fc04..3e31e9b5aa 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs @@ -88,13 +88,17 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// An to configure the . /// /// A new . - public WebApplicationFactory WithWebHostBuilder(Action configuration) + public WebApplicationFactory WithWebHostBuilder(Action configuration) => + WithWebHostBuilderCore(configuration); + + internal virtual WebApplicationFactory WithWebHostBuilderCore(Action configuration) { var factory = new DelegatedWebApplicationFactory( ClientOptions, CreateServer, CreateWebHostBuilder, GetTestAssemblies, + ConfigureClient, builder => { _configuration(builder); @@ -300,26 +304,14 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// A list of instances to set up on the /// . /// The . - public HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) => - CreateDefaultClient(new Uri("http://localhost"), handlers); - - /// - /// Creates a new instance of an that can be used to - /// send to the server. - /// - /// The base address of the instance. - /// A list of instances to set up on the - /// . - /// The . - public HttpClient CreateDefaultClient(Uri baseAddress, params DelegatingHandler[] handlers) + public HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) { EnsureServer(); + + HttpClient client; if (handlers == null || handlers.Length == 0) { - var client = _server.CreateClient(); - client.BaseAddress = baseAddress; - - return client; + client = _server.CreateClient(); } else { @@ -331,15 +323,44 @@ namespace Microsoft.AspNetCore.Mvc.Testing var serverHandler = _server.CreateHandler(); handlers[handlers.Length - 1].InnerHandler = serverHandler; - var client = new HttpClient(handlers[0]) - { - BaseAddress = baseAddress - }; - - _clients.Add(client); - - return client; + client = new HttpClient(handlers[0]); } + + _clients.Add(client); + + ConfigureClient(client); + + return client; + } + + /// + /// Configures instances created by this . + /// + /// The instance getting configured. + protected virtual void ConfigureClient(HttpClient client) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + client.BaseAddress = new Uri("http://localhost"); + } + + /// + /// Creates a new instance of an that can be used to + /// send to the server. + /// + /// The base address of the instance. + /// A list of instances to set up on the + /// . + /// The . + public HttpClient CreateDefaultClient(Uri baseAddress, params DelegatingHandler[] handlers) + { + var client = CreateDefaultClient(handlers); + client.BaseAddress = baseAddress; + + return client; } /// @@ -377,7 +398,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing _server?.Dispose(); } - + _disposed = true; } @@ -386,18 +407,21 @@ namespace Microsoft.AspNetCore.Mvc.Testing private readonly Func _createServer; private readonly Func _createWebHostBuilder; private readonly Func> _getTestAssemblies; + private readonly Action _configureClient; public DelegatedWebApplicationFactory( WebApplicationFactoryClientOptions options, Func createServer, Func createWebHostBuilder, Func> getTestAssemblies, + Action configureClient, Action configureWebHost) { ClientOptions = new WebApplicationFactoryClientOptions(options); _createServer = createServer; _createWebHostBuilder = createWebHostBuilder; _getTestAssemblies = getTestAssemblies; + _configureClient = configureClient; _configuration = configureWebHost; } @@ -408,6 +432,23 @@ namespace Microsoft.AspNetCore.Mvc.Testing protected override IEnumerable GetTestAssemblies() => _getTestAssemblies(); protected override void ConfigureWebHost(IWebHostBuilder builder) => _configuration(builder); + + protected override void ConfigureClient(HttpClient client) => _configureClient(client); + + internal override WebApplicationFactory WithWebHostBuilderCore(Action configuration) + { + return new DelegatedWebApplicationFactory( + ClientOptions, + _createServer, + _createWebHostBuilder, + _getTestAssemblies, + _configureClient, + builder => + { + _configuration(builder); + configuration(builder); + }); + } } } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog deleted file mode 100644 index 372871a1fb..0000000000 Binary files a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog and /dev/null differ