From 6f47f6f28d29ec747befe0e668d80df2fc4c4782 Mon Sep 17 00:00:00 2001 From: jericho Date: Fri, 31 May 2024 18:19:25 -0400 Subject: [PATCH 1/4] Only reposition streams that are "seekable" Resolves #125 --- Client/Internal/Response.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/Internal/Response.cs b/Client/Internal/Response.cs index 75171f4..ababdc7 100644 --- a/Client/Internal/Response.cs +++ b/Client/Internal/Response.cs @@ -95,7 +95,7 @@ public async Task AsStream() #endif ) .ConfigureAwait(false); - stream.Position = 0; + if (stream.CanSeek) stream.Position = 0; return stream; } From e9ccadbb967a8631c672bdb483545ac77ccc57c1 Mon Sep 17 00:00:00 2001 From: jericho Date: Sun, 2 Jun 2024 08:18:53 -0400 Subject: [PATCH 2/4] Add integration test --- Tests/Integration/IntegrationTests.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tests/Integration/IntegrationTests.cs b/Tests/Integration/IntegrationTests.cs index 70bab21..3d7579e 100644 --- a/Tests/Integration/IntegrationTests.cs +++ b/Tests/Integration/IntegrationTests.cs @@ -1,6 +1,9 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Linq.Expressions; +using System.Net.Http; + + #if NET452 using System.Net; #endif @@ -50,6 +53,25 @@ public class IntegrationTests }; + /********* + ** Unit tests + *********/ + [Test(Description = "The client can stream a large file.")] + public async Task StreamLargeFile() + { + // arrange + IClient client = this.ConstructClient("https://proof.ovh.net/"); + + // act + var result = await client + .GetAsync("files/1Mb.dat") // Files of various sizes are available for testing purposes. See this article: https://www.buildsometech.com/download-test-files/#Download_Test_Files_100GB_50GB_10GB_5GB_4GB_2GB_1GB_High-Speed + .WithOptions(completeWhen: HttpCompletionOption.ResponseHeadersRead) + .AsStream(); + + // assert + Assert.IsNotNull(result); + } + /********* ** Unit tests *********/ From 8c644e88534b749d867797b7de54b2570beb17b7 Mon Sep 17 00:00:00 2001 From: jericho Date: Sun, 2 Jun 2024 13:08:54 -0400 Subject: [PATCH 3/4] Remove duplicate XML comment --- Tests/Integration/IntegrationTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Tests/Integration/IntegrationTests.cs b/Tests/Integration/IntegrationTests.cs index 3d7579e..6331088 100644 --- a/Tests/Integration/IntegrationTests.cs +++ b/Tests/Integration/IntegrationTests.cs @@ -72,9 +72,6 @@ public async Task StreamLargeFile() Assert.IsNotNull(result); } - /********* - ** Unit tests - *********/ [Test(Description = "The client can fetch a resource from the English Wikipedia's API.")] public async Task EnglishWikipedia() { From e83cf0ef9af41538be322db89991471d0b4c28f3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 5 Jun 2024 22:34:02 -0400 Subject: [PATCH 4/4] tweak new code --- Client/Internal/Response.cs | 5 ++++- Tests/Integration/IntegrationTests.cs | 7 +++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Client/Internal/Response.cs b/Client/Internal/Response.cs index ababdc7..9d7a980 100644 --- a/Client/Internal/Response.cs +++ b/Client/Internal/Response.cs @@ -95,7 +95,10 @@ public async Task AsStream() #endif ) .ConfigureAwait(false); - if (stream.CanSeek) stream.Position = 0; + + if (stream.CanSeek) + stream.Position = 0; + return stream; } diff --git a/Tests/Integration/IntegrationTests.cs b/Tests/Integration/IntegrationTests.cs index 6331088..50dec0c 100644 --- a/Tests/Integration/IntegrationTests.cs +++ b/Tests/Integration/IntegrationTests.cs @@ -1,12 +1,11 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq.Expressions; -using System.Net.Http; - - #if NET452 using System.Net; #endif +using System.Net.Http; using System.Threading.Tasks; using NUnit.Framework; using Pathoschild.Http.Client; @@ -63,7 +62,7 @@ public async Task StreamLargeFile() IClient client = this.ConstructClient("https://proof.ovh.net/"); // act - var result = await client + Stream result = await client .GetAsync("files/1Mb.dat") // Files of various sizes are available for testing purposes. See this article: https://www.buildsometech.com/download-test-files/#Download_Test_Files_100GB_50GB_10GB_5GB_4GB_2GB_1GB_High-Speed .WithOptions(completeWhen: HttpCompletionOption.ResponseHeadersRead) .AsStream();