Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flurl.Http 4.0.2 #804

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading