From dfc15069688f31f20aa2e795fdf331d35159ce5a Mon Sep 17 00:00:00 2001 From: Raif Atef Date: Wed, 10 Jan 2024 22:36:54 +0200 Subject: [PATCH] Fix #794 with passing unit test while preserving existing behavior. --- .../Configuration/DefaultJsonSerializer.cs | 4 +++- test/Flurl.Test/Http/RealHttpTests.cs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Flurl.Http/Configuration/DefaultJsonSerializer.cs b/src/Flurl.Http/Configuration/DefaultJsonSerializer.cs index 580217cd..0bc346b3 100644 --- a/src/Flurl.Http/Configuration/DefaultJsonSerializer.cs +++ b/src/Flurl.Http/Configuration/DefaultJsonSerializer.cs @@ -35,6 +35,8 @@ public DefaultJsonSerializer(JsonSerializerOptions options = null) { /// Deserializes the specified stream to an object of type T. /// /// The stream to deserialize. - public T Deserialize(Stream stream) => stream.Length == 0 ? default : JsonSerializer.Deserialize(stream, _options); + public T Deserialize(Stream stream) => stream.CanSeek && stream.Length == 0 + ? default + : JsonSerializer.Deserialize(stream, _options); } } \ No newline at end of file diff --git a/test/Flurl.Test/Http/RealHttpTests.cs b/test/Flurl.Test/Http/RealHttpTests.cs index 8bfca19c..98763281 100644 --- a/test/Flurl.Test/Http/RealHttpTests.cs +++ b/test/Flurl.Test/Http/RealHttpTests.cs @@ -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>(completionOption); + + Assert.AreEqual(true, ((JsonElement)result["gzipped"]).GetBoolean()); + } + [Test] public async Task can_get_stream() { using (var stream = await "https://www.google.com".GetStreamAsync())