Skip to content

Commit

Permalink
#803 prepend global base URL with clientless
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Jan 17, 2024
1 parent 6429dae commit e0f915a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Flurl.Http/FlurlHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public static IFlurlClientBuilder ConfigureClientForUrl(string url) {
/// <summary>
/// Builds a cache key consisting of URL scheme, host, and port. This is the default client caching strategy.
/// </summary>
public static string BuildClientNameByHost(IFlurlRequest req) => $"{req.Url.Scheme}|{req.Url.Host}|{req.Url.Port}";
public static string BuildClientNameByHost(IFlurlRequest req) => $"{req.Url?.Scheme}|{req.Url?.Host}|{req.Url?.Port}";
}
}
14 changes: 14 additions & 0 deletions src/Flurl.Http/FlurlRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public IFlurlClient Client {
set {
_client = value;
Settings.Parent = _client?.Settings;
SyncBaseUrl(_client, this);
SyncHeaders(_client, this);
}
}
Expand Down Expand Up @@ -163,6 +164,19 @@ internal static void SyncHeaders(IFlurlClient client, IFlurlRequest request) {
}
}

/// <summary>
/// Prepends client.BaseUrl to this.Url, but only if this.Url isn't already a valid, absolute URL.
/// </summary>
private static void SyncBaseUrl(IFlurlClient client, IFlurlRequest request) {
if (string.IsNullOrEmpty(client?.BaseUrl))
return;

if (request.Url == null)
request.Url = client.BaseUrl;
else if (!Url.IsValid(request.Url))
request.Url = Url.Combine(client.BaseUrl, request.Url);
}

private void ApplyCookieJar(CookieJar jar) {
_jar = jar;
if (jar == null)
Expand Down
19 changes: 19 additions & 0 deletions test/Flurl.Test/Http/GlobalConfigTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Flurl.Http;
using Flurl.Http.Testing;
using NUnit.Framework;

namespace Flurl.Test.Http
Expand Down Expand Up @@ -74,5 +75,23 @@ public void can_configure_client_for_url() {
Assert.AreEqual(123, cli2.Settings.Timeout.Value.TotalSeconds);
Assert.AreNotEqual(123, cli3.Settings.Timeout.Value.TotalSeconds);
}

[Test] // #803
public async Task can_prepend_global_base_url() {
FlurlHttp.Clients.WithDefaults(builder =>
builder.ConfigureHttpClient(cli => cli.BaseAddress = new Uri("https://api.com")));

using var test = new HttpTest();

await "".GetAsync();
await "/path/1".GetAsync();
await "path/2".GetAsync();
await "https://other.com/path/3".GetAsync();

test.ShouldHaveCalled("https://api.com/").Times(1);
test.ShouldHaveCalled("https://api.com/path/1").Times(1);
test.ShouldHaveCalled("https://api.com/path/2").Times(1);
test.ShouldHaveCalled("https://other.com/path/3").Times(1);
}
}
}

0 comments on commit e0f915a

Please sign in to comment.