diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs index f1edf8fc04..bd3ff2c023 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs @@ -95,6 +95,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing CreateServer, CreateWebHostBuilder, GetTestAssemblies, + CreateDefaultClient, builder => { _configuration(builder); @@ -300,20 +301,11 @@ 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 virtual HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) { EnsureServer(); + + var baseAddress = new Uri("http://localhost"); if (handlers == null || handlers.Length == 0) { var client = _server.CreateClient(); @@ -342,6 +334,22 @@ namespace Microsoft.AspNetCore.Mvc.Testing } } + /// + /// 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; + } + /// public void Dispose() { @@ -386,18 +394,21 @@ namespace Microsoft.AspNetCore.Mvc.Testing private readonly Func _createServer; private readonly Func _createWebHostBuilder; private readonly Func> _getTestAssemblies; + private readonly Func _createDefaultClient; public DelegatedWebApplicationFactory( WebApplicationFactoryClientOptions options, Func createServer, Func createWebHostBuilder, Func> getTestAssemblies, + Func createClient, Action configureWebHost) { ClientOptions = new WebApplicationFactoryClientOptions(options); _createServer = createServer; _createWebHostBuilder = createWebHostBuilder; _getTestAssemblies = getTestAssemblies; + _createDefaultClient = createClient; _configuration = configureWebHost; } @@ -408,6 +419,8 @@ namespace Microsoft.AspNetCore.Mvc.Testing protected override IEnumerable GetTestAssemblies() => _getTestAssemblies(); protected override void ConfigureWebHost(IWebHostBuilder builder) => _configuration(builder); + + public override HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) => _createDefaultClient(handlers); } } }