Skip to content

Commit

Permalink
Merge pull request #804 from tmenier/dev
Browse files Browse the repository at this point in the history
Flurl.Http 4.0.2
  • Loading branch information
tmenier authored Jan 17, 2024
2 parents 7ea321c + 363416c commit c3b9bb8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Flurl.Http/Configuration/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public DefaultJsonSerializer(JsonSerializerOptions options = null) {
/// Deserializes the specified stream to an object of type T.
/// </summary>
/// <param name="stream">The stream to deserialize.</param>
public T Deserialize<T>(Stream stream) => stream.Length == 0 ? default : JsonSerializer.Deserialize<T>(stream, _options);
public T Deserialize<T>(Stream stream) => stream.CanSeek && stream.Length == 0
? default
: JsonSerializer.Deserialize<T>(stream, _options);
}
}
2 changes: 1 addition & 1 deletion src/Flurl.Http/Flurl.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<LangVersion>9.0</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>Flurl.Http</PackageId>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
<Authors>Todd Menier</Authors>
<Description>A fluent, testable HTTP client library.</Description>
<Copyright>Copyright (c) Todd Menier 2024.</Copyright>
Expand Down
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);
}
}
}
13 changes: 13 additions & 0 deletions test/Flurl.Test/Http/RealHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public async Task can_post_and_receive_json() {
Assert.AreEqual(2, result.json["b"].GetInt32());
}

[Test]
[TestCase(HttpCompletionOption.ResponseHeadersRead)]
[TestCase(HttpCompletionOption.ResponseContentRead)]
public async Task can_get_json_with_http_completion_option_headers(HttpCompletionOption completionOption)
{
var result = await "https://httpbin.org"
.AppendPathSegment("gzip")
.WithHeader("Accept-encoding", "gzip")
.GetJsonAsync<Dictionary<string, object>>(completionOption);

Assert.AreEqual(true, ((JsonElement)result["gzipped"]).GetBoolean());
}

[Test]
public async Task can_get_stream() {
using (var stream = await "https://www.google.com".GetStreamAsync())
Expand Down

0 comments on commit c3b9bb8

Please sign in to comment.