Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Sep 7, 2020
1 parent fb92cee commit 09418e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
24 changes: 17 additions & 7 deletions Test/Flurl.Test/Http/FlurlClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ namespace Flurl.Test.Http
public class FlurlClientFactoryTests
{
[Test]
public void per_host_factory_provides_same_client_per_host() {
var fac = new PerHostFlurlClientFactory();
public void default_factory_provides_same_client_per_host_scheme_port() {
var fac = new DefaultFlurlClientFactory();
var cli1 = fac.Get("http://api.com/foo");
var cli2 = fac.Get("https://api.com/bar");
var cli2 = fac.Get("http://api.com/bar");
var cli3 = fac.Get("https://api.com/foo");
var cli4 = fac.Get("https://api.com/bar");
var cli5 = fac.Get("https://api.com:1234/foo");
var cli6 = fac.Get("https://api.com:1234/bar");

Assert.AreSame(cli1, cli2);
Assert.AreSame(cli3, cli4);
Assert.AreSame(cli5, cli6);

Assert.AreNotSame(cli1, cli3);
Assert.AreNotSame(cli3, cli5);
}

[Test]
[Test]
public void per_base_url_factory_provides_same_client_per_provided_url() {
var fac = new PerBaseUrlFlurlClientFactory();
var cli1 = fac.Get("http://api.com/foo");
Expand All @@ -32,15 +42,15 @@ public void per_base_url_factory_provides_same_client_per_provided_url() {

[Test]
public void can_configure_client_from_factory() {
var fac = new PerHostFlurlClientFactory()
var fac = new DefaultFlurlClientFactory()
.ConfigureClient("http://api.com/foo", c => c.Settings.Timeout = TimeSpan.FromSeconds(123));
Assert.AreEqual(TimeSpan.FromSeconds(123), fac.Get("https://api.com/bar").Settings.Timeout);
Assert.AreEqual(TimeSpan.FromSeconds(123), fac.Get("http://api.com/bar").Settings.Timeout);
Assert.AreNotEqual(TimeSpan.FromSeconds(123), fac.Get("http://api2.com/foo").Settings.Timeout);
}

[Test]
public async Task ConfigureClient_is_thread_safe() {
var fac = new PerHostFlurlClientFactory();
var fac = new DefaultFlurlClientFactory();
var sequence = new List<int>();

var task1 = Task.Run(() => fac.ConfigureClient("http://api.com", c => {
Expand Down
2 changes: 1 addition & 1 deletion Test/Flurl.Test/Http/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void can_configure_global_from_FlurlHttp_object() {
[Test]
public void can_configure_client_from_FlurlHttp_object() {
FlurlHttp.ConfigureClient("http://host1.com/foo", cli => cli.Settings.Redirects.Enabled = false);
Assert.IsFalse(new FlurlRequest("https://host1.com/bar").Client.Settings.Redirects.Enabled); // different URL but same host, so should use same client
Assert.IsFalse(new FlurlRequest("http://host1.com/bar").Client.Settings.Redirects.Enabled); // different URL but same host, so should use same client
Assert.IsTrue(new FlurlRequest("http://host2.com").Client.Settings.Redirects.Enabled);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/Flurl.Http/Configuration/DefaultFlurlClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flurl.Http.Configuration
{
/// <summary>
/// An IFlurlClientFactory implementation that caches and reuses the same one instance of
/// FlurlClient per combination of scheme, host, and port. This is the default
/// implementation used when calls are made fluently off Urls/strings.
/// </summary>
public class DefaultFlurlClientFactory : FlurlClientFactoryBase
{
/// <summary>
/// Returns a unique cache key based on scheme, host, and port of the given URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>The cache key</returns>
protected override string GetCacheKey(Url url) => $"{url.Scheme}|{url.Host}|{url.Port}";
}
}
2 changes: 1 addition & 1 deletion src/Flurl.Http/Configuration/FlurlHttpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public override void ResetDefaults() {
Timeout = TimeSpan.FromSeconds(100); // same as HttpClient
JsonSerializer = new NewtonsoftJsonSerializer(null);
UrlEncodedSerializer = new DefaultUrlEncodedSerializer();
FlurlClientFactory = new PerHostFlurlClientFactory();
FlurlClientFactory = new DefaultFlurlClientFactory();
HttpClientFactory = new DefaultHttpClientFactory();
Redirects.Enabled = true;
Redirects.AllowSecureToInsecure = false;
Expand Down
25 changes: 0 additions & 25 deletions src/Flurl.Http/Configuration/PerHostFlurlClientFactory.cs

This file was deleted.

0 comments on commit 09418e5

Please sign in to comment.