From 1f0e841daee69271b7d590117f291a415388e9d6 Mon Sep 17 00:00:00 2001 From: Douglas-Cleghorn Date: Thu, 1 Feb 2018 08:41:48 -0700 Subject: [PATCH] WebClient progress monitoring uses ContentLength header (dotnet/corefx#26462) * WebClient progress monitoring uses ContentLength header * Added test for download progress * Fixed build issue * Set contentLength to zero if the header is zero * Fixing WebClient async tests Commit migrated from https://github.com/dotnet/corefx/commit/3c4115d601335a61d9d170a71bdfb71eb2177beb --- .../src/System/Net/WebClient.cs | 5 ++++ .../tests/System.Net.WebClient.Tests.csproj | 3 +++ .../System.Net.WebClient/tests/WebClientTest.cs | 27 ++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs b/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs index 68cf3bd..364046b 100644 --- a/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs +++ b/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs @@ -875,6 +875,11 @@ namespace System.Net } writeStream.SetLength(copyBuffer.Length); } + + if (contentLength >= 0) + { + _progress.TotalBytesToReceive = contentLength; + } using (writeStream) using (Stream readStream = response.GetResponseStream()) diff --git a/src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj b/src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj index 7c5b8ce..37e1c73 100644 --- a/src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj +++ b/src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj @@ -20,6 +20,9 @@ Common\System\Net\Http\LoopbackServer.cs + + Common\System\Threading\Tasks\TaskTimeoutExtensions.cs + \ No newline at end of file diff --git a/src/libraries/System.Net.WebClient/tests/WebClientTest.cs b/src/libraries/System.Net.WebClient/tests/WebClientTest.cs index 64efdb0..575f7de 100644 --- a/src/libraries/System.Net.WebClient/tests/WebClientTest.cs +++ b/src/libraries/System.Net.WebClient/tests/WebClientTest.cs @@ -476,6 +476,8 @@ namespace System.Net.Tests public abstract class WebClientTestBase { + public const int TimeoutMilliseconds = 30 * 1000; + public static readonly object[][] EchoServers = System.Net.Test.Common.Configuration.Http.EchoServers; const string ExpectedText = "To be, or not to be, that is the question:" + @@ -548,7 +550,11 @@ namespace System.Net.Tests "\r\n" + $"{ExpectedText}"); Assert.Equal(ExpectedText, Encoding.ASCII.GetString(await download)); - Assert.True(!IsAsync || await downloadProgressInvoked.Task, "Expected download progress callback to be invoked"); + + if (IsAsync) + { + await downloadProgressInvoked.Task.TimeoutAfter(TimeoutMilliseconds); + } }); } @@ -559,7 +565,16 @@ namespace System.Net.Tests { string largeText = GetRandomText(1024 * 1024); + var downloadProgressInvokedWithContentLength = new TaskCompletionSource(); var wc = new WebClient(); + wc.DownloadProgressChanged += (s, e) => + { + if (e.TotalBytesToReceive == largeText.Length && e.BytesReceived < e.TotalBytesToReceive) + { + downloadProgressInvokedWithContentLength.TrySetResult(true); + } + }; + Task download = DownloadDataAsync(wc, url.ToString()); await LoopbackServer.ReadRequestAndSendResponseAsync(server, "HTTP/1.1 200 OK\r\n" + @@ -568,6 +583,11 @@ namespace System.Net.Tests "\r\n" + $"{largeText}"); Assert.Equal(largeText, Encoding.ASCII.GetString(await download)); + + if (IsAsync) + { + await downloadProgressInvokedWithContentLength.Task.TimeoutAfter(TimeoutMilliseconds); + } }); } @@ -644,7 +664,10 @@ namespace System.Net.Tests byte[] result = await UploadDataAsync(wc, echoServer.ToString(), Encoding.UTF8.GetBytes(ExpectedText)); Assert.Contains(ExpectedText, Encoding.UTF8.GetString(result)); - Assert.True(!IsAsync || await uploadProgressInvoked.Task, "Expected upload progress callback to be invoked"); + if(IsAsync) + { + await uploadProgressInvoked.Task.TimeoutAfter(TimeoutMilliseconds); + } } [OuterLoop("Networking test talking to remote server: issue #11345")] -- 2.7.4