From 1cd54bf21770c8e52a06102c4be8868748f71e31 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 14 Nov 2024 10:16:45 -0900 Subject: [PATCH 001/119] Add NetwrokDownloadTests and WebListner --- .../Windows/Forms/NetworkDownloadTests.vb | 1573 +++++++++++++++++ .../System/Windows/Forms/WebListener.vb | 98 + 2 files changed, 1671 insertions(+) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb new file mode 100644 index 00000000000..55967664bd3 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb @@ -0,0 +1,1573 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports FluentAssertions +Imports Microsoft.VisualBasic.FileIO +Imports Xunit + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class NetworkTests + Inherits VbFileCleanupTestBase + + Private Const DefaultPassword As String = NameOf(DefaultPassword) + Private Const DefaultUserName As String = NameOf(DefaultUserName) + Private Const DownloadLargeFileSize As Integer = 104_857_600 + Private Const DownloadSmallFileSize As Integer = 18_135 + Private Const InvalidUrlAddress As String = "invalidURL" + Private Const TestingConnectionTimeout As Integer = 100_000 + ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, + ' they are not accessible in this project they come from WebClient. + Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." + Private Const SR_net_webstatus_Unauthorized As String = + "The remote server returned an error: (401) Unauthorized." + + Private Shared Sub CleanUpListener(listener As HttpListener) + listener.Stop() + listener.Close() + End Sub + + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' The full path and filename of the new file. + ''' + Private Shared Sub VerifyAndCleanupFailedDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) + + If Not String.IsNullOrWhiteSpace(testDirectory) Then + Directory.Exists(testDirectory).Should.BeTrue() + End If + If Not String.IsNullOrWhiteSpace(destinationFileName) Then + Call New FileInfo(destinationFileName).Exists.Should.BeFalse() + End If + CleanUpListener(listener) + End Sub + + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' + ''' The size in bytes of the destination file, this saves the caller from having to + ''' do another FileInfo call. + ''' + ''' The full path and filename of the new file. + ''' + Private Shared Function VerifyAndCleanupSuccessfulDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) As Long + + Directory.Exists(testDirectory).Should.BeTrue() + Dim fileInfo As New FileInfo(destinationFileName) + fileInfo.Exists.Should.BeTrue() + Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() + ' This directory should not be systems Temp Directory because it must be created + Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) + CleanUpListener(listener) + Return fileInfo.Length + End Function + + + Public Sub DownloadFile_UriOnly_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UriOnlyWhereAddressIsEmptyString_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(String.Empty), + destinationFileName) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriOnlyWhereAddressIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(Nothing), + destinationFileName) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) + Dim testDirectory As String = CreateTempDirectory() + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancel_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( + destinationFileName As String) + + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadLargeFileSize) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim networkCredentials As New NetworkCredential(DefaultUserName, password) + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should().Throw(Of UriFormatException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsAndNetworkCredentials_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=Nothing, + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsAndNetworkCredentials_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsAndNetworkCredentialsTimeout0_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=0, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UriWithAllOptionsExceptOnUserCancelWhereDestinationFileNameInvalidOverwriteThrows( + destinationFileName As String) + + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsWhereTargetDirectoryNonexistent_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Directory.Delete(testDirectory, recursive:=True) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsWithAllOptions_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UriWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UriWithUserNamePassword_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + + Public Sub DownloadFile_UriWithUserNamePasswordWherePasswordWrong_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=String.Empty) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UriWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlOnly_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + Public Sub DownloadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address, + destinationFileName) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) + Dim testDirectory As String = CreateTempDirectory() + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( + destinationFileName As String) + + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadLargeFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadLargeFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsAndNetworkCredentials_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + + Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(destinationFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWhereTargetDirectoryNonexistent_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Directory.Delete(testDirectory, recursive:=True) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWithAllOptions_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + Public Sub DownloadFile_UrlWithUserNamePassword_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Sub + + + + + Public Sub DownloadFile_UrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=String.Empty) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + + + + Public Sub DownloadFile_UrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=DownloadSmallFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Dim listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + End Sub + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb new file mode 100644 index 00000000000..f8cd3aef554 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb @@ -0,0 +1,98 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports System.Runtime.CompilerServices +Imports System.Threading + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class WebListener + Private ReadOnly _downloadFileUrlPrefix As String + Private ReadOnly _fileSize As Integer + Private ReadOnly _password As String + Private ReadOnly _userName As String + + ''' + ''' The name of the function that creates the server is uses to establish the file to be downloaded. + ''' + ''' Is used to create the file name and the size of download. + ''' Used to establish the file path to be downloaded. + Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) + _fileSize = fileSize + _downloadFileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" + Address = $"{_downloadFileUrlPrefix}T{fileSize}" + End Sub + + ''' + ''' Used for authenticated download. + ''' + ''' Passed to Me.New. + ''' Name to match for authorization. + ''' Password to match for authorization. + ''' Passed to Me.New. + Public Sub New( + fileSize As Integer, + userName As String, + password As String, + Optional memberName As String = Nothing) + + Me.New(fileSize, memberName) + _userName = userName + _password = password + End Sub + + Public ReadOnly Property Address As String + + Friend Function ProcessRequests() As HttpListener + ' Create a listener and add the prefixes. + Dim listener As New HttpListener() + + listener.Prefixes.Add(_downloadFileUrlPrefix) + If _userName IsNot Nothing OrElse _password IsNot Nothing Then + listener.AuthenticationSchemes = AuthenticationSchemes.Basic + End If + listener.Start() + Dim action As Action = + Sub() + ' Start the listener to begin listening for requests. + Dim response As HttpListenerResponse = Nothing + Try + ' Note: GetContext blocks while waiting for a request. + Dim context As HttpListenerContext = listener.GetContext() + ' Create the response. + response = context.Response + + If context.User?.Identity.IsAuthenticated Then + Dim identity As HttpListenerBasicIdentity = + CType(context.User?.Identity, HttpListenerBasicIdentity) + If String.IsNullOrWhiteSpace(identity.Name) _ + OrElse identity.Name <> _userName _ + OrElse String.IsNullOrWhiteSpace(identity.Password) _ + OrElse identity.Password <> _password Then + + response.StatusCode = HttpStatusCode.Unauthorized + Exit Try + End If + End If + ' Simulate network traffic + Thread.Sleep(millisecondsTimeout:=20) + Dim responseString As String = Strings.StrDup(_fileSize, "A") + Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) + response.ContentLength64 = buffer.Length + Using output As Stream = response.OutputStream + output.Write(buffer, offset:=0, count:=buffer.Length) + End Using + Finally + response?.Close() + response = Nothing + End Try + End Sub + + Task.Run(action) + Return listener + End Function + + End Class +End Namespace From 66298abc5f4380aa577721dc0cd543596ec68c8c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 14 Nov 2024 12:22:08 -0900 Subject: [PATCH 002/119] Fix merge issues --- .../WindowsFormsApplicationBase.vb | 3 +- .../Devices/Network.DownloadFile.vb | 527 +++++++++++------- .../Devices/Network.DownloadFileAsync.vb | 321 +++++++++++ .../VisualBasic/Devices/Network.Ping.vb | 11 +- .../VisualBasic/Devices/Network.UploadFile.vb | 8 +- .../VisualBasic/Devices/NetworkUtilities.vb | 4 +- .../VisualBasic/MyServices/ClipboardProxy.vb | 65 ++- .../MyServices/Internal/HttpClientCopy.vb | 190 +++++++ .../MyServices/Internal/WebClientCopy.vb | 88 +-- .../src/Resources/SR.resx | 60 +- .../src/Resources/xlf/SR.cs.xlf | 10 + .../src/Resources/xlf/SR.de.xlf | 10 + .../src/Resources/xlf/SR.es.xlf | 10 + .../src/Resources/xlf/SR.fr.xlf | 10 + .../src/Resources/xlf/SR.it.xlf | 10 + .../src/Resources/xlf/SR.ja.xlf | 10 + .../src/Resources/xlf/SR.ko.xlf | 10 + .../src/Resources/xlf/SR.pl.xlf | 10 + .../src/Resources/xlf/SR.pt-BR.xlf | 10 + .../src/Resources/xlf/SR.ru.xlf | 10 + .../src/Resources/xlf/SR.tr.xlf | 10 + .../src/Resources/xlf/SR.zh-Hans.xlf | 10 + .../src/Resources/xlf/SR.zh-Hant.xlf | 10 + .../Windows/Forms/NetworkDownloadTests.vb | 27 +- .../{Forms => TestUtilities}/WebListener.vb | 0 25 files changed, 1082 insertions(+), 352 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb create mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb rename src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/{Forms => TestUtilities}/WebListener.vb (100%) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb index f5ac6664952..2546f49b58e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb @@ -154,7 +154,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices ' have our principal on the thread before that happens. If authenticationMode = AuthenticationMode.Windows Then Try - ' Consider: Sadly, a call to: System.Security.SecurityManager.IsGranted(New SecurityPermission(SecurityPermissionFlag.ControlPrincipal)) + ' Consider: Sadly, a call to: + ' Security.SecurityManager.IsGranted(New SecurityPermission(SecurityPermissionFlag.ControlPrincipal)) ' Will only check the THIS caller so you'll always get TRUE. ' What we need is a way to get to the value of this on a demand basis. ' So I try/catch instead for now but would rather be able to IF my way around this block. diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 37b4776aa91..8da1972b718 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -2,7 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net -Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal @@ -24,30 +23,21 @@ Namespace Microsoft.VisualBasic.Devices ''' Address to the remote file, http, ftp etc... ''' Name and path of file where download is saved. Public Sub DownloadFile(address As String, destinationFileName As String) - DownloadFile( - address, - destinationFileName, - userName:=DEFAULT_USERNAME, - password:=DEFAULT_PASSWORD, - showUI:=False, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False) - End Sub - - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' to the remote file. - ''' Name and path of file where download is saved. - Public Sub DownloadFile(address As Uri, destinationFileName As String) - DownloadFile( - address, - destinationFileName, - userName:=DEFAULT_USERNAME, - password:=DEFAULT_PASSWORD, - showUI:=False, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False) + Try + DownloadFileAsync( + address, + destinationFileName, + userName:=DEFAULT_USERNAME, + password:=DEFAULT_PASSWORD, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + End Try End Sub ''' @@ -63,37 +53,21 @@ Namespace Microsoft.VisualBasic.Devices userName As String, password As String) - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI:=False, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False) - End Sub - - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' to the remote file. - ''' Name and path of file where download is saved. - ''' The name of the user performing the download. - ''' The user's password. - Public Sub DownloadFile( - address As Uri, - destinationFileName As String, - userName As String, - password As String) - - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI:=False, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False) + Try + DownloadFileAsync( + address, + destinationFileName, + userName, + password, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + End Try End Sub ''' @@ -117,15 +91,51 @@ Namespace Microsoft.VisualBasic.Devices connectionTimeout As Integer, overwrite As Boolean) - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI, - connectionTimeout, - overwrite, - UICancelOption.ThrowException) + Dim dialog As ProgressDialog = Nothing + Try + dialog = GetProgressDialog(address, destinationFileName, showUI) + + Dim t As Task = DownloadFileAsync( + address, + destinationFileName, + userName, + password, + dialog, + connectionTimeout, + overwrite, + onUserCancel:=UICancelOption.ThrowException) + + If t.IsFaulted Then + ' This will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If + End If + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + If TryCast(ex.InnerException, OperationCanceledException) IsNot Nothing AndAlso + Environment.UserInteractive Then + + If showUI AndAlso Environment.UserInteractive Then + Try + IO.File.Delete(destinationFileName) + Catch + ' ignore error + End Try + Throw New OperationCanceledException() + End If + End If + + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub ''' @@ -137,12 +147,11 @@ Namespace Microsoft.VisualBasic.Devices ''' The user's password. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. - ''' - ''' Indicates whether or not the file should be overwritten - ''' if local file already exists. + ''' Indicates whether or not the file should be + ''' overwritten if local file already exists. ''' ''' - ''' Indicates what to do if user cancels dialog (either or do nothing). + ''' Indicates what to do if user cancels dialog (either throw or do nothing). ''' Public Sub DownloadFile( address As String, @@ -154,8 +163,8 @@ Namespace Microsoft.VisualBasic.Devices overwrite As Boolean, onUserCancel As UICancelOption) - ' We're safe from DownloadFile(Nothing, ...) due to overload failure (DownloadFile(String,...) - ' vs. DownloadFile(Uri,...)). + ' We're safe from DownloadFile(Nothing, ...) due to overload failure (DownloadFile(String,...) vs. + ' DownloadFile(Uri,...)). ' However, it is good practice to verify address before calling Trim. If String.IsNullOrWhiteSpace(address) Then Throw VbUtils.GetArgumentNullException(NameOf(address)) @@ -166,216 +175,348 @@ Namespace Microsoft.VisualBasic.Devices ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - DownloadFile( - address:=addressUri, - destinationFileName, - networkCredentials, - showUI, - connectionTimeout, - overwrite, - onUserCancel) + Dim dialog As ProgressDialog = Nothing + Try + If showUI AndAlso Environment.UserInteractive Then + ' Construct the local file. This will validate the full name and path + Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( + path:=destinationFileName, + paramName:=NameOf(destinationFileName)) + + dialog = GetProgressDialog(address, destinationFileName, showUI) + End If + + Dim t As Task = DownloadFileAsync( + addressUri, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If + End If + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub ''' ''' Downloads a file from the network to the specified path. ''' - ''' to the remote file, - ''' - ''' Name and path of file where download is saved. - ''' - ''' The name of the user performing the download. - ''' The user's password. + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + ''' The credentials of the user performing the download. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' ''' Indicates whether or not the file should be overwritten if local file already exists. ''' + ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - userName As String, - password As String, + networkCredentials As ICredentials, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean) - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI, - connectionTimeout, - overwrite, - UICancelOption.ThrowException) + If address Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(address)) + End If + + Dim dialog As ProgressDialog = Nothing + Try + dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + Dim t As Task = DownloadFileAsync( + addressUri:=address, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If + End If + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub ''' ''' Downloads a file from the network to the specified path. ''' - ''' to the remote file. - ''' - ''' Name and path of file where download is saved. - ''' - ''' The name of the user performing the download. - ''' The user's password. + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + ''' The credentials of the user performing the download. ''' Indicates whether or not to show a progress bar. - ''' - ''' Time allotted before giving up on a connection. - ''' + ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten - ''' if local file already exists. + ''' Indicates whether or not the file should be overwritten if local file already exists. ''' ''' - ''' Indicates what to do if user cancels dialog (either or do nothing). + ''' Indicates what to do if user cancels dialog (either throw or do nothing). ''' + ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - userName As String, - password As String, + networkCredentials As ICredentials, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean, onUserCancel As UICancelOption) - ' Get network credentials - Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + If connectionTimeout <= 0 Then + Throw VbUtils.GetArgumentExceptionWithArgName( + argumentName:=NameOf(connectionTimeout), + resourceKey:=SR.Network_BadConnectionTimeout) + End If - DownloadFile( - address, - destinationFileName, - networkCredentials, - showUI, - connectionTimeout, - overwrite, - onUserCancel) + If address Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(address)) + End If + + Dim dialog As ProgressDialog = Nothing + Try + dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + Dim t As Task = DownloadFileAsync( + addressUri:=address, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If + End If + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub ''' ''' Downloads a file from the network to the specified path. ''' - ''' to the remote file. - ''' - ''' Name and path of file where download is saved. - ''' - ''' - ''' The credentials of the user performing the download. - ''' + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten - ''' if local file already exists. + ''' Indicates whether or not the file should be overwritten if local file already exists. ''' - ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - networkCredentials As ICredentials, + userName As String, + password As String, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean) - DownloadFile( - address, - destinationFileName, - networkCredentials, - showUI, - connectionTimeout, - overwrite, - UICancelOption.ThrowException) + If address Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(address)) + End If + + Dim dialog As ProgressDialog = Nothing + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + Try + dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + Dim t As Task = DownloadFileAsync( + addressUri:=address, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel:=UICancelOption.ThrowException) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If + End If + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub ''' ''' Downloads a file from the network to the specified path. ''' - ''' to the remote file. + ''' Uri to the remote file. ''' Name and path of file where download is saved. - ''' The of the user performing the download. + ''' The name of the user performing the download. + ''' The user's password. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten - ''' if local file already exists. + ''' Indicates whether or not the file should be overwritten if local file already exists. ''' ''' - ''' Indicates what to do if user cancels dialog (either or do nothing). + ''' Indicates what to do if user cancels dialog (either throw or do nothing). ''' - ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - networkCredentials As ICredentials, + userName As String, + password As String, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean, onUserCancel As UICancelOption) If connectionTimeout <= 0 Then - Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + Throw VbUtils.GetArgumentExceptionWithArgName( + argumentName:=NameOf(connectionTimeout), + resourceKey:=SR.Network_BadConnectionTimeout) End If If address Is Nothing Then Throw VbUtils.GetArgumentNullException(NameOf(address)) End If - Using client As New WebClientExtended - client.Timeout = connectionTimeout - - ' Don't use passive mode if we're showing UI - client.UseNonPassiveFtp = showUI - - 'Construct the local file. This will validate the full name and path - Dim fullFilename As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) + ' Get network credentials + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really - ' have a file and path - If IO.Directory.Exists(fullFilename) Then - Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + Dim dialog As ProgressDialog = Nothing + Try + dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + Dim t As Task = DownloadFileAsync( + addressUri:=address, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception + End If End If - - 'Throw if the file exists and the user doesn't want to overwrite - If IO.File.Exists(fullFilename) And Not overwrite Then - Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) - End If - - ' Set credentials if we have any - If networkCredentials IsNot Nothing Then - client.Credentials = networkCredentials - End If - - Dim dialog As ProgressDialog = GetProgressDialog(address.AbsolutePath, fullFilename, showUI) - - 'Check to see if the target directory exists. If it doesn't, create it - Dim targetDirectory As String = IO.Path.GetDirectoryName(fullFilename) - - ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect - If String.IsNullOrEmpty(targetDirectory) Then - Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException End If + Throw + Finally + CloseProgressDialog(dialog) + End Try + End Sub - If Not IO.Directory.Exists(targetDirectory) Then - IO.Directory.CreateDirectory(targetDirectory) + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + Public Sub DownloadFile(address As Uri, destinationFileName As String) + Try + + DownloadFileAsync( + addressUri:=address, + destinationFileName, + userName:=DEFAULT_USERNAME, + password:=DEFAULT_PASSWORD, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException End If + Throw + End Try + End Sub - 'Create the copier - Dim copier As New WebClientCopy(client, dialog) - - 'Download the file - copier.DownloadFile(address, fullFilename) + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + Public Sub DownloadFile( + address As Uri, + destinationFileName As String, + userName As String, + password As String) - 'Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then - If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then - Throw New OperationCanceledException() - End If + Try + DownloadFileAsync( + addressUri:=address, + destinationFileName, + userName, + password, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException End If - - End Using - + Throw + End Try End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb new file mode 100644 index 00000000000..155b2e9f196 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -0,0 +1,321 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.Net +Imports System.Net.Http +Imports System.Threading +Imports Microsoft.VisualBasic.CompilerServices +Imports Microsoft.VisualBasic.FileIO +Imports Microsoft.VisualBasic.MyServices.Internal + +Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils + +Namespace Microsoft.VisualBasic.Devices + + Partial Public Class Network + + ''' + ''' Sends and receives a packet to and from the passed in Uri. + ''' Maps older networkCredentials to HttpClientHandler. + ''' + ''' Uri to the remote file. + ''' Name and path of file where download is saved. + ''' The credentials of the user performing the download. + ''' A ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' + Friend Shared Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + networkCredentials As ICredentials, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + onUserCancel As UICancelOption, + Optional cancelToken As CancellationToken = Nothing) As Task + + Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials} + ) + Return DownloadFileAsync(addressUri, + destinationFileName, + clientHandler, + dialog, + connectionTimeout, + overwrite, + onUserCancel, + cancelToken) + End Function + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + ''' A ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + Friend Shared Async Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + userName As String, + password As String, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + Optional cancelToken As CancellationToken = Nothing) As Task + + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + + Await DownloadFileAsync( + addressUri, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel:=UICancelOption.ThrowException, + cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + End Function + +#If False Then ' This is here of API review determains its needed + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + ''' ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + Friend Shared Async Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + userName As String, + password As String, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + onUserCancel As UICancelOption, + Optional cancelToken As CancellationToken = Nothing) As Task + + ' Get network credentials + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + + Await DownloadFileAsync( + addressUri, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel + cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + End Function +#End If + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file + ''' Name and path of file where download is saved. + ''' An HttpClientHandler of the user performing the download. + ''' Progress Dialog. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Calls to all the other overloads will come through here. + Friend Shared Async Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + clientHandler As HttpClientHandler, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + onUserCancel As UICancelOption, + cancelToken As CancellationToken) As Task + + If cancelToken = Nothing Then + cancelToken = New CancellationTokenSource().Token + End If + + + If connectionTimeout <= 0 Then + Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + End If + + If addressUri Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(addressUri)) + End If + + Dim client As HttpClient = If(clientHandler Is Nothing, + New HttpClient(), + New HttpClient(clientHandler) + ) + + ' Set credentials if we have any + client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) + + 'Construct the local file. This will validate the full name and path + Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) + ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really + ' have a file and path + If IO.Directory.Exists(normalizedFilePath) Then + Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + End If + + 'Throw if the file exists and the user doesn't want to overwrite + If Not overwrite AndAlso IO.File.Exists(normalizedFilePath) Then + Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) + End If + + 'Check to see if the target directory exists. If it doesn't, create it + Dim targetDirectory As String = IO.Path.GetDirectoryName(normalizedFilePath) + + ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect + If String.IsNullOrEmpty(targetDirectory) Then + Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + End If + + If Not IO.Directory.Exists(targetDirectory) Then + IO.Directory.CreateDirectory(targetDirectory) + End If + + 'Create the copier + Dim copier As New HttpClientCopy(client, dialog) + + 'Download the file + Try + Await copier.DownloadFileWorkerAsync( + addressUri, + normalizedFilePath, + externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + Catch ex As Exception + If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then + Throw + End If + End Try + + End Function + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Address to the remote file, http, ftp etc... + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + ''' A ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + Friend Shared Async Function DownloadFileAsync( + address As String, + destinationFileName As String, + userName As String, + password As String, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + Optional cancelToken As CancellationToken = Nothing) As Task + + Await DownloadFileAsync( + address, + destinationFileName, + userName, + password, + dialog, + connectionTimeout, + overwrite, + onUserCancel:=UICancelOption.ThrowException, + cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + End Function + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Address to the remote file, http, ftp etc... + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + ''' A ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + Friend Shared Async Function DownloadFileAsync( + address As String, + destinationFileName As String, + userName As String, + password As String, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + onUserCancel As UICancelOption, + Optional cancelToken As CancellationToken = Nothing) As Task + + If String.IsNullOrWhiteSpace(address) Then + Throw VbUtils.GetArgumentNullException(NameOf(address)) + End If + + Dim addressUri As Uri = GetUri(address.Trim()) + + ' Get network credentials + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + + Await DownloadFileAsync( + addressUri, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel, + cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + End Function + + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file + ''' Name and path of file where download is saved. + ''' The credentials of the user performing the download. + ''' A ProgressDialog or Nothing. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' + ''' Function will Throw on unhandled exceptions. + ''' + Friend Shared Async Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + networkCredentials As ICredentials, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + Optional cancelToken As CancellationToken = Nothing) As Task + + Await DownloadFileAsync( + addressUri, + destinationFileName, + networkCredentials, + dialog, + connectionTimeout, + overwrite, + onUserCancel:=UICancelOption.ThrowException, + cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + End Function + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.Ping.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.Ping.vb index 2d149593d59..108fd13ee4e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.Ping.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.Ping.vb @@ -8,13 +8,13 @@ Namespace Microsoft.VisualBasic.Devices Partial Public Class Network - 'Size of Ping.exe buffer + ' Size of Ping.exe buffer Private Const BUFFER_SIZE As Integer = 32 ' Default timeout for Ping Private Const DEFAULT_PING_TIMEOUT As Integer = 1000 - 'Holds the buffer for pinging. We lazy initialize on first use + ' Holds the buffer for pinging. We lazy initialize on first use Private _pingBuffer() As Byte ''' @@ -26,9 +26,10 @@ Namespace Microsoft.VisualBasic.Devices If _pingBuffer Is Nothing Then ReDim _pingBuffer(BUFFER_SIZE - 1) For i As Integer = 0 To BUFFER_SIZE - 1 - 'This is the same logic Ping.exe uses to fill it's buffer - Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture - _pingBuffer(i) = Convert.ToByte(Asc("a"c) + (i Mod 23), provider) + ' This is the same logic Ping.exe uses to fill it's buffer + _pingBuffer(i) = Convert.ToByte( + value:=Asc("a"c) + (i Mod 23), + provider:=Globalization.CultureInfo.InvariantCulture) Next End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index f012efecd59..40ea15623a6 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -264,7 +264,7 @@ Namespace Microsoft.VisualBasic.Devices sourceFileName = FileSystemUtils.NormalizeFilePath(sourceFileName, NameOf(sourceFileName)) - 'Make sure the file exists + ' Make sure the file exists If Not IO.File.Exists(sourceFileName) Then Dim message As String = GetResourceString(SR.IO_FileNotFound_Path, sourceFileName) Throw New IO.FileNotFoundException(message) @@ -299,13 +299,13 @@ Namespace Microsoft.VisualBasic.Devices } End If - 'Create the copier + ' Create the copier Dim copier As New WebClientCopy(client, dialog) - 'Download the file + ' Download the file copier.UploadFile(sourceFileName, address) - 'Handle a dialog cancel + ' Handle a dialog cancel If showUI AndAlso Environment.UserInteractive Then If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then Throw New OperationCanceledException() diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index fec0451ee24..1458e5fcefe 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -83,7 +83,9 @@ Namespace Microsoft.VisualBasic.Devices ''' We also use this function to validate the UriString (remote file address). ''' ''' The remote file address. - ''' A if successful, otherwise it throws an . + ''' + ''' A if successful, otherwise it throws an . + ''' Friend Function GetUri(address As String) As Uri Try Return New Uri(address) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index 2a747f9d266..0b1c5913ace 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -10,7 +10,7 @@ Imports System.Windows.Forms Namespace Microsoft.VisualBasic.MyServices ''' - ''' A class that wraps System.Windows.Forms.Clipboard so that + ''' A class that wraps so that ''' a clipboard can be instanced. ''' @@ -23,22 +23,23 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Removes everything from the clipboard. + ''' Removes all data from the . ''' Public Sub Clear() Clipboard.Clear() End Sub ''' - ''' Indicates whether or not there's an audio stream saved to the clipboard. + ''' Indicates whether there is data on the in the format./>. ''' - ''' if an audio stream is available, otherwise . + ''' if an audio is available, otherwise . Public Function ContainsAudio() As Boolean Return Clipboard.ContainsAudio() End Function ''' - ''' Indicates whether or not there is data on the clipboard in the passed in format. + ''' Indicates whether or not there is data on the in the passed in format + ''' or can be converted to that format. ''' ''' ''' if there's data in the passed in format, otherwise . @@ -47,7 +48,8 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not a file drop list has been saved to the clipboard. + ''' Indicates whether there is data on the that is in the + ''' format or can be converted to that format. ''' ''' if a file drop list is available, otherwise . Public Function ContainsFileDropList() As Boolean @@ -55,7 +57,8 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicate whether or not an image has been saved to the clipboard. + ''' Indicates whether there Is data on the that Is in the + ''' format or can be converted to that format. ''' ''' if an image is available, otherwise . Public Function ContainsImage() As Boolean @@ -63,7 +66,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard. + ''' Indicates whether there is text data on the in format. ''' ''' if text is available, otherwise . Public Function ContainsText() As Boolean @@ -71,8 +74,8 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard in - ''' the passed in format. + ''' Indicates whether there is text data on the in the format indicated by the + ''' specified value. ''' ''' The type of text being checked for. ''' if text is available, otherwise . @@ -81,15 +84,15 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an audio stream from the clipboard. + ''' Retrieves an audio stream from the . ''' - ''' The audio stream as a Stream. + ''' The audio stream as a . Public Function GetAudioStream() As Stream Return Clipboard.GetAudioStream() End Function ''' - ''' Gets data from the clipboard that's been saved in the passed in format. + ''' Gets data from the that's been saved in the passed in format. ''' ''' The type of data being sought. ''' The data. @@ -98,9 +101,9 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets a from the clipboard. + ''' Retrieves the data that is currently on the system . ''' - ''' The data object. + ''' The . ''' This gives the ability to save an object in multiple formats. Public Function GetDataObject() As IDataObject @@ -108,7 +111,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets a file drop list from the clipboard. + ''' Retrieves a collection of file names from the . ''' ''' The list of file paths as a . Public Function GetFileDropList() As StringCollection @@ -116,32 +119,33 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an Image from the clipboard. + ''' Retrieves an from the . ''' - ''' The image. + ''' Public Function GetImage() As Image Return Clipboard.GetImage() End Function ''' - ''' Gets text from the clipboard. + ''' Retrieves text data from the in the format. ''' - ''' The text as a String. + ''' The text as a . Public Function GetText() As String Return Clipboard.GetText() End Function ''' - ''' Gets text from the clipboard saved in the passed in format. + ''' Retrieves text data from the in the format indicated by the specified + ''' value. ''' ''' The type of text to get. - ''' The text as a String. + ''' The text as a . Public Function GetText(format As TextDataFormat) As String Return Clipboard.GetText(format) End Function ''' - ''' Saves the passed in audio byte array to the clipboard. + ''' Clears the and then adds data in the format. ''' ''' The byte array to be saved. Public Sub SetAudio(audioBytes As Byte()) @@ -149,15 +153,15 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in audio stream to the clipboard. + ''' Clears the and then adds data in the format. ''' - ''' The stream to be saved. + ''' The to be saved. Public Sub SetAudio(audioStream As Stream) Clipboard.SetAudio(audioStream) End Sub ''' - ''' Saves the passed in data to the clipboard in the passed in format. + ''' Clears the and then adds data in the specified format. ''' ''' The format in which to save the data. ''' The data to be saved. @@ -166,9 +170,9 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves a to the clipboard. + ''' Places nonpersistent on the . ''' - ''' The data object to be saved. + ''' The to be saved. ''' This gives the ability to save an object in multiple formats. Public Sub SetDataObject(data As DataObject) @@ -176,7 +180,8 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in file drop list to the clipboard. + ''' Clears the > and then adds a collection of file names + ''' in the format. ''' ''' The file drop list as a . Public Sub SetFileDropList(filePaths As StringCollection) @@ -192,7 +197,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in String to the clipboard. + ''' Clears the Clipboard and then adds text data in the format. ''' ''' The to save. Public Sub SetText(text As String) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb new file mode 100644 index 00000000000..11f6da8e967 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -0,0 +1,190 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports System.Net.Http +Imports System.Threading +Imports Microsoft.VisualBasic.Devices.NetworkUtilities + +Namespace Microsoft.VisualBasic.MyServices.Internal + + ''' + ''' Class that controls the thread that does the actual work of downloading or uploading. + ''' + Friend NotInheritable Class HttpClientCopy + + ' Dialog shown if user wants to see progress UI. Allows the user to cancel the file transfer. + Private WithEvents _progressDialog As ProgressDialog + + ' The WebClient performs the downloading or uploading operations for us + Private ReadOnly _httpClient As HttpClient + + Private _cancelTokenSourceGet As CancellationTokenSource + Private _cancelTokenSourceRead As CancellationTokenSource + Private _cancelTokenSourceReadStream As CancellationTokenSource + Private _cancelTokenSourceWrite As CancellationTokenSource + + ' The percentage of the operation completed + Private _percentage As Integer + + ' Used for invoking ProgressDialog.Increment + Private Delegate Sub DoIncrement(Increment As Integer) + + ''' + ''' Creates an instance of a HttpClientCopy, used to download or upload a file. + ''' + ''' The HttpClient used to do the downloading or uploading. + ''' UI for indicating progress. + Public Sub New(client As HttpClient, dialog As ProgressDialog) + + Debug.Assert(client IsNot Nothing, "No HttpClient") + + _httpClient = client + _progressDialog = dialog + If _progressDialog IsNot Nothing Then + AddHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel + End If + + End Sub + + ''' + ''' Notifies the progress dialog to increment the progress bar. + ''' + ''' The percentage of bytes read. + Private Sub InvokeIncrement(progressPercentage As Integer) + ' Don't invoke unless dialog is up and running + If _progressDialog IsNot Nothing Then + If _progressDialog.IsHandleCreated Then + + ' For performance, don't invoke if increment is 0 + Dim increment As Integer = progressPercentage - _percentage + _percentage = progressPercentage + If increment > 0 Then + _progressDialog.BeginInvoke(New DoIncrement(AddressOf _progressDialog.Increment), increment) + End If + + End If + End If + End Sub + + ''' + ''' If the user clicks cancel on the Progress dialog, we need to cancel + ''' the current async file transfer operation. + ''' + ''' + ''' Note that we don't want to close the progress dialog here. Wait until + ''' the actual file transfer cancel event comes through and do it there. + ''' + Private Sub _progressDialog_UserHitCancel() + 'cancel the upload/download transfer. We'll close the ProgressDialog as soon as the HttpClient cancels the xfer. + _cancelTokenSourceGet.Cancel() + _cancelTokenSourceRead.Cancel() + _cancelTokenSourceReadStream.Cancel() + _cancelTokenSourceWrite.Cancel() + End Sub + + ''' + ''' Downloads a file. + ''' + ''' The source for the file. + ''' The path and name where the file is to be saved. + Friend Async Function DownloadFileWorkerAsync(addressUri As Uri, normalizedFilePath As String, externalToken As CancellationToken) As Task + Debug.Assert(_httpClient IsNot Nothing, "No HttpClient") + Debug.Assert(addressUri IsNot Nothing, "No address") + Debug.Assert((Not String.IsNullOrWhiteSpace(normalizedFilePath)) AndAlso Directory.Exists(Path.GetDirectoryName(Path.GetFullPath(normalizedFilePath))), "Invalid path") + + + Dim response As HttpResponseMessage = Nothing + + _cancelTokenSourceGet = New CancellationTokenSource() + Using linkedCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceGet.Token, externalToken) + Try + response = Await _httpClient.GetAsync(addressUri, HttpCompletionOption.ResponseHeadersRead, _cancelTokenSourceGet.Token).ConfigureAwait(False) + Catch ex As TaskCanceledException + If ex.CancellationToken = externalToken Then + externalToken.ThrowIfCancellationRequested() + ElseIf ex.CancellationToken = _cancelTokenSourceGet.Token Then + ' a real cancellation, triggered by the caller + Throw + Else + Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) + End If + End Try + End Using + Select Case response.StatusCode + Case HttpStatusCode.OK + _cancelTokenSourceRead = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) + _cancelTokenSourceReadStream = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) + _cancelTokenSourceWrite = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) + Dim contentLength? As Long = response?.Content.Headers.ContentLength + If contentLength.HasValue Then + Using responseStream As Stream = Await response.Content.ReadAsStreamAsync(_cancelTokenSourceReadStream.Token).ConfigureAwait(False) + Using fileStream As New FileStream(normalizedFilePath, FileMode.Create, FileAccess.Write, FileShare.None) + + Dim buffer(8191) As Byte + Dim totalBytesRead As Long = 0 + Dim bytesRead As Integer + Try + bytesRead = Await responseStream.ReadAsync(buffer.AsMemory(0, buffer.Length), _cancelTokenSourceRead.Token).ConfigureAwait(False) + Do While bytesRead > 0 + totalBytesRead += bytesRead + + Await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead), _cancelTokenSourceWrite.Token).ConfigureAwait(False) + If _progressDialog IsNot Nothing Then + Dim percentage As Integer = CInt(totalBytesRead / contentLength.Value * 100) + InvokeIncrement(percentage) + End If + bytesRead = Await responseStream.ReadAsync(buffer.AsMemory(0, buffer.Length), _cancelTokenSourceRead.Token).ConfigureAwait(False) + Loop + Finally + CloseProgressDialog(_progressDialog) + End Try + End Using + End Using + If response.StatusCode = HttpStatusCode.OK Then + End If + + If _progressDialog IsNot Nothing Then + RemoveHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel + End If + End If + + Case HttpStatusCode.Unauthorized + Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case Else + Throw New WebException() + End Select + response?.Dispose() + End Function + +#If False Then + ''' + ''' Uploads a file + ''' + ''' The name and path of the source file + ''' The address to which the file is uploaded + Public Sub UploadFile(sourceFileName As String, address As Uri) + Debug.Assert(_httpClient IsNot Nothing, "No WebClient") + Debug.Assert(address IsNot Nothing, "No address") + Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) AndAlso File.Exists(sourceFileName), "Invalid file") + + ' If we have a dialog we need to set up an async download + If _progressDialog IsNot Nothing Then + _httpClient.UploadFileAsync(address, sourceFileName) + _progressDialog.ShowProgressDialog() 'returns when the download sequence is over, whether due to success, error, or being canceled + Else + _httpClient.UploadFile(address, sourceFileName) + End If + + 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. + If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then + If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then + Throw _exceptionEncounteredDuringFileTransfer + End If + End If + End Sub +#End If + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index 203d72a44d2..54a9396cb18 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -17,7 +17,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ' The WebClient performs the downloading or uploading operations for us Private WithEvents _webClient As WebClient - 'Keeps track of the error that happened during upload/download so we can throw it once we can guarantee we are back on the main thread + ' Keeps track of the error that happened during upload/download so we can throw it + ' once we can guarantee we are back on the main thread Private _exceptionEncounteredDuringFileTransfer As Exception ' The percentage of the operation completed @@ -47,46 +48,11 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ''' the actual file transfer cancel event comes through and do it there. ''' Private Sub _progressDialog_UserHitCancel() Handles _progressDialog.UserHitCancel - 'cancel the upload/download transfer. We'll close the ProgressDialog - 'as soon as the WebClient cancels the xfer. + ' Cancel the upload/download transfer. We'll close the ProgressDialog + ' as soon as the WebClient cancels the xfer. _webClient.CancelAsync() End Sub - ''' - ''' Handles the WebClient's DownloadFileCompleted event. - ''' - ''' - ''' - Private Sub _webClient_DownloadFileCompleted(sender As Object, e As ComponentModel.AsyncCompletedEventArgs) _ - Handles _webClient.DownloadFileCompleted - - Try - ' If the download was interrupted by an exception, keep track of the exception, - ' which we'll throw from the main thread - If e.Error IsNot Nothing Then - _exceptionEncounteredDuringFileTransfer = e.Error - End If - - If Not e.Cancelled AndAlso e.Error Is Nothing Then - InvokeIncrement(100) - End If - Finally - 'We don't close the dialog until we receive the WebClient.DownloadFileCompleted event - CloseProgressDialog(_progressDialog) - End Try - End Sub - - ''' - ''' Handles event WebClient fires whenever progress of download changes. - ''' - ''' - ''' - Private Sub _webClient_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) _ - Handles _webClient.DownloadProgressChanged - - InvokeIncrement(e.ProgressPercentage) - End Sub - ''' ''' Handles the WebClient's UploadFileCompleted event. ''' @@ -95,8 +61,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Private Sub _webClient_UploadFileCompleted(sender As Object, e As UploadFileCompletedEventArgs) _ Handles _webClient.UploadFileCompleted - ' If the upload was interrupted by an exception, keep track of the - ' exception, which we'll throw from the main thread + ' If the upload was interrupted by an exception, keep track of the exception, + ' which we'll throw from the main thread Try If e.Error IsNot Nothing Then _exceptionEncounteredDuringFileTransfer = e.Error @@ -105,8 +71,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal InvokeIncrement(100) End If Finally - 'We don't close the dialog until we receive the - 'WebClient.DownloadFileCompleted event + ' We don't close the dialog until we receive the + ' WebClient.DownloadFileCompleted event CloseProgressDialog(_progressDialog) End Try End Sub @@ -145,36 +111,6 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End If End Sub - ''' - ''' Downloads a file. - ''' - ''' The source for the file. - ''' The path and name where the file is saved. - Public Sub DownloadFile(address As Uri, destinationFileName As String) - Debug.Assert(_webClient IsNot Nothing, $"No {NameOf(_webClient)}") - Debug.Assert(address IsNot Nothing, $"No {NameOf(address)}") - Dim path As String = IO.Path.GetDirectoryName(IO.Path.GetFullPath(destinationFileName)) - Debug.Assert((Not String.IsNullOrWhiteSpace(destinationFileName)) _ - AndAlso IO.Directory.Exists(path), $"Invalid {NameOf(path)}") - - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.DownloadFileAsync(address, destinationFileName) - 'returns when the download sequence is over, whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.DownloadFile(address, destinationFileName) - End If - - 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. - If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then - If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then - Throw _exceptionEncounteredDuringFileTransfer - End If - End If - - End Sub - ''' ''' Uploads a file. ''' @@ -189,14 +125,16 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ' If we have a dialog we need to set up an async download If _progressDialog IsNot Nothing Then _webClient.UploadFileAsync(address, sourceFileName) - 'returns when the download sequence is over, whether due to success, error, or being canceled + + ' Returns when the download sequence is over, + ' whether due to success, error, or being canceled _progressDialog.ShowProgressDialog() Else _webClient.UploadFile(address, sourceFileName) End If - 'Now that we are back on the main thread, throw the exception we - 'encountered if the user didn't cancel. + ' Now that we are back on the main thread, throw the exception we + ' encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then Throw _exceptionEncounteredDuringFileTransfer diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx index ff406c3aee6..47f58111e22 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx @@ -1,16 +1,16 @@  - @@ -221,4 +221,10 @@ Environment variable is not defined: '{0}'. + + The network operation has timed out. + + + The network operation is unauthorized. + diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf index f737611c412..9b04fd1c94c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf @@ -177,6 +177,16 @@ Ukládání {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf index e4e2a7e5e3e..4241a9c79fc 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf @@ -177,6 +177,16 @@ {0} wird hochgeladen + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf index 006fe62ea7d..b928124e52c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf @@ -177,6 +177,16 @@ Cargando {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf index 3e06f665f6a..cfa6763c203 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf @@ -177,6 +177,16 @@ Chargement de {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf index cc73272c979..a040a2d48e4 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf @@ -177,6 +177,16 @@ Caricamento di {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf index d9c1b872741..95057a734e3 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf @@ -177,6 +177,16 @@ {0} のアップロード中 + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf index 5546130053f..90a42911b9a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf @@ -177,6 +177,16 @@ {0} 업로드 중 + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf index 35284bb512e..96ece95f46b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf @@ -177,6 +177,16 @@ Przekazywanie: {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf index 1e1b8f1416f..4f87094e257 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf @@ -177,6 +177,16 @@ Carregando {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf index e400e684656..bfa18e769da 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf @@ -177,6 +177,16 @@ Идет отправка {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf index 751c15e6832..42b54249b7b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf @@ -177,6 +177,16 @@ {0} karşıya yükleniyor + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf index af55164f9e7..27bbc168d6f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf @@ -177,6 +177,16 @@ 正在上传 {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf index c3df5ab1fb7..b9e74cf4c97 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf @@ -177,6 +177,16 @@ 正在上傳 {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb index 55967664bd3..801d484af23 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb @@ -18,11 +18,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private Const DownloadSmallFileSize As Integer = 18_135 Private Const InvalidUrlAddress As String = "invalidURL" Private Const TestingConnectionTimeout As Integer = 100_000 - ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, - ' they are not accessible in this project they come from WebClient. - Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." - Private Const SR_net_webstatus_Unauthorized As String = - "The remote server returned an error: (401) Unauthorized." Private Shared Sub CleanUpListener(listener As HttpListener) listener.Stop() @@ -292,7 +287,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -315,8 +310,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Timeout) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -874,7 +869,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -900,7 +895,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -1102,7 +1097,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -1151,7 +1146,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Where(Function(e) e.Message.StartsWith(value)) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -1225,8 +1220,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -1539,7 +1534,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub @@ -1565,7 +1560,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb similarity index 100% rename from src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb rename to src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb From 08be576b8577436d1af5eb522ea3dcdfd388d1d1 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 14 Nov 2024 15:36:04 -0900 Subject: [PATCH 003/119] Sync changes from PR #11867 --- .../WindowsFormsApplicationBase.vb | 55 ++++++++++++------- .../MyServices/Internal/HttpClientCopy.vb | 55 +++++++++++++++---- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb index 2546f49b58e..d2bc36d4ffd 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb @@ -383,7 +383,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices _unhandledExceptionHandlers.Add(value) - ' Only add the listener once so we don't fire the UnHandledException event over and over for the same exception + ' Only add the listener once so we don't fire the + ' UnHandledException event over and over for the same exception If _unhandledExceptionHandlers.Count = 1 Then AddHandler Application.ThreadException, AddressOf OnUnhandledExceptionEventAdaptor End If @@ -632,7 +633,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices _formLoadWaiter = New AutoResetEvent(False) Task.Run(Async Function() As Task - Await _splashScreenCompletionSource.Task.ConfigureAwait(False) + Await _splashScreenCompletionSource.Task.ConfigureAwait(continueOnCapturedContext:=False) _formLoadWaiter.Set() End Function) @@ -654,7 +655,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices If _splashTimer IsNot Nothing Then - 'We only have a timer if there was a minimum timeout on the splash screen. + ' We only have a timer if there was a minimum timeout on the splash screen. _splashTimer.Dispose() _splashTimer = Nothing End If @@ -726,7 +727,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices Protected Sub HideSplashScreen() - 'This ultimately wasn't necessary. I suppose we better keep it for backwards compatibility. + ' This ultimately wasn't necessary. I suppose we better keep it for backwards compatibility. SyncLock _splashLock ' .NET Framework 4.0 (Dev10 #590587) - we now activate the main form before calling @@ -767,7 +768,11 @@ Namespace Microsoft.VisualBasic.ApplicationServices ''' variants was passed in. ''' ''' - ''' Returning True indicates that we should continue on with the application Startup sequence. + ''' + ''' Returning + ''' Indicates that we should continue on with the application Startup sequence. + ''' + ''' ''' ''' This extensibility point is exposed for people who want to override ''' the Startup sequence at the earliest possible point to @@ -868,26 +873,29 @@ Namespace Microsoft.VisualBasic.ApplicationServices End If ' When we have a splash screen that hasn't timed out before the main form is ready to paint, we want to - ' block the main form from painting. To do that I let the form get past the Load() event and hold it until - ' the splash screen goes down. Then I let the main form continue it's startup sequence. The ordering of - ' Form startup events for reference is: Ctor(), Load Event, Layout event, Shown event, Activated event, Paint event. + ' block the main form from painting. To do that I let the form get past the Load() event and + ' hold it until the splash screen goes down. Then I let the main form continue it's startup sequence. + ' The ordering of Form startup events for reference is: + ' Ctor(), Load Event, Layout event, Shown event, Activated event, Paint event. AddHandler MainForm.Load, AddressOf MainFormLoadingDone End If ' Run() eats all exceptions (unless running under the debugger). If the user wrote an ' UnhandledException handler we will hook the System.Windows.Forms.Application.ThreadException event ' (see Public Custom Event UnhandledException) which will raise our UnhandledException Event. - ' If our user didn't write an UnhandledException event, then we land in the try/catch handler for Forms.Application.Run(). + ' If our user didn't write an UnhandledException event, then we land in the try/catch handler + ' for Forms.Application.Run(). Try Application.Run(_appContext) Finally - ' When Run() returns, the context we pushed in our ctor (which was a WindowsFormsSynchronizationContext) - ' is restored. But we are going to dispose it so we need to disconnect the network listener so that it + ' When Run() returns, the context we pushed in our ctor + ' (which was a WindowsFormsSynchronizationContext) is restored. + ' But we are going to dispose it so we need to disconnect the network listener so that it ' can't fire any events in response to changing network availability conditions through a dead context. If _networkObject IsNot Nothing Then _networkObject.DisconnectListener() - 'Restore the prior sync context. + ' Restore the prior sync context. AsyncOperationManager.SynchronizationContext = _appSynchronizationContext _appSynchronizationContext = Nothing End Try @@ -915,11 +923,12 @@ Namespace Microsoft.VisualBasic.ApplicationServices ' The timing is important because the network object has an AsyncOperationsManager in it that marshals ' the network changed event to the main thread. The asyncOperationsManager does a CreateOperation() ' which makes a copy of the executionContext. That execution context shows up on your thread during - ' the callback so I delay creating the network object (and consequently the capturing of the execution context) - ' until the principal has been set on the thread. This avoids the problem where My.User isn't set - ' during the NetworkAvailabilityChanged event. This problem would just extend itself to any future - ' callback that involved the asyncOperationsManager so this is where we need to create objects that - ' have a asyncOperationsContext in them. + ' the callback so I delay creating the network object + ' (and consequently the capturing of the execution context) until the principal has been set on the thread. + ' This avoids the problem where My.User isn't set during the NetworkAvailabilityChanged event. + ' This problem would just extend itself to any future callback that involved + ' the asyncOperationsManager so this is where we need to create objects that have + ' a asyncOperationsContext in them. If _turnOnNetworkListener And _networkObject Is Nothing Then ' The is-nothing-check is to avoid hooking the object more than once. @@ -957,7 +966,10 @@ Namespace Microsoft.VisualBasic.ApplicationServices ''' that execution shouldn't continue. ''' ''' - ''' indicates the exception event was raised / it was not. + ''' + ''' indicates the exception event was raised + ''' it was not. + ''' Protected Overridable Function OnUnhandledException(e As UnhandledExceptionEventArgs) As Boolean @@ -1051,7 +1063,10 @@ Namespace Microsoft.VisualBasic.ApplicationServices Using pipeServer Dim tokenSource As New CancellationTokenSource() #Disable Warning BC42358 ' Call is not awaited. - WaitForClientConnectionsAsync(pipeServer, AddressOf OnStartupNextInstanceMarshallingAdaptor, cancellationToken:=tokenSource.Token) + WaitForClientConnectionsAsync( + pipeServer, + AddressOf OnStartupNextInstanceMarshallingAdaptor, + cancellationToken:=tokenSource.Token) #Enable Warning BC42358 DoApplicationModel() tokenSource.Cancel() @@ -1073,7 +1088,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices Throw New CantStartSingleInstanceException() End Try End If - End If 'Single-Instance application + End If ' Single-Instance application End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index 11f6da8e967..8a38aba6efe 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -77,7 +77,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ''' the actual file transfer cancel event comes through and do it there. ''' Private Sub _progressDialog_UserHitCancel() - 'cancel the upload/download transfer. We'll close the ProgressDialog as soon as the HttpClient cancels the xfer. + ' Cancel the upload/download transfer. We'll close the ProgressDialog + ' as soon as the HttpClient cancels the xfer. _cancelTokenSourceGet.Cancel() _cancelTokenSourceRead.Cancel() _cancelTokenSourceReadStream.Cancel() @@ -92,15 +93,25 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Friend Async Function DownloadFileWorkerAsync(addressUri As Uri, normalizedFilePath As String, externalToken As CancellationToken) As Task Debug.Assert(_httpClient IsNot Nothing, "No HttpClient") Debug.Assert(addressUri IsNot Nothing, "No address") - Debug.Assert((Not String.IsNullOrWhiteSpace(normalizedFilePath)) AndAlso Directory.Exists(Path.GetDirectoryName(Path.GetFullPath(normalizedFilePath))), "Invalid path") + Dim directoryPath As String = Path.GetDirectoryName(Path.GetFullPath(normalizedFilePath)) + Debug.Assert((Not String.IsNullOrWhiteSpace(normalizedFilePath)) AndAlso + Directory.Exists(directoryPath), "Invalid path") + _cancelTokenSourceGet = New CancellationTokenSource() + _cancelTokenSourceRead = New CancellationTokenSource() + _cancelTokenSourceReadStream = New CancellationTokenSource() + _cancelTokenSourceWrite = New CancellationTokenSource() Dim response As HttpResponseMessage = Nothing _cancelTokenSourceGet = New CancellationTokenSource() Using linkedCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceGet.Token, externalToken) Try - response = Await _httpClient.GetAsync(addressUri, HttpCompletionOption.ResponseHeadersRead, _cancelTokenSourceGet.Token).ConfigureAwait(False) + response = Await _httpClient.GetAsync( + requestUri:=addressUri, + completionOption:=HttpCompletionOption.ResponseHeadersRead, + cancellationToken:=_cancelTokenSourceGet.Token).ConfigureAwait(continueOnCapturedContext:=False) + Catch ex As TaskCanceledException If ex.CancellationToken = externalToken Then externalToken.ThrowIfCancellationRequested() @@ -119,23 +130,41 @@ Namespace Microsoft.VisualBasic.MyServices.Internal _cancelTokenSourceWrite = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) Dim contentLength? As Long = response?.Content.Headers.ContentLength If contentLength.HasValue Then - Using responseStream As Stream = Await response.Content.ReadAsStreamAsync(_cancelTokenSourceReadStream.Token).ConfigureAwait(False) - Using fileStream As New FileStream(normalizedFilePath, FileMode.Create, FileAccess.Write, FileShare.None) + Using responseStream As Stream = Await response.Content.ReadAsStreamAsync( + cancellationToken:=_cancelTokenSourceReadStream.Token). + ConfigureAwait(continueOnCapturedContext:=False) + + Using fileStream As New FileStream( + path:=normalizedFilePath, + mode:=FileMode.Create, + access:=FileAccess.Write, + share:=FileShare.None) Dim buffer(8191) As Byte Dim totalBytesRead As Long = 0 Dim bytesRead As Integer Try - bytesRead = Await responseStream.ReadAsync(buffer.AsMemory(0, buffer.Length), _cancelTokenSourceRead.Token).ConfigureAwait(False) + bytesRead = Await responseStream.ReadAsync( + buffer:=buffer.AsMemory(start:=0, buffer.Length), + cancellationToken:=_cancelTokenSourceRead.Token). + ConfigureAwait(continueOnCapturedContext:=False) + Do While bytesRead > 0 totalBytesRead += bytesRead - Await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead), _cancelTokenSourceWrite.Token).ConfigureAwait(False) + Await fileStream.WriteAsync( + buffer:=buffer.AsMemory(0, bytesRead), + cancellationToken:=_cancelTokenSourceWrite.Token). + ConfigureAwait(continueOnCapturedContext:=False) + If _progressDialog IsNot Nothing Then Dim percentage As Integer = CInt(totalBytesRead / contentLength.Value * 100) InvokeIncrement(percentage) End If - bytesRead = Await responseStream.ReadAsync(buffer.AsMemory(0, buffer.Length), _cancelTokenSourceRead.Token).ConfigureAwait(False) + bytesRead = Await responseStream.ReadAsync( + buffer:=buffer.AsMemory(start:=0, buffer.Length), + cancellationToken:=_cancelTokenSourceRead.Token). + ConfigureAwait(continueOnCapturedContext:=False) Loop Finally CloseProgressDialog(_progressDialog) @@ -158,7 +187,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal response?.Dispose() End Function -#If False Then +#If False Then ' Future code to implement upload using Http ''' ''' Uploads a file ''' @@ -167,17 +196,19 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Public Sub UploadFile(sourceFileName As String, address As Uri) Debug.Assert(_httpClient IsNot Nothing, "No WebClient") Debug.Assert(address IsNot Nothing, "No address") - Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) AndAlso File.Exists(sourceFileName), "Invalid file") + Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) AndAlso + File.Exists(sourceFileName), "Invalid file") ' If we have a dialog we need to set up an async download If _progressDialog IsNot Nothing Then _httpClient.UploadFileAsync(address, sourceFileName) - _progressDialog.ShowProgressDialog() 'returns when the download sequence is over, whether due to success, error, or being canceled + ' returns when the download sequence is over, whether due to success, error, or being canceled + _progressDialog.ShowProgressDialog() Else _httpClient.UploadFile(address, sourceFileName) End If - 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. + ' Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then Throw _exceptionEncounteredDuringFileTransfer From 24551f194243917ab272d57b9ebab0efd3db78da Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 14 Nov 2024 22:55:18 -0900 Subject: [PATCH 004/119] Simplify code --- .../Devices/Network.DownloadFile.vb | 19 ++++++++----------- .../ApplicationServicesExceptionsTests.vb | 4 ++-- .../Windows/Forms/UserTests.UserPrincipal.vb | 7 +------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 8da1972b718..33b6f595b50 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -117,17 +117,14 @@ Namespace Microsoft.VisualBasic.Devices End If Catch ex As Exception If ex.InnerException IsNot Nothing Then - If TryCast(ex.InnerException, OperationCanceledException) IsNot Nothing AndAlso - Environment.UserInteractive Then - - If showUI AndAlso Environment.UserInteractive Then - Try - IO.File.Delete(destinationFileName) - Catch - ' ignore error - End Try - Throw New OperationCanceledException() - End If + If TryCast(ex.InnerException, OperationCanceledException) IsNot Nothing _ + AndAlso Environment.UserInteractive Then + + Try + IO.File.Delete(destinationFileName) + Catch + ' ignore error + End Try End If Throw ex.InnerException diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ApplicationServicesExceptionsTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ApplicationServicesExceptionsTests.vb index 22c08968981..8a299c6934a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ApplicationServicesExceptionsTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ApplicationServicesExceptionsTests.vb @@ -18,7 +18,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub NewCantStartSingleInstanceException() - Dim ex As Exception = New CantStartSingleInstanceException() + Dim ex As New CantStartSingleInstanceException() Dim expected As String = VbUtils.GetResourceString(SR.AppModel_SingleInstanceCantConnect) ex.Message.Should.Be(expected) @@ -35,7 +35,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub NewNoStartupFormException() - Dim ex As Exception = New NoStartupFormException() + Dim ex As New NoStartupFormException() Dim expected As String = VbUtils.GetResourceString(SR.AppModel_NoStartupForm) ex.Message.Should.Be(expected) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UserTests.UserPrincipal.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UserTests.UserPrincipal.vb index 3ea9f2d844e..28dd79d6c35 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UserTests.UserPrincipal.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UserTests.UserPrincipal.vb @@ -11,18 +11,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests Implements IPrincipal Private ReadOnly _role As String - Private ReadOnly _userIdentity As IIdentity Public Sub New(authenticationType As String, name As String, isAuthenticated As Boolean, role As String) - _userIdentity = New UserIdentity(authenticationType, name, isAuthenticated) + Identity = New UserIdentity(authenticationType, name, isAuthenticated) _role = role End Sub Public ReadOnly Property Identity As IIdentity Implements IPrincipal.Identity - Get - Return _userIdentity - End Get - End Property Public Function IsInRole(role As String) As Boolean Implements IPrincipal.IsInRole Return role = _role From 17829151d281219e0c36e8ffcc713b5f76f081a5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 24 Nov 2024 15:23:16 -0800 Subject: [PATCH 005/119] Rename NetworkTests to DownloadTests --- .../Forms/{NetworkDownloadTests.vb => DownloadFileTests.vb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/{NetworkDownloadTests.vb => DownloadFileTests.vb} (99%) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb similarity index 99% rename from src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb rename to src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 55967664bd3..983b28d2585 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -9,7 +9,7 @@ Imports Xunit Namespace Microsoft.VisualBasic.Forms.Tests - Public Class NetworkTests + Public Class DownloadFileTests Inherits VbFileCleanupTestBase Private Const DefaultPassword As String = NameOf(DefaultPassword) From 69a4b602934536e36277178aface44dd57d19f0b Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 24 Nov 2024 15:25:19 -0800 Subject: [PATCH 006/119] Rename NetworkDownloadTests to DownloadFileTests --- .../Forms/{NetworkDownloadTests.vb => DownloadFileTests.vb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/{NetworkDownloadTests.vb => DownloadFileTests.vb} (99%) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb similarity index 99% rename from src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb rename to src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 801d484af23..655aa73e7ce 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/NetworkDownloadTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -9,7 +9,7 @@ Imports Xunit Namespace Microsoft.VisualBasic.Forms.Tests - Public Class NetworkTests + Public Class DownloadFileTests Inherits VbFileCleanupTestBase Private Const DefaultPassword As String = NameOf(DefaultPassword) From 2434e293e39b6153417624978a98457b3b87548c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 24 Nov 2024 16:04:59 -0800 Subject: [PATCH 007/119] Simplify DownloadFileAsync and improve code coverage --- .../VisualBasic/Devices/Network.DownloadFileAsync.vb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index 155b2e9f196..6cbf6ff8af9 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -84,8 +84,6 @@ Namespace Microsoft.VisualBasic.Devices cancelToken).ConfigureAwait(continueOnCapturedContext:=False) End Function -#If False Then ' This is here of API review determains its needed - ''' ''' Downloads a file from the network to the specified path. ''' @@ -118,10 +116,9 @@ Namespace Microsoft.VisualBasic.Devices dialog, connectionTimeout, overwrite, - onUserCancel + onUserCancel, cancelToken).ConfigureAwait(continueOnCapturedContext:=False) End Function -#End If ''' ''' Downloads a file from the network to the specified path. @@ -149,7 +146,6 @@ Namespace Microsoft.VisualBasic.Devices cancelToken = New CancellationTokenSource().Token End If - If connectionTimeout <= 0 Then Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) End If From 2d9209641f3962b8c885ebd24b9cde5a5df61c37 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 14:47:38 -0800 Subject: [PATCH 008/119] Simplify dialog cancel Cleanup --- .../VisualBasic/Devices/Network.DownloadFile.vb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 37b4776aa91..cee291870c4 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -125,7 +125,7 @@ Namespace Microsoft.VisualBasic.Devices showUI, connectionTimeout, overwrite, - UICancelOption.ThrowException) + onUserCancel:=UICancelOption.ThrowException) End Sub ''' @@ -286,7 +286,6 @@ Namespace Microsoft.VisualBasic.Devices connectionTimeout, overwrite, UICancelOption.ThrowException) - End Sub ''' @@ -368,14 +367,13 @@ Namespace Microsoft.VisualBasic.Devices copier.DownloadFile(address, fullFilename) 'Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then - If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then - Throw New OperationCanceledException() - End If + If showUI _ + AndAlso Environment.UserInteractive _ + AndAlso onUserCancel = UICancelOption.ThrowException _ + AndAlso dialog.UserCanceledTheDialog Then + Throw New OperationCanceledException() End If - End Using - End Sub End Class From 63e68f1a9335c3692ff4093509571cc84e3bcdc1 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 14:53:29 -0800 Subject: [PATCH 009/119] Cleanup formatting --- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 33b6f595b50..237e209f05e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -168,10 +168,7 @@ Namespace Microsoft.VisualBasic.Devices End If Dim addressUri As Uri = GetUri(address.Trim()) - - ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - Dim dialog As ProgressDialog = Nothing Try If showUI AndAlso Environment.UserInteractive Then @@ -515,6 +512,5 @@ Namespace Microsoft.VisualBasic.Devices Throw End Try End Sub - End Class End Namespace From 4324b8b934dc8109f372ed7d86d47c3e3b9850ed Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 15:09:59 -0800 Subject: [PATCH 010/119] Improve test coverage for Null or Empty network address --- .../System/Windows/Forms/DownloadFileTests.vb | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 983b28d2585..a2c3a33a48b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -17,13 +17,16 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private Const DownloadLargeFileSize As Integer = 104_857_600 Private Const DownloadSmallFileSize As Integer = 18_135 Private Const InvalidUrlAddress As String = "invalidURL" - Private Const TestingConnectionTimeout As Integer = 100_000 + ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, ' they are not accessible in this project they come from WebClient. Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." + Private Const SR_net_webstatus_Unauthorized As String = "The remote server returned an error: (401) Unauthorized." + Private Const TestingConnectionTimeout As Integer = 100_000 + Private Shared Sub CleanUpListener(listener As HttpListener) listener.Stop() listener.Close() @@ -1230,6 +1233,27 @@ Namespace Microsoft.VisualBasic.Forms.Tests VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWhereAddressIsNothingOrEmpty_Throws(address As String) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Sub + Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) From 91829aa8735316e7790e6522b8433288c4972c92 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 15:21:22 -0800 Subject: [PATCH 011/119] Fix formatting --- .../System/Windows/Forms/DownloadFileTests.vb | 59 +++++++++++++------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 655aa73e7ce..fd2e639fff7 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -735,14 +735,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) End Sub testCode.Should.NotThrow() @@ -1225,6 +1225,27 @@ Namespace Microsoft.VisualBasic.Forms.Tests VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) End Sub + + + Public Sub DownloadFile_UrlWithAllOptionsWhereAddressIsNothingOrEmpty_Throws(address As String) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Sub + Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) @@ -1398,17 +1419,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener(DownloadSmallFileSize) Dim listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub testCode.Should.NotThrow() VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ From df41d1e56a7d4c7d548a7ea3a7ac09bdf008ab74 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 15:45:14 -0800 Subject: [PATCH 012/119] Improve test coverage --- .../Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 237e209f05e..26a8a982387 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -424,16 +424,14 @@ Namespace Microsoft.VisualBasic.Devices Throw VbUtils.GetArgumentNullException(NameOf(address)) End If - ' Get network credentials - Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - Dim dialog As ProgressDialog = Nothing Try dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( addressUri:=address, destinationFileName, - networkCredentials, + userName, + password, dialog, connectionTimeout, overwrite, From ebdc88d9e2df4f899d49f7a5519ce11edebb969e Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 15:56:46 -0800 Subject: [PATCH 013/119] Dispose of HttpListener Move test constants to separate module for future reuse --- .../System/Windows/Forms/DownloadFileTests.vb | 1904 +++++++++-------- .../DownloadFileTestConstants.vb | 13 + 2 files changed, 987 insertions(+), 930 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index a2c3a33a48b..8e37e9b4135 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -12,44 +12,27 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase - Private Const DefaultPassword As String = NameOf(DefaultPassword) - Private Const DefaultUserName As String = NameOf(DefaultUserName) - Private Const DownloadLargeFileSize As Integer = 104_857_600 - Private Const DownloadSmallFileSize As Integer = 18_135 - Private Const InvalidUrlAddress As String = "invalidURL" - ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, ' they are not accessible in this project they come from WebClient. Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." - Private Const SR_net_webstatus_Unauthorized As String = "The remote server returned an error: (401) Unauthorized." - Private Const TestingConnectionTimeout As Integer = 100_000 - - Private Shared Sub CleanUpListener(listener As HttpListener) - listener.Stop() - listener.Close() - End Sub - ''' ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. ''' ''' A Unique directory under the systems Temp directory. ''' The full path and filename of the new file. ''' - Private Shared Sub VerifyAndCleanupFailedDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) - + Private Shared Sub VerifyFailedDownload(testDirectory As String, destinationFileName As String, listener As HttpListener) If Not String.IsNullOrWhiteSpace(testDirectory) Then Directory.Exists(testDirectory).Should.BeTrue() End If If Not String.IsNullOrWhiteSpace(destinationFileName) Then Call New FileInfo(destinationFileName).Exists.Should.BeFalse() End If - CleanUpListener(listener) + listener.Stop() + listener.Close() End Sub ''' @@ -61,19 +44,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' do another FileInfo call. ''' ''' The full path and filename of the new file. + ''' ''' - Private Shared Function VerifyAndCleanupSuccessfulDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) As Long - + Private Shared Function VerifySuccessfulDownload(testDirectory As String, destinationFileName As String, listener As HttpListener) As Long Directory.Exists(testDirectory).Should.BeTrue() Dim fileInfo As New FileInfo(destinationFileName) fileInfo.Exists.Should.BeTrue() Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() ' This directory should not be systems Temp Directory because it must be created Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) - CleanUpListener(listener) + listener.Stop() + listener.Close() Return fileInfo.Length End Function @@ -82,17 +63,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Using - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) End Sub @@ -100,16 +84,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(String.Empty), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(String.Empty), + destinationFileName) + End Sub - testCode.Should.Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -117,16 +102,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(Nothing), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(Nothing), + destinationFileName) + End Sub - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -134,18 +120,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub - testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -156,22 +143,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -180,23 +168,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + End Using End Sub @@ -204,24 +193,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) - testCode.Should() _ - .Throw(Of IOException)() _ - .Where(Function(e) e.Message.Equals(value)) - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Using End Sub @@ -229,22 +219,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadLargeFileSize) + End Using End Sub @@ -252,22 +243,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -280,23 +272,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - Dim networkCredentials As New NetworkCredential(DefaultUserName, password) - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim networkCredentials As New NetworkCredential(DefaultUserName, password) + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -304,23 +297,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=1, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Timeout) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -328,23 +322,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=-1, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -352,23 +347,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -376,21 +372,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(InvalidUrlAddress), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should().Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should().Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -398,22 +395,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=Nothing, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -424,22 +422,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=Nothing, - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=Nothing, + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -450,23 +449,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -477,22 +477,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=0, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.Throw(Of ArgumentException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=0, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -500,21 +501,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(InvalidUrlAddress), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -523,23 +525,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + End Using End Sub @@ -549,49 +552,51 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + End Using End Sub @@ -601,25 +606,26 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -629,27 +635,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -657,23 +664,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -683,24 +691,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -710,27 +719,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -739,23 +749,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -763,48 +774,50 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub Public Sub DownloadFile_UriWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -812,24 +825,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=testDirectory, ' This is a Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .WithMessage(SR.Network_DownloadNeedsFilename) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -840,19 +854,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -865,20 +880,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=String.Empty) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -891,20 +907,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -912,17 +929,18 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -931,18 +949,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address, - destinationFileName) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address, + destinationFileName) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -950,18 +969,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -970,23 +990,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + End Using End Sub @@ -994,24 +1015,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ .Throw(Of IOException)() _ .Where(Function(e) e.Message.Equals(value)) - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Using End Sub @@ -1019,24 +1041,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1044,22 +1067,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadLargeFileSize) + End Using End Sub @@ -1067,22 +1091,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1090,23 +1115,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=1, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Timeout) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1114,23 +1140,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=-1, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1138,24 +1165,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1163,22 +1191,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=Nothing, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1189,23 +1218,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1213,24 +1243,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1258,24 +1289,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + End Using End Sub @@ -1285,27 +1317,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1315,27 +1348,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1343,23 +1377,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1369,24 +1404,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1396,27 +1432,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1425,23 +1462,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1449,24 +1487,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1474,23 +1513,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1498,24 +1538,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=testDirectory, ' This is a Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .WithMessage(SR.Network_DownloadNeedsFilename) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1526,19 +1567,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1551,20 +1593,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=String.Empty) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR_net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1577,20 +1620,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb new file mode 100644 index 00000000000..b21897cac3e --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -0,0 +1,13 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Namespace Microsoft.VisualBasic.Forms.Tests + Public Module DownloadFileTestConstants + Friend Const DefaultPassword As String = NameOf(DefaultPassword) + Friend Const DefaultUserName As String = NameOf(DefaultUserName) + Friend Const DownloadLargeFileSize As Integer = 104_857_600 + Friend Const DownloadSmallFileSize As Integer = 18_135 + Friend Const InvalidUrlAddress As String = "invalidURL" + Friend Const TestingConnectionTimeout As Integer = 100_000 + End Module +End Namespace From 271837041a9f48d33adad488d581c53138dc709d Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 18:26:04 -0800 Subject: [PATCH 014/119] Fix formating --- .../tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 8e37e9b4135..4811a817d3b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -76,7 +76,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) End Using - End Sub From 4002f5496ed342484911a66510ea8f3c9f0d7252 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 19:00:00 -0800 Subject: [PATCH 015/119] Manual merge with tests only --- .../System/Windows/Forms/DownloadFileTests.vb | 1868 +++++++++-------- .../DownloadFileTestConstants.vb | 13 + 2 files changed, 970 insertions(+), 911 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index fd2e639fff7..41cd2067d8e 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -12,25 +12,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase - Private Const DefaultPassword As String = NameOf(DefaultPassword) - Private Const DefaultUserName As String = NameOf(DefaultUserName) - Private Const DownloadLargeFileSize As Integer = 104_857_600 - Private Const DownloadSmallFileSize As Integer = 18_135 - Private Const InvalidUrlAddress As String = "invalidURL" - Private Const TestingConnectionTimeout As Integer = 100_000 - - Private Shared Sub CleanUpListener(listener As HttpListener) - listener.Stop() - listener.Close() - End Sub - ''' ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. ''' ''' A Unique directory under the systems Temp directory. ''' The full path and filename of the new file. ''' - Private Shared Sub VerifyAndCleanupFailedDownload( + Private Shared Sub VerifyFailedDownload( testDirectory As String, destinationFileName As String, listener As HttpListener) @@ -41,7 +29,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests If Not String.IsNullOrWhiteSpace(destinationFileName) Then Call New FileInfo(destinationFileName).Exists.Should.BeFalse() End If - CleanUpListener(listener) + listener.Stop() + listener.Close() End Sub ''' @@ -54,7 +43,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' ''' The full path and filename of the new file. ''' - Private Shared Function VerifyAndCleanupSuccessfulDownload( + Private Shared Function VerifySuccessfulDownload( testDirectory As String, destinationFileName As String, listener As HttpListener) As Long @@ -65,7 +54,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() ' This directory should not be systems Temp Directory because it must be created Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) - CleanUpListener(listener) + listener.Stop() + listener.Close() Return fileInfo.Length End Function @@ -74,17 +64,18 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -92,16 +83,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(String.Empty), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(String.Empty), + destinationFileName) + End Sub - testCode.Should.Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -109,16 +101,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(Nothing), - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(Nothing), + destinationFileName) + End Sub - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -126,18 +119,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -148,22 +142,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -172,23 +167,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Using End Sub @@ -196,24 +192,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ .Throw(Of IOException)() _ .Where(Function(e) e.Message.Equals(value)) - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Using End Sub @@ -221,22 +218,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadLargeFileSize) + End Using End Sub @@ -244,22 +242,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -272,23 +271,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - Dim networkCredentials As New NetworkCredential(DefaultUserName, password) - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -296,23 +295,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=1, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -320,23 +320,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=-1, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -344,23 +345,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -368,21 +370,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(InvalidUrlAddress), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should().Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should().Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -390,22 +393,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=Nothing, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -416,22 +420,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=Nothing, - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=Nothing, + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -442,23 +447,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -469,22 +475,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=0, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.Throw(Of ArgumentException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=0, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -492,21 +499,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(InvalidUrlAddress), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.Throw(Of UriFormatException)() - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(InvalidUrlAddress), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -515,23 +523,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Using End Sub @@ -541,49 +550,51 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.Throw(Of ArgumentNullException)() - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Using End Sub @@ -593,25 +604,26 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -621,27 +633,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -649,23 +662,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -675,24 +689,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -702,27 +717,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -731,23 +747,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -755,24 +772,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -780,23 +798,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -804,24 +823,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=testDirectory, ' This is a Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .WithMessage(SR.Network_DownloadNeedsFilename) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -832,19 +852,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -857,20 +878,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=String.Empty) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -883,20 +905,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -904,17 +927,18 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -923,18 +947,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address, - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile(address, destinationFileName) + End Sub - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -942,18 +965,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName) - End Sub + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile(address:=webListener.Address, destinationFileName) + End Sub - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -962,23 +984,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Using End Sub @@ -986,24 +1009,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) + testCode.Should() _ .Throw(Of IOException)() _ .Where(Function(e) e.Message.Equals(value)) - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) + End Using End Sub @@ -1011,24 +1035,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1036,22 +1061,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadLargeFileSize) + End Using End Sub @@ -1059,22 +1085,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1082,23 +1109,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=1, - overwrite:=True) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1, + overwrite:=True) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1106,23 +1134,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadLargeFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=-1, - overwrite:=False) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1, + overwrite:=False) + End Sub + + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1130,24 +1159,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1155,22 +1185,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=Nothing, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1181,23 +1212,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - networkCredentials, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1205,24 +1237,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=InvalidUrlAddress, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False) - End Sub - - Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=InvalidUrlAddress, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1250,24 +1283,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=Nothing, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=Nothing, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) + End Using End Sub @@ -1277,27 +1311,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1307,27 +1342,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=$"{destinationFileName}{separator}", - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=$"{destinationFileName}{separator}", + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1335,23 +1371,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1361,24 +1398,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1388,27 +1426,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName:=root, ' This is a Root Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName:=root, ' This is a Root Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) - testCode.Should() _ + testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1417,23 +1456,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1441,24 +1481,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=CType(Nothing, Uri), - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=True, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=False, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=CType(Nothing, Uri), + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1466,23 +1507,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1490,24 +1532,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName:=testDirectory, ' This is a Directory! - userName:=String.Empty, - password:=String.Empty, - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True, - onUserCancel:=UICancelOption.DoNothing) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName:=testDirectory, ' This is a Directory! + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ .Throw(Of InvalidOperationException)() _ .WithMessage(SR.Network_DownloadNeedsFilename) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1518,19 +1561,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) - End Sub - - testCode.Should.NotThrow() - VerifyAndCleanupSuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ .Be(DownloadSmallFileSize) + End Using End Sub @@ -1543,20 +1587,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=String.Empty) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub @@ -1569,20 +1614,21 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=DownloadSmallFileSize, userName:=DefaultUserName, password:=DefaultPassword) - Dim listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - VerifyAndCleanupFailedDownload(testDirectory, destinationFileName, listener) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb new file mode 100644 index 00000000000..b21897cac3e --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -0,0 +1,13 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Namespace Microsoft.VisualBasic.Forms.Tests + Public Module DownloadFileTestConstants + Friend Const DefaultPassword As String = NameOf(DefaultPassword) + Friend Const DefaultUserName As String = NameOf(DefaultUserName) + Friend Const DownloadLargeFileSize As Integer = 104_857_600 + Friend Const DownloadSmallFileSize As Integer = 18_135 + Friend Const InvalidUrlAddress As String = "invalidURL" + Friend Const TestingConnectionTimeout As Integer = 100_000 + End Module +End Namespace From 175440ad3bb3eb0c8acd282017d558638dedd350 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 19:06:45 -0800 Subject: [PATCH 016/119] Clean up formatting issues --- .../System/Windows/Forms/DownloadFileTests.vb | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 4811a817d3b..8a21e47d864 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -24,7 +24,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' A Unique directory under the systems Temp directory. ''' The full path and filename of the new file. ''' - Private Shared Sub VerifyFailedDownload(testDirectory As String, destinationFileName As String, listener As HttpListener) + Private Shared Sub VerifyFailedDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) + If Not String.IsNullOrWhiteSpace(testDirectory) Then Directory.Exists(testDirectory).Should.BeTrue() End If @@ -44,9 +48,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' do another FileInfo call. ''' ''' The full path and filename of the new file. - ''' ''' - Private Shared Function VerifySuccessfulDownload(testDirectory As String, destinationFileName As String, listener As HttpListener) As Long + Private Shared Function VerifySuccessfulDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) As Long + Directory.Exists(testDirectory).Should.BeTrue() Dim fileInfo As New FileInfo(destinationFileName) fileInfo.Exists.Should.BeTrue() @@ -64,7 +71,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener(DownloadSmallFileSize) Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( @@ -183,7 +189,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -274,11 +280,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() - Dim networkCredentials As New NetworkCredential(DefaultUserName, password) My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -540,7 +545,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -594,7 +599,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -951,9 +956,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() - My.Computer.Network.DownloadFile( - address, - destinationFileName) + My.Computer.Network.DownloadFile(address, destinationFileName) End Sub testCode.Should() _ @@ -971,9 +974,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName) + My.Computer.Network.DownloadFile(address:=webListener.Address, destinationFileName) End Sub testCode.Should() _ @@ -1005,7 +1006,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -1305,7 +1306,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, destinationFileName:=destinationFileName, listener:=listener) + VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub From 1bf35911e98c9580100616ffc0ff8ec6b511120b Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 22:05:44 -0800 Subject: [PATCH 017/119] Add some Async Tests --- .../Windows/Forms/DownloadFileAsyncTests.vb | 96 +++++++++++++++++++ .../System/Windows/Forms/DownloadFileTests.vb | 47 --------- .../TestUtilities/DownloadFileVerifiers.vb | 59 ++++++++++++ 3 files changed, 155 insertions(+), 47 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb new file mode 100644 index 00000000000..49f1f57d354 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb @@ -0,0 +1,96 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.Net +Imports System.Threading +Imports FluentAssertions +Imports Microsoft.VisualBasic.FileIO +Imports Xunit + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class DownloadFileAsyncTests + Inherits VbFileCleanupTestBase + + + Public Sub DownloadFileAsync_AllOptionsClientHandlerNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim t As Task = Devices.Network.DownloadFileAsync( + addressUri:=Nothing, + destinationFileName, + clientHandler:=Nothing, + dialog:=Nothing, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing, + cancelToken:=CancellationToken.None) + If t.IsFaulted Then + Throw t.Exception + End If + End Sub + + testCode.Should.Throw(Of ArgumentNullException)().WithParameterName("addressUri") + End Using + End Sub + + + Public Sub DownloadFileAsync_AllOptionsInvalidTimeout_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim t As Task = Devices.Network.DownloadFileAsync( + addressUri:=New Uri(uriString:=webListener.Address), + destinationFileName, + clientHandler:=Nothing, + dialog:=Nothing, + connectionTimeout:=-1, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing, + cancelToken:=CancellationToken.None) + If t.IsFaulted Then + Throw t.Exception + End If + End Sub + + testCode.Should.Throw(Of ArgumentException)().WithParameterName("connectionTimeout") + End Using + End Sub + + + Public Sub DownloadFileAsync_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim t As Task = Devices.Network.DownloadFileAsync( + addressUri:=New Uri(uriString:=webListener.Address), + destinationFileName, + userName:=Nothing, + password:=String.Empty, + dialog:=Nothing, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=False, + CancellationToken.None) + If t.IsFaulted Then + Throw t.Exception + End If + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Using + End Sub + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 41cd2067d8e..bb74219ba13 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -12,53 +12,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase - ''' - ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. - ''' - ''' A Unique directory under the systems Temp directory. - ''' The full path and filename of the new file. - ''' - Private Shared Sub VerifyFailedDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) - - If Not String.IsNullOrWhiteSpace(testDirectory) Then - Directory.Exists(testDirectory).Should.BeTrue() - End If - If Not String.IsNullOrWhiteSpace(destinationFileName) Then - Call New FileInfo(destinationFileName).Exists.Should.BeFalse() - End If - listener.Stop() - listener.Close() - End Sub - - ''' - ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. - ''' - ''' A Unique directory under the systems Temp directory. - ''' - ''' The size in bytes of the destination file, this saves the caller from having to - ''' do another FileInfo call. - ''' - ''' The full path and filename of the new file. - ''' - Private Shared Function VerifySuccessfulDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) As Long - - Directory.Exists(testDirectory).Should.BeTrue() - Dim fileInfo As New FileInfo(destinationFileName) - fileInfo.Exists.Should.BeTrue() - Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() - ' This directory should not be systems Temp Directory because it must be created - Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) - listener.Stop() - listener.Close() - Return fileInfo.Length - End Function - Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb new file mode 100644 index 00000000000..80728ba5fa1 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb @@ -0,0 +1,59 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports FluentAssertions + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Module DownloadFileVerifiers + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' The full path and filename of the new file. + ''' + Friend Sub VerifyFailedDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) + + If Not String.IsNullOrWhiteSpace(testDirectory) Then + Directory.Exists(testDirectory).Should.BeTrue() + End If + If Not String.IsNullOrWhiteSpace(destinationFileName) Then + Call New FileInfo(destinationFileName).Exists.Should.BeFalse() + End If + listener.Stop() + listener.Close() + End Sub + + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' + ''' The size in bytes of the destination file, this saves the caller from having to + ''' do another FileInfo call. + ''' + ''' The full path and filename of the new file. + ''' + Friend Function VerifySuccessfulDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) As Long + + Directory.Exists(testDirectory).Should.BeTrue() + Dim fileInfo As New FileInfo(destinationFileName) + fileInfo.Exists.Should.BeTrue() + Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() + ' This directory should not be systems Temp Directory because it must be created + Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) + listener.Stop() + listener.Close() + Return fileInfo.Length + End Function + + End Module +End Namespace From f1c6e6501c88cb26ac01a388ae63ff8394550ecc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 22:14:31 -0800 Subject: [PATCH 018/119] Move download verifiers to separate file --- .../System/Windows/Forms/DownloadFileTests.vb | 47 --------------- .../TestUtilities/DownloadFileVerifiers.vb | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 8a21e47d864..e03b6593cb5 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -18,53 +18,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private Const SR_net_webstatus_Unauthorized As String = "The remote server returned an error: (401) Unauthorized." - ''' - ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. - ''' - ''' A Unique directory under the systems Temp directory. - ''' The full path and filename of the new file. - ''' - Private Shared Sub VerifyFailedDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) - - If Not String.IsNullOrWhiteSpace(testDirectory) Then - Directory.Exists(testDirectory).Should.BeTrue() - End If - If Not String.IsNullOrWhiteSpace(destinationFileName) Then - Call New FileInfo(destinationFileName).Exists.Should.BeFalse() - End If - listener.Stop() - listener.Close() - End Sub - - ''' - ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. - ''' - ''' A Unique directory under the systems Temp directory. - ''' - ''' The size in bytes of the destination file, this saves the caller from having to - ''' do another FileInfo call. - ''' - ''' The full path and filename of the new file. - ''' - Private Shared Function VerifySuccessfulDownload( - testDirectory As String, - destinationFileName As String, - listener As HttpListener) As Long - - Directory.Exists(testDirectory).Should.BeTrue() - Dim fileInfo As New FileInfo(destinationFileName) - fileInfo.Exists.Should.BeTrue() - Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() - ' This directory should not be systems Temp Directory because it must be created - Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) - listener.Stop() - listener.Close() - Return fileInfo.Length - End Function - Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb new file mode 100644 index 00000000000..80728ba5fa1 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb @@ -0,0 +1,59 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports FluentAssertions + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Module DownloadFileVerifiers + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' The full path and filename of the new file. + ''' + Friend Sub VerifyFailedDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) + + If Not String.IsNullOrWhiteSpace(testDirectory) Then + Directory.Exists(testDirectory).Should.BeTrue() + End If + If Not String.IsNullOrWhiteSpace(destinationFileName) Then + Call New FileInfo(destinationFileName).Exists.Should.BeFalse() + End If + listener.Stop() + listener.Close() + End Sub + + ''' + ''' Verify that testDirectory exists, that destinationFileName exist and what its length is. + ''' + ''' A Unique directory under the systems Temp directory. + ''' + ''' The size in bytes of the destination file, this saves the caller from having to + ''' do another FileInfo call. + ''' + ''' The full path and filename of the new file. + ''' + Friend Function VerifySuccessfulDownload( + testDirectory As String, + destinationFileName As String, + listener As HttpListener) As Long + + Directory.Exists(testDirectory).Should.BeTrue() + Dim fileInfo As New FileInfo(destinationFileName) + fileInfo.Exists.Should.BeTrue() + Directory.Exists(fileInfo.DirectoryName).Should.BeTrue() + ' This directory should not be systems Temp Directory because it must be created + Path.GetTempPath.Should.NotBe(fileInfo.DirectoryName) + listener.Stop() + listener.Close() + Return fileInfo.Length + End Function + + End Module +End Namespace From 1d0b3e2da20d0e1006fcbd9d89576ad3c45690f9 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 28 Nov 2024 14:29:01 -0800 Subject: [PATCH 019/119] Fix broken test --- .../Windows/Forms/DownloadFileAsyncTests.vb | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb index 49f1f57d354..22243fa0028 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net +Imports System.Net.Http Imports System.Threading Imports FluentAssertions Imports Microsoft.VisualBasic.FileIO @@ -12,6 +13,35 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileAsyncTests Inherits VbFileCleanupTestBase + + Public Sub DownloadFileAsync_AllOptions_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(DownloadSmallFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + Dim t As Task = Devices.Network.DownloadFileAsync( + addressUri:=New Uri(uriString:=webListener.Address), + destinationFileName, + clientHandler:=New HttpClientHandler, + dialog:=Nothing, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True, + onUserCancel:=UICancelOption.DoNothing, + cancelToken:=CancellationToken.None) + If t.IsFaulted Then + Throw t.Exception + End If + t.Wait() + End Sub + + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(DownloadSmallFileSize) + End Using + End Sub + Public Sub DownloadFileAsync_AllOptionsClientHandlerNothing_Throws() Dim testDirectory As String = CreateTempDirectory() @@ -72,7 +102,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() - Dim t As Task = Devices.Network.DownloadFileAsync( + Devices.Network.DownloadFileAsync( addressUri:=New Uri(uriString:=webListener.Address), destinationFileName, userName:=Nothing, @@ -80,15 +110,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests dialog:=Nothing, connectionTimeout:=TestingConnectionTimeout, overwrite:=False, - CancellationToken.None) - If t.IsFaulted Then - Throw t.Exception - End If + cancelToken:=CancellationToken.None).Wait() End Sub - testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(DownloadSmallFileSize) End Using End Sub From fbd2c7cd477bac0b6ab2c6c66c69704308f61c1f Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 28 Nov 2024 15:15:53 -0800 Subject: [PATCH 020/119] Remove Imports Microsoft.VisualBasic.CompilerServices --- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index cee291870c4..0fd76aeed3b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -2,7 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net -Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal From 0baeb02dbadb7233afbe3aa26f3415d9338387a4 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 28 Nov 2024 15:25:50 -0800 Subject: [PATCH 021/119] Fix error cause myy removal of Imports Microsoft.VisualBasic.CompilerServices --- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 0fd76aeed3b..46ebeb6a771 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -327,7 +327,9 @@ Namespace Microsoft.VisualBasic.Devices client.UseNonPassiveFtp = showUI 'Construct the local file. This will validate the full name and path - Dim fullFilename As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) + Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( + path:=destinationFileName, + paramName:=NameOf(destinationFileName)) ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really ' have a file and path From 2b7303ca0aea7b4ada96da1b22abff1189c33278 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 14 Dec 2024 18:40:54 -0800 Subject: [PATCH 022/119] Fix behaviour cancel dialog behavior to match WebCleintDownload --- .../System/Windows/Forms/DownloadFileTests.vb | 22 +++++++++++++++++++ .../System/Windows/Forms/WebListener.vb | 6 ++++- .../TestUtilities/DownloadFileVerifiers.vb | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index e03b6593cb5..bcf3bbd479c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1039,6 +1039,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(DownloadLargeFileSize * 10) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should.Throw(Of OperationCanceledException)() + End Using + End Sub + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb index f8cd3aef554..4467c61603f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb @@ -82,7 +82,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) response.ContentLength64 = buffer.Length Using output As Stream = response.OutputStream - output.Write(buffer, offset:=0, count:=buffer.Length) + Try + output.Write(buffer, offset:=0, count:=buffer.Length) + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try End Using Finally response?.Close() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb index 80728ba5fa1..14f7044d9f2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileVerifiers.vb @@ -23,7 +23,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Directory.Exists(testDirectory).Should.BeTrue() End If If Not String.IsNullOrWhiteSpace(destinationFileName) Then - Call New FileInfo(destinationFileName).Exists.Should.BeFalse() + File.Exists(destinationFileName).Should.BeFalse() End If listener.Stop() listener.Close() From ccdf42b37067c197f619a66b56cb3ecfa7751546 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 12:17:06 -0800 Subject: [PATCH 023/119] Add UploadFile Tests Move some constants around Rename some files that are shared between upload and download --- .../System/Windows/Forms/DownloadFileTests.vb | 164 +- .../System/Windows/Forms/UploadFileTests.vb | 1322 +++++++++++++++++ .../System/Windows/Forms/WebListener.vb | 65 +- .../DownloadFileTestConstants.vb | 5 +- .../TestUtilities/VbFileCleanupTestBase.vb | 58 +- 5 files changed, 1486 insertions(+), 128 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index bcf3bbd479c..012ec2cac07 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -22,7 +22,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -33,7 +33,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -41,7 +41,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsEmptyString_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -59,7 +59,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -77,7 +77,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -98,7 +98,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -116,7 +116,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -125,7 +125,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -150,7 +150,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -176,7 +176,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -192,7 +192,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadLargeFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -200,7 +200,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -216,7 +216,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -227,7 +227,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -253,7 +253,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -278,7 +278,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -303,7 +303,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -328,7 +328,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -351,7 +351,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -367,7 +367,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -376,7 +376,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -403,7 +403,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -422,7 +422,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -431,7 +431,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -457,7 +457,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -481,7 +481,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsExceptOnUserCancelWhereDestinationFileNameInvalidOverwriteThrows( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -508,7 +508,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -536,7 +536,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -562,7 +562,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -591,7 +591,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -620,7 +620,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -637,7 +637,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -647,7 +647,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -675,7 +675,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -705,7 +705,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -722,7 +722,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -730,7 +730,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -756,7 +756,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -773,7 +773,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -781,7 +781,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -808,7 +808,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -823,7 +823,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -834,7 +834,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -861,7 +861,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -885,7 +885,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -896,7 +896,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -905,7 +905,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -923,7 +923,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -942,7 +942,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -967,7 +967,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -993,7 +993,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1019,7 +1019,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1035,15 +1035,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadLargeFileSize) + .Be(LargeTestFileSize) End Using End Sub - + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize * 10) + Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1065,7 +1065,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1081,7 +1081,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1089,7 +1089,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1114,7 +1114,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1139,7 +1139,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1165,7 +1165,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1181,7 +1181,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1190,7 +1190,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1209,7 +1209,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1217,7 +1217,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1263,7 +1263,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1291,7 +1291,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1322,7 +1322,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1351,7 +1351,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1368,7 +1368,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1378,7 +1378,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1406,7 +1406,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1436,7 +1436,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1453,7 +1453,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1461,7 +1461,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1487,7 +1487,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1504,7 +1504,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1512,7 +1512,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1539,7 +1539,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1554,7 +1554,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1565,7 +1565,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -1592,7 +1592,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb new file mode 100644 index 00000000000..e2a3156c973 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -0,0 +1,1322 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports FluentAssertions +Imports Microsoft.VisualBasic.FileIO +Imports Xunit + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class UploadFileTests + Inherits VbFileCleanupTestBase + + ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, + ' they are not accessible in this project they come from WebClient. + Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." + + Private Const SR_net_webstatus_Unauthorized As String = + "The remote server returned an error: (401) Unauthorized." + + + Public Sub UploadFile_UriOnly_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address)) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriOnlyWhereAddressIsEmptyString_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(String.Empty)) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + End Using + End Sub + + + Public Sub UploadFile_UriOnlyWhereAddressIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(Nothing)) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + End Using + End Sub + + + + Public Sub UploadFile_UriOnlyWhereSourceFileNameInvalidAddressOnly_Throws(sourceFileName As String) + Dim testDirectory As String = CreateTempDirectory() + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address)) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancel_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + End Using + End Sub + + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereSourceFileNameInvalid_Throws( + sourceFileName As String) + + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=CType(Nothing, Uri), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(InvalidUrlAddress), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should().Throw(Of UriFormatException)() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentials_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=Nothing, + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentials_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentialsTimeout0_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + networkCredentials, + showUI:=False, + connectionTimeout:=0, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.Throw(Of ArgumentException)() + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(InvalidUrlAddress), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.Throw(Of UriFormatException)() + End Using + End Sub + + + + Public Sub UploadFile_UriWithAllOptionsExceptOnUserCancelWhereSourceFileNameInvalidThrows( + sourceFileName As String) + + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=$"{sourceFileName}{separator}", + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=root, ' This is a Root Directory! + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=$"{sourceFileName}{separator}", + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=root, ' This is a Root Directory! + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should().Throw(Of FileNotFoundException)() + End Using + End Sub + + + + + Public Sub UploadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=root, ' This is a Root Directory! + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + + Public Sub UploadFile_UriWithAllOptionsWhereSourceFileNameInvalid_Throws(sourceFileName As String) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.Throw(Of ArgumentNullException)() + VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=CType(Nothing, Uri), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + Public Sub UploadFile_UriWithAllOptionsWithAllOptions_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UriWithUserNamePassword_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + + Public Sub UploadFile_UriWithUserNamePasswordWherePasswordWrong_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=String.Empty) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + End Using + End Sub + + + + + Public Sub UploadFile_UriWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + End Using + End Sub + + + Public Sub UploadFile_UrlOnly_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + Public Sub UploadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile(sourceFileName, address) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + + Public Sub UploadFile_UrlOnlyWhereSourceFileNameInvalidAddressOnly_Throws(sourceFileName As String) + Dim testDirectory As String = CreateTempDirectory() + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile(sourceFileName, address:=webListener.Address) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=InvalidUrlAddress, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + End Using + End Sub + + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereSourceFileNameInvalid_Throws( + sourceFileName As String) + + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=1) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=-1) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) + Dim webListener As New WebListener(ExtraLargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.Throw(Of OperationCanceledException)() + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=InvalidUrlAddress, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=Nothing, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptionsAndNetworkCredentials_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + networkCredentials, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.ThrowException) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=InvalidUrlAddress, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) + End Using + End Sub + + + + Public Sub UploadFile_UrlWithAllOptionsWhereAddressIsNothingOrEmpty_Throws(address As String) + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Sub + + + + + Public Sub UploadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=root, ' This is a Root Directory! + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + + + Public Sub UploadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=$"{sourceFileName}{separator}", + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + + Public Sub UploadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=root, ' This is a Root Directory! + address:=New Uri(webListener.Address), + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) + testCode.Should() _ + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) + End Using + End Sub + + + + Public Sub UploadFile_UrlWithAllOptionsWhereSourceFileNameInvalid_Throws(sourceFileName As String) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName:=Nothing, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=CType(Nothing, Uri), + userName:=String.Empty, + password:=String.Empty, + showUI:=True, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should() _ + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + End Using + End Sub + + + Public Sub UploadFile_UrlWithAllOptionsWithAllOptions_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=String.Empty, + password:=String.Empty, + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + onUserCancel:=UICancelOption.DoNothing) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + Public Sub UploadFile_UrlWithUserNamePassword_Success() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=DefaultUserName, + password:=DefaultPassword) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + + + + + Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=String.Empty) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + End Using + End Sub + + + + + Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener( + fileSize:=SmallTestFileSize, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) + End Using + End Sub + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb index 4467c61603f..25e0fcbede1 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb @@ -9,7 +9,7 @@ Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener - Private ReadOnly _downloadFileUrlPrefix As String + Private ReadOnly _fileUrlPrefix As String Private ReadOnly _fileSize As Integer Private ReadOnly _password As String Private ReadOnly _userName As String @@ -21,8 +21,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) _fileSize = fileSize - _downloadFileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" - Address = $"{_downloadFileUrlPrefix}T{fileSize}" + _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" + Address = $"{_fileUrlPrefix}T{fileSize}" End Sub ''' @@ -49,7 +49,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ' Create a listener and add the prefixes. Dim listener As New HttpListener() - listener.Prefixes.Add(_downloadFileUrlPrefix) + listener.Prefixes.Add(_fileUrlPrefix) If _userName IsNot Nothing OrElse _password IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If @@ -66,7 +66,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests If context.User?.Identity.IsAuthenticated Then Dim identity As HttpListenerBasicIdentity = - CType(context.User?.Identity, HttpListenerBasicIdentity) + CType(context.User?.Identity, HttpListenerBasicIdentity) + If String.IsNullOrWhiteSpace(identity.Name) _ OrElse identity.Name <> _userName _ OrElse String.IsNullOrWhiteSpace(identity.Password) _ @@ -78,16 +79,50 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If ' Simulate network traffic Thread.Sleep(millisecondsTimeout:=20) - Dim responseString As String = Strings.StrDup(_fileSize, "A") - Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) - response.ContentLength64 = buffer.Length - Using output As Stream = response.OutputStream - Try - output.Write(buffer, offset:=0, count:=buffer.Length) - Catch ex As Exception - ' ignore it will be handled elsewhere - End Try - End Using + If listener.Prefixes(0).Contains("UploadFile") Then + Dim request As HttpListenerRequest = context.Request + If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ + AndAlso request.HasEntityBody Then + + Using bodyStream As Stream = request.InputStream + Using reader As New StreamReader(bodyStream, request.ContentEncoding, True, 4096) + Try + Dim boundary As String = request.ContentType.Split(";"c)(1) _ + .Split("="c)(1).Trim + Dim content As String = reader.ReadToEnd() + ' Extract file data from the multipart form data + Dim startIndex As Integer = content.IndexOf("filename=""", StringComparison.OrdinalIgnoreCase) + 10 + Dim endIndex As Integer = content.IndexOf("""", startIndex, StringComparison.OrdinalIgnoreCase) + Dim fileName As String = content.Substring(startIndex, endIndex - startIndex) + startIndex = content.IndexOf($"{vbCrLf}{vbCrLf}", endIndex, StringComparison.OrdinalIgnoreCase) + 4 + endIndex = content.LastIndexOf($"--{boundary}", StringComparison.OrdinalIgnoreCase) - 2 + Dim fileData As Byte() = request.ContentEncoding.GetBytes(content.Substring(startIndex, endIndex - startIndex)) + + If _fileSize <> fileData.Length Then + Throw New IOException($"File size mismatch, expected {_fileSize} actual {fileData.Length}") + End If + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + End If + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try + End Using + End Using + End If + response.StatusCode = 200 + Else + Dim responseString As String = Strings.StrDup(_fileSize, "A") + Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) + response.ContentLength64 = buffer.Length + Using output As Stream = response.OutputStream + Try + output.Write(buffer, offset:=0, count:=buffer.Length) + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try + End Using + End If Finally response?.Close() response = Nothing diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb index b21897cac3e..85381891d6f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -5,8 +5,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Module DownloadFileTestConstants Friend Const DefaultPassword As String = NameOf(DefaultPassword) Friend Const DefaultUserName As String = NameOf(DefaultUserName) - Friend Const DownloadLargeFileSize As Integer = 104_857_600 - Friend Const DownloadSmallFileSize As Integer = 18_135 + Friend Const ExtraLargeTestFileSize As Integer = 1_048_576_000 + Friend Const LargeTestFileSize As Integer = 104_857_600 + Friend Const SmallTestFileSize As Integer = 18_135 Friend Const InvalidUrlAddress As String = "invalidURL" Friend Const TestingConnectionTimeout As Integer = 100_000 End Module diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 72ce4a6a912..18bc3f06182 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -10,7 +10,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Implements IDisposable Private Shared ReadOnly s_baseTempPath As String = Path.Combine(Path.GetTempPath, "DownLoadTest9d9e3a8-7a46-4333-a0eb-4faf76994801") - + Friend Const DefaultFileName As String = "Testing.Txt" Friend ReadOnly _testDirectories As New HashSet(Of String) Protected Overrides Sub Finalize() @@ -35,29 +35,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Try End Sub - ''' - ''' Creates or returns a directory based on the name of the function that - ''' call it. The base directory is described above. - ''' Even if directory exists this call will success and just return it. - ''' - ''' - ''' If >0 use line number as part of name. - ''' The name of a directory that is safe to write to and is verified to exist. - Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String - Dim folder As String - If lineNumber > 0 Then - folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") - Else - folder = Path.Combine(BaseTempPath, memberName) - End If - - If _testDirectories.Add(folder) Then - Directory.CreateDirectory(folder) - End If - - Return folder - End Function - ''' ''' If size >= 0 then create the file with size length. ''' @@ -68,7 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' The full path and file name of the created file. ''' If size = -1 no file is create but the full path is returned. ''' - Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = "Testing.Txt", Optional size As Integer = -1) As String + Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) If size >= 0 Then @@ -104,14 +81,37 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return True End Function + Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String + Return Path.Combine(testDirectory, GetUniqueFileName()) + End Function + + ''' + ''' Creates or returns a directory based on the name of the function that + ''' call it. The base directory is described above. + ''' Even if directory exists this call will success and just return it. + ''' + ''' + ''' If >0 use line number as part of name. + ''' The name of a directory that is safe to write to and is verified to exist. + Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String + Dim folder As String + If lineNumber > 0 Then + folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") + Else + folder = Path.Combine(BaseTempPath, memberName) + End If + + If _testDirectories.Add(folder) Then + Directory.CreateDirectory(folder) + End If + + Return folder + End Function + Friend Sub Dispose() Implements IDisposable.Dispose Dispose(disposing:=True) GC.SuppressFinalize(Me) End Sub - Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String - Return Path.Combine(testDirectory, GetUniqueFileName()) - End Function - End Class End Namespace From c7542cc7df09b43282039b5a9f208d305729a588 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 12:25:47 -0800 Subject: [PATCH 024/119] Avoid merge issues --- .../Windows/Forms/DownloadFileAsyncTests.vb | 12 +- .../System/Windows/Forms/DownloadFileTests.vb | 162 +++++++++--------- .../DownloadFileTestConstants.vb | 5 +- .../TestUtilities/VbFileCleanupTestBase.vb | 58 +++---- .../Windows/TestUtilities/WebListener.vb | 65 +++++-- 5 files changed, 169 insertions(+), 133 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb index 22243fa0028..15f80a1fbe8 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb @@ -17,7 +17,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -38,7 +38,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -46,7 +46,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptionsClientHandlerNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -72,7 +72,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptionsInvalidTimeout_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -98,7 +98,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -114,7 +114,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index d3c03ac8f03..35cf75c9078 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -16,7 +16,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -27,7 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -35,7 +35,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsEmptyString_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -53,7 +53,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -71,7 +71,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -92,7 +92,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -110,7 +110,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -119,7 +119,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -144,7 +144,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -170,7 +170,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -186,7 +186,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadLargeFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -194,7 +194,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -210,7 +210,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -221,7 +221,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -247,7 +247,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -272,7 +272,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -297,7 +297,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -322,7 +322,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -345,7 +345,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -361,7 +361,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -370,7 +370,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -397,7 +397,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -416,7 +416,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -425,7 +425,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -451,7 +451,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -475,7 +475,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsExceptOnUserCancelWhereDestinationFileNameInvalidOverwriteThrows( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -502,7 +502,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -530,7 +530,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -556,7 +556,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -585,7 +585,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -614,7 +614,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -631,7 +631,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -641,7 +641,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -669,7 +669,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -699,7 +699,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -716,7 +716,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -724,7 +724,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -750,7 +750,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -767,7 +767,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -775,7 +775,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -802,7 +802,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -817,7 +817,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -828,7 +828,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -855,7 +855,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -879,7 +879,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -890,7 +890,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -899,7 +899,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -917,7 +917,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -936,7 +936,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -961,7 +961,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -987,7 +987,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1013,7 +1013,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1029,7 +1029,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadLargeFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -1037,7 +1037,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadLargeFileSize * 10) + Dim webListener As New WebListener(LargeTestFileSize * 10) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1059,7 +1059,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1075,7 +1075,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1083,7 +1083,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1108,7 +1108,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadLargeFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1133,7 +1133,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1159,7 +1159,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1175,7 +1175,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1184,7 +1184,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1203,7 +1203,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1211,7 +1211,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1257,7 +1257,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1285,7 +1285,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1316,7 +1316,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1345,7 +1345,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1362,7 +1362,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1372,7 +1372,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1400,7 +1400,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1430,7 +1430,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1447,7 +1447,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1455,7 +1455,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1481,7 +1481,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1498,7 +1498,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1506,7 +1506,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(DownloadSmallFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1533,7 +1533,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1548,7 +1548,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(DownloadSmallFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1559,7 +1559,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -1586,7 +1586,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=DownloadSmallFileSize, + fileSize:=SmallTestFileSize, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb index b21897cac3e..85381891d6f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -5,8 +5,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Module DownloadFileTestConstants Friend Const DefaultPassword As String = NameOf(DefaultPassword) Friend Const DefaultUserName As String = NameOf(DefaultUserName) - Friend Const DownloadLargeFileSize As Integer = 104_857_600 - Friend Const DownloadSmallFileSize As Integer = 18_135 + Friend Const ExtraLargeTestFileSize As Integer = 1_048_576_000 + Friend Const LargeTestFileSize As Integer = 104_857_600 + Friend Const SmallTestFileSize As Integer = 18_135 Friend Const InvalidUrlAddress As String = "invalidURL" Friend Const TestingConnectionTimeout As Integer = 100_000 End Module diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 72ce4a6a912..18bc3f06182 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -10,7 +10,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Implements IDisposable Private Shared ReadOnly s_baseTempPath As String = Path.Combine(Path.GetTempPath, "DownLoadTest9d9e3a8-7a46-4333-a0eb-4faf76994801") - + Friend Const DefaultFileName As String = "Testing.Txt" Friend ReadOnly _testDirectories As New HashSet(Of String) Protected Overrides Sub Finalize() @@ -35,29 +35,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Try End Sub - ''' - ''' Creates or returns a directory based on the name of the function that - ''' call it. The base directory is described above. - ''' Even if directory exists this call will success and just return it. - ''' - ''' - ''' If >0 use line number as part of name. - ''' The name of a directory that is safe to write to and is verified to exist. - Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String - Dim folder As String - If lineNumber > 0 Then - folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") - Else - folder = Path.Combine(BaseTempPath, memberName) - End If - - If _testDirectories.Add(folder) Then - Directory.CreateDirectory(folder) - End If - - Return folder - End Function - ''' ''' If size >= 0 then create the file with size length. ''' @@ -68,7 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' The full path and file name of the created file. ''' If size = -1 no file is create but the full path is returned. ''' - Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = "Testing.Txt", Optional size As Integer = -1) As String + Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) If size >= 0 Then @@ -104,14 +81,37 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return True End Function + Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String + Return Path.Combine(testDirectory, GetUniqueFileName()) + End Function + + ''' + ''' Creates or returns a directory based on the name of the function that + ''' call it. The base directory is described above. + ''' Even if directory exists this call will success and just return it. + ''' + ''' + ''' If >0 use line number as part of name. + ''' The name of a directory that is safe to write to and is verified to exist. + Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String + Dim folder As String + If lineNumber > 0 Then + folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") + Else + folder = Path.Combine(BaseTempPath, memberName) + End If + + If _testDirectories.Add(folder) Then + Directory.CreateDirectory(folder) + End If + + Return folder + End Function + Friend Sub Dispose() Implements IDisposable.Dispose Dispose(disposing:=True) GC.SuppressFinalize(Me) End Sub - Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String - Return Path.Combine(testDirectory, GetUniqueFileName()) - End Function - End Class End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 4467c61603f..9c6e98832e2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -9,8 +9,8 @@ Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener - Private ReadOnly _downloadFileUrlPrefix As String Private ReadOnly _fileSize As Integer + Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String Private ReadOnly _userName As String @@ -21,8 +21,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) _fileSize = fileSize - _downloadFileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" - Address = $"{_downloadFileUrlPrefix}T{fileSize}" + _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" + Address = $"{_fileUrlPrefix}T{fileSize}" End Sub ''' @@ -49,7 +49,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ' Create a listener and add the prefixes. Dim listener As New HttpListener() - listener.Prefixes.Add(_downloadFileUrlPrefix) + listener.Prefixes.Add(_fileUrlPrefix) If _userName IsNot Nothing OrElse _password IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If @@ -66,7 +66,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests If context.User?.Identity.IsAuthenticated Then Dim identity As HttpListenerBasicIdentity = - CType(context.User?.Identity, HttpListenerBasicIdentity) + CType(context.User?.Identity, HttpListenerBasicIdentity) + If String.IsNullOrWhiteSpace(identity.Name) _ OrElse identity.Name <> _userName _ OrElse String.IsNullOrWhiteSpace(identity.Password) _ @@ -78,16 +79,50 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If ' Simulate network traffic Thread.Sleep(millisecondsTimeout:=20) - Dim responseString As String = Strings.StrDup(_fileSize, "A") - Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) - response.ContentLength64 = buffer.Length - Using output As Stream = response.OutputStream - Try - output.Write(buffer, offset:=0, count:=buffer.Length) - Catch ex As Exception - ' ignore it will be handled elsewhere - End Try - End Using + If listener.Prefixes(0).Contains("UploadFile") Then + Dim request As HttpListenerRequest = context.Request + If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ + AndAlso request.HasEntityBody Then + + Using bodyStream As Stream = request.InputStream + Using reader As New StreamReader(bodyStream, request.ContentEncoding, True, 4096) + Try + Dim boundary As String = request.ContentType.Split(";"c)(1) _ + .Split("="c)(1).Trim + Dim content As String = reader.ReadToEnd() + ' Extract file data from the multipart form data + Dim startIndex As Integer = content.IndexOf("filename=""", StringComparison.OrdinalIgnoreCase) + 10 + Dim endIndex As Integer = content.IndexOf("""", startIndex, StringComparison.OrdinalIgnoreCase) + Dim fileName As String = content.Substring(startIndex, endIndex - startIndex) + startIndex = content.IndexOf($"{vbCrLf}{vbCrLf}", endIndex, StringComparison.OrdinalIgnoreCase) + 4 + endIndex = content.LastIndexOf($"--{boundary}", StringComparison.OrdinalIgnoreCase) - 2 + Dim fileData As Byte() = request.ContentEncoding.GetBytes(content.Substring(startIndex, endIndex - startIndex)) + + If _fileSize <> fileData.Length Then + Throw New IOException($"File size mismatch, expected {_fileSize} actual {fileData.Length}") + End If + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + End If + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try + End Using + End Using + End If + response.StatusCode = 200 + Else + Dim responseString As String = Strings.StrDup(_fileSize, "A") + Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) + response.ContentLength64 = buffer.Length + Using output As Stream = response.OutputStream + Try + output.Write(buffer, offset:=0, count:=buffer.Length) + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try + End Using + End If Finally response?.Close() response = Nothing From 7324bb1c945deb2f28306112aafd55cf027cfde2 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 12:27:19 -0800 Subject: [PATCH 025/119] Sort private variables --- .../tests/UnitTests/System/Windows/Forms/WebListener.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb index 25e0fcbede1..9c6e98832e2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb @@ -9,8 +9,8 @@ Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener - Private ReadOnly _fileUrlPrefix As String Private ReadOnly _fileSize As Integer + Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String Private ReadOnly _userName As String From 65cbc86e009a8559f99208568d4f4a49edb122c9 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 12:48:43 -0800 Subject: [PATCH 026/119] Move WebListener.vb to TestUtilities Folder --- .../System/Windows/{Forms => TestUtilities}/WebListener.vb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/{Forms => TestUtilities}/WebListener.vb (100%) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb similarity index 100% rename from src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WebListener.vb rename to src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb From ae6e5c6a15bd1542d8107e98dfa77a43d5f001a5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:02:22 -0800 Subject: [PATCH 027/119] Fix formatting --- .../System/Windows/Forms/DownloadFileTests.vb | 140 +++++++++--------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 35cf75c9078..fa09d23f813 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -81,8 +81,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -134,8 +134,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -160,8 +160,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) testCode.Should() _ - .Throw(Of IOException)() _ - .Where(Function(e) e.Message.Equals(value)) + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) End Using End Sub @@ -237,8 +237,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -262,8 +262,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -287,8 +287,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -312,8 +312,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -490,8 +490,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -521,8 +521,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -604,8 +604,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -657,8 +657,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -688,8 +688,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -740,8 +740,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -791,8 +791,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .WithMessage(SR.Network_DownloadNeedsFilename) + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -842,8 +842,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -869,8 +869,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -907,8 +907,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -925,8 +925,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -951,8 +951,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -977,8 +977,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) testCode.Should() _ - .Throw(Of IOException)() _ - .Where(Function(e) e.Message.Equals(value)) + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) End Using End Sub @@ -1003,8 +1003,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1033,11 +1033,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(LargeTestFileSize * 10) + Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1098,8 +1098,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1123,8 +1123,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1149,8 +1149,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1227,8 +1227,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1273,8 +1273,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -1304,8 +1304,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1335,8 +1335,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1388,8 +1388,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1419,8 +1419,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1471,8 +1471,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1522,8 +1522,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .WithMessage(SR.Network_DownloadNeedsFilename) + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1573,8 +1573,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1600,8 +1600,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub From f3cc2d00ec54d438f6954f69a9cd4af2138b64c0 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:08:01 -0800 Subject: [PATCH 028/119] Formatting fixes --- .../System/Windows/Forms/DownloadFileTests.vb | 118 +++++++++--------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 012ec2cac07..f330d463851 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -140,8 +140,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -243,8 +243,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -268,8 +268,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -293,8 +293,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -318,8 +318,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -496,8 +496,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -527,8 +527,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -610,8 +610,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -663,8 +663,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -694,8 +694,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -746,8 +746,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -797,8 +797,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .WithMessage(SR.Network_DownloadNeedsFilename) + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -848,8 +848,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -875,8 +875,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -913,8 +913,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -931,8 +931,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -957,8 +957,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -983,8 +983,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.IO_FileExists_Path.Replace("{0}", destinationFileName) testCode.Should() _ - .Throw(Of IOException)() _ - .Where(Function(e) e.Message.Equals(value)) + .Throw(Of IOException)() _ + .Where(Function(e) e.Message.Equals(value)) VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should.Be(1) End Using End Sub @@ -1009,8 +1009,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1104,8 +1104,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1129,8 +1129,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1155,7 +1155,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ + .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using @@ -1279,8 +1279,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, destinationFileName, listener) End Using End Sub @@ -1310,8 +1310,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1341,8 +1341,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1394,8 +1394,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) + .Throw(Of InvalidOperationException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_DownloadNeedsFilename)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1425,8 +1425,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(destinationFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1477,8 +1477,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1528,8 +1528,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of InvalidOperationException)() _ - .WithMessage(SR.Network_DownloadNeedsFilename) + .Throw(Of InvalidOperationException)() _ + .WithMessage(SR.Network_DownloadNeedsFilename) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1579,8 +1579,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub From 5a79bf2fbc5e2af565377c931a66ec67db524b41 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:09:52 -0800 Subject: [PATCH 029/119] Fix formatting --- .../tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index fa09d23f813..f98dccfd412 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -27,7 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub From cdab3aab6736118a3307a23dea6a81978b835bbc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:11:46 -0800 Subject: [PATCH 030/119] Fix formatting --- .../System/Windows/Forms/DownloadFileTests.vb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index f330d463851..49f1c10c262 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -44,11 +44,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(String.Empty), - destinationFileName) - End Sub + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(String.Empty), + destinationFileName) + End Sub testCode.Should.Throw(Of UriFormatException)() VerifyFailedDownload(testDirectory, destinationFileName, listener) From bd3bd661236a24334cfafd1b754e50ebb4b3257c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 00:46:25 -0800 Subject: [PATCH 031/119] Remove WebClientCopy and replace with HttpClientCopy Fixed bug in message dispaly for ProgressDialog Add FileUploadTests --- .../src/Microsoft.VisualBasic.Forms.vbproj | 4 + .../VisualBasic/Devices/Network.UploadFile.vb | 63 ++++---- .../Devices/Network.UploadFileAsync.vb | 81 ++++++++++ .../VisualBasic/Devices/NetworkUtilities.vb | 8 +- .../MyServices/Internal/HttpClientCopy.vb | 141 ++++++++++------- .../Internal/ProgressableStreamContent.vb | 52 +++++++ .../MyServices/Internal/WebClientCopy.vb | 146 ------------------ .../System/Windows/Forms/UploadFileTests.vb | 82 +++++----- .../Windows/TestUtilities/WebListener.vb | 100 +++++++++--- 9 files changed, 388 insertions(+), 289 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb create mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb delete mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft.VisualBasic.Forms.vbproj b/src/Microsoft.VisualBasic.Forms/src/Microsoft.VisualBasic.Forms.vbproj index 095dcd70100..92143067eeb 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft.VisualBasic.Forms.vbproj +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft.VisualBasic.Forms.vbproj @@ -18,6 +18,10 @@ true + + + + diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 40ea15623a6..f8f5fab06c7 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net +Imports System.Net.Http Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal @@ -280,38 +281,48 @@ Namespace Microsoft.VisualBasic.Devices Throw GetArgumentNullException(NameOf(address)) End If - Using client As New WebClientExtended() - client.Timeout = connectionTimeout - - ' Set credentials if we have any - If networkCredentials IsNot Nothing Then - client.Credentials = networkCredentials - End If + ' Get network credentials + Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) + Dim dialog As ProgressDialog = Nothing + Try - Dim dialog As ProgressDialog = Nothing If showUI AndAlso Environment.UserInteractive Then - dialog = New ProgressDialog With { - .Text = GetResourceString(SR.ProgressDialogUploadingTitle, sourceFileName), - .LabelText = GetResourceString( - resourceKey:=SR.ProgressDialogUploadingLabel, - sourceFileName, - address.AbsolutePath) - } - End If + ' Construct the local file. This will validate the full name and path + Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( + path:=sourceFileName, + paramName:=NameOf(sourceFileName)) - ' Create the copier - Dim copier As New WebClientCopy(client, dialog) - - ' Download the file - copier.UploadFile(sourceFileName, address) + dialog = GetProgressDialog(address.AbsolutePath, sourceFileName, showUI) + End If - ' Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then - If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then - Throw New OperationCanceledException() + Dim t As Task = UploadFileAsync( + sourceFileName, + addressUri:=address, + clientHandler, + dialog, + connectionTimeout, + onUserCancel) + + If t.IsFaulted Then + ' IsFaulted will be true if any parameters are bad + Throw t.Exception + Else + dialog?.ShowProgressDialog() + t.Wait() + If t.IsFaulted Then + Throw t.Exception End If End If - End Using + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + Finally + CloseProgressDialog(dialog) + End Try End Sub diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb new file mode 100644 index 00000000000..064f038175a --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -0,0 +1,81 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.Net +Imports System.Net.Http +Imports System.Threading +Imports Microsoft.VisualBasic.CompilerServices +Imports Microsoft.VisualBasic.FileIO +Imports Microsoft.VisualBasic.MyServices.Internal + +Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils + +Namespace Microsoft.VisualBasic.Devices + + Partial Public Class Network + ''' + ''' Uploads a file to the network to the specified path. + ''' + ''' Name and path of file to be uploaded. + ''' Uri to the remote file + ''' An HttpClientHandler of the user performing the download. + ''' Progress Dialog. + ''' Time allotted before giving up on a connection. + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Calls to all the other overloads will come through here. + Friend Shared Async Function UploadFileAsync( + sourceFileName As String, + addressUri As Uri, + clientHandler As HttpClientHandler, + dialog As ProgressDialog, + connectionTimeout As Integer, + onUserCancel As UICancelOption, + Optional cancelToken As CancellationToken = Nothing) As Task + + If cancelToken = Nothing Then + cancelToken = New CancellationTokenSource().Token + End If + + If connectionTimeout <= 0 Then + Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + End If + + If addressUri Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(addressUri)) + End If + + Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath(sourceFileName, NameOf(sourceFileName)) + + ' Make sure we have a meaningful file. + If String.IsNullOrEmpty(normalizedFilePath) Then + Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + End If + + Dim client As HttpClient = If(clientHandler Is Nothing, + New HttpClient(), + New HttpClient(clientHandler) + ) + client.Timeout = TimeSpan.FromMilliseconds(connectionTimeout) + 'Create the copier + Dim copier As New HttpClientCopy(client, dialog) + + 'Upload the file + Try + Await copier.UploadFileWorkerAsync( + filePath:=normalizedFilePath, + requestURI:=addressUri, + externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + Catch ex As Exception + If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then + If ex.Message.Contains("401") Then + Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + End If + Throw + End If + End Try + + End Function + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 1458e5fcefe..bb37591afa9 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -6,6 +6,8 @@ Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal +Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils + Namespace Microsoft.VisualBasic.Devices Friend Module NetworkUtilities @@ -68,9 +70,9 @@ Namespace Microsoft.VisualBasic.Devices path:=destinationFileName, paramName:=NameOf(destinationFileName)) Return New ProgressDialog With { - .Text = Utils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = Utils.GetResourceString( - ResourceKey:=SR.ProgressDialogDownloadingLabel, + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), + .LabelText = VbUtils.GetResourceString( + resourceKey:=SR.ProgressDialogDownloadingLabel, address, fullFilename) } diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index 8a38aba6efe..085e7d1fccb 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Net Imports System.Net.Http +Imports System.Net.Http.Headers Imports System.Threading Imports Microsoft.VisualBasic.Devices.NetworkUtilities @@ -17,13 +18,15 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ' Dialog shown if user wants to see progress UI. Allows the user to cancel the file transfer. Private WithEvents _progressDialog As ProgressDialog + Private Const BufferSize As Integer = 8192 + ' The WebClient performs the downloading or uploading operations for us Private ReadOnly _httpClient As HttpClient Private _cancelTokenSourceGet As CancellationTokenSource - Private _cancelTokenSourceRead As CancellationTokenSource + Private _cancelTokenSourcePost As CancellationTokenSource Private _cancelTokenSourceReadStream As CancellationTokenSource - Private _cancelTokenSourceWrite As CancellationTokenSource + Private _cancelTokenSourceWriteStream As CancellationTokenSource ' The percentage of the operation completed Private _percentage As Integer @@ -48,6 +51,23 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End Sub + ''' + ''' If the user clicks cancel on the Progress dialog, we need to cancel + ''' the current async file transfer operation. + ''' + ''' + ''' Note that we don't want to close the progress dialog here. Wait until + ''' the actual file transfer cancel event comes through and do it there. + ''' + Private Sub _progressDialog_UserHitCancel() + ' Cancel the upload/download transfer. We'll close the ProgressDialog + ' as soon as the HttpClient cancels the xfer. + _cancelTokenSourceGet?.Cancel() + _cancelTokenSourceReadStream?.Cancel() + _cancelTokenSourcePost?.Cancel() + _cancelTokenSourceWriteStream?.Cancel() + End Sub + ''' ''' Notifies the progress dialog to increment the progress bar. ''' @@ -61,30 +81,13 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Dim increment As Integer = progressPercentage - _percentage _percentage = progressPercentage If increment > 0 Then - _progressDialog.BeginInvoke(New DoIncrement(AddressOf _progressDialog.Increment), increment) + _progressDialog.BeginInvoke(method:=New DoIncrement(AddressOf _progressDialog.Increment), increment) End If End If End If End Sub - ''' - ''' If the user clicks cancel on the Progress dialog, we need to cancel - ''' the current async file transfer operation. - ''' - ''' - ''' Note that we don't want to close the progress dialog here. Wait until - ''' the actual file transfer cancel event comes through and do it there. - ''' - Private Sub _progressDialog_UserHitCancel() - ' Cancel the upload/download transfer. We'll close the ProgressDialog - ' as soon as the HttpClient cancels the xfer. - _cancelTokenSourceGet.Cancel() - _cancelTokenSourceRead.Cancel() - _cancelTokenSourceReadStream.Cancel() - _cancelTokenSourceWrite.Cancel() - End Sub - ''' ''' Downloads a file. ''' @@ -98,20 +101,17 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Directory.Exists(directoryPath), "Invalid path") _cancelTokenSourceGet = New CancellationTokenSource() - _cancelTokenSourceRead = New CancellationTokenSource() _cancelTokenSourceReadStream = New CancellationTokenSource() - _cancelTokenSourceWrite = New CancellationTokenSource() + _cancelTokenSourceWriteStream = New CancellationTokenSource() Dim response As HttpResponseMessage = Nothing - - _cancelTokenSourceGet = New CancellationTokenSource() + Dim totalBytesRead As Long = 0 Using linkedCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceGet.Token, externalToken) Try response = Await _httpClient.GetAsync( requestUri:=addressUri, completionOption:=HttpCompletionOption.ResponseHeadersRead, cancellationToken:=_cancelTokenSourceGet.Token).ConfigureAwait(continueOnCapturedContext:=False) - Catch ex As TaskCanceledException If ex.CancellationToken = externalToken Then externalToken.ThrowIfCancellationRequested() @@ -125,9 +125,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End Using Select Case response.StatusCode Case HttpStatusCode.OK - _cancelTokenSourceRead = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) _cancelTokenSourceReadStream = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) - _cancelTokenSourceWrite = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) + _cancelTokenSourceWriteStream = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) Dim contentLength? As Long = response?.Content.Headers.ContentLength If contentLength.HasValue Then Using responseStream As Stream = Await response.Content.ReadAsStreamAsync( @@ -140,13 +139,12 @@ Namespace Microsoft.VisualBasic.MyServices.Internal access:=FileAccess.Write, share:=FileShare.None) - Dim buffer(8191) As Byte - Dim totalBytesRead As Long = 0 + Dim buffer(BufferSize - 1) As Byte Dim bytesRead As Integer Try bytesRead = Await responseStream.ReadAsync( buffer:=buffer.AsMemory(start:=0, buffer.Length), - cancellationToken:=_cancelTokenSourceRead.Token). + cancellationToken:=_cancelTokenSourceReadStream.Token). ConfigureAwait(continueOnCapturedContext:=False) Do While bytesRead > 0 @@ -154,7 +152,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Await fileStream.WriteAsync( buffer:=buffer.AsMemory(0, bytesRead), - cancellationToken:=_cancelTokenSourceWrite.Token). + cancellationToken:=_cancelTokenSourceWriteStream.Token). ConfigureAwait(continueOnCapturedContext:=False) If _progressDialog IsNot Nothing Then @@ -163,7 +161,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End If bytesRead = Await responseStream.ReadAsync( buffer:=buffer.AsMemory(start:=0, buffer.Length), - cancellationToken:=_cancelTokenSourceRead.Token). + cancellationToken:=_cancelTokenSourceReadStream.Token). ConfigureAwait(continueOnCapturedContext:=False) Loop Finally @@ -171,8 +169,6 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End Try End Using End Using - If response.StatusCode = HttpStatusCode.OK Then - End If If _progressDialog IsNot Nothing Then RemoveHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel @@ -187,35 +183,68 @@ Namespace Microsoft.VisualBasic.MyServices.Internal response?.Dispose() End Function -#If False Then ' Future code to implement upload using Http ''' ''' Uploads a file ''' - ''' The name and path of the source file - ''' The address to which the file is uploaded - Public Sub UploadFile(sourceFileName As String, address As Uri) + ''' The name and path of the source file + ''' The address to which the file is uploaded + Public Async Function UploadFileWorkerAsync(filePath As String, requestURI As Uri, externalToken As CancellationToken) As Task Debug.Assert(_httpClient IsNot Nothing, "No WebClient") - Debug.Assert(address IsNot Nothing, "No address") - Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) AndAlso - File.Exists(sourceFileName), "Invalid file") + Debug.Assert(requestURI IsNot Nothing, "No address") - ' If we have a dialog we need to set up an async download + _cancelTokenSourceReadStream = New CancellationTokenSource() + _cancelTokenSourcePost = New CancellationTokenSource() + Dim contentLength As Long = New FileInfo(filePath).Length + Dim totalBytesRead As Long = 0 + Using linkedStreamCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceReadStream.Token, _cancelTokenSourcePost.Token, externalToken) + Try + Using content As New MultipartFormDataContent("----boundary") + Dim progress As Action(Of Long, Long) = + Sub(bytesRead As Long, streamLength As Long) + totalBytesRead += bytesRead + If _progressDialog IsNot Nothing Then + Dim progressPercentage As Integer = CInt(totalBytesRead / streamLength * 100) + InvokeIncrement(progressPercentage) + End If + End Sub + Dim fileContent As New StreamContent(File.OpenRead(filePath)) + Dim progressContent As New ProgressableStreamContent(fileContent, progress, BufferSize) + Dim fileName As String = Path.GetFileName(filePath) + content.Add(fileContent, "file", fileName) + fileContent.Headers.ContentDisposition = + New ContentDispositionHeaderValue(dispositionType:="form-data") With { + .Name = """file""", + .FileName = $"""{fileName}"""} + + Dim response As HttpResponseMessage = + Await _httpClient.PostAsync(requestURI, content, cancellationToken:=linkedStreamCts.Token).ConfigureAwait(continueOnCapturedContext:=False) + response.EnsureSuccessStatusCode() + Select Case response.StatusCode + Case HttpStatusCode.OK + Case HttpStatusCode.Unauthorized + Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case Else + Throw New WebException() + End Select + response?.Dispose() + End Using + Catch ex As HttpRequestException + Throw + Catch ex As TaskCanceledException + If ex.CancellationToken = externalToken Then + externalToken.ThrowIfCancellationRequested() + ElseIf linkedStreamCts.IsCancellationRequested Then + ' a real cancellation, triggered by the caller + Throw + Else + Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) + End If + End Try + End Using If _progressDialog IsNot Nothing Then - _httpClient.UploadFileAsync(address, sourceFileName) - ' returns when the download sequence is over, whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _httpClient.UploadFile(address, sourceFileName) - End If - - ' Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. - If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then - If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then - Throw _exceptionEncounteredDuringFileTransfer - End If + RemoveHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel End If - End Sub -#End If + End Function End Class End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb new file mode 100644 index 00000000000..fd3b66d0e31 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb @@ -0,0 +1,52 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.IO +Imports System.Net +Imports System.Net.Http + +Namespace Microsoft.VisualBasic.MyServices.Internal + + Friend Class ProgressableStreamContent + Inherits HttpContent + + Private ReadOnly _content As HttpContent + Private ReadOnly _bufferSize As Integer + Private ReadOnly _progress As Action(Of Long, Long) + + Public Sub New(content As HttpContent, progress As Action(Of Long, Long), Optional bufferSize As Integer = 4096) + _content = content + _progress = progress + _bufferSize = bufferSize + For Each header As KeyValuePair(Of String, IEnumerable(Of String)) In content.Headers + Headers.TryAddWithoutValidation(header.Key, header.Value) + Next + End Sub + + Protected Overrides Async Function SerializeToStreamAsync(stream As Stream, context As TransportContext) As Task + Dim buffer(_bufferSize - 1) As Byte + Dim size As Long + Dim uploaded As Long = 0 + + TryComputeLength(size) + + Using sInput As Stream = Await _content.ReadAsStreamAsync().ConfigureAwait(continueOnCapturedContext:=False) + While True + Dim length As Integer = Await sInput.ReadAsync(buffer).ConfigureAwait(continueOnCapturedContext:=False) + If length <= 0 Then Exit While + + Await stream.WriteAsync(buffer.AsMemory(start:=0, length)).ConfigureAwait(continueOnCapturedContext:=False) + uploaded += length + _progress?.Invoke(uploaded, size) + End While + End Using + End Function + + + Protected Overrides Function TryComputeLength(ByRef length As Long) As Boolean + length = _content.Headers.ContentLength.GetValueOrDefault() + Return True + End Function + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb deleted file mode 100644 index 54a9396cb18..00000000000 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ /dev/null @@ -1,146 +0,0 @@ -' Licensed to the .NET Foundation under one or more agreements. -' The .NET Foundation licenses this file to you under the MIT license. - -Imports System.Net -Imports Microsoft.VisualBasic.Devices - -Namespace Microsoft.VisualBasic.MyServices.Internal - - ''' - ''' Class that controls the thread that does the actual work of downloading or uploading. - ''' - Friend NotInheritable Class WebClientCopy - - ' Dialog shown if user wants to see progress UI. Allows the user to cancel the file transfer. - Private WithEvents _progressDialog As ProgressDialog - - ' The WebClient performs the downloading or uploading operations for us - Private WithEvents _webClient As WebClient - - ' Keeps track of the error that happened during upload/download so we can throw it - ' once we can guarantee we are back on the main thread - Private _exceptionEncounteredDuringFileTransfer As Exception - - ' The percentage of the operation completed - Private _percentage As Integer - - ' Used for invoking ProgressDialog.Increment - Private Delegate Sub DoIncrement(Increment As Integer) - - ''' - ''' Creates an instance of a WebClientCopy, used to download or upload a file. - ''' - ''' The used to do the downloading or uploading. - ''' UI for indicating progress. - Public Sub New(client As WebClient, dialog As ProgressDialog) - Debug.Assert(client IsNot Nothing, $"No {client}") - - _webClient = client - _progressDialog = dialog - End Sub - - ''' - ''' If the user clicks cancel on the Progress dialog, we need to cancel - ''' the current async file transfer operation. - ''' - ''' - ''' Note: that we don't want to close the progress dialog here. Wait until - ''' the actual file transfer cancel event comes through and do it there. - ''' - Private Sub _progressDialog_UserHitCancel() Handles _progressDialog.UserHitCancel - ' Cancel the upload/download transfer. We'll close the ProgressDialog - ' as soon as the WebClient cancels the xfer. - _webClient.CancelAsync() - End Sub - - ''' - ''' Handles the WebClient's UploadFileCompleted event. - ''' - ''' - ''' - Private Sub _webClient_UploadFileCompleted(sender As Object, e As UploadFileCompletedEventArgs) _ - Handles _webClient.UploadFileCompleted - - ' If the upload was interrupted by an exception, keep track of the exception, - ' which we'll throw from the main thread - Try - If e.Error IsNot Nothing Then - _exceptionEncounteredDuringFileTransfer = e.Error - End If - If Not e.Cancelled AndAlso e.Error Is Nothing Then - InvokeIncrement(100) - End If - Finally - ' We don't close the dialog until we receive the - ' WebClient.DownloadFileCompleted event - CloseProgressDialog(_progressDialog) - End Try - End Sub - - ''' - ''' Handles event WebClient fires whenever progress of upload changes. - ''' - ''' - ''' - Private Sub _webClient_UploadProgressChanged(sender As Object, e As UploadProgressChangedEventArgs) _ - Handles _webClient.UploadProgressChanged - - Dim increment As Long = (e.BytesSent * 100) \ e.TotalBytesToSend - InvokeIncrement(CInt(increment)) - End Sub - - ''' - ''' Notifies the progress dialog to increment the progress bar. - ''' - ''' The percentage of bytes read. - Private Sub InvokeIncrement(progressPercentage As Integer) - ' Don't invoke unless dialog is up and running - If _progressDialog IsNot Nothing Then - If _progressDialog.IsHandleCreated Then - - ' For performance, don't invoke if increment is 0 - Dim increment As Integer = progressPercentage - _percentage - _percentage = progressPercentage - If increment > 0 Then - _progressDialog.BeginInvoke( - New DoIncrement(AddressOf _progressDialog.Increment), - increment) - End If - - End If - End If - End Sub - - ''' - ''' Uploads a file. - ''' - ''' The name and path of the source file. - ''' The address to which the file is uploaded. - Public Sub UploadFile(sourceFileName As String, address As Uri) - Debug.Assert(_webClient IsNot Nothing, $"No {NameOf(_webClient)}") - Debug.Assert(address IsNot Nothing, $"No {NameOf(address)}") - Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) _ - AndAlso IO.File.Exists(sourceFileName), "Invalid file") - - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.UploadFileAsync(address, sourceFileName) - - ' Returns when the download sequence is over, - ' whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.UploadFile(address, sourceFileName) - End If - - ' Now that we are back on the main thread, throw the exception we - ' encountered if the user didn't cancel. - If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then - If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then - Throw _exceptionEncounteredDuringFileTransfer - End If - End If - End Sub - - End Class -End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index e2a3156c973..a39bb0ca2fe 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -12,13 +12,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class UploadFileTests Inherits VbFileCleanupTestBase - ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, - ' they are not accessible in this project they come from WebClient. - Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." - - Private Const SR_net_webstatus_Unauthorized As String = - "The remote server returned an error: (401) Unauthorized." - Public Sub UploadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() @@ -43,11 +36,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(String.Empty)) - End Sub + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(String.Empty)) + End Sub testCode.Should.Throw(Of UriFormatException)() End Using @@ -89,6 +82,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub + + Public Sub UploadFile_UriOnlyWrongFileSize_Throw() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address)) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancel_Success() Dim testDirectory As String = CreateTempDirectory() @@ -136,7 +146,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -154,7 +164,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -184,7 +194,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -201,7 +211,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=-1) End Sub @@ -211,7 +221,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) @@ -245,7 +255,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -268,7 +278,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(InvalidUrlAddress), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -289,7 +299,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=Nothing, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -428,7 +438,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -611,7 +621,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -634,7 +644,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -711,7 +721,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -737,7 +747,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -831,7 +841,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -861,7 +871,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -878,7 +888,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=-1) End Sub @@ -909,7 +919,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) @@ -943,7 +953,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=InvalidUrlAddress, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -967,7 +977,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=Nothing, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -1055,7 +1065,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -1186,7 +1196,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -1211,7 +1221,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -1288,7 +1298,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -1314,7 +1324,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 9c6e98832e2..4116e2882b8 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -4,11 +4,13 @@ Imports System.IO Imports System.Net Imports System.Runtime.CompilerServices +Imports System.Text.RegularExpressions Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener + Private Const BufferSize As Integer = 4096 Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String @@ -45,6 +47,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public ReadOnly Property Address As String + Private Shared Function GetBoundary(contentType As String) As String + Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) + Dim element As String = elements.FirstOrDefault(Function(e) e.Trim().StartsWith("boundary=", StringComparison.OrdinalIgnoreCase)) + Return If(element IsNot Nothing, element.Substring(startIndex:=element.IndexOf("="c) + 1).Trim().Trim(""""c), String.Empty) + End Function + Friend Function ProcessRequests() As HttpListener ' Create a listener and add the prefixes. Dim listener As New HttpListener() @@ -84,30 +92,31 @@ Namespace Microsoft.VisualBasic.Forms.Tests If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ AndAlso request.HasEntityBody Then + Dim formData As Dictionary(Of String, String) = GetMultipartFormData(request) + Using bodyStream As Stream = request.InputStream - Using reader As New StreamReader(bodyStream, request.ContentEncoding, True, 4096) - Try - Dim boundary As String = request.ContentType.Split(";"c)(1) _ - .Split("="c)(1).Trim - Dim content As String = reader.ReadToEnd() - ' Extract file data from the multipart form data - Dim startIndex As Integer = content.IndexOf("filename=""", StringComparison.OrdinalIgnoreCase) + 10 - Dim endIndex As Integer = content.IndexOf("""", startIndex, StringComparison.OrdinalIgnoreCase) - Dim fileName As String = content.Substring(startIndex, endIndex - startIndex) - startIndex = content.IndexOf($"{vbCrLf}{vbCrLf}", endIndex, StringComparison.OrdinalIgnoreCase) + 4 - endIndex = content.LastIndexOf($"--{boundary}", StringComparison.OrdinalIgnoreCase) - 2 - Dim fileData As Byte() = request.ContentEncoding.GetBytes(content.Substring(startIndex, endIndex - startIndex)) - - If _fileSize <> fileData.Length Then - Throw New IOException($"File size mismatch, expected {_fileSize} actual {fileData.Length}") - End If - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") - End If - Catch ex As Exception - ' ignore it will be handled elsewhere - End Try + Using reader As New StreamReader( + stream:=bodyStream, + encoding:=request.ContentEncoding, + detectEncodingFromByteOrderMarks:=True, + BufferSize) End Using + Try + Dim dataLength As String = formData(NameOf(dataLength)) + If _fileSize.ToString <> dataLength Then + Throw New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") + End If + + Dim fileName As String = formData("file") + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + End If + Catch ioEx As IOException + Throw + Catch ex As Exception + Stop + ' ignore it will be handled elsewhere + End Try End Using End If response.StatusCode = 200 @@ -133,5 +142,52 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return listener End Function + ''' + ''' Parses a and gets the fileName of the uploaded file + ''' and the lenght of the data file in bytes + ''' + ''' + ''' + ''' A that contains the filename and lenght of the data file. + ''' + Public Function GetMultipartFormData(request As HttpListenerRequest) As Dictionary(Of String, String) + Dim result As New Dictionary(Of String, String) + + If request.ContentType.StartsWith("multipart/form-data", StringComparison.OrdinalIgnoreCase) Then + Dim boundary As String = GetBoundary(request.ContentType) + Using reader As New StreamReader(request.InputStream, request.ContentEncoding) + Dim content As String = reader.ReadToEnd() + Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) + + For Each part As String In parts + If part.Trim() <> "--" Then + Dim separator As String() = New String() {Environment.NewLine} + Dim lines As String() = part.Split(separator, StringSplitOptions.RemoveEmptyEntries) + If lines.Length > 2 Then + Dim headerParts As String() = lines(0).Split({":"c}, count:=2) + If headerParts.Length = 2 _ + AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", + comparisonType:=StringComparison.OrdinalIgnoreCase) Then + + Dim nameMatch As Match = Regex.Match(input:=headerParts(1), pattern:="name=""(?[^""]+)""") + If nameMatch.Success Then + Dim name As String = nameMatch.Groups("name").Value + Dim value As String = headerParts(1).Split("filename=")(1).Trim(""""c) + result.Add(name, value.Trim()) + If lines.Length > 2 Then + result.Add("dataLength", lines(1).Length.ToString) + End If + Exit For + End If + End If + End If + End If + Next + End Using + End If + + Return result + End Function + End Class End Namespace From 47f12cd3e84a26bd4eb7c706c0802f47b2bf5933 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 00:58:33 -0800 Subject: [PATCH 032/119] Correct messaages for ProgressDialog which calls wrong GetResourceString. --- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 1458e5fcefe..bb37591afa9 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -6,6 +6,8 @@ Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal +Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils + Namespace Microsoft.VisualBasic.Devices Friend Module NetworkUtilities @@ -68,9 +70,9 @@ Namespace Microsoft.VisualBasic.Devices path:=destinationFileName, paramName:=NameOf(destinationFileName)) Return New ProgressDialog With { - .Text = Utils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = Utils.GetResourceString( - ResourceKey:=SR.ProgressDialogDownloadingLabel, + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), + .LabelText = VbUtils.GetResourceString( + resourceKey:=SR.ProgressDialogDownloadingLabel, address, fullFilename) } From 6a8252341e05ece19269d98b055ed23e01c5d222 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 03:01:21 -0800 Subject: [PATCH 033/119] Add more tests and check that files are properly uploaded with correct filename and size (the data is not saved). Wrap and indent long lines corectly. --- .../SingleInstanceHelpers.vb | 28 ++-- .../WindowsFormsApplicationBase.vb | 3 +- .../src/Microsoft/VisualBasic/Interaction.vb | 3 +- .../System/Windows/Forms/DownloadFileTests.vb | 40 ++--- .../Forms/SingleInstanceHelpersTests.vb | 9 +- .../System/Windows/Forms/UploadFileTests.vb | 149 ++++++++++-------- .../Windows/TestUtilities/WebListener.vb | 102 +++++++++--- 7 files changed, 201 insertions(+), 133 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb index 4801df5276b..44ad4424f25 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb @@ -21,17 +21,16 @@ Namespace Microsoft.VisualBasic.ApplicationServices Using stream As New MemoryStream While True Dim buffer As Byte() = New Byte(bufferLength - 1) {} - Dim bytesRead As Integer = Await pipeServer.ReadAsync( - buffer:=buffer.AsMemory(start:=0, length:=bufferLength), - cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Dim bytesRead As Integer = + Await pipeServer.ReadAsync( + buffer:=buffer.AsMemory(start:=0, length:=bufferLength), + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) If bytesRead = 0 Then Exit While End If Await stream.WriteAsync( buffer:=buffer.AsMemory(start:=0, length:=bytesRead), - cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End While stream.Seek(0, SeekOrigin.Begin) Dim serializer As New DataContractSerializer(GetType(String())) @@ -55,8 +54,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices content = stream.ToArray() End Using Await pipeClient.WriteAsync( - buffer:=content.AsMemory(start:=0, length:=content.Length), cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + buffer:=content.AsMemory(start:=0, length:=content.Length), + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End Function Friend Async Function SendSecondInstanceArgsAsync( @@ -70,10 +69,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices direction:=PipeDirection.Out, options:=NamedPipeOptions) - Await pipeClient.ConnectAsync(cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) - Await WriteArgsAsync(pipeClient, args, cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Await pipeClient.ConnectAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) + Await WriteArgsAsync(pipeClient, args, cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End Using End Function @@ -102,11 +99,10 @@ Namespace Microsoft.VisualBasic.ApplicationServices While True cancellationToken.ThrowIfCancellationRequested() - Await pipeServer.WaitForConnectionAsync(cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Await pipeServer.WaitForConnectionAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) Try - Dim args() As String = Await ReadArgsAsync(pipeServer, cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Dim args() As String = + Await ReadArgsAsync(pipeServer, cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) If args IsNot Nothing Then callback(args) End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb index 9ee86a368fe..18a97c87960 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb @@ -1082,8 +1082,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices Dim awaitable As ConfiguredTaskAwaitable = SendSecondInstanceArgsAsync( pipeName:=applicationInstanceID, args:=commandLine, - cancellationToken:=tokenSource.Token) _ - .ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken:=tokenSource.Token).ConfigureAwait(continueOnCapturedContext:=False) awaitable.GetAwaiter().GetResult() Catch ex As Exception diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb index a0ba0ebffaf..14004e09e8b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb @@ -250,7 +250,8 @@ Namespace Microsoft.VisualBasic 'Low-order 4 bits (0x000f), legal values: 0, 1, 2, 3, 4, 5 ' next 4 bits (0x00f0), legal values: 0, &H10, &H20, &H30, &H40 ' next 4 bits (0x0f00), legal values: 0, &H100, &H200 - If ((Buttons And &HFI) > MsgBoxStyle.RetryCancel) OrElse ((Buttons And &HF0I) > MsgBoxStyle.Information) _ + If ((Buttons And &HFI) > MsgBoxStyle.RetryCancel) _ + OrElse ((Buttons And &HF0I) > MsgBoxStyle.Information) _ OrElse ((Buttons And &HF00I) > MsgBoxStyle.DefaultButton3) Then Buttons = MsgBoxStyle.OkOnly End If diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 49f1c10c262..4f779b0afaf 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -12,8 +12,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase - ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, - ' they are not accessible in this project they come from WebClient. + ' The next 2 Constants need to be SR Resources, + ' they are not accessible in this project they come from WebClient. Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." Private Const SR_net_webstatus_Unauthorized As String = "The remote server returned an error: (401) Unauthorized." @@ -116,7 +116,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -192,7 +192,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -216,7 +216,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -367,7 +367,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -422,7 +422,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -637,7 +637,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -722,7 +722,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -773,7 +773,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -823,7 +823,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -896,7 +896,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1035,7 +1035,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -1081,7 +1081,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1181,7 +1181,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1209,7 +1209,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1368,7 +1368,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1453,7 +1453,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1504,7 +1504,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1554,7 +1554,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb index 297c174fe58..b93e5c6ef48 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb @@ -61,15 +61,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim awaitable As ConfiguredTaskAwaitable = SendSecondInstanceArgsAsync( pipeName, args:=commandLine, - cancellationToken:=tokenSource.Token) _ - .ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken:=tokenSource.Token).ConfigureAwait(continueOnCapturedContext:=False) awaitable.GetAwaiter().GetResult() Dim CancelToken As New CancellationToken Dim buffer As Byte() = New Byte(commandLine.Length) {} - Dim count As Integer = - Await pipeServer.ReadAsync(buffer:=buffer.AsMemory(start:=0, length:=commandLine.Length)) _ - .ConfigureAwait(continueOnCapturedContext:=True) + Dim count As Integer = Await pipeServer.ReadAsync( + buffer:=buffer.AsMemory(start:=0, length:=commandLine.Length)) _ + .ConfigureAwait(continueOnCapturedContext:=True) ' Ensure the result is set Do diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index e2a3156c973..ae71c54a258 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -89,6 +89,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub + + Public Sub UploadFile_UriOnlyWrongFileSize_Throw() + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address)) + End Sub + + testCode.Should.NotThrow() + End Using + End Sub + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancel_Success() Dim testDirectory As String = CreateTempDirectory() @@ -135,8 +152,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) End Using End Sub @@ -154,13 +171,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -183,8 +200,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) End Using End Sub @@ -201,13 +218,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=-1) End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) End Using End Sub @@ -245,13 +262,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -268,7 +285,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(InvalidUrlAddress), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -289,7 +306,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=Nothing, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -428,20 +445,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub - - + + Public Sub UploadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) @@ -463,8 +480,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -519,8 +536,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -594,8 +611,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -611,7 +628,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -634,14 +651,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -710,8 +727,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) End Using End Sub @@ -736,8 +753,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) End Using End Sub @@ -771,8 +788,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -788,8 +805,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -812,8 +829,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) End Using End Sub @@ -831,13 +848,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -860,8 +877,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Timeout) End Using End Sub @@ -878,13 +895,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=-1) End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) End Using End Sub @@ -943,13 +960,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=InvalidUrlAddress, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ + .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) End Using End Sub @@ -967,7 +984,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=Nothing, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -1055,7 +1072,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub @@ -1089,8 +1106,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1118,8 +1135,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1169,8 +1186,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1186,14 +1203,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address, userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -1211,14 +1228,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=CType(Nothing, Uri), userName:=String.Empty, password:=String.Empty, - showUI:=True, + showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.DoNothing) End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -1287,8 +1304,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR_net_webstatus_Unauthorized) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 9c6e98832e2..64ab80a4680 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -4,18 +4,20 @@ Imports System.IO Imports System.Net Imports System.Runtime.CompilerServices +Imports System.Text.RegularExpressions Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener + Private Const BufferSize As Integer = 4096 Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String Private ReadOnly _userName As String ''' - ''' The name of the function that creates the server is uses to establish the file to be downloaded. + ''' The name of the function that creates the server is used to establish the file to be downloaded. ''' ''' Is used to create the file name and the size of download. ''' Used to establish the file path to be downloaded. @@ -45,6 +47,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public ReadOnly Property Address As String + Private Shared Function GetBoundary(contentType As String) As String + Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) + Dim element As String = elements.FirstOrDefault(Function(e) e.Trim().StartsWith("boundary=", StringComparison.OrdinalIgnoreCase)) + Return If(element IsNot Nothing, element.Substring(startIndex:=element.IndexOf("="c) + 1).Trim().Trim(""""c), String.Empty) + End Function + Friend Function ProcessRequests() As HttpListener ' Create a listener and add the prefixes. Dim listener As New HttpListener() @@ -84,30 +92,31 @@ Namespace Microsoft.VisualBasic.Forms.Tests If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ AndAlso request.HasEntityBody Then + Dim formData As Dictionary(Of String, String) = GetMultipartFormData(request) + Using bodyStream As Stream = request.InputStream - Using reader As New StreamReader(bodyStream, request.ContentEncoding, True, 4096) - Try - Dim boundary As String = request.ContentType.Split(";"c)(1) _ - .Split("="c)(1).Trim - Dim content As String = reader.ReadToEnd() - ' Extract file data from the multipart form data - Dim startIndex As Integer = content.IndexOf("filename=""", StringComparison.OrdinalIgnoreCase) + 10 - Dim endIndex As Integer = content.IndexOf("""", startIndex, StringComparison.OrdinalIgnoreCase) - Dim fileName As String = content.Substring(startIndex, endIndex - startIndex) - startIndex = content.IndexOf($"{vbCrLf}{vbCrLf}", endIndex, StringComparison.OrdinalIgnoreCase) + 4 - endIndex = content.LastIndexOf($"--{boundary}", StringComparison.OrdinalIgnoreCase) - 2 - Dim fileData As Byte() = request.ContentEncoding.GetBytes(content.Substring(startIndex, endIndex - startIndex)) - - If _fileSize <> fileData.Length Then - Throw New IOException($"File size mismatch, expected {_fileSize} actual {fileData.Length}") - End If - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") - End If - Catch ex As Exception - ' ignore it will be handled elsewhere - End Try + Using reader As New StreamReader( + stream:=bodyStream, + encoding:=request.ContentEncoding, + detectEncodingFromByteOrderMarks:=True, + BufferSize) End Using + Try + Dim dataLength As String = formData(NameOf(dataLength)) + If _fileSize.ToString <> dataLength Then + Throw New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") + End If + + Dim fileName As String = formData("file") + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + End If + Catch ioEx As IOException + Throw + Catch ex As Exception + Stop + ' ignore it will be handled elsewhere + End Try End Using End If response.StatusCode = 200 @@ -133,5 +142,52 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return listener End Function + ''' + ''' Parses a and gets the fileName of the uploaded file + ''' and the lenght of the data file in bytes + ''' + ''' + ''' + ''' A that contains the filename and lenght of the data file. + ''' + Public Function GetMultipartFormData(request As HttpListenerRequest) As Dictionary(Of String, String) + Dim result As New Dictionary(Of String, String) + + If request.ContentType.StartsWith("multipart/form-data", StringComparison.OrdinalIgnoreCase) Then + Dim boundary As String = GetBoundary(request.ContentType) + Using reader As New StreamReader(request.InputStream, request.ContentEncoding) + Dim content As String = reader.ReadToEnd() + Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) + + For Each part As String In parts + If part.Trim() <> "--" Then + Dim separator As String() = New String() {Environment.NewLine} + Dim lines As String() = part.Split(separator, StringSplitOptions.RemoveEmptyEntries) + If lines.Length > 2 Then + Dim headerParts As String() = lines(0).Split({":"c}, count:=2) + If headerParts.Length = 2 _ + AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", + comparisonType:=StringComparison.OrdinalIgnoreCase) Then + + Dim nameMatch As Match = Regex.Match(input:=headerParts(1), pattern:="name=""(?[^""]+)""") + If nameMatch.Success Then + Dim name As String = nameMatch.Groups("name").Value + Dim value As String = headerParts(1).Split("filename=")(1).Trim(""""c) + result.Add(name, value.Trim()) + If lines.Length > 2 Then + result.Add("dataLength", lines(1).Length.ToString) + End If + Exit For + End If + End If + End If + End If + Next + End Using + End If + + Return result + End Function + End Class End Namespace From 8693579564f24f3b90adca60d8f902eeca12fb33 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 03:14:22 -0800 Subject: [PATCH 034/119] Address PF for tabbing after _ --- .../System/Windows/Forms/DownloadFileTests.vb | 36 +++++++++---------- .../System/Windows/Forms/UploadFileTests.vb | 20 +++++------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index f98dccfd412..141d61b5b04 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -110,7 +110,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -186,7 +186,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -210,7 +210,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -361,7 +361,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -416,7 +416,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -631,7 +631,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -716,7 +716,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -767,7 +767,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -817,7 +817,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -890,7 +890,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1029,7 +1029,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(LargeTestFileSize) End Using End Sub @@ -1075,7 +1075,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1175,7 +1175,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1203,7 +1203,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1362,7 +1362,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1447,7 +1447,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1498,7 +1498,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub @@ -1548,7 +1548,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(SmallTestFileSize) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index a39bb0ca2fe..261688e980b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -145,8 +145,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -169,8 +169,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -193,8 +193,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -450,8 +450,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub UploadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) @@ -1297,8 +1297,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub From 21106085ed49199652841d809ea2205e80139832 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 03:54:11 -0800 Subject: [PATCH 035/119] Add char specifier to Inline single character strings --- .../System/Windows/Forms/DownloadFileTests.vb | 12 ++++++------ .../System/Windows/Forms/FileSystemProxyTests.vb | 2 +- .../System/Windows/Forms/UploadFileTests.vb | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 4f779b0afaf..f0a41948441 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -503,8 +503,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) @@ -586,8 +586,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) @@ -1317,8 +1317,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb index 523cf1fb5bd..dab64ebc6fc 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb @@ -283,7 +283,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests - + Public Sub OpenTextFieldParserProxyTest(delimiter As String) Dim sourceDirectoryName As String = CreateTempDirectory() Dim fileCSV As String = CreateTempFile(sourceDirectoryName) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index ae71c54a258..072218e3127 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -513,8 +513,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub UploadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) @@ -1112,8 +1112,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + + Public Sub UploadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) From a3ead0cc16e113302f90eb4ea716536e23292e0f Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 05:02:52 -0800 Subject: [PATCH 036/119] Fix some missing spaces --- .../VisualBasic/Devices/Network.DownloadFile.vb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 46ebeb6a771..082b0a651d5 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -326,7 +326,7 @@ Namespace Microsoft.VisualBasic.Devices ' Don't use passive mode if we're showing UI client.UseNonPassiveFtp = showUI - 'Construct the local file. This will validate the full name and path + ' Construct the local file. This will validate the full name and path Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( path:=destinationFileName, paramName:=NameOf(destinationFileName)) @@ -337,7 +337,7 @@ Namespace Microsoft.VisualBasic.Devices Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) End If - 'Throw if the file exists and the user doesn't want to overwrite + ' Throw if the file exists and the user doesn't want to overwrite If IO.File.Exists(fullFilename) And Not overwrite Then Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) End If @@ -349,7 +349,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = GetProgressDialog(address.AbsolutePath, fullFilename, showUI) - 'Check to see if the target directory exists. If it doesn't, create it + ' Check to see if the target directory exists. If it doesn't, create it Dim targetDirectory As String = IO.Path.GetDirectoryName(fullFilename) ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect @@ -361,13 +361,13 @@ Namespace Microsoft.VisualBasic.Devices IO.Directory.CreateDirectory(targetDirectory) End If - 'Create the copier + ' Create the copier Dim copier As New WebClientCopy(client, dialog) - 'Download the file + ' Download the file copier.DownloadFile(address, fullFilename) - 'Handle a dialog cancel + ' Handle a dialog cancel If showUI _ AndAlso Environment.UserInteractive _ AndAlso onUserCancel = UICancelOption.ThrowException _ From 41f8a193f9dafeae604fe30301c60d564ddcf68c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 16:07:41 -0800 Subject: [PATCH 037/119] Use PathSeparatorTestData --- .../System/Windows/Forms/DownloadFileTests.vb | 9 +++------ .../Windows/Forms/TestUtilitiesTests.vb | 7 +++++++ .../System/Windows/Forms/UploadFileTests.vb | 9 +++------ .../TestData/PathSeparatorTestData.vb | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index f0a41948441..99618feddd4 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -503,8 +503,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) @@ -586,8 +585,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) @@ -1317,8 +1315,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb index 970251e2476..c006f2d36b1 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb @@ -23,6 +23,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests testClass.Any.Should.BeTrue() End Sub + + Public Sub PathSeparatorDataIteratorTests() + Dim testClass As New PathSeparatorTestData + testClass.IEnumerable_GetEnumerator.Should.NotBeNull() + testClass.Any.Should.BeTrue() + End Sub + Public Sub TimeTestDataIteratorTests() Dim testClass As New TimeTestData diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 072218e3127..aa2f8e259de 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -457,8 +457,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub UploadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) @@ -513,8 +512,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub UploadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) @@ -1112,8 +1110,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - - + Public Sub UploadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb new file mode 100644 index 00000000000..fe324539cd7 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb @@ -0,0 +1,19 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class PathSeparatorTestData + Implements IEnumerable(Of Object()) + + Public Iterator Function GetEnumerator() As IEnumerator(Of Object()) Implements IEnumerable(Of Object()).GetEnumerator + Yield {IO.Path.DirectorySeparatorChar} + Yield {IO.Path.AltDirectorySeparatorChar} + End Function + + Public Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator + Return GetEnumerator() + End Function + + End Class +End Namespace From eb6ba5c6c2157dd6c75c209165c6f6f1d6181404 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 17:27:53 -0800 Subject: [PATCH 038/119] Fix order of functions --- .../Devices/Network.DownloadFile.vb | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 082b0a651d5..59da1da477f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -127,6 +127,41 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel:=UICancelOption.ThrowException) End Sub + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' to the remote file, + ''' + ''' Name and path of file where download is saved. + ''' + ''' The name of the user performing the download. + ''' The user's password. + ''' Indicates whether or not to show a progress bar. + ''' Time allotted before giving up on a connection. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + + Public Sub DownloadFile( + address As Uri, + destinationFileName As String, + userName As String, + password As String, + showUI As Boolean, + connectionTimeout As Integer, + overwrite As Boolean) + + DownloadFile( + address, + destinationFileName, + userName, + password, + showUI, + connectionTimeout, + overwrite, + UICancelOption.ThrowException) + End Sub + ''' ''' Downloads a file from the network to the specified path. ''' @@ -175,40 +210,6 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel) End Sub - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' to the remote file, - ''' - ''' Name and path of file where download is saved. - ''' - ''' The name of the user performing the download. - ''' The user's password. - ''' Indicates whether or not to show a progress bar. - ''' Time allotted before giving up on a connection. - ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' - Public Sub DownloadFile( - address As Uri, - destinationFileName As String, - userName As String, - password As String, - showUI As Boolean, - connectionTimeout As Integer, - overwrite As Boolean) - - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI, - connectionTimeout, - overwrite, - UICancelOption.ThrowException) - End Sub - ''' ''' Downloads a file from the network to the specified path. ''' From 180ffc3468e381ebe2fec35f0aa88d9f96fcbf8c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 17:50:48 -0800 Subject: [PATCH 039/119] Sort and cleanup comments --- .../Devices/Network.DownloadFile.vb | 255 +++++++++--------- 1 file changed, 135 insertions(+), 120 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 26a8a982387..f4fae0605aa 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -40,6 +40,30 @@ Namespace Microsoft.VisualBasic.Devices End Try End Sub + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' to the remote file. + ''' Name and path of file where download is saved. + Public Sub DownloadFile(address As Uri, destinationFileName As String) + Try + + DownloadFileAsync( + addressUri:=address, + destinationFileName, + userName:=DEFAULT_USERNAME, + password:=DEFAULT_PASSWORD, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + End Try + End Sub + ''' ''' Downloads a file from the network to the specified path. ''' @@ -70,6 +94,36 @@ Namespace Microsoft.VisualBasic.Devices End Try End Sub + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' to the remote file. + ''' Name and path of file where download is saved. + ''' The name of the user performing the download. + ''' The user's password. + Public Sub DownloadFile( + address As Uri, + destinationFileName As String, + userName As String, + password As String) + + Try + DownloadFileAsync( + addressUri:=address, + destinationFileName, + userName, + password, + dialog:=Nothing, + connectionTimeout:=DEFAULT_TIMEOUT, + overwrite:=False).Wait() + Catch ex As Exception + If ex.InnerException IsNot Nothing Then + Throw ex.InnerException + End If + Throw + End Try + End Sub + ''' ''' Downloads a file from the network to the specified path. ''' @@ -138,56 +192,43 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' Address to the remote file, http, ftp etc... - ''' Name and path of file where download is saved. + ''' to the remote file. + ''' + ''' Name and path of file where download is saved. + ''' ''' The name of the user performing the download. ''' The user's password. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be - ''' overwritten if local file already exists. - ''' - ''' - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. ''' Public Sub DownloadFile( - address As String, + address As Uri, destinationFileName As String, userName As String, password As String, showUI As Boolean, connectionTimeout As Integer, - overwrite As Boolean, - onUserCancel As UICancelOption) + overwrite As Boolean) - ' We're safe from DownloadFile(Nothing, ...) due to overload failure (DownloadFile(String,...) vs. - ' DownloadFile(Uri,...)). - ' However, it is good practice to verify address before calling Trim. - If String.IsNullOrWhiteSpace(address) Then + If address Is Nothing Then Throw VbUtils.GetArgumentNullException(NameOf(address)) End If - Dim addressUri As Uri = GetUri(address.Trim()) - Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) Dim dialog As ProgressDialog = Nothing - Try - If showUI AndAlso Environment.UserInteractive Then - ' Construct the local file. This will validate the full name and path - Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( - path:=destinationFileName, - paramName:=NameOf(destinationFileName)) - - dialog = GetProgressDialog(address, destinationFileName, showUI) - End If + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + Try + dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( - addressUri, + addressUri:=address, destinationFileName, networkCredentials, dialog, connectionTimeout, overwrite, - onUserCancel) + onUserCancel:=UICancelOption.ThrowException) If t.IsFaulted Then ' IsFaulted will be true if any parameters are bad @@ -212,37 +253,57 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' Uri to the remote file. + ''' Address to the remote file, http, ftp etc... ''' Name and path of file where download is saved. - ''' The credentials of the user performing the download. + ''' The name of the user performing the download. + ''' The user's password. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates whether or not the file should be overwritten + ''' if local file already exists. + ''' + ''' + ''' Indicates what to do if user cancels dialog (either or do nothing). ''' - ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( - address As Uri, + address As String, destinationFileName As String, - networkCredentials As ICredentials, + userName As String, + password As String, showUI As Boolean, connectionTimeout As Integer, - overwrite As Boolean) + overwrite As Boolean, + onUserCancel As UICancelOption) - If address Is Nothing Then + ' We're safe from DownloadFile(Nothing, ...) due to overload failure (DownloadFile(String,...) + ' vs. DownloadFile(Uri,...)). + ' However, it is good practice to verify address before calling Trim. + If String.IsNullOrWhiteSpace(address) Then Throw VbUtils.GetArgumentNullException(NameOf(address)) End If + Dim addressUri As Uri = GetUri(address.Trim()) + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) Dim dialog As ProgressDialog = Nothing Try - dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + If showUI AndAlso Environment.UserInteractive Then + ' Construct the local file. This will validate the full name and path + Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( + path:=destinationFileName, + paramName:=NameOf(destinationFileName)) + + dialog = GetProgressDialog(address, destinationFileName, showUI) + End If + Dim t As Task = DownloadFileAsync( - addressUri:=address, + addressUri, destinationFileName, networkCredentials, dialog, connectionTimeout, - overwrite) + overwrite, + onUserCancel) If t.IsFaulted Then ' IsFaulted will be true if any parameters are bad @@ -267,22 +328,28 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' Uri to the remote file. - ''' Name and path of file where download is saved. - ''' The credentials of the user performing the download. + ''' to the remote file. + ''' + ''' Name and path of file where download is saved. + ''' + ''' The name of the user performing the download. + ''' The user's password. ''' Indicates whether or not to show a progress bar. - ''' Time allotted before giving up on a connection. + ''' + ''' Time allotted before giving up on a connection. + ''' ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates whether or not the file should be overwritten + ''' if local file already exists. ''' ''' - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' Indicates what to do if user cancels dialog (either or do nothing). ''' - ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - networkCredentials As ICredentials, + userName As String, + password As String, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean, @@ -304,7 +371,8 @@ Namespace Microsoft.VisualBasic.Devices Dim t As Task = DownloadFileAsync( addressUri:=address, destinationFileName, - networkCredentials, + userName, + password, dialog, connectionTimeout, overwrite, @@ -333,20 +401,24 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' Uri to the remote file. - ''' Name and path of file where download is saved. - ''' The name of the user performing the download. - ''' The user's password. + ''' to the remote file. + ''' + ''' Name and path of file where download is saved. + ''' + ''' + ''' The credentials of the user performing the download. + ''' ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates whether or not the file should be overwritten + ''' if local file already exists. ''' + ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - userName As String, - password As String, + networkCredentials As ICredentials, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean) @@ -356,8 +428,6 @@ Namespace Microsoft.VisualBasic.Devices End If Dim dialog As ProgressDialog = Nothing - Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - Try dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( @@ -366,8 +436,7 @@ Namespace Microsoft.VisualBasic.Devices networkCredentials, dialog, connectionTimeout, - overwrite, - onUserCancel:=UICancelOption.ThrowException) + overwrite) If t.IsFaulted Then ' IsFaulted will be true if any parameters are bad @@ -392,23 +461,23 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' Uri to the remote file. + ''' to the remote file. ''' Name and path of file where download is saved. - ''' The name of the user performing the download. - ''' The user's password. + ''' The of the user performing the download. ''' Indicates whether or not to show a progress bar. ''' Time allotted before giving up on a connection. ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates whether or not the file should be overwritten + ''' if local file already exists. ''' ''' - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' Indicates what to do if user cancels dialog (either or do nothing). ''' + ''' Calls to all the other overloads will come through here. Public Sub DownloadFile( address As Uri, destinationFileName As String, - userName As String, - password As String, + networkCredentials As ICredentials, showUI As Boolean, connectionTimeout As Integer, overwrite As Boolean, @@ -430,8 +499,7 @@ Namespace Microsoft.VisualBasic.Devices Dim t As Task = DownloadFileAsync( addressUri:=address, destinationFileName, - userName, - password, + networkCredentials, dialog, connectionTimeout, overwrite, @@ -457,58 +525,5 @@ Namespace Microsoft.VisualBasic.Devices End Try End Sub - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' Uri to the remote file. - ''' Name and path of file where download is saved. - Public Sub DownloadFile(address As Uri, destinationFileName As String) - Try - - DownloadFileAsync( - addressUri:=address, - destinationFileName, - userName:=DEFAULT_USERNAME, - password:=DEFAULT_PASSWORD, - dialog:=Nothing, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False).Wait() - Catch ex As Exception - If ex.InnerException IsNot Nothing Then - Throw ex.InnerException - End If - Throw - End Try - End Sub - - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' Uri to the remote file. - ''' Name and path of file where download is saved. - ''' The name of the user performing the download. - ''' The user's password. - Public Sub DownloadFile( - address As Uri, - destinationFileName As String, - userName As String, - password As String) - - Try - DownloadFileAsync( - addressUri:=address, - destinationFileName, - userName, - password, - dialog:=Nothing, - connectionTimeout:=DEFAULT_TIMEOUT, - overwrite:=False).Wait() - Catch ex As Exception - If ex.InnerException IsNot Nothing Then - Throw ex.InnerException - End If - Throw - End Try - End Sub End Class End Namespace From a7b20cc579fe14eb7c310d8c700f5cae4a00c7c8 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 17:56:06 -0800 Subject: [PATCH 040/119] Minor formatting changes --- .../VisualBasic/Devices/Network.DownloadFile.vb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 59da1da477f..6cc637e161b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -130,7 +130,7 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Downloads a file from the network to the specified path. ''' - ''' to the remote file, + ''' to the remote file. ''' ''' Name and path of file where download is saved. ''' @@ -141,7 +141,6 @@ Namespace Microsoft.VisualBasic.Devices ''' ''' Indicates whether or not the file should be overwritten if local file already exists. ''' - Public Sub DownloadFile( address As Uri, destinationFileName As String, @@ -196,8 +195,6 @@ Namespace Microsoft.VisualBasic.Devices End If Dim addressUri As Uri = GetUri(address.Trim()) - - ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) DownloadFile( @@ -314,7 +311,9 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel As UICancelOption) If connectionTimeout <= 0 Then - Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + Throw VbUtils.GetArgumentExceptionWithArgName( + argumentName:=NameOf(connectionTimeout), + resourceKey:=SR.Network_BadConnectionTimeout) End If If address Is Nothing Then From 4a920ddfccef2eec382b3a6e2a9d8a573c12ed7d Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 18:09:49 -0800 Subject: [PATCH 041/119] Clean up XML Comments in ClipBoardProxy --- .../VisualBasic/MyServices/ClipboardProxy.vb | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index 7a54f04eefc..06fd1fd8392 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -25,7 +25,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Removes all data from the . + ''' Removes everything from the . ''' Public Sub Clear() Clipboard.Clear() @@ -68,7 +68,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether there is text data on the in format. + ''' Indicates whether or not text is available on the . ''' ''' if text is available, otherwise . Public Function ContainsText() As Boolean @@ -86,7 +86,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Retrieves an audio stream from the . + ''' Gets an audio stream from the . ''' ''' The audio stream as a . Public Function GetAudioStream() As Stream @@ -110,9 +110,9 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Retrieves the data that is currently on the system . + ''' Gets a from the clipboard. ''' - ''' The . + ''' The data object. ''' This gives the ability to save an object in multiple formats. Public Function GetDataObject() As IDataObject @@ -120,7 +120,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Retrieves a collection of file names from the . + ''' Gets a file drop list from the clipboard. ''' ''' The list of file paths as a . Public Function GetFileDropList() As StringCollection @@ -130,31 +130,30 @@ Namespace Microsoft.VisualBasic.MyServices ''' ''' Retrieves an from the . ''' - ''' + ''' The . Public Function GetImage() As Image Return Clipboard.GetImage() End Function ''' - ''' Retrieves text data from the in the format. + ''' Gets text from the . ''' - ''' The text as a . + ''' The text as a . Public Function GetText() As String Return Clipboard.GetText() End Function ''' - ''' Retrieves text data from the in the format indicated by the specified - ''' value. + ''' Gets text from the clipboard saved in the passed in format. ''' ''' The type of text to get. - ''' The text as a . + ''' The text as a . Public Function GetText(format As TextDataFormat) As String Return Clipboard.GetText(format) End Function ''' - ''' Clears the and then adds data in the format. + ''' Saves the passed in audio byte array to the . ''' ''' The byte array to be saved. Public Sub SetAudio(audioBytes As Byte()) @@ -162,7 +161,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Clears the and then adds data in the format. + ''' Saves the passed in audio stream to the . ''' ''' The to be saved. Public Sub SetAudio(audioStream As Stream) @@ -170,7 +169,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Clears the and then adds data in the specified format. + ''' Saves the passed in data to the in the passed in format. ''' ''' The format in which to save the data. ''' The data to be saved. @@ -179,9 +178,9 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Places nonpersistent on the . + ''' Saves a to the clipboard. ''' - ''' The to be saved. + ''' The data object to be saved. ''' This gives the ability to save an object in multiple formats. Public Sub SetDataObject(data As DataObject) @@ -189,8 +188,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Clears the > and then adds a collection of file names - ''' in the format. + ''' Saves the passed in file drop list to the . ''' ''' The file drop list as a . Public Sub SetFileDropList(filePaths As StringCollection) From c633809bff424f6174eca8ad90e240b399a7866f Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 18:20:55 -0800 Subject: [PATCH 042/119] Update cooment for Cllipboard to restore changes lost in merge --- .../VisualBasic/MyServices/ClipboardProxy.vb | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index ee2e28cc510..f52a19afede 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices Namespace Microsoft.VisualBasic.MyServices ''' - ''' A class that wraps System.Windows.Forms.Clipboard so that + ''' A class that wraps so that ''' a clipboard can be instanced. ''' @@ -25,22 +25,26 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Removes everything from the clipboard. + ''' Removes everything from the . ''' Public Sub Clear() Clipboard.Clear() End Sub ''' - ''' Indicates whether or not there's an audio stream saved to the clipboard. + ''' Indicates whether or not there's an audio stream saved to the . ''' - ''' if an audio stream is available, otherwise . + ''' + ''' see langword="True"/> if an audio is available, + ''' otherwise . + ''' Public Function ContainsAudio() As Boolean Return Clipboard.ContainsAudio() End Function ''' - ''' Indicates whether or not there is data on the clipboard in the passed in format. + ''' Indicates whether or not there is data on the in the passed in format. + ''' or can be converted to that format. ''' ''' ''' if there's data in the passed in format, otherwise . @@ -57,7 +61,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicate whether or not an image has been saved to the clipboard. + ''' Indicate whether or not an image has been saved to the . ''' ''' if an image is available, otherwise . Public Function ContainsImage() As Boolean @@ -65,7 +69,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard. + ''' Indicates whether or not text is available on the . ''' ''' if text is available, otherwise . Public Function ContainsText() As Boolean @@ -73,7 +77,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard in + ''' Indicates whether or not text is available on the in ''' the passed in format. ''' ''' The type of text being checked for. @@ -83,15 +87,15 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an audio stream from the clipboard. + ''' Gets an audio stream from the . ''' - ''' The audio stream as a Stream. + ''' The audio stream as a . Public Function GetAudioStream() As Stream Return Clipboard.GetAudioStream() End Function ''' - ''' Gets data from the clipboard that's been saved in the passed in format. + ''' Gets data from the that's been saved in the passed in format. ''' ''' The type of data being sought. ''' The data. @@ -125,7 +129,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an Image from the clipboard. + ''' Retrieves an from the . ''' ''' The image. Public Function GetImage() As Image @@ -133,7 +137,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets text from the clipboard. + ''' Gets text from the . ''' ''' The text as a String. Public Function GetText() As String @@ -150,7 +154,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Saves the passed in audio byte array to the clipboard. + ''' Saves the passed in audio byte array to the . ''' ''' The byte array to be saved. Public Sub SetAudio(audioBytes As Byte()) @@ -158,15 +162,15 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in audio stream to the clipboard. + ''' Saves the passed in audio stream to the . ''' - ''' The stream to be saved. + ''' The to be saved. Public Sub SetAudio(audioStream As Stream) Clipboard.SetAudio(audioStream) End Sub ''' - ''' Saves the passed in data to the clipboard in the passed in format. + ''' Saves the passed in data to the in the passed in format. ''' ''' The format in which to save the data. ''' The data to be saved. @@ -185,7 +189,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in file drop list to the clipboard. + ''' Saves the passed in file drop list to the . ''' ''' The file drop list as a . Public Sub SetFileDropList(filePaths As StringCollection) From 0302c9cdc77e19ce2e48d484fed4e62909c96508 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 18:21:57 -0800 Subject: [PATCH 043/119] Sync ClipBoardProxy --- .../VisualBasic/MyServices/ClipboardProxy.vb | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index 06fd1fd8392..f52a19afede 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -32,15 +32,18 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Indicates whether there is data on the in the format./>. + ''' Indicates whether or not there's an audio stream saved to the . ''' - ''' if an audio is available, otherwise . + ''' + ''' see langword="True"/> if an audio is available, + ''' otherwise . + ''' Public Function ContainsAudio() As Boolean Return Clipboard.ContainsAudio() End Function ''' - ''' Indicates whether or not there is data on the in the passed in format + ''' Indicates whether or not there is data on the in the passed in format. ''' or can be converted to that format. ''' ''' @@ -50,8 +53,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether there is data on the that is in the - ''' format or can be converted to that format. + ''' Indicates whether or not a file drop list has been saved to the clipboard. ''' ''' if a file drop list is available, otherwise . Public Function ContainsFileDropList() As Boolean @@ -59,8 +61,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether there Is data on the that Is in the - ''' format or can be converted to that format. + ''' Indicate whether or not an image has been saved to the . ''' ''' if an image is available, otherwise . Public Function ContainsImage() As Boolean @@ -76,8 +77,8 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether there is text data on the in the format indicated by the - ''' specified value. + ''' Indicates whether or not text is available on the in + ''' the passed in format. ''' ''' The type of text being checked for. ''' if text is available, otherwise . @@ -130,7 +131,7 @@ Namespace Microsoft.VisualBasic.MyServices ''' ''' Retrieves an from the . ''' - ''' The . + ''' The image. Public Function GetImage() As Image Return Clipboard.GetImage() End Function @@ -138,7 +139,7 @@ Namespace Microsoft.VisualBasic.MyServices ''' ''' Gets text from the . ''' - ''' The text as a . + ''' The text as a String. Public Function GetText() As String Return Clipboard.GetText() End Function @@ -147,7 +148,7 @@ Namespace Microsoft.VisualBasic.MyServices ''' Gets text from the clipboard saved in the passed in format. ''' ''' The type of text to get. - ''' The text as a . + ''' The text as a String. Public Function GetText(format As TextDataFormat) As String Return Clipboard.GetText(format) End Function From 4f9d051f7b2116259083cd3757dd1f42e625702a Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:05:00 -0800 Subject: [PATCH 044/119] Cleanup NetworkUploadFile and consolidate is interactive --- .../VisualBasic/Devices/Network.UploadFile.vb | 16 ++++------ .../VisualBasic/Devices/NetworkUtilities.vb | 29 +++++++++++-------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 40ea15623a6..fb68d48c432 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -288,16 +288,10 @@ Namespace Microsoft.VisualBasic.Devices client.Credentials = networkCredentials End If - Dim dialog As ProgressDialog = Nothing - If showUI AndAlso Environment.UserInteractive Then - dialog = New ProgressDialog With { - .Text = GetResourceString(SR.ProgressDialogUploadingTitle, sourceFileName), - .LabelText = GetResourceString( - resourceKey:=SR.ProgressDialogUploadingLabel, - sourceFileName, - address.AbsolutePath) - } - End If + Dim dialog As ProgressDialog = GetProgressDialog( + address:=address.AbsolutePath, + fileNameWithPath:=sourceFileName, + showUI) ' Create the copier Dim copier As New WebClientCopy(client, dialog) @@ -306,7 +300,7 @@ Namespace Microsoft.VisualBasic.Devices copier.UploadFile(sourceFileName, address) ' Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then Throw New OperationCanceledException() End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index bb37591afa9..6deb2014cc8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -56,26 +56,27 @@ Namespace Microsoft.VisualBasic.Devices ''' Centralize setup a to be used with FileDownload and FileUpload. ''' ''' Address to the remote file, http, ftp etc... - ''' Name and path of file where download is saved. + ''' Name and path of file where download is saved. ''' Indicates whether or not to show a progress bar. - ''' . + ''' + ''' if InteractiveEnvironment + ''' otherwise return . + ''' Friend Function GetProgressDialog( address As String, - destinationFileName As String, + fileNameWithPath As String, showUI As Boolean) As ProgressDialog - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then 'Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( - path:=destinationFileName, - paramName:=NameOf(destinationFileName)) + path:=fileNameWithPath, + paramName:=NameOf(fileNameWithPath)) Return New ProgressDialog With { - .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = VbUtils.GetResourceString( - resourceKey:=SR.ProgressDialogDownloadingLabel, - address, - fullFilename) - } + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), + .LabelText = VbUtils.GetResourceString( + resourceKey:=SR.ProgressDialogDownloadingLabel, + address, fullFilename)} End If Return Nothing End Function @@ -100,5 +101,9 @@ Namespace Microsoft.VisualBasic.Devices End Try End Function + Friend Function InteractiveEnvironment(showUI As Boolean) As Boolean + Return showUI AndAlso Environment.UserInteractive + End Function + End Module End Namespace From 5769aee46c4ccca98931dffca81a4c7c7d324a81 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:18:47 -0800 Subject: [PATCH 045/119] Add and use InteractiveEnvironment --- .../Devices/Network.DownloadFile.vb | 16 +++---------- .../VisualBasic/Devices/Network.UploadFile.vb | 2 +- .../VisualBasic/Devices/NetworkUtilities.vb | 23 +++++++++++-------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index f4fae0605aa..671e0223c65 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -148,7 +148,6 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try dialog = GetProgressDialog(address, destinationFileName, showUI) - Dim t As Task = DownloadFileAsync( address, destinationFileName, @@ -216,9 +215,8 @@ Namespace Microsoft.VisualBasic.Devices Throw VbUtils.GetArgumentNullException(NameOf(address)) End If - Dim dialog As ProgressDialog = Nothing Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - + Dim dialog As ProgressDialog = Nothing Try dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( @@ -287,15 +285,7 @@ Namespace Microsoft.VisualBasic.Devices Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) Dim dialog As ProgressDialog = Nothing Try - If showUI AndAlso Environment.UserInteractive Then - ' Construct the local file. This will validate the full name and path - Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( - path:=destinationFileName, - paramName:=NameOf(destinationFileName)) - - dialog = GetProgressDialog(address, destinationFileName, showUI) - End If - + dialog = GetProgressDialog(addressUri.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( addressUri, destinationFileName, @@ -495,7 +485,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try - dialog = GetProgressDialog(address.AbsolutePath, destinationFileName, showUI) + dialog = GetProgressDialog(address:=address.AbsolutePath, fileNameWithPath:=destinationFileName, showUI) Dim t As Task = DownloadFileAsync( addressUri:=address, destinationFileName, diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index f8f5fab06c7..b504e3300ff 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -288,7 +288,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then ' Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=sourceFileName, diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index bb37591afa9..72a687c6b05 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -56,26 +56,27 @@ Namespace Microsoft.VisualBasic.Devices ''' Centralize setup a to be used with FileDownload and FileUpload. ''' ''' Address to the remote file, http, ftp etc... - ''' Name and path of file where download is saved. + ''' Name and path of file where download is saved. ''' Indicates whether or not to show a progress bar. - ''' . + ''' + ''' if InteractiveEnvironment + ''' otherwise return . + ''' Friend Function GetProgressDialog( address As String, - destinationFileName As String, + fileNameWithPath As String, showUI As Boolean) As ProgressDialog - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then 'Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( - path:=destinationFileName, - paramName:=NameOf(destinationFileName)) + path:=fileNameWithPath, + paramName:=NameOf(fileNameWithPath)) Return New ProgressDialog With { .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), .LabelText = VbUtils.GetResourceString( resourceKey:=SR.ProgressDialogDownloadingLabel, - address, - fullFilename) - } + address, fullFilename)} End If Return Nothing End Function @@ -100,5 +101,9 @@ Namespace Microsoft.VisualBasic.Devices End Try End Function + Friend Function InteractiveEnvironment(showUI As Boolean) As Boolean + Return showUI AndAlso Environment.UserInteractive + End Function + End Module End Namespace From 1a977c9898448fa3de48b428d1bd600fdcfbef6b Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:27:30 -0800 Subject: [PATCH 046/119] Fix merge issues --- .../Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 9 +++------ .../Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 8 ++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index b504e3300ff..16921d6bad9 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -287,15 +287,12 @@ Namespace Microsoft.VisualBasic.Devices New HttpClientHandler With {.Credentials = networkCredentials}) Dim dialog As ProgressDialog = Nothing Try - - If InteractiveEnvironment(showUI) Then - ' Construct the local file. This will validate the full name and path - Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( + ' Construct the local file. This will validate the full name and path + Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=sourceFileName, paramName:=NameOf(sourceFileName)) - dialog = GetProgressDialog(address.AbsolutePath, sourceFileName, showUI) - End If + dialog = GetProgressDialog(address.AbsolutePath, sourceFileName, showUI) Dim t As Task = UploadFileAsync( sourceFileName, diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 72a687c6b05..dc6aa6b022a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -73,10 +73,10 @@ Namespace Microsoft.VisualBasic.Devices path:=fileNameWithPath, paramName:=NameOf(fileNameWithPath)) Return New ProgressDialog With { - .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = VbUtils.GetResourceString( - resourceKey:=SR.ProgressDialogDownloadingLabel, - address, fullFilename)} + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), + .LabelText = VbUtils.GetResourceString( + resourceKey:=SR.ProgressDialogDownloadingLabel, + address, fullFilename)} End If Return Nothing End Function From 15aa370a8d0c45fe665bcbd1286160851cba8ebc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:32:14 -0800 Subject: [PATCH 047/119] Fix spacing --- .../src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 5 ++++- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 16921d6bad9..e7115a45972 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -292,7 +292,10 @@ Namespace Microsoft.VisualBasic.Devices path:=sourceFileName, paramName:=NameOf(sourceFileName)) - dialog = GetProgressDialog(address.AbsolutePath, sourceFileName, showUI) + dialog = GetProgressDialog( + address:=address.AbsolutePath, + fileNameWithPath:=sourceFileName, + showUI) Dim t As Task = UploadFileAsync( sourceFileName, diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index dc6aa6b022a..0f217e544a3 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -76,7 +76,7 @@ Namespace Microsoft.VisualBasic.Devices .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), .LabelText = VbUtils.GetResourceString( resourceKey:=SR.ProgressDialogDownloadingLabel, - address, fullFilename)} + address, fullFilename)} End If Return Nothing End Function From 8ae6b248fe352fb44bd8f5ec0659788af0bde6d5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:34:33 -0800 Subject: [PATCH 048/119] Move clientHandler initialization --- .../Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index e7115a45972..666a163a2c6 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -282,9 +282,6 @@ Namespace Microsoft.VisualBasic.Devices End If ' Get network credentials - Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, - New HttpClientHandler, - New HttpClientHandler With {.Credentials = networkCredentials}) Dim dialog As ProgressDialog = Nothing Try ' Construct the local file. This will validate the full name and path @@ -292,6 +289,10 @@ Namespace Microsoft.VisualBasic.Devices path:=sourceFileName, paramName:=NameOf(sourceFileName)) + Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) + dialog = GetProgressDialog( address:=address.AbsolutePath, fileNameWithPath:=sourceFileName, From d74231adfcd7ea3c372b64f09bed7e8b030548a8 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:41:46 -0800 Subject: [PATCH 049/119] Fix formatting --- .../VisualBasic/Devices/Network.UploadFile.vb | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 666a163a2c6..c1595f44107 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -286,12 +286,13 @@ Namespace Microsoft.VisualBasic.Devices Try ' Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( - path:=sourceFileName, - paramName:=NameOf(sourceFileName)) + path:=sourceFileName, + paramName:=NameOf(sourceFileName)) - Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, - New HttpClientHandler, - New HttpClientHandler With {.Credentials = networkCredentials}) + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) dialog = GetProgressDialog( address:=address.AbsolutePath, @@ -299,12 +300,12 @@ Namespace Microsoft.VisualBasic.Devices showUI) Dim t As Task = UploadFileAsync( - sourceFileName, - addressUri:=address, - clientHandler, - dialog, - connectionTimeout, - onUserCancel) + sourceFileName, + addressUri:=address, + clientHandler, + dialog, + connectionTimeout, + onUserCancel) If t.IsFaulted Then ' IsFaulted will be true if any parameters are bad From cf837e1dc452e910b2067b2f9c3792fef42c0283 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:51:16 -0800 Subject: [PATCH 050/119] Fix formatting --- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 0f217e544a3..6deb2014cc8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -76,7 +76,7 @@ Namespace Microsoft.VisualBasic.Devices .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), .LabelText = VbUtils.GetResourceString( resourceKey:=SR.ProgressDialogDownloadingLabel, - address, fullFilename)} + address, fullFilename)} End If Return Nothing End Function From 29b729696d357ec10855192b65ad9edddcc4c1d1 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 02:31:42 -0800 Subject: [PATCH 051/119] Get all tests to work --- .../Devices/Network.DownloadFileAsync.vb | 2 +- .../Devices/Network.UploadFileAsync.vb | 6 +- .../MyServices/Internal/HttpClientCopy.vb | 28 +++-- .../Internal/ProgressableStreamContent.vb | 20 +-- .../System/Windows/Forms/DownloadFileTests.vb | 38 +++--- .../System/Windows/Forms/UploadFileTests.vb | 116 +++++++++--------- 6 files changed, 105 insertions(+), 105 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index 6cbf6ff8af9..e1a22b78625 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -154,12 +154,12 @@ Namespace Microsoft.VisualBasic.Devices Throw VbUtils.GetArgumentNullException(NameOf(addressUri)) End If + ' Set credentials if we have any Dim client As HttpClient = If(clientHandler Is Nothing, New HttpClient(), New HttpClient(clientHandler) ) - ' Set credentials if we have any client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) 'Construct the local file. This will validate the full name and path diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb index 064f038175a..aae79b77762 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -54,9 +54,9 @@ Namespace Microsoft.VisualBasic.Devices Dim client As HttpClient = If(clientHandler Is Nothing, New HttpClient(), - New HttpClient(clientHandler) - ) - client.Timeout = TimeSpan.FromMilliseconds(connectionTimeout) + New HttpClient(clientHandler)) + client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) + 'Create the copier Dim copier As New HttpClientCopy(client, dialog) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index 085e7d1fccb..d1c2a814f8b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -196,19 +196,19 @@ Namespace Microsoft.VisualBasic.MyServices.Internal _cancelTokenSourcePost = New CancellationTokenSource() Dim contentLength As Long = New FileInfo(filePath).Length Dim totalBytesRead As Long = 0 + Dim progress As Action(Of Long, Long) = + Sub(bytesRead As Long, streamLength As Long) + totalBytesRead += bytesRead + If _progressDialog IsNot Nothing Then + Dim progressPercentage As Integer = CInt(totalBytesRead / streamLength * 100) + InvokeIncrement(progressPercentage) + End If + End Sub Using linkedStreamCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceReadStream.Token, _cancelTokenSourcePost.Token, externalToken) Try Using content As New MultipartFormDataContent("----boundary") - Dim progress As Action(Of Long, Long) = - Sub(bytesRead As Long, streamLength As Long) - totalBytesRead += bytesRead - If _progressDialog IsNot Nothing Then - Dim progressPercentage As Integer = CInt(totalBytesRead / streamLength * 100) - InvokeIncrement(progressPercentage) - End If - End Sub - Dim fileContent As New StreamContent(File.OpenRead(filePath)) - Dim progressContent As New ProgressableStreamContent(fileContent, progress, BufferSize) + Dim fileStream As FileStream = File.OpenRead(filePath) + Dim fileContent As New ProgressableStreamContent(New StreamContent(fileStream), progress) Dim fileName As String = Path.GetFileName(filePath) content.Add(fileContent, "file", fileName) fileContent.Headers.ContentDisposition = @@ -217,7 +217,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal .FileName = $"""{fileName}"""} Dim response As HttpResponseMessage = - Await _httpClient.PostAsync(requestURI, content, cancellationToken:=linkedStreamCts.Token).ConfigureAwait(continueOnCapturedContext:=False) + Await _httpClient.PostAsync(requestURI, content, cancellationToken:=linkedStreamCts.Token) _ + .ConfigureAwait(continueOnCapturedContext:=False) response.EnsureSuccessStatusCode() Select Case response.StatusCode Case HttpStatusCode.OK @@ -227,6 +228,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Throw New WebException() End Select response?.Dispose() + fileContent.Dispose() + Await fileStream.DisposeAsync().ConfigureAwait(False) End Using Catch ex As HttpRequestException Throw @@ -239,12 +242,13 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Else Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) End If + Finally + _progressDialog?.Close() End Try End Using If _progressDialog IsNot Nothing Then RemoveHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel End If End Function - End Class End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb index fd3b66d0e31..c9b2c91826d 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb @@ -31,14 +31,18 @@ Namespace Microsoft.VisualBasic.MyServices.Internal TryComputeLength(size) Using sInput As Stream = Await _content.ReadAsStreamAsync().ConfigureAwait(continueOnCapturedContext:=False) - While True - Dim length As Integer = Await sInput.ReadAsync(buffer).ConfigureAwait(continueOnCapturedContext:=False) - If length <= 0 Then Exit While - - Await stream.WriteAsync(buffer.AsMemory(start:=0, length)).ConfigureAwait(continueOnCapturedContext:=False) - uploaded += length - _progress?.Invoke(uploaded, size) - End While + If sInput.CanRead Then + While True + Dim length As Integer = Await sInput.ReadAsync(buffer).ConfigureAwait(continueOnCapturedContext:=False) + If length <= 0 Then Exit While + + Await stream.WriteAsync(buffer.AsMemory(start:=0, length)).ConfigureAwait(continueOnCapturedContext:=False) + uploaded += length + _progress?.Invoke(uploaded, size) + End While + Else + Stop + End If End Using End Function diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 922623e5459..018b1071331 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Net Imports FluentAssertions +Imports Microsoft.VisualBasic.Devices Imports Microsoft.VisualBasic.FileIO Imports Xunit @@ -96,13 +97,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -230,7 +230,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + GetNetworkCredentials(DefaultUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -374,13 +374,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=Nothing, destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -401,13 +400,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -429,13 +427,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=0, overwrite:=True, @@ -1007,11 +1004,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1025,17 +1022,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests overwrite:=True) End Sub - testCode.Should.NotThrow() - VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + testCode.Should.Throw(Of OperationCanceledException)() End Using End Sub - - Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(ExtraLargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1049,7 +1044,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests overwrite:=True) End Sub - testCode.Should.Throw(Of OperationCanceledException)() + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(LargeTestFileSize) End Using End Sub @@ -1186,13 +1183,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 5a8592140f1..9b4c678b415 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Net Imports FluentAssertions +Imports Microsoft.VisualBasic.Devices Imports Microsoft.VisualBasic.FileIO Imports Xunit @@ -36,11 +37,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(String.Empty)) - End Sub + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(String.Empty)) + End Sub testCode.Should.Throw(Of UriFormatException)() End Using @@ -108,13 +109,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -132,14 +132,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener( fileSize:=SmallTestFileSize, userName:=DefaultUserName, - password:=DefaultPassword) + password:=password) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + New NetworkCredential(DefaultUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -216,12 +216,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) End Using End Sub - + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) @@ -260,8 +260,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -337,13 +337,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=Nothing, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -362,13 +361,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -387,13 +385,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=0, onUserCancel:=UICancelOption.ThrowException) @@ -443,8 +440,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -472,8 +469,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -527,8 +524,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -602,8 +599,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -648,8 +645,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -718,8 +715,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -744,8 +741,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -779,8 +776,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -796,8 +793,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub @@ -820,8 +817,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(value)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(value)) End Using End Sub @@ -844,8 +841,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -868,8 +865,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Timeout) + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -891,8 +888,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + .Throw(Of ArgumentException)() _ + .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) End Using End Sub @@ -917,7 +914,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) @@ -957,7 +954,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim value As String = SR.Network_InvalidUriString.Replace("{0}", "invalidURL") testCode.Should() _ - .Throw(Of ArgumentException)() _ + .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) End Using End Sub @@ -1013,13 +1010,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -1097,8 +1093,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1125,8 +1121,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1176,8 +1172,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ - .Throw(Of ArgumentException)() _ - .Where(exceptionExpression) + .Throw(Of ArgumentException)() _ + .Where(exceptionExpression) End Using End Sub @@ -1199,8 +1195,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) End Using End Sub @@ -1224,8 +1220,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should() _ - .Throw(Of ArgumentNullException)() _ - .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + .Throw(Of ArgumentNullException)() _ + .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) End Using End Sub From 354ccdf686dd1894cb00d15644dcf4e3268a1d11 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 02:42:17 -0800 Subject: [PATCH 052/119] Improve UploadFile and DownloadFile Tests --- .../System/Windows/Forms/DownloadFileTests.vb | 38 +++++++++---------- .../System/Windows/Forms/UploadFileTests.vb | 20 ++++------ 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 99618feddd4..bd3838928e2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Net Imports FluentAssertions +Imports Microsoft.VisualBasic.Devices Imports Microsoft.VisualBasic.FileIO Imports Xunit @@ -102,13 +103,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -236,7 +236,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + GetNetworkCredentials(DefaultUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -380,13 +380,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=Nothing, destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -407,13 +406,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -435,13 +433,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=0, overwrite:=True, @@ -1013,11 +1010,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1031,17 +1028,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests overwrite:=True) End Sub - testCode.Should.NotThrow() - VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + testCode.Should.Throw(Of OperationCanceledException)() End Using End Sub - - Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() + + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(ExtraLargeTestFileSize) + Dim webListener As New WebListener(LargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1055,7 +1050,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests overwrite:=True) End Sub - testCode.Should.Throw(Of OperationCanceledException)() + testCode.Should.NotThrow() + VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ + .Be(LargeTestFileSize) End Using End Sub @@ -1192,13 +1189,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index aa2f8e259de..1c638a76faf 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Net Imports FluentAssertions +Imports Microsoft.VisualBasic.Devices Imports Microsoft.VisualBasic.FileIO Imports Xunit @@ -115,13 +116,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -139,14 +139,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener( fileSize:=SmallTestFileSize, userName:=DefaultUserName, - password:=DefaultPassword) + password:=password) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials:=New NetworkCredential(userName:=DefaultUserName, password), + New NetworkCredential(DefaultUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -344,13 +344,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=Nothing, - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -369,13 +368,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -394,13 +392,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=0, onUserCancel:=UICancelOption.ThrowException) @@ -1020,13 +1017,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() - Dim networkCredentials As New NetworkCredential(DefaultUserName, DefaultPassword) Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - networkCredentials, + GetNetworkCredentials(DefaultUserName, DefaultPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) From 468e669af016777bd1517d2081fba71d94368a15 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 08:36:34 -0800 Subject: [PATCH 053/119] Add SR strings from WebClient --- .../src/Resources/SR.resx | 6 ++++++ .../System/Windows/Forms/DownloadFileTests.vb | 20 +++++++----------- .../System/Windows/Forms/UploadFileTests.vb | 21 +++++++------------ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx index ff406c3aee6..2d86b1c6b73 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx @@ -221,4 +221,10 @@ Environment variable is not defined: '{0}'. + + The network operation has timed out. + + + The network operation is unauthorized. + diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index bd3838928e2..a5f5733f75d 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -13,12 +13,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase - ' The next 2 Constants need to be SR Resources, - ' they are not accessible in this project they come from WebClient. - Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." - Private Const SR_net_webstatus_Unauthorized As String = - "The remote server returned an error: (401) Unauthorized." - Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() @@ -244,7 +238,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -269,7 +263,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -844,7 +838,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -871,7 +865,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1100,7 +1094,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1573,7 +1567,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub @@ -1600,7 +1594,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) VerifyFailedDownload(testDirectory, destinationFileName, listener) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 1c638a76faf..9b4c678b415 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -13,13 +13,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class UploadFileTests Inherits VbFileCleanupTestBase - ' REVIEWER NOTE: The next 2 Constants need to be SR Resources, - ' they are not accessible in this project they come from WebClient. - Private Const SR_net_webstatus_Timeout As String = "The operation has timed out." - - Private Const SR_net_webstatus_Unauthorized As String = - "The remote server returned an error: (401) Unauthorized." - Public Sub UploadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() @@ -153,7 +146,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -201,7 +194,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -723,7 +716,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -749,7 +742,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -873,7 +866,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Timeout) + .WithMessage(SR.net_webstatus_Timeout) End Using End Sub @@ -1298,7 +1291,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub @@ -1324,7 +1317,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ - .WithMessage(SR_net_webstatus_Unauthorized) + .WithMessage(SR.net_webstatus_Unauthorized) End Using End Sub From 6e313e235f440613b8695b009a92c2f416eb9685 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 09:33:44 -0800 Subject: [PATCH 054/119] Fix error handling and add new strings for to replace WebClient --- .../MyServices/Internal/WebClientCopy.vb | 60 +++++++++++++------ .../src/Resources/xlf/SR.cs.xlf | 10 ++++ .../src/Resources/xlf/SR.de.xlf | 10 ++++ .../src/Resources/xlf/SR.es.xlf | 10 ++++ .../src/Resources/xlf/SR.fr.xlf | 10 ++++ .../src/Resources/xlf/SR.it.xlf | 10 ++++ .../src/Resources/xlf/SR.ja.xlf | 10 ++++ .../src/Resources/xlf/SR.ko.xlf | 10 ++++ .../src/Resources/xlf/SR.pl.xlf | 10 ++++ .../src/Resources/xlf/SR.pt-BR.xlf | 10 ++++ .../src/Resources/xlf/SR.ru.xlf | 10 ++++ .../src/Resources/xlf/SR.tr.xlf | 10 ++++ .../src/Resources/xlf/SR.zh-Hans.xlf | 10 ++++ .../src/Resources/xlf/SR.zh-Hant.xlf | 10 ++++ .../System/Windows/Forms/UploadFileTests.vb | 2 +- 15 files changed, 172 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index 5ebced7ff68..a2669e82cf7 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -158,14 +158,26 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Debug.Assert((Not String.IsNullOrWhiteSpace(destinationFileName)) _ AndAlso IO.Directory.Exists(path), $"Invalid {NameOf(path)}") - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.DownloadFileAsync(address, destinationFileName) - 'returns when the download sequence is over, whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.DownloadFile(address, destinationFileName) - End If + Try + + ' If we have a dialog we need to set up an async download + If _progressDialog IsNot Nothing Then + _webClient.DownloadFileAsync(address, destinationFileName) + 'returns when the download sequence is over, whether due to success, error, or being canceled + _progressDialog.ShowProgressDialog() + Else + _webClient.DownloadFile(address, destinationFileName) + End If + Catch exTimeout As TimeoutException + Throw New WebException(SR.net_webstatus_Timeout) + Catch exWebException As WebException + If exWebException.Message.Contains("401") Then + Throw New WebException(SR.net_webstatus_Unauthorized) + End If + Throw New WebException(SR.net_webstatus_Timeout) + Catch Ex As Exception + Throw + End Try 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then @@ -186,17 +198,27 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Debug.Assert(address IsNot Nothing, $"No {NameOf(address)}") Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) _ AndAlso IO.File.Exists(sourceFileName), "Invalid file") - - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.UploadFileAsync(address, sourceFileName) - - ' Returns when the download sequence is over, - ' whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.UploadFile(address, sourceFileName) - End If + Try + ' If we have a dialog we need to set up an async download + If _progressDialog IsNot Nothing Then + _webClient.UploadFileAsync(address, sourceFileName) + + ' Returns when the download sequence is over, + ' whether due to success, error, or being canceled + _progressDialog.ShowProgressDialog() + Else + _webClient.UploadFile(address, sourceFileName) + End If + Catch exTimeout As TimeoutException + Throw New WebException(SR.net_webstatus_Timeout) + Catch exWebException As WebException + If exWebException.Message.Contains("401") Then + Throw New WebException(SR.net_webstatus_Unauthorized) + End If + Throw New WebException(SR.net_webstatus_Timeout) + Catch Ex As Exception + Throw + End Try ' Now that we are back on the main thread, throw the exception we ' encountered if the user didn't cancel. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf index f737611c412..9b04fd1c94c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf @@ -177,6 +177,16 @@ Ukládání {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf index e4e2a7e5e3e..4241a9c79fc 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf @@ -177,6 +177,16 @@ {0} wird hochgeladen + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf index 006fe62ea7d..b928124e52c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf @@ -177,6 +177,16 @@ Cargando {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf index 3e06f665f6a..cfa6763c203 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf @@ -177,6 +177,16 @@ Chargement de {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf index cc73272c979..a040a2d48e4 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf @@ -177,6 +177,16 @@ Caricamento di {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf index d9c1b872741..95057a734e3 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf @@ -177,6 +177,16 @@ {0} のアップロード中 + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf index 5546130053f..90a42911b9a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf @@ -177,6 +177,16 @@ {0} 업로드 중 + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf index 35284bb512e..96ece95f46b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf @@ -177,6 +177,16 @@ Przekazywanie: {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf index 1e1b8f1416f..4f87094e257 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf @@ -177,6 +177,16 @@ Carregando {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf index e400e684656..bfa18e769da 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf @@ -177,6 +177,16 @@ Идет отправка {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf index 751c15e6832..42b54249b7b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf @@ -177,6 +177,16 @@ {0} karşıya yükleniyor + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf index af55164f9e7..27bbc168d6f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf @@ -177,6 +177,16 @@ 正在上传 {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf index c3df5ab1fb7..b9e74cf4c97 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf @@ -177,6 +177,16 @@ 正在上傳 {0} + + The network operation has timed out. + The network operation has timed out. + + + + The network operation is unauthorized. + The network operation is unauthorized. + + \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 9b4c678b415..0c43c19d3ef 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -132,7 +132,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener( fileSize:=SmallTestFileSize, userName:=DefaultUserName, - password:=password) + password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() From b8de54ba9197796dc8de1c16606e1b21422df0f6 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 09:45:58 -0800 Subject: [PATCH 055/119] Fix error handling --- .../MyServices/Internal/WebClientCopy.vb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index a2669e82cf7..daf49d1fa59 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -168,15 +168,11 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Else _webClient.DownloadFile(address, destinationFileName) End If - Catch exTimeout As TimeoutException - Throw New WebException(SR.net_webstatus_Timeout) - Catch exWebException As WebException - If exWebException.Message.Contains("401") Then + Catch ex As WebException + If ex.Message.Contains("401") Then Throw New WebException(SR.net_webstatus_Unauthorized) End If Throw New WebException(SR.net_webstatus_Timeout) - Catch Ex As Exception - Throw End Try 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. @@ -209,15 +205,11 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Else _webClient.UploadFile(address, sourceFileName) End If - Catch exTimeout As TimeoutException - Throw New WebException(SR.net_webstatus_Timeout) - Catch exWebException As WebException - If exWebException.Message.Contains("401") Then + Catch ex As WebException + If ex.Message.Contains("401") Then Throw New WebException(SR.net_webstatus_Unauthorized) End If Throw New WebException(SR.net_webstatus_Timeout) - Catch Ex As Exception - Throw End Try ' Now that we are back on the main thread, throw the exception we From 70283e4d34efa1a660145471e0b286baa4920872 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 21 Dec 2024 17:58:09 -0800 Subject: [PATCH 056/119] Improve WebListener --- .../Windows/TestUtilities/WebListener.vb | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 64ab80a4680..99239cd1fe2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -162,20 +162,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests For Each part As String In parts If part.Trim() <> "--" Then Dim separator As String() = New String() {Environment.NewLine} - Dim lines As String() = part.Split(separator, StringSplitOptions.RemoveEmptyEntries) - If lines.Length > 2 Then - Dim headerParts As String() = lines(0).Split({":"c}, count:=2) - If headerParts.Length = 2 _ - AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", - comparisonType:=StringComparison.OrdinalIgnoreCase) Then + Dim lines As List(Of String) = part.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList + If lines.Count > 2 Then + Dim headerParts As String() = Nothing + Dim dispositionIndex As Integer = GetContentDispositionHeader(lines, headerParts) + If dispositionIndex > -1 Then Dim nameMatch As Match = Regex.Match(input:=headerParts(1), pattern:="name=""(?[^""]+)""") If nameMatch.Success Then Dim name As String = nameMatch.Groups("name").Value Dim value As String = headerParts(1).Split("filename=")(1).Trim(""""c) + value = value.Split(";"c)(0).Trim(""""c) result.Add(name, value.Trim()) - If lines.Length > 2 Then - result.Add("dataLength", lines(1).Length.ToString) + If lines.Count > dispositionIndex + 1 Then + result.Add("dataLength", lines(dispositionIndex + 1).Length.ToString) End If Exit For End If @@ -189,5 +189,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return result End Function + Private Shared Function GetContentDispositionHeader( + lines As List(Of String), + ByRef headerParts() As String) As Integer + For dispositionIndex As Integer = 0 To lines.Count - 1 + Dim line As String = lines(dispositionIndex) + If line.Contains("Content-Disposition", StringComparison.InvariantCultureIgnoreCase) Then + headerParts = line.Split({":"c}, count:=2) + Dim result As Boolean = headerParts.Length = 2 _ + AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", + comparisonType:=StringComparison.OrdinalIgnoreCase) + Return dispositionIndex + End If + Next + Return -1 + End Function End Class End Namespace From 0fa9cd3274064b3edf013a9f4fa5b49381b2b311 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 21 Dec 2024 23:58:35 -0800 Subject: [PATCH 057/119] Fix logic in WebListener return 500 for server errors Fix Upload logic to handle Web error 500 --- .../MyServices/Internal/WebClientCopy.vb | 3 + .../System/Windows/Forms/UploadFileTests.vb | 6 +- .../Windows/TestUtilities/WebListener.vb | 65 +++++++++++-------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index daf49d1fa59..cc2cb53dbe7 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -209,6 +209,9 @@ Namespace Microsoft.VisualBasic.MyServices.Internal If ex.Message.Contains("401") Then Throw New WebException(SR.net_webstatus_Unauthorized) End If + If ex.Message.Contains("500") Then + Throw + End If Throw New WebException(SR.net_webstatus_Timeout) End Try diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 0c43c19d3ef..c0a70a009b9 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -96,7 +96,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address)) End Sub - testCode.Should.NotThrow() + testCode.Should.Throw(Of WebException)() End Using End Sub @@ -981,7 +981,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub - Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(SmallTestFileSize) @@ -997,7 +997,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.NotThrow() + testCode.Should.Throw(Of WebException)() End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 99239cd1fe2..5a60a1efcdc 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -4,7 +4,6 @@ Imports System.IO Imports System.Net Imports System.Runtime.CompilerServices -Imports System.Text.RegularExpressions Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests @@ -101,25 +100,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests detectEncodingFromByteOrderMarks:=True, BufferSize) End Using - Try - Dim dataLength As String = formData(NameOf(dataLength)) - If _fileSize.ToString <> dataLength Then - Throw New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") - End If - - Dim fileName As String = formData("file") - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") - End If - Catch ioEx As IOException - Throw - Catch ex As Exception - Stop - ' ignore it will be handled elsewhere - End Try + + response.StatusCode = 200 + Dim dataLength As String = formData(NameOf(dataLength)) + If _fileSize.ToString <> dataLength Then + response.StatusCode = 500 + End If + + Dim fileName As String = formData("filename") + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + response.StatusCode = 500 + End If End Using End If - response.StatusCode = 200 Else Dim responseString As String = Strings.StrDup(_fileSize, "A") Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) @@ -168,17 +161,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim dispositionIndex As Integer = GetContentDispositionHeader(lines, headerParts) If dispositionIndex > -1 Then - Dim nameMatch As Match = Regex.Match(input:=headerParts(1), pattern:="name=""(?[^""]+)""") - If nameMatch.Success Then - Dim name As String = nameMatch.Groups("name").Value - Dim value As String = headerParts(1).Split("filename=")(1).Trim(""""c) - value = value.Split(";"c)(0).Trim(""""c) - result.Add(name, value.Trim()) - If lines.Count > dispositionIndex + 1 Then - result.Add("dataLength", lines(dispositionIndex + 1).Length.ToString) - End If - Exit For + result.Add("filename", GetFilename(headerParts)) + If lines.Count > dispositionIndex + 1 Then + result.Add("dataLength", GetDataLength(lines, dispositionIndex).ToString) End If + Exit For End If End If End If @@ -189,6 +176,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return result End Function + Private Shared Function GetFilename(headerParts() As String) As String + Dim value As String = "" + Dim line As String = headerParts(1) + Dim startIndex As Integer = line.IndexOf("filename=""", StringComparison.InvariantCultureIgnoreCase) + If startIndex > -1 Then + line = line.Substring(startIndex + 10) + Dim length As Integer = line.IndexOf(""""c) + value = line.Substring(0, length) + End If + + Return value + End Function + + Private Shared Function GetDataLength(lines As List(Of String), dispositionIndex As Integer) As Integer + For Each line As String In lines + If line.Substring(0, 1) = vbNullChar Then + Return line.Length + End If + Next + Return 0 + End Function + Private Shared Function GetContentDispositionHeader( lines As List(Of String), ByRef headerParts() As String) As Integer From a6413df3990193a6557575f15d457a324126e507 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 00:03:26 -0800 Subject: [PATCH 058/119] Sort new functions --- .../Windows/TestUtilities/WebListener.vb | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 5a60a1efcdc..876840f0137 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -52,6 +52,44 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return If(element IsNot Nothing, element.Substring(startIndex:=element.IndexOf("="c) + 1).Trim().Trim(""""c), String.Empty) End Function + Private Shared Function GetContentDispositionHeader( + lines As List(Of String), + ByRef headerParts() As String) As Integer + For dispositionIndex As Integer = 0 To lines.Count - 1 + Dim line As String = lines(dispositionIndex) + If line.Contains("Content-Disposition", StringComparison.InvariantCultureIgnoreCase) Then + headerParts = line.Split({":"c}, count:=2) + Dim result As Boolean = headerParts.Length = 2 _ + AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", + comparisonType:=StringComparison.OrdinalIgnoreCase) + Return dispositionIndex + End If + Next + Return -1 + End Function + + Private Shared Function GetDataLength(lines As List(Of String), dispositionIndex As Integer) As Integer + For Each line As String In lines + If line.Substring(0, 1) = vbNullChar Then + Return line.Length + End If + Next + Return 0 + End Function + + Private Shared Function GetFilename(headerParts() As String) As String + Dim value As String = "" + Dim line As String = headerParts(1) + Dim startIndex As Integer = line.IndexOf("filename=""", StringComparison.InvariantCultureIgnoreCase) + If startIndex > -1 Then + line = line.Substring(startIndex + 10) + Dim length As Integer = line.IndexOf(""""c) + value = line.Substring(0, length) + End If + + Return value + End Function + Friend Function ProcessRequests() As HttpListener ' Create a listener and add the prefixes. Dim listener As New HttpListener() @@ -176,42 +214,5 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return result End Function - Private Shared Function GetFilename(headerParts() As String) As String - Dim value As String = "" - Dim line As String = headerParts(1) - Dim startIndex As Integer = line.IndexOf("filename=""", StringComparison.InvariantCultureIgnoreCase) - If startIndex > -1 Then - line = line.Substring(startIndex + 10) - Dim length As Integer = line.IndexOf(""""c) - value = line.Substring(0, length) - End If - - Return value - End Function - - Private Shared Function GetDataLength(lines As List(Of String), dispositionIndex As Integer) As Integer - For Each line As String In lines - If line.Substring(0, 1) = vbNullChar Then - Return line.Length - End If - Next - Return 0 - End Function - - Private Shared Function GetContentDispositionHeader( - lines As List(Of String), - ByRef headerParts() As String) As Integer - For dispositionIndex As Integer = 0 To lines.Count - 1 - Dim line As String = lines(dispositionIndex) - If line.Contains("Content-Disposition", StringComparison.InvariantCultureIgnoreCase) Then - headerParts = line.Split({":"c}, count:=2) - Dim result As Boolean = headerParts.Length = 2 _ - AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", - comparisonType:=StringComparison.OrdinalIgnoreCase) - Return dispositionIndex - End If - Next - Return -1 - End Function End Class End Namespace From 53bbcac954276d93069ed52cb371ce7c9c0e48b9 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:25:15 -0800 Subject: [PATCH 059/119] Fix a;l remaining tests --- .../VisualBasic/Devices/Network.UploadFile.vb | 12 +- .../Devices/Network.UploadFileAsync.vb | 3 + .../VisualBasic/Devices/WebClientExtended.vb | 77 ------------ .../MyServices/Internal/HttpClientCopy.vb | 32 ++--- .../Internal/ProgressableStreamContent.vb | 35 +++--- .../System/Windows/Forms/UploadFileTests.vb | 84 ++++++++++--- .../DownloadFileTestConstants.vb | 1 + .../Windows/TestUtilities/WebListener.vb | 119 ++++++++++++------ 8 files changed, 195 insertions(+), 168 deletions(-) delete mode 100644 src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/WebClientExtended.vb diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index c1595f44107..468185ebd55 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -3,6 +3,7 @@ Imports System.Net Imports System.Net.Http +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal @@ -281,7 +282,6 @@ Namespace Microsoft.VisualBasic.Devices Throw GetArgumentNullException(NameOf(address)) End If - ' Get network credentials Dim dialog As ProgressDialog = Nothing Try ' Construct the local file. This will validate the full name and path @@ -289,11 +289,11 @@ Namespace Microsoft.VisualBasic.Devices path:=sourceFileName, paramName:=NameOf(sourceFileName)) + ' Get network credentials Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, New HttpClientHandler, New HttpClientHandler With {.Credentials = networkCredentials}) - dialog = GetProgressDialog( address:=address.AbsolutePath, fileNameWithPath:=sourceFileName, @@ -312,10 +312,16 @@ Namespace Microsoft.VisualBasic.Devices Throw t.Exception Else dialog?.ShowProgressDialog() - t.Wait() + Do While Not (t.IsCompleted OrElse t.IsFaulted OrElse t.IsCanceled) + 'prevent UI freeze + Application.DoEvents() + Loop If t.IsFaulted Then Throw t.Exception End If + If t.IsCanceled Then + Throw New OperationCanceledException + End If End If Catch ex As Exception If ex.InnerException IsNot Nothing Then diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb index aae79b77762..13490f47232 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -66,6 +66,9 @@ Namespace Microsoft.VisualBasic.Devices filePath:=normalizedFilePath, requestURI:=addressUri, externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + Catch ioEx As IO.IOException + Throw + Catch ex As Exception If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then If ex.Message.Contains("401") Then diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/WebClientExtended.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/WebClientExtended.vb deleted file mode 100644 index ca95e62b799..00000000000 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/WebClientExtended.vb +++ /dev/null @@ -1,77 +0,0 @@ -' Licensed to the .NET Foundation under one or more agreements. -' The .NET Foundation licenses this file to you under the MIT license. - -Imports System.Net - -Namespace Microsoft.VisualBasic.Devices - - ''' - ''' Temporary class used to provide WebClient with a timeout property. - ''' - ''' This class will be deleted when Timeout is added to WebClient. - Friend NotInheritable Class WebClientExtended - Inherits WebClient - - ' The Timeout value to be used by WebClient's WebRequest for Downloading or Uploading a file - Private _timeout As Integer = 100000 - - ' Flag used to indicate whether or not we should use passive mode when ftp downloading - Private _useNonPassiveFtp As Boolean - -#Disable Warning BC41004 ' First statement of this 'Sub New' should be an explicit call to 'MyBase.New' or 'MyClass.New' because the constructor in the base class is marked obsolete - - Friend Sub New() - End Sub - -#Enable Warning BC41004 ' First statement of this 'Sub New' should be an explicit call to 'MyBase.New' or 'MyClass.New' because the constructor in the base class is marked obsolete - - ''' - ''' Sets or indicates the timeout used by WebRequest used by WebClient - ''' - Public WriteOnly Property Timeout() As Integer - Set(value As Integer) - Debug.Assert(value > 0, "illegal value for timeout") - _timeout = value - End Set - End Property - - ''' - ''' Enables switching the server to non passive mode. - ''' - ''' We need this in order for the progress UI on a download to work - Public WriteOnly Property UseNonPassiveFtp() As Boolean - Set(value As Boolean) - _useNonPassiveFtp = value - End Set - End Property - - ''' - ''' Makes sure that the timeout value for WebRequests (used for all Download - ''' and Upload methods) is set to the Timeout value. - ''' - ''' - Protected Overrides Function GetWebRequest(address As Uri) As WebRequest - Dim request As WebRequest = MyBase.GetWebRequest(address) - - Debug.Assert(request IsNot Nothing, "Unable to get WebRequest from base class") - If request IsNot Nothing Then - request.Timeout = _timeout - If _useNonPassiveFtp Then - Dim ftpRequest As FtpWebRequest = TryCast(request, FtpWebRequest) - If ftpRequest IsNot Nothing Then - ftpRequest.UsePassive = False - End If - End If - - Dim httpRequest As HttpWebRequest = TryCast(request, HttpWebRequest) - If httpRequest IsNot Nothing Then - httpRequest.AllowAutoRedirect = False - End If - - End If - - Return request - End Function - - End Class -End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index d1c2a814f8b..d69cb641c1f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -202,23 +202,28 @@ Namespace Microsoft.VisualBasic.MyServices.Internal If _progressDialog IsNot Nothing Then Dim progressPercentage As Integer = CInt(totalBytesRead / streamLength * 100) InvokeIncrement(progressPercentage) + Thread.Sleep(millisecondsTimeout:=1) End If End Sub Using linkedStreamCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceReadStream.Token, _cancelTokenSourcePost.Token, externalToken) Try - Using content As New MultipartFormDataContent("----boundary") - Dim fileStream As FileStream = File.OpenRead(filePath) - Dim fileContent As New ProgressableStreamContent(New StreamContent(fileStream), progress) + Dim response As HttpResponseMessage + Using multipartContent As New MultipartFormDataContent("----boundary") + Dim fileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read) + Dim fileContent As New StreamContent(fileStream) + fileContent.Headers.ContentType = New MediaTypeHeaderValue("application/octet-stream") Dim fileName As String = Path.GetFileName(filePath) - content.Add(fileContent, "file", fileName) - fileContent.Headers.ContentDisposition = - New ContentDispositionHeaderValue(dispositionType:="form-data") With { - .Name = """file""", - .FileName = $"""{fileName}"""} - - Dim response As HttpResponseMessage = - Await _httpClient.PostAsync(requestURI, content, cancellationToken:=linkedStreamCts.Token) _ - .ConfigureAwait(continueOnCapturedContext:=False) + multipartContent.Add(fileContent, "file", $"""{fileName}""") + If _progressDialog Is Nothing Then + response = + Await _httpClient.PostAsync(requestURI, multipartContent, cancellationToken:=linkedStreamCts.Token) _ + .ConfigureAwait(continueOnCapturedContext:=False) + Else + Dim progressContent As New ProgressableStreamContent(multipartContent, progress, BufferSize) + response = + Await _httpClient.PostAsync(requestURI, progressContent, cancellationToken:=linkedStreamCts.Token) _ + .ConfigureAwait(continueOnCapturedContext:=False) + End If response.EnsureSuccessStatusCode() Select Case response.StatusCode Case HttpStatusCode.OK @@ -228,7 +233,6 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Throw New WebException() End Select response?.Dispose() - fileContent.Dispose() Await fileStream.DisposeAsync().ConfigureAwait(False) End Using Catch ex As HttpRequestException @@ -243,7 +247,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) End If Finally - _progressDialog?.Close() + CloseProgressDialog(_progressDialog) End Try End Using If _progressDialog IsNot Nothing Then diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb index c9b2c91826d..2edd4cd8332 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressableStreamContent.vb @@ -10,11 +10,12 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Friend Class ProgressableStreamContent Inherits HttpContent - Private ReadOnly _content As HttpContent Private ReadOnly _bufferSize As Integer + Private ReadOnly _content As HttpContent Private ReadOnly _progress As Action(Of Long, Long) - Public Sub New(content As HttpContent, progress As Action(Of Long, Long), Optional bufferSize As Integer = 4096) + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize) + ArgumentNullException.ThrowIfNull(content) _content = content _progress = progress _bufferSize = bufferSize @@ -23,30 +24,30 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Next End Sub + Protected Overrides Sub Dispose(disposing As Boolean) + If disposing Then _content.Dispose() + MyBase.Dispose(disposing) + End Sub + Protected Overrides Async Function SerializeToStreamAsync(stream As Stream, context As TransportContext) As Task Dim buffer(_bufferSize - 1) As Byte Dim size As Long Dim uploaded As Long = 0 - TryComputeLength(size) - Using sInput As Stream = Await _content.ReadAsStreamAsync().ConfigureAwait(continueOnCapturedContext:=False) - If sInput.CanRead Then - While True - Dim length As Integer = Await sInput.ReadAsync(buffer).ConfigureAwait(continueOnCapturedContext:=False) - If length <= 0 Then Exit While - - Await stream.WriteAsync(buffer.AsMemory(start:=0, length)).ConfigureAwait(continueOnCapturedContext:=False) - uploaded += length - _progress?.Invoke(uploaded, size) - End While - Else - Stop - End If + Using sinput As Stream = Await _content.ReadAsStreamAsync().ConfigureAwait(False) + While True + Dim bytesRead As Long = Await sinput.ReadAsync(buffer).ConfigureAwait(False) + If bytesRead <= 0 Then Exit While + uploaded += bytesRead + _progress?.Invoke(uploaded, size) + Await stream.WriteAsync(buffer.AsMemory(0, CInt(bytesRead))).ConfigureAwait(False) + Await Stream.FlushAsync().ConfigureAwait(False) + End While End Using + Await Stream.FlushAsync().ConfigureAwait(False) End Function - Protected Overrides Function TryComputeLength(ByRef length As Long) As Boolean length = _content.Headers.ContentLength.GetValueOrDefault() Return True diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 9b4c678b415..aa524220993 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -27,6 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -44,6 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -61,6 +63,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -80,6 +83,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -87,7 +91,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWrongFileSize_Throw() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(1) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -97,6 +101,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeOfType(Of IOException)() End Using End Sub @@ -120,6 +125,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -132,7 +138,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim webListener As New WebListener( fileSize:=SmallTestFileSize, userName:=DefaultUserName, - password:=password) + password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -147,6 +153,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -171,7 +178,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -195,6 +202,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -218,14 +226,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + webListener.ServerFault.Should.BeNull() End Using End Sub Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -235,10 +244,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=String.Empty, password:=String.Empty, showUI:=True, - connectionTimeout:=TestingConnectionTimeout) + connectionTimeout:=TestingConnectionLargeTimeout) End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -262,6 +272,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -283,6 +294,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -304,6 +316,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -325,6 +338,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -349,6 +363,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -373,6 +388,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -397,6 +413,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -418,6 +435,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -442,7 +460,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -471,6 +489,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -495,8 +514,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = - Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ - AndAlso e.Message.Contains(NameOf(sourceFileName)) + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) End Using End Sub @@ -521,11 +540,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = - Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ - AndAlso e.Message.Contains(NameOf(sourceFileName)) + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -548,6 +568,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -572,6 +593,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of FileNotFoundException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -601,6 +623,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -622,7 +645,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -647,6 +670,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -669,6 +693,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -691,6 +716,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -717,6 +743,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -743,6 +770,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -760,6 +788,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -778,6 +807,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -795,6 +825,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -819,6 +850,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -843,7 +875,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -867,6 +899,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -890,6 +923,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -911,14 +945,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of OperationCanceledException)() + webListener.ServerFault.Should.BeNull() End Using End Sub Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -932,6 +967,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -956,6 +992,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -977,11 +1014,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub - Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) Dim webListener As New WebListener(SmallTestFileSize) @@ -998,6 +1036,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeOfType(Of IOException)() End Using End Sub @@ -1022,6 +1061,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1046,6 +1086,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1095,6 +1136,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1123,6 +1165,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1145,6 +1188,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1174,6 +1218,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1197,7 +1242,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1222,6 +1267,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1244,6 +1290,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1266,6 +1313,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1292,6 +1340,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1318,6 +1367,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb index 85381891d6f..1689e74e87c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -9,6 +9,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Const LargeTestFileSize As Integer = 104_857_600 Friend Const SmallTestFileSize As Integer = 18_135 Friend Const InvalidUrlAddress As String = "invalidURL" + Friend Const TestingConnectionLargeTimeout As Integer = 100_000_000 Friend Const TestingConnectionTimeout As Integer = 100_000 End Module End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 64ab80a4680..b3445fcf538 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -4,7 +4,6 @@ Imports System.IO Imports System.Net Imports System.Runtime.CompilerServices -Imports System.Text.RegularExpressions Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests @@ -22,6 +21,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Is used to create the file name and the size of download. ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) + Debug.Assert(fileSize > 0) _fileSize = fileSize _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" Address = $"{_fileUrlPrefix}T{fileSize}" @@ -46,6 +46,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Public ReadOnly Property Address As String + Public Property ServerFault As Exception Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -53,6 +54,44 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return If(element IsNot Nothing, element.Substring(startIndex:=element.IndexOf("="c) + 1).Trim().Trim(""""c), String.Empty) End Function + Private Shared Function GetContentDispositionHeader( + lines As List(Of String), + ByRef headerParts() As String) As Integer + For dispositionIndex As Integer = 0 To lines.Count - 1 + Dim line As String = lines(dispositionIndex) + If line.Contains("Content-Disposition", StringComparison.InvariantCultureIgnoreCase) Then + headerParts = line.Split({":"c}, count:=2) + Dim result As Boolean = headerParts.Length = 2 _ + AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", + comparisonType:=StringComparison.OrdinalIgnoreCase) + Return dispositionIndex + End If + Next + Return -1 + End Function + + Private Shared Function GetDataLength(lines As List(Of String), dispositionIndex As Integer) As Integer + For Each line As String In lines + If line.Substring(0, 1) = vbNullChar Then + Return line.Length + End If + Next + Return 0 + End Function + + Private Shared Function GetFilename(headerParts() As String) As String + Dim value As String = "" + Dim line As String = headerParts(1) + Dim startIndex As Integer = line.IndexOf("filename=""", StringComparison.InvariantCultureIgnoreCase) + If startIndex > -1 Then + line = line.Substring(startIndex + 10) + Dim length As Integer = line.IndexOf(""""c) + value = line.Substring(0, length) + End If + + Return value + End Function + Friend Function ProcessRequests() As HttpListener ' Create a listener and add the prefixes. Dim listener As New HttpListener() @@ -101,23 +140,20 @@ Namespace Microsoft.VisualBasic.Forms.Tests detectEncodingFromByteOrderMarks:=True, BufferSize) End Using - Try - Dim dataLength As String = formData(NameOf(dataLength)) - If _fileSize.ToString <> dataLength Then - Throw New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") - End If - - Dim fileName As String = formData("file") - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - Throw New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") - End If - Catch ioEx As IOException - Throw - Catch ex As Exception - Stop - ' ignore it will be handled elsewhere - End Try End Using + Try + Dim dataLength As String = formData(NameOf(dataLength)) + If _fileSize.ToString <> dataLength Then + ServerFault = New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") + End If + + Dim fileName As String = formData("filename") + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + ServerFault = New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + End If + Catch ex As Exception + ' Ignore is case upload is cancelled + End Try End If response.StatusCode = 200 Else @@ -133,7 +169,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End If Finally - response?.Close() + Try + response?.Close() + Catch ex As Exception + + End Try response = Nothing End Try End Sub @@ -156,33 +196,32 @@ Namespace Microsoft.VisualBasic.Forms.Tests If request.ContentType.StartsWith("multipart/form-data", StringComparison.OrdinalIgnoreCase) Then Dim boundary As String = GetBoundary(request.ContentType) Using reader As New StreamReader(request.InputStream, request.ContentEncoding) - Dim content As String = reader.ReadToEnd() - Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) - - For Each part As String In parts - If part.Trim() <> "--" Then - Dim separator As String() = New String() {Environment.NewLine} - Dim lines As String() = part.Split(separator, StringSplitOptions.RemoveEmptyEntries) - If lines.Length > 2 Then - Dim headerParts As String() = lines(0).Split({":"c}, count:=2) - If headerParts.Length = 2 _ - AndAlso headerParts(0).Trim().Equals(value:="Content-Disposition", - comparisonType:=StringComparison.OrdinalIgnoreCase) Then - - Dim nameMatch As Match = Regex.Match(input:=headerParts(1), pattern:="name=""(?[^""]+)""") - If nameMatch.Success Then - Dim name As String = nameMatch.Groups("name").Value - Dim value As String = headerParts(1).Split("filename=")(1).Trim(""""c) - result.Add(name, value.Trim()) - If lines.Length > 2 Then - result.Add("dataLength", lines(1).Length.ToString) + Try + + Dim content As String = reader.ReadToEnd() + Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) + + For Each part As String In parts + If part.Trim() <> "--" Then + Dim separator As String() = New String() {Environment.NewLine} + Dim lines As List(Of String) = part.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList + If lines.Count > 2 Then + Dim headerParts As String() = Nothing + Dim dispositionIndex As Integer = GetContentDispositionHeader(lines, headerParts) + + If dispositionIndex > -1 Then + result.Add("filename", GetFilename(headerParts)) + If lines.Count > dispositionIndex + 1 Then + result.Add("dataLength", GetDataLength(lines, dispositionIndex).ToString) End If Exit For End If End If End If - End If - Next + Next + Catch ex As Exception + ' ignore + End Try End Using End If From 8449f48244f8dc05007e2fa40b3fc0dba15f17f3 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:36:07 -0800 Subject: [PATCH 060/119] Improve test server and test --- .../System/Windows/Forms/UploadFileTests.vb | 84 +++++++++++++++---- .../DownloadFileTestConstants.vb | 1 + .../Windows/TestUtilities/WebListener.vb | 61 ++++++++------ 3 files changed, 105 insertions(+), 41 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index c0a70a009b9..aa524220993 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -27,6 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -44,6 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -61,6 +63,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -80,6 +83,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -87,7 +91,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWrongFileSize_Throw() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(1) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -96,7 +100,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address)) End Sub - testCode.Should.Throw(Of WebException)() + testCode.Should.NotThrow() + webListener.ServerFault.Should.BeOfType(Of IOException)() End Using End Sub @@ -120,6 +125,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -147,6 +153,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -171,7 +178,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -195,6 +202,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -218,14 +226,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + webListener.ServerFault.Should.BeNull() End Using End Sub Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -235,10 +244,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests userName:=String.Empty, password:=String.Empty, showUI:=True, - connectionTimeout:=TestingConnectionTimeout) + connectionTimeout:=TestingConnectionLargeTimeout) End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -262,6 +272,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -283,6 +294,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -304,6 +316,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -325,6 +338,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -349,6 +363,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -373,6 +388,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -397,6 +413,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -418,6 +435,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -442,7 +460,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -471,6 +489,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -495,8 +514,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = - Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ - AndAlso e.Message.Contains(NameOf(sourceFileName)) + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should.Throw(Of ArgumentException)().Where(exceptionExpression) End Using End Sub @@ -521,11 +540,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Dim exceptionExpression As Expressions.Expression(Of Func(Of ArgumentException, Boolean)) = - Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ - AndAlso e.Message.Contains(NameOf(sourceFileName)) + Function(e) e.Message.StartsWith(SR.IO_FilePathException) _ + AndAlso e.Message.Contains(NameOf(sourceFileName)) testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -548,6 +568,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -572,6 +593,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of FileNotFoundException)() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -601,6 +623,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -622,7 +645,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -647,6 +670,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -669,6 +693,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -691,6 +716,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -717,6 +743,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -743,6 +770,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -760,6 +788,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -778,6 +807,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -795,6 +825,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -819,6 +850,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -843,7 +875,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -867,6 +899,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -890,6 +923,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -911,14 +945,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of OperationCanceledException)() + webListener.ServerFault.Should.BeNull() End Using End Sub Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim webListener As New WebListener(SmallTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -932,6 +967,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -956,6 +992,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -977,6 +1014,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -997,7 +1035,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.Throw(Of WebException)() + testCode.Should.NotThrow() + webListener.ServerFault.Should.BeOfType(Of IOException)() End Using End Sub @@ -1022,6 +1061,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1046,6 +1086,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1095,6 +1136,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1123,6 +1165,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1145,6 +1188,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1174,6 +1218,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1197,7 +1242,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - VerifyFailedDownload(testDirectory:=Nothing, sourceFileName, listener) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1222,6 +1267,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1244,6 +1290,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1266,6 +1313,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1292,6 +1340,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub @@ -1318,6 +1367,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) + webListener.ServerFault.Should.BeNull() End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb index 85381891d6f..1689e74e87c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -9,6 +9,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Const LargeTestFileSize As Integer = 104_857_600 Friend Const SmallTestFileSize As Integer = 18_135 Friend Const InvalidUrlAddress As String = "invalidURL" + Friend Const TestingConnectionLargeTimeout As Integer = 100_000_000 Friend Const TestingConnectionTimeout As Integer = 100_000 End Module End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 876840f0137..1dbfb9b2b0c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -21,6 +21,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Is used to create the file name and the size of download. ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) + Debug.Assert(fileSize > 0) _fileSize = fileSize _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" Address = $"{_fileUrlPrefix}T{fileSize}" @@ -45,6 +46,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Public ReadOnly Property Address As String + Public Property ServerFault As Exception Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -138,19 +140,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests detectEncodingFromByteOrderMarks:=True, BufferSize) End Using - - response.StatusCode = 200 + End Using + Try Dim dataLength As String = formData(NameOf(dataLength)) If _fileSize.ToString <> dataLength Then - response.StatusCode = 500 + ServerFault = New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") End If Dim fileName As String = formData("filename") If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - response.StatusCode = 500 + ServerFault = New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") End If - End Using + Catch ex As Exception + ' Ignore is case upload is cancelled + End Try End If + response.StatusCode = 200 Else Dim responseString As String = Strings.StrDup(_fileSize, "A") Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) @@ -164,7 +169,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End If Finally - response?.Close() + Try + response?.Close() + Catch ex As Exception + + End Try response = Nothing End Try End Sub @@ -187,27 +196,31 @@ Namespace Microsoft.VisualBasic.Forms.Tests If request.ContentType.StartsWith("multipart/form-data", StringComparison.OrdinalIgnoreCase) Then Dim boundary As String = GetBoundary(request.ContentType) Using reader As New StreamReader(request.InputStream, request.ContentEncoding) - Dim content As String = reader.ReadToEnd() - Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) - - For Each part As String In parts - If part.Trim() <> "--" Then - Dim separator As String() = New String() {Environment.NewLine} - Dim lines As List(Of String) = part.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList - If lines.Count > 2 Then - Dim headerParts As String() = Nothing - Dim dispositionIndex As Integer = GetContentDispositionHeader(lines, headerParts) - - If dispositionIndex > -1 Then - result.Add("filename", GetFilename(headerParts)) - If lines.Count > dispositionIndex + 1 Then - result.Add("dataLength", GetDataLength(lines, dispositionIndex).ToString) + Try + Dim content As String = reader.ReadToEnd() + Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) + + For Each part As String In parts + If part.Trim() <> "--" Then + Dim separator As String() = New String() {Environment.NewLine} + Dim lines As List(Of String) = part.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList + If lines.Count > 2 Then + Dim headerParts As String() = Nothing + Dim dispositionIndex As Integer = GetContentDispositionHeader(lines, headerParts) + + If dispositionIndex > -1 Then + result.Add("filename", GetFilename(headerParts)) + If lines.Count > dispositionIndex + 1 Then + result.Add("dataLength", GetDataLength(lines, dispositionIndex).ToString) + End If + Exit For End If - Exit For End If End If - End If - Next + Next + Catch ex As Exception + ' ignore + End Try End Using End If From feb51c6a012ca5a7a4d5cf56fa45da81ccc9203d Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:37:37 -0800 Subject: [PATCH 061/119] Minor formatiting correction --- .../tests/UnitTests/System/Windows/TestUtilities/WebListener.vb | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index b3445fcf538..1dbfb9b2b0c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -197,7 +197,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim boundary As String = GetBoundary(request.ContentType) Using reader As New StreamReader(request.InputStream, request.ContentEncoding) Try - Dim content As String = reader.ReadToEnd() Dim parts As String() = content.Split(boundary, StringSplitOptions.RemoveEmptyEntries) From 6ee4575102ea417ee1470930d8b98bb82f6ea4e2 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:44:21 -0800 Subject: [PATCH 062/119] Fix large file manual test --- .../tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb | 4 ++-- .../tests/UnitTests/System/Windows/Forms/UploadFileTests.vb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 018b1071331..8b77ef3bc0b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1004,10 +1004,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index aa524220993..4cc984be374 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -927,7 +927,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) From 6e3b58e8a4812c176f802cc92a6fceef07006dc5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:51:11 -0800 Subject: [PATCH 063/119] Improve a couple of Skip comments Correct a test --- .../tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb | 4 ++-- .../tests/UnitTests/System/Windows/Forms/UploadFileTests.vb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index a5f5733f75d..590022fa926 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1004,10 +1004,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) Dim webListener As New WebListener(ExtraLargeTestFileSize) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index aa524220993..4cc984be374 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -927,7 +927,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) From c9f8d7ca3df64716d0907b68ff8cd4238d265307 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 24 Dec 2024 18:59:53 -0800 Subject: [PATCH 064/119] Improove responsiveness of upload and download and sync the code --- .../Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 8 +++++++- .../Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 8 +++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 671e0223c65..9b35e93a876 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -2,6 +2,8 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net +Imports System.Threading +Imports System.Windows.Forms Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal @@ -373,7 +375,11 @@ Namespace Microsoft.VisualBasic.Devices Throw t.Exception Else dialog?.ShowProgressDialog() - t.Wait() + Do While Not (t.IsCompleted OrElse t.IsFaulted OrElse t.IsCanceled) + 'prevent UI freeze + Thread.Sleep(10) + Application.DoEvents() + Loop If t.IsFaulted Then Throw t.Exception End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 468185ebd55..aaaab6fe5b5 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -3,6 +3,7 @@ Imports System.Net Imports System.Net.Http +Imports System.Threading Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO @@ -264,6 +265,7 @@ Namespace Microsoft.VisualBasic.Devices connectionTimeout As Integer, onUserCancel As UICancelOption) + ' Construct the local file. This will validate the full name and path sourceFileName = FileSystemUtils.NormalizeFilePath(sourceFileName, NameOf(sourceFileName)) ' Make sure the file exists @@ -284,11 +286,6 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try - ' Construct the local file. This will validate the full name and path - Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( - path:=sourceFileName, - paramName:=NameOf(sourceFileName)) - ' Get network credentials Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, @@ -314,6 +311,7 @@ Namespace Microsoft.VisualBasic.Devices dialog?.ShowProgressDialog() Do While Not (t.IsCompleted OrElse t.IsFaulted OrElse t.IsCanceled) 'prevent UI freeze + Thread.Sleep(10) Application.DoEvents() Loop If t.IsFaulted Then From c67d2a906b737023464c246cb29b5b7c98f89696 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 31 Dec 2024 16:34:38 -0800 Subject: [PATCH 065/119] Simplify DownloadFileAsync --- .../Devices/Network.DownloadFileAsync.vb | 224 ++++++++++-------- 1 file changed, 125 insertions(+), 99 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index e1a22b78625..8c52d572846 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -36,11 +36,12 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel As UICancelOption, Optional cancelToken As CancellationToken = Nothing) As Task - Dim clientHandler As HttpClientHandler = If(networkCredentials Is Nothing, - New HttpClientHandler, - New HttpClientHandler With {.Credentials = networkCredentials} - ) - Return DownloadFileAsync(addressUri, + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) + Return DownloadFileAsync( + addressUri, destinationFileName, clientHandler, dialog, @@ -72,11 +73,14 @@ Namespace Microsoft.VisualBasic.Devices Optional cancelToken As CancellationToken = Nothing) As Task Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) Await DownloadFileAsync( addressUri, destinationFileName, - networkCredentials, + clientHandler, dialog, connectionTimeout, overwrite, @@ -108,11 +112,13 @@ Namespace Microsoft.VisualBasic.Devices ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) - Await DownloadFileAsync( addressUri, destinationFileName, - networkCredentials, + clientHandler:=If( + networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}), dialog, connectionTimeout, overwrite, @@ -120,90 +126,6 @@ Namespace Microsoft.VisualBasic.Devices cancelToken).ConfigureAwait(continueOnCapturedContext:=False) End Function - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' Uri to the remote file - ''' Name and path of file where download is saved. - ''' An HttpClientHandler of the user performing the download. - ''' Progress Dialog. - ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' Indicates what to do if user cancels dialog (either throw or do nothing). - ''' - ''' Calls to all the other overloads will come through here. - Friend Shared Async Function DownloadFileAsync( - addressUri As Uri, - destinationFileName As String, - clientHandler As HttpClientHandler, - dialog As ProgressDialog, - connectionTimeout As Integer, - overwrite As Boolean, - onUserCancel As UICancelOption, - cancelToken As CancellationToken) As Task - - If cancelToken = Nothing Then - cancelToken = New CancellationTokenSource().Token - End If - - If connectionTimeout <= 0 Then - Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) - End If - - If addressUri Is Nothing Then - Throw VbUtils.GetArgumentNullException(NameOf(addressUri)) - End If - - ' Set credentials if we have any - Dim client As HttpClient = If(clientHandler Is Nothing, - New HttpClient(), - New HttpClient(clientHandler) - ) - - client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) - - 'Construct the local file. This will validate the full name and path - Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) - ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really - ' have a file and path - If IO.Directory.Exists(normalizedFilePath) Then - Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) - End If - - 'Throw if the file exists and the user doesn't want to overwrite - If Not overwrite AndAlso IO.File.Exists(normalizedFilePath) Then - Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) - End If - - 'Check to see if the target directory exists. If it doesn't, create it - Dim targetDirectory As String = IO.Path.GetDirectoryName(normalizedFilePath) - - ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect - If String.IsNullOrEmpty(targetDirectory) Then - Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) - End If - - If Not IO.Directory.Exists(targetDirectory) Then - IO.Directory.CreateDirectory(targetDirectory) - End If - - 'Create the copier - Dim copier As New HttpClientCopy(client, dialog) - - 'Download the file - Try - Await copier.DownloadFileWorkerAsync( - addressUri, - normalizedFilePath, - externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) - Catch ex As Exception - If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then - Throw - End If - End Try - - End Function - ''' ''' Downloads a file from the network to the specified path. ''' @@ -225,15 +147,27 @@ Namespace Microsoft.VisualBasic.Devices overwrite As Boolean, Optional cancelToken As CancellationToken = Nothing) As Task + If String.IsNullOrWhiteSpace(address) Then + Throw VbUtils.GetArgumentNullException(NameOf(address)) + End If + + Dim addressUri As Uri = GetUri(address.Trim()) + + ' Get network credentials + Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) Await DownloadFileAsync( - address, + addressUri, destinationFileName, - userName, - password, + clientHandler, dialog, connectionTimeout, overwrite, - onUserCancel:=UICancelOption.ThrowException, + UICancelOption.ThrowException, cancelToken).ConfigureAwait(continueOnCapturedContext:=False) End Function @@ -269,10 +203,14 @@ Namespace Microsoft.VisualBasic.Devices ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) Await DownloadFileAsync( addressUri, destinationFileName, - networkCredentials, + clientHandler, dialog, connectionTimeout, overwrite, @@ -302,10 +240,14 @@ Namespace Microsoft.VisualBasic.Devices overwrite As Boolean, Optional cancelToken As CancellationToken = Nothing) As Task + Dim clientHandler As HttpClientHandler = + If(networkCredentials Is Nothing, + New HttpClientHandler, + New HttpClientHandler With {.Credentials = networkCredentials}) Await DownloadFileAsync( addressUri, destinationFileName, - networkCredentials, + clientHandler, dialog, connectionTimeout, overwrite, @@ -313,5 +255,89 @@ Namespace Microsoft.VisualBasic.Devices cancelToken).ConfigureAwait(continueOnCapturedContext:=False) End Function + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' Uri to the remote file + ''' Name and path of file where download is saved. + ''' An HttpClientHandler of the user performing the download. + ''' Progress Dialog. + ''' Time allotted before giving up on a connection. + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Calls to all the other overloads will come through here. + Friend Shared Async Function DownloadFileAsync( + addressUri As Uri, + destinationFileName As String, + clientHandler As HttpClientHandler, + dialog As ProgressDialog, + connectionTimeout As Integer, + overwrite As Boolean, + onUserCancel As UICancelOption, + cancelToken As CancellationToken) As Task + + If cancelToken = Nothing Then + cancelToken = New CancellationTokenSource().Token + End If + + If connectionTimeout <= 0 Then + Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + End If + + If addressUri Is Nothing Then + Throw VbUtils.GetArgumentNullException(NameOf(addressUri)) + End If + + ' Set credentials if we have any + Dim client As HttpClient = If(clientHandler Is Nothing, + New HttpClient(), + New HttpClient(clientHandler) + ) + + client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) + + 'Construct the local file. This will validate the full name and path + Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) + ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really + ' have a file and path + If IO.Directory.Exists(normalizedFilePath) Then + Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + End If + + 'Throw if the file exists and the user doesn't want to overwrite + If Not overwrite AndAlso IO.File.Exists(normalizedFilePath) Then + Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) + End If + + 'Check to see if the target directory exists. If it doesn't, create it + Dim targetDirectory As String = IO.Path.GetDirectoryName(normalizedFilePath) + + ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect + If String.IsNullOrEmpty(targetDirectory) Then + Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) + End If + + If Not IO.Directory.Exists(targetDirectory) Then + IO.Directory.CreateDirectory(targetDirectory) + End If + + 'Create the copier + Dim copier As New HttpClientCopy(client, dialog) + + 'Download the file + Try + Await copier.DownloadFileWorkerAsync( + addressUri, + normalizedFilePath, + externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) + Catch ex As Exception + If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then + Throw + End If + End Try + + End Function + End Class End Namespace From 4d3c165e9c526268758c8a375f9a479299e49dd3 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 1 Jan 2025 18:37:15 -0800 Subject: [PATCH 066/119] Shorten long lines in Network.DownloadFileAsync --- .../Devices/Network.DownloadFileAsync.vb | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index 8c52d572846..bf0c35ee968 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -23,7 +23,9 @@ Namespace Microsoft.VisualBasic.Devices ''' The credentials of the user performing the download. ''' A ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' ''' ''' Friend Shared Function DownloadFileAsync( @@ -60,7 +62,9 @@ Namespace Microsoft.VisualBasic.Devices ''' The user's password. ''' A ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' ''' Friend Shared Async Function DownloadFileAsync( addressUri As Uri, @@ -97,8 +101,13 @@ Namespace Microsoft.VisualBasic.Devices ''' The user's password. ''' ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Friend Shared Async Function DownloadFileAsync( addressUri As Uri, destinationFileName As String, @@ -112,6 +121,7 @@ Namespace Microsoft.VisualBasic.Devices ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) + Await DownloadFileAsync( addressUri, destinationFileName, @@ -135,7 +145,9 @@ Namespace Microsoft.VisualBasic.Devices ''' The user's password. ''' A ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' ''' Friend Shared Async Function DownloadFileAsync( address As String, @@ -180,8 +192,12 @@ Namespace Microsoft.VisualBasic.Devices ''' The user's password. ''' A ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' ''' Friend Shared Async Function DownloadFileAsync( address As String, @@ -226,7 +242,9 @@ Namespace Microsoft.VisualBasic.Devices ''' The credentials of the user performing the download. ''' A ProgressDialog or Nothing. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' ''' ''' ''' Function will Throw on unhandled exceptions. From 134f52fc70b437be80aadbefdec494a03a84bc90 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 1 Jan 2025 18:57:29 -0800 Subject: [PATCH 067/119] Cleanup comments without space after single quote --- .../Devices/Network.DownloadFileAsync.vb | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index bf0c35ee968..6cdfe79301e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -281,8 +281,12 @@ Namespace Microsoft.VisualBasic.Devices ''' An HttpClientHandler of the user performing the download. ''' Progress Dialog. ''' Time allotted before giving up on a connection. - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + ''' + ''' Indicates what to do if user cancels dialog (either throw or do nothing). + ''' ''' ''' Calls to all the other overloads will come through here. Friend Shared Async Function DownloadFileAsync( @@ -300,7 +304,9 @@ Namespace Microsoft.VisualBasic.Devices End If If connectionTimeout <= 0 Then - Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) + Throw VbUtils.GetArgumentExceptionWithArgName( + argumentName:=NameOf(connectionTimeout), + resourceKey:=SR.Network_BadConnectionTimeout) End If If addressUri Is Nothing Then @@ -315,20 +321,24 @@ Namespace Microsoft.VisualBasic.Devices client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) - 'Construct the local file. This will validate the full name and path - Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) - ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really - ' have a file and path + ' Construct the local file. This will validate the full name and path + Dim normalizedFilePath As String = FileSystemUtils.NormalizeFilePath( + path:=destinationFileName, + paramName:=NameOf(destinationFileName)) + + ' Sometime a path that can't be parsed is normalized to the current directory. + ' This makes sure we really have a file and path If IO.Directory.Exists(normalizedFilePath) Then Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) End If - 'Throw if the file exists and the user doesn't want to overwrite + ' Throw if the file exists and the user doesn't want to overwrite If Not overwrite AndAlso IO.File.Exists(normalizedFilePath) Then - Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) + Throw New IO.IOException( + message:=VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) End If - 'Check to see if the target directory exists. If it doesn't, create it + ' Check to see if the target directory exists. If it doesn't, create it Dim targetDirectory As String = IO.Path.GetDirectoryName(normalizedFilePath) ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect @@ -340,10 +350,10 @@ Namespace Microsoft.VisualBasic.Devices IO.Directory.CreateDirectory(targetDirectory) End If - 'Create the copier + ' Create the copier Dim copier As New HttpClientCopy(client, dialog) - 'Download the file + ' Download the file Try Await copier.DownloadFileWorkerAsync( addressUri, From 553b8861bdaaab38d7e977d401e04612e7474fdf Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 1 Jan 2025 19:36:43 -0800 Subject: [PATCH 068/119] FIX comments in Network files --- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 2 +- .../src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 2 +- .../Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 9b35e93a876..4a711860216 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -376,7 +376,7 @@ Namespace Microsoft.VisualBasic.Devices Else dialog?.ShowProgressDialog() Do While Not (t.IsCompleted OrElse t.IsFaulted OrElse t.IsCanceled) - 'prevent UI freeze + ' prevent UI freeze Thread.Sleep(10) Application.DoEvents() Loop diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index aaaab6fe5b5..f2c99fa538f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -310,7 +310,7 @@ Namespace Microsoft.VisualBasic.Devices Else dialog?.ShowProgressDialog() Do While Not (t.IsCompleted OrElse t.IsFaulted OrElse t.IsCanceled) - 'prevent UI freeze + ' prevent UI freeze Thread.Sleep(10) Application.DoEvents() Loop diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb index 13490f47232..cb827f53070 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -57,10 +57,10 @@ Namespace Microsoft.VisualBasic.Devices New HttpClient(clientHandler)) client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) - 'Create the copier + ' Create the copier Dim copier As New HttpClientCopy(client, dialog) - 'Upload the file + ' Upload the file Try Await copier.UploadFileWorkerAsync( filePath:=normalizedFilePath, From 51e23d31f5722bd5a3403bf430859767b9e62068 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 1 Jan 2025 19:41:51 -0800 Subject: [PATCH 069/119] Fix a few more missing spaces --- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 6deb2014cc8..eaecb613a9a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -68,7 +68,7 @@ Namespace Microsoft.VisualBasic.Devices showUI As Boolean) As ProgressDialog If InteractiveEnvironment(showUI) Then - 'Construct the local file. This will validate the full name and path + ' Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=fileNameWithPath, paramName:=NameOf(fileNameWithPath)) @@ -93,7 +93,7 @@ Namespace Microsoft.VisualBasic.Devices Try Return New Uri(address) Catch ex As UriFormatException - 'Throw an exception with an error message more appropriate to our API + ' Throw an exception with an error message more appropriate to our API Throw GetArgumentExceptionWithArgName( argumentName:=NameOf(address), resourceKey:=SR.Network_InvalidUriString, From b8a85167afc992142401a12ef5f944d0c363d578 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 5 Jan 2025 18:50:43 -0800 Subject: [PATCH 070/119] Fix some cancel's of DownloadFile and UploadFile being ignored. --- .../Devices/Network.DownloadFile.vb | 6 ++-- .../Devices/Network.DownloadFileAsync.vb | 1 + .../Devices/Network.UploadFileAsync.vb | 2 ++ .../MyServices/Internal/HttpClientCopy.vb | 32 +++++++++---------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 4a711860216..a449f1e11c5 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -150,6 +150,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try dialog = GetProgressDialog(address, destinationFileName, showUI) + Dim cancelToken As New CancellationToken Dim t As Task = DownloadFileAsync( address, destinationFileName, @@ -158,7 +159,8 @@ Namespace Microsoft.VisualBasic.Devices dialog, connectionTimeout, overwrite, - onUserCancel:=UICancelOption.ThrowException) + onUserCancel:=UICancelOption.ThrowException, + cancelToken) If t.IsFaulted Then ' This will be true if any parameters are bad @@ -166,7 +168,7 @@ Namespace Microsoft.VisualBasic.Devices Else dialog?.ShowProgressDialog() t.Wait() - If t.IsFaulted Then + If t.IsFaulted OrElse t.IsCanceled Then Throw t.Exception End If End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index 6cdfe79301e..0f350ed9c0a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -361,6 +361,7 @@ Namespace Microsoft.VisualBasic.Devices externalToken:=cancelToken).ConfigureAwait(continueOnCapturedContext:=False) Catch ex As Exception If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then + CloseProgressDialog(dialog) Throw End If End Try diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb index cb827f53070..7e046144fb2 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -76,6 +76,8 @@ Namespace Microsoft.VisualBasic.Devices End If Throw End If + Finally + CloseProgressDialog(dialog) End Try End Function diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index d69cb641c1f..f2cfe043a11 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -106,23 +106,23 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Dim response As HttpResponseMessage = Nothing Dim totalBytesRead As Long = 0 - Using linkedCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceGet.Token, externalToken) - Try + Try + Using linkedCts As CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancelTokenSourceGet.Token, externalToken) response = Await _httpClient.GetAsync( - requestUri:=addressUri, - completionOption:=HttpCompletionOption.ResponseHeadersRead, - cancellationToken:=_cancelTokenSourceGet.Token).ConfigureAwait(continueOnCapturedContext:=False) - Catch ex As TaskCanceledException - If ex.CancellationToken = externalToken Then - externalToken.ThrowIfCancellationRequested() - ElseIf ex.CancellationToken = _cancelTokenSourceGet.Token Then - ' a real cancellation, triggered by the caller - Throw - Else - Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) - End If - End Try - End Using + requestUri:=addressUri, + completionOption:=HttpCompletionOption.ResponseHeadersRead, + cancellationToken:=_cancelTokenSourceGet.Token).ConfigureAwait(continueOnCapturedContext:=False) + End Using + Catch ex As TaskCanceledException + If ex.CancellationToken = externalToken Then + externalToken.ThrowIfCancellationRequested() + ElseIf ex.CancellationToken = _cancelTokenSourceGet.Token Then + ' a real cancellation, triggered by the caller + Throw + Else + Throw New WebException(SR.net_webstatus_Timeout, WebExceptionStatus.Timeout) + End If + End Try Select Case response.StatusCode Case HttpStatusCode.OK _cancelTokenSourceReadStream = CancellationTokenSource.CreateLinkedTokenSource(New CancellationTokenSource().Token, externalToken) From ac82c9f9bf37885f56d5ab5606bebd8666108795 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 7 Jan 2025 23:40:27 -0800 Subject: [PATCH 071/119] PR feedback --- .../Devices/Network.DownloadFile.vb | 79 +++++++++---------- .../VisualBasic/Devices/Network.UploadFile.vb | 14 +++- .../VisualBasic/Devices/NetworkUtilities.vb | 20 ++--- .../Windows/Forms/FileSystemProxyTests.vb | 2 +- 4 files changed, 60 insertions(+), 55 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 6cc637e161b..8bee94db45f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net +Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal @@ -116,40 +117,6 @@ Namespace Microsoft.VisualBasic.Devices connectionTimeout As Integer, overwrite As Boolean) - DownloadFile( - address, - destinationFileName, - userName, - password, - showUI, - connectionTimeout, - overwrite, - onUserCancel:=UICancelOption.ThrowException) - End Sub - - ''' - ''' Downloads a file from the network to the specified path. - ''' - ''' to the remote file. - ''' - ''' Name and path of file where download is saved. - ''' - ''' The name of the user performing the download. - ''' The user's password. - ''' Indicates whether or not to show a progress bar. - ''' Time allotted before giving up on a connection. - ''' - ''' Indicates whether or not the file should be overwritten if local file already exists. - ''' - Public Sub DownloadFile( - address As Uri, - destinationFileName As String, - userName As String, - password As String, - showUI As Boolean, - connectionTimeout As Integer, - overwrite As Boolean) - DownloadFile( address, destinationFileName, @@ -195,6 +162,8 @@ Namespace Microsoft.VisualBasic.Devices End If Dim addressUri As Uri = GetUri(address.Trim()) + + ' Get network credentials Dim networkCredentials As ICredentials = GetNetworkCredentials(userName, password) DownloadFile( @@ -207,6 +176,40 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel) End Sub + ''' + ''' Downloads a file from the network to the specified path. + ''' + ''' to the remote file, + ''' + ''' Name and path of file where download is saved. + ''' + ''' The name of the user performing the download. + ''' The user's password. + ''' Indicates whether or not to show a progress bar. + ''' Time allotted before giving up on a connection. + ''' + ''' Indicates whether or not the file should be overwritten if local file already exists. + ''' + Public Sub DownloadFile( + address As Uri, + destinationFileName As String, + userName As String, + password As String, + showUI As Boolean, + connectionTimeout As Integer, + overwrite As Boolean) + + DownloadFile( + address, + destinationFileName, + userName, + password, + showUI, + connectionTimeout, + overwrite, + UICancelOption.ThrowException) + End Sub + ''' ''' Downloads a file from the network to the specified path. ''' @@ -311,9 +314,7 @@ Namespace Microsoft.VisualBasic.Devices onUserCancel As UICancelOption) If connectionTimeout <= 0 Then - Throw VbUtils.GetArgumentExceptionWithArgName( - argumentName:=NameOf(connectionTimeout), - resourceKey:=SR.Network_BadConnectionTimeout) + Throw VbUtils.GetArgumentExceptionWithArgName(NameOf(connectionTimeout), SR.Network_BadConnectionTimeout) End If If address Is Nothing Then @@ -327,9 +328,7 @@ Namespace Microsoft.VisualBasic.Devices client.UseNonPassiveFtp = showUI ' Construct the local file. This will validate the full name and path - Dim fullFilename As String = CompilerServices.FileSystemUtils.NormalizeFilePath( - path:=destinationFileName, - paramName:=NameOf(destinationFileName)) + Dim fullFilename As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really ' have a file and path diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index fb68d48c432..983f97bc755 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -288,10 +288,16 @@ Namespace Microsoft.VisualBasic.Devices client.Credentials = networkCredentials End If - Dim dialog As ProgressDialog = GetProgressDialog( - address:=address.AbsolutePath, - fileNameWithPath:=sourceFileName, - showUI) + Dim dialog As ProgressDialog = Nothing + If showUI AndAlso Environment.UserInteractive Then + dialog = New ProgressDialog With { + .Text = GetResourceString(SR.ProgressDialogUploadingTitle, sourceFileName), + .LabelText = GetResourceString( + resourceKey:=SR.ProgressDialogUploadingLabel, + sourceFileName, + address.AbsolutePath) + } + End If ' Create the copier Dim copier As New WebClientCopy(client, dialog) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 6deb2014cc8..6077cf1e0e8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -6,8 +6,6 @@ Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal -Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils - Namespace Microsoft.VisualBasic.Devices Friend Module NetworkUtilities @@ -56,7 +54,7 @@ Namespace Microsoft.VisualBasic.Devices ''' Centralize setup a to be used with FileDownload and FileUpload. ''' ''' Address to the remote file, http, ftp etc... - ''' Name and path of file where download is saved. + ''' Name and path of file where download is saved. ''' Indicates whether or not to show a progress bar. ''' ''' if InteractiveEnvironment @@ -64,19 +62,21 @@ Namespace Microsoft.VisualBasic.Devices ''' Friend Function GetProgressDialog( address As String, - fileNameWithPath As String, + destinationFileName As String, showUI As Boolean) As ProgressDialog If InteractiveEnvironment(showUI) Then 'Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( - path:=fileNameWithPath, - paramName:=NameOf(fileNameWithPath)) + path:=destinationFileName, + paramName:=NameOf(destinationFileName)) Return New ProgressDialog With { - .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = VbUtils.GetResourceString( - resourceKey:=SR.ProgressDialogDownloadingLabel, - address, fullFilename)} + .Text = Utils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), + .LabelText = Utils.GetResourceString( + ResourceKey:=SR.ProgressDialogDownloadingLabel, + address, + fullFilename) + } End If Return Nothing End Function diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb index dab64ebc6fc..523cf1fb5bd 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb @@ -283,7 +283,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests - + Public Sub OpenTextFieldParserProxyTest(delimiter As String) Dim sourceDirectoryName As String = CreateTempDirectory() Dim fileCSV As String = CreateTempFile(sourceDirectoryName) From e2ff0732ad1a70d3fefb71b76343e8d3fb1e5c76 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 00:05:45 -0800 Subject: [PATCH 072/119] PR Feedback Cleanup files in Separate PR --- .../SingleInstanceHelpers.vb | 28 ++++----- .../WindowsFormsApplicationBase.vb | 3 +- .../Devices/Network.DownloadFile.vb | 24 ++++---- .../VisualBasic/Devices/Network.UploadFile.vb | 4 +- .../VisualBasic/Devices/NetworkUtilities.vb | 11 +++- .../src/Microsoft/VisualBasic/Interaction.vb | 3 +- .../VisualBasic/MyServices/ClipboardProxy.vb | 35 +++++------ .../Windows/Forms/TestUtilitiesTests.vb | 7 +++ .../TestData/PathSeparatorTestData.vb | 19 ++++++ .../TestUtilities/VbFileCleanupTestBase.vb | 58 +++++++++---------- 10 files changed, 110 insertions(+), 82 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb index 4801df5276b..44ad4424f25 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/SingleInstanceHelpers.vb @@ -21,17 +21,16 @@ Namespace Microsoft.VisualBasic.ApplicationServices Using stream As New MemoryStream While True Dim buffer As Byte() = New Byte(bufferLength - 1) {} - Dim bytesRead As Integer = Await pipeServer.ReadAsync( - buffer:=buffer.AsMemory(start:=0, length:=bufferLength), - cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Dim bytesRead As Integer = + Await pipeServer.ReadAsync( + buffer:=buffer.AsMemory(start:=0, length:=bufferLength), + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) If bytesRead = 0 Then Exit While End If Await stream.WriteAsync( buffer:=buffer.AsMemory(start:=0, length:=bytesRead), - cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End While stream.Seek(0, SeekOrigin.Begin) Dim serializer As New DataContractSerializer(GetType(String())) @@ -55,8 +54,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices content = stream.ToArray() End Using Await pipeClient.WriteAsync( - buffer:=content.AsMemory(start:=0, length:=content.Length), cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + buffer:=content.AsMemory(start:=0, length:=content.Length), + cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End Function Friend Async Function SendSecondInstanceArgsAsync( @@ -70,10 +69,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices direction:=PipeDirection.Out, options:=NamedPipeOptions) - Await pipeClient.ConnectAsync(cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) - Await WriteArgsAsync(pipeClient, args, cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Await pipeClient.ConnectAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) + Await WriteArgsAsync(pipeClient, args, cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) End Using End Function @@ -102,11 +99,10 @@ Namespace Microsoft.VisualBasic.ApplicationServices While True cancellationToken.ThrowIfCancellationRequested() - Await pipeServer.WaitForConnectionAsync(cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Await pipeServer.WaitForConnectionAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) Try - Dim args() As String = Await ReadArgsAsync(pipeServer, cancellationToken) _ - .ConfigureAwait(continueOnCapturedContext:=False) + Dim args() As String = + Await ReadArgsAsync(pipeServer, cancellationToken).ConfigureAwait(continueOnCapturedContext:=False) If args IsNot Nothing Then callback(args) End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb index 9ee86a368fe..18a97c87960 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb @@ -1082,8 +1082,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices Dim awaitable As ConfiguredTaskAwaitable = SendSecondInstanceArgsAsync( pipeName:=applicationInstanceID, args:=commandLine, - cancellationToken:=tokenSource.Token) _ - .ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken:=tokenSource.Token).ConfigureAwait(continueOnCapturedContext:=False) awaitable.GetAwaiter().GetResult() Catch ex As Exception diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 37b4776aa91..8bee94db45f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -286,7 +286,6 @@ Namespace Microsoft.VisualBasic.Devices connectionTimeout, overwrite, UICancelOption.ThrowException) - End Sub ''' @@ -328,7 +327,7 @@ Namespace Microsoft.VisualBasic.Devices ' Don't use passive mode if we're showing UI client.UseNonPassiveFtp = showUI - 'Construct the local file. This will validate the full name and path + ' Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath(destinationFileName, NameOf(destinationFileName)) ' Sometime a path that can't be parsed is normalized to the current directory. This makes sure we really @@ -337,7 +336,7 @@ Namespace Microsoft.VisualBasic.Devices Throw VbUtils.GetInvalidOperationException(SR.Network_DownloadNeedsFilename) End If - 'Throw if the file exists and the user doesn't want to overwrite + ' Throw if the file exists and the user doesn't want to overwrite If IO.File.Exists(fullFilename) And Not overwrite Then Throw New IO.IOException(VbUtils.GetResourceString(SR.IO_FileExists_Path, destinationFileName)) End If @@ -349,7 +348,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = GetProgressDialog(address.AbsolutePath, fullFilename, showUI) - 'Check to see if the target directory exists. If it doesn't, create it + ' Check to see if the target directory exists. If it doesn't, create it Dim targetDirectory As String = IO.Path.GetDirectoryName(fullFilename) ' Make sure we have a meaningful directory. If we don't, the destinationFileName is suspect @@ -361,21 +360,20 @@ Namespace Microsoft.VisualBasic.Devices IO.Directory.CreateDirectory(targetDirectory) End If - 'Create the copier + ' Create the copier Dim copier As New WebClientCopy(client, dialog) - 'Download the file + ' Download the file copier.DownloadFile(address, fullFilename) - 'Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then - If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then - Throw New OperationCanceledException() - End If + ' Handle a dialog cancel + If showUI _ + AndAlso Environment.UserInteractive _ + AndAlso onUserCancel = UICancelOption.ThrowException _ + AndAlso dialog.UserCanceledTheDialog Then + Throw New OperationCanceledException() End If - End Using - End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 40ea15623a6..9a2ee54c814 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -289,7 +289,7 @@ Namespace Microsoft.VisualBasic.Devices End If Dim dialog As ProgressDialog = Nothing - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then dialog = New ProgressDialog With { .Text = GetResourceString(SR.ProgressDialogUploadingTitle, sourceFileName), .LabelText = GetResourceString( @@ -306,7 +306,7 @@ Namespace Microsoft.VisualBasic.Devices copier.UploadFile(sourceFileName, address) ' Handle a dialog cancel - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then If onUserCancel = UICancelOption.ThrowException And dialog.UserCanceledTheDialog Then Throw New OperationCanceledException() End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 1458e5fcefe..6077cf1e0e8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -56,13 +56,16 @@ Namespace Microsoft.VisualBasic.Devices ''' Address to the remote file, http, ftp etc... ''' Name and path of file where download is saved. ''' Indicates whether or not to show a progress bar. - ''' . + ''' + ''' if InteractiveEnvironment + ''' otherwise return . + ''' Friend Function GetProgressDialog( address As String, destinationFileName As String, showUI As Boolean) As ProgressDialog - If showUI AndAlso Environment.UserInteractive Then + If InteractiveEnvironment(showUI) Then 'Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=destinationFileName, @@ -98,5 +101,9 @@ Namespace Microsoft.VisualBasic.Devices End Try End Function + Friend Function InteractiveEnvironment(showUI As Boolean) As Boolean + Return showUI AndAlso Environment.UserInteractive + End Function + End Module End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb index a0ba0ebffaf..14004e09e8b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb @@ -250,7 +250,8 @@ Namespace Microsoft.VisualBasic 'Low-order 4 bits (0x000f), legal values: 0, 1, 2, 3, 4, 5 ' next 4 bits (0x00f0), legal values: 0, &H10, &H20, &H30, &H40 ' next 4 bits (0x0f00), legal values: 0, &H100, &H200 - If ((Buttons And &HFI) > MsgBoxStyle.RetryCancel) OrElse ((Buttons And &HF0I) > MsgBoxStyle.Information) _ + If ((Buttons And &HFI) > MsgBoxStyle.RetryCancel) _ + OrElse ((Buttons And &HF0I) > MsgBoxStyle.Information) _ OrElse ((Buttons And &HF00I) > MsgBoxStyle.DefaultButton3) Then Buttons = MsgBoxStyle.OkOnly End If diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index ee2e28cc510..d3a5135af94 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices Namespace Microsoft.VisualBasic.MyServices ''' - ''' A class that wraps System.Windows.Forms.Clipboard so that + ''' A class that wraps so that ''' a clipboard can be instanced. ''' @@ -25,14 +25,14 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Removes everything from the clipboard. + ''' Removes everything from the . ''' Public Sub Clear() Clipboard.Clear() End Sub ''' - ''' Indicates whether or not there's an audio stream saved to the clipboard. + ''' Indicates whether or not there's an audio stream saved to the . ''' ''' if an audio stream is available, otherwise . Public Function ContainsAudio() As Boolean @@ -40,7 +40,8 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not there is data on the clipboard in the passed in format. + ''' Indicates whether or not there is data on the in the passed in format. + ''' or can be converted to that format. ''' ''' ''' if there's data in the passed in format, otherwise . @@ -57,7 +58,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicate whether or not an image has been saved to the clipboard. + ''' Indicate whether or not an image has been saved to the . ''' ''' if an image is available, otherwise . Public Function ContainsImage() As Boolean @@ -65,7 +66,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard. + ''' Indicates whether or not text is available on the . ''' ''' if text is available, otherwise . Public Function ContainsText() As Boolean @@ -73,7 +74,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Indicates whether or not text is available on the clipboard in + ''' Indicates whether or not text is available on the in ''' the passed in format. ''' ''' The type of text being checked for. @@ -83,15 +84,15 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an audio stream from the clipboard. + ''' Gets an audio stream from the . ''' - ''' The audio stream as a Stream. + ''' The audio stream as a . Public Function GetAudioStream() As Stream Return Clipboard.GetAudioStream() End Function ''' - ''' Gets data from the clipboard that's been saved in the passed in format. + ''' Gets data from the that's been saved in the passed in format. ''' ''' The type of data being sought. ''' The data. @@ -125,7 +126,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets an Image from the clipboard. + ''' Retrieves an from the . ''' ''' The image. Public Function GetImage() As Image @@ -133,7 +134,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Gets text from the clipboard. + ''' Gets text from the . ''' ''' The text as a String. Public Function GetText() As String @@ -150,7 +151,7 @@ Namespace Microsoft.VisualBasic.MyServices End Function ''' - ''' Saves the passed in audio byte array to the clipboard. + ''' Saves the passed in audio byte array to the . ''' ''' The byte array to be saved. Public Sub SetAudio(audioBytes As Byte()) @@ -158,15 +159,15 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in audio stream to the clipboard. + ''' Saves the passed in audio stream to the . ''' - ''' The stream to be saved. + ''' The to be saved. Public Sub SetAudio(audioStream As Stream) Clipboard.SetAudio(audioStream) End Sub ''' - ''' Saves the passed in data to the clipboard in the passed in format. + ''' Saves the passed in data to the in the passed in format. ''' ''' The format in which to save the data. ''' The data to be saved. @@ -185,7 +186,7 @@ Namespace Microsoft.VisualBasic.MyServices End Sub ''' - ''' Saves the passed in file drop list to the clipboard. + ''' Saves the passed in file drop list to the . ''' ''' The file drop list as a . Public Sub SetFileDropList(filePaths As StringCollection) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb index 970251e2476..c006f2d36b1 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/TestUtilitiesTests.vb @@ -23,6 +23,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests testClass.Any.Should.BeTrue() End Sub + + Public Sub PathSeparatorDataIteratorTests() + Dim testClass As New PathSeparatorTestData + testClass.IEnumerable_GetEnumerator.Should.NotBeNull() + testClass.Any.Should.BeTrue() + End Sub + Public Sub TimeTestDataIteratorTests() Dim testClass As New TimeTestData diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb new file mode 100644 index 00000000000..fe324539cd7 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/PathSeparatorTestData.vb @@ -0,0 +1,19 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class PathSeparatorTestData + Implements IEnumerable(Of Object()) + + Public Iterator Function GetEnumerator() As IEnumerator(Of Object()) Implements IEnumerable(Of Object()).GetEnumerator + Yield {IO.Path.DirectorySeparatorChar} + Yield {IO.Path.AltDirectorySeparatorChar} + End Function + + Public Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator + Return GetEnumerator() + End Function + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 72ce4a6a912..18bc3f06182 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -10,7 +10,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Implements IDisposable Private Shared ReadOnly s_baseTempPath As String = Path.Combine(Path.GetTempPath, "DownLoadTest9d9e3a8-7a46-4333-a0eb-4faf76994801") - + Friend Const DefaultFileName As String = "Testing.Txt" Friend ReadOnly _testDirectories As New HashSet(Of String) Protected Overrides Sub Finalize() @@ -35,29 +35,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Try End Sub - ''' - ''' Creates or returns a directory based on the name of the function that - ''' call it. The base directory is described above. - ''' Even if directory exists this call will success and just return it. - ''' - ''' - ''' If >0 use line number as part of name. - ''' The name of a directory that is safe to write to and is verified to exist. - Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String - Dim folder As String - If lineNumber > 0 Then - folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") - Else - folder = Path.Combine(BaseTempPath, memberName) - End If - - If _testDirectories.Add(folder) Then - Directory.CreateDirectory(folder) - End If - - Return folder - End Function - ''' ''' If size >= 0 then create the file with size length. ''' @@ -68,7 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' The full path and file name of the created file. ''' If size = -1 no file is create but the full path is returned. ''' - Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = "Testing.Txt", Optional size As Integer = -1) As String + Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) If size >= 0 Then @@ -104,14 +81,37 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return True End Function + Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String + Return Path.Combine(testDirectory, GetUniqueFileName()) + End Function + + ''' + ''' Creates or returns a directory based on the name of the function that + ''' call it. The base directory is described above. + ''' Even if directory exists this call will success and just return it. + ''' + ''' + ''' If >0 use line number as part of name. + ''' The name of a directory that is safe to write to and is verified to exist. + Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String + Dim folder As String + If lineNumber > 0 Then + folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") + Else + folder = Path.Combine(BaseTempPath, memberName) + End If + + If _testDirectories.Add(folder) Then + Directory.CreateDirectory(folder) + End If + + Return folder + End Function + Friend Sub Dispose() Implements IDisposable.Dispose Dispose(disposing:=True) GC.SuppressFinalize(Me) End Sub - Friend Shared Function GetUniqueFileNameWithPath(testDirectory As String) As String - Return Path.Combine(testDirectory, GetUniqueFileName()) - End Function - End Class End Namespace From f24382a0298d597936cf72239c309e1f48d818b3 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 00:21:07 -0800 Subject: [PATCH 073/119] Prevent Merge issues --- .../VisualBasic/Devices/Network.UploadFile.vb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index f2c99fa538f..dcf939a4809 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -291,10 +291,15 @@ Namespace Microsoft.VisualBasic.Devices If(networkCredentials Is Nothing, New HttpClientHandler, New HttpClientHandler With {.Credentials = networkCredentials}) - dialog = GetProgressDialog( - address:=address.AbsolutePath, - fileNameWithPath:=sourceFileName, - showUI) + If InteractiveEnvironment(showUI) Then + dialog = New ProgressDialog With { + .Text = GetResourceString(SR.ProgressDialogUploadingTitle, sourceFileName), + .LabelText = GetResourceString( + resourceKey:=SR.ProgressDialogUploadingLabel, + sourceFileName, + address.AbsolutePath) + } + End If Dim t As Task = UploadFileAsync( sourceFileName, From 3f71dd5e8dc8373df553520f53c12012adf3dcd1 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 00:58:52 -0800 Subject: [PATCH 074/119] Fix merge error --- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index a449f1e11c5..93f0d91c79c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -493,7 +493,7 @@ Namespace Microsoft.VisualBasic.Devices Dim dialog As ProgressDialog = Nothing Try - dialog = GetProgressDialog(address:=address.AbsolutePath, fileNameWithPath:=destinationFileName, showUI) + dialog = GetProgressDialog(address:=address.AbsolutePath, destinationFileName, showUI) Dim t As Task = DownloadFileAsync( addressUri:=address, destinationFileName, From 6509c30b29509965809e44d1ee2797ee57f65126 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 19:00:25 -0800 Subject: [PATCH 075/119] Fix merge error --- .../src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index f52a19afede..d3a5135af94 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -34,10 +34,7 @@ Namespace Microsoft.VisualBasic.MyServices ''' ''' Indicates whether or not there's an audio stream saved to the . ''' - ''' - ''' see langword="True"/> if an audio is available, - ''' otherwise . - ''' + ''' if an audio stream is available, otherwise . Public Function ContainsAudio() As Boolean Return Clipboard.ContainsAudio() End Function From e46d87e679bbf221987276c62440db5a38b16bdc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 19:01:38 -0800 Subject: [PATCH 076/119] Fix merge error --- .../src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index f52a19afede..d3a5135af94 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -34,10 +34,7 @@ Namespace Microsoft.VisualBasic.MyServices ''' ''' Indicates whether or not there's an audio stream saved to the . ''' - ''' - ''' see langword="True"/> if an audio is available, - ''' otherwise . - ''' + ''' if an audio stream is available, otherwise . Public Function ContainsAudio() As Boolean Return Clipboard.ContainsAudio() End Function From bf4d70ad4b9efb082c681754d2580651fec7dcd7 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 10 Jan 2025 19:49:16 -0800 Subject: [PATCH 077/119] PR Feedback Don't create new exceptions. The server will now write issues as text to a public property ServerFault. The tests can then verify that there were no protocal issues except here expected. --- .../System/Windows/Forms/UploadFileTests.vb | 107 +++++++++--------- .../Windows/TestUtilities/WebListener.vb | 13 ++- 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 4cc984be374..d7e5a696ce5 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -27,7 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -45,7 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -63,7 +63,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -83,7 +83,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -101,7 +101,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeOfType(Of IOException)() + webListener.ServerFaultMessage.Should.StartWith("File size mismatch") End Using End Sub @@ -125,7 +125,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -153,7 +153,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -178,7 +178,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -202,7 +202,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) - webListener.ServerFault.Should.BeNull() End Using End Sub @@ -226,7 +225,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -248,7 +247,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -272,7 +271,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -294,7 +293,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of UriFormatException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -316,7 +315,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -338,7 +337,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -363,7 +362,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -388,7 +387,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -413,7 +412,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -435,7 +434,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -460,7 +459,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -489,7 +488,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -545,7 +544,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -568,7 +567,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -593,7 +592,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of FileNotFoundException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -623,7 +622,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -645,7 +644,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -670,7 +669,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -693,7 +692,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -716,7 +715,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -743,7 +742,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -770,7 +769,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -788,7 +787,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -807,7 +806,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -825,7 +824,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -850,7 +849,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -875,7 +874,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -899,7 +898,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Timeout) - webListener.ServerFault.Should.BeNull() End Using End Sub @@ -923,7 +921,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -945,7 +943,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of OperationCanceledException)() - webListener.ServerFault.Should.BeNull() End Using End Sub @@ -967,7 +964,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -992,7 +989,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1014,7 +1011,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1036,7 +1033,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeOfType(Of IOException)() + webListener.ServerFaultMessage.Should.StartWith("File size mismatch") End Using End Sub @@ -1061,7 +1058,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1086,7 +1083,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1136,7 +1133,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1165,7 +1162,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1188,7 +1185,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1218,7 +1215,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1242,7 +1239,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1267,7 +1264,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1290,7 +1287,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1313,7 +1310,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1340,7 +1337,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub @@ -1367,7 +1364,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFault.Should.BeNull() + webListener.ServerFaultMessage.Should.BeNull() End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 1dbfb9b2b0c..f952126e35f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -46,7 +46,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Public ReadOnly Property Address As String - Public Property ServerFault As Exception + ''' + ''' This server will save a when something in the stream doesn't + ''' match what is expected. These will never be visible to the user. + ''' + ''' A with a description of the issue or + Public Property ServerFaultMessage As String Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -144,12 +149,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests Try Dim dataLength As String = formData(NameOf(dataLength)) If _fileSize.ToString <> dataLength Then - ServerFault = New IOException($"File size mismatch, expected {_fileSize} actual {dataLength}") + ServerFaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" End If Dim fileName As String = formData("filename") If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - ServerFault = New IOException($"Filename incorrect, expected 'Testing.Txt', actual {fileName}") + ServerFaultMessage = $"Filename incorrect, expected 'Testing.Txt', actual {fileName}" End If Catch ex As Exception ' Ignore is case upload is cancelled @@ -219,7 +224,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If Next Catch ex As Exception - ' ignore + ServerFaultMessage = "MultipartFormData format Error" End Try End Using End If From b7ad7ae9d94f8fc7f05e5f35fa8c8d280dfbfa24 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 11 Jan 2025 20:11:57 -0800 Subject: [PATCH 078/119] Sort Unauthorized Access tests together Fix error in Progress Progress Dialog Labels Use Enum to define testing file sizes Get FileSize and URL from WebListner to allow 1 source of truth This will also allow easier testing with real file server --- .../VisualBasic/Devices/NetworkUtilities.vb | 15 +- .../System/Windows/Forms/DownloadFileTests.vb | 428 +++++++++--------- .../System/Windows/Forms/UploadFileTests.vb | 206 ++++----- .../DownloadFileTestConstants.vb | 3 - .../Windows/TestUtilities/EnumFileSizes.vb | 12 + .../Windows/TestUtilities/WebListener.vb | 169 +++---- 6 files changed, 430 insertions(+), 403 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 6077cf1e0e8..2d23637c398 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -6,6 +6,8 @@ Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal +Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils + Namespace Microsoft.VisualBasic.Devices Friend Module NetworkUtilities @@ -70,13 +72,14 @@ Namespace Microsoft.VisualBasic.Devices Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=destinationFileName, paramName:=NameOf(destinationFileName)) + Dim addressTrimmed As String = address.Replace("//", "/") Return New ProgressDialog With { - .Text = Utils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), - .LabelText = Utils.GetResourceString( - ResourceKey:=SR.ProgressDialogDownloadingLabel, - address, - fullFilename) - } + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, addressTrimmed), + .LabelText = VbUtils.GetResourceString( + resourceKey:=SR.ProgressDialogDownloadingLabel, + addressTrimmed, + fullFilename) + } End If Return Nothing End Function diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 590022fa926..fbbd040c096 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -13,11 +13,148 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class DownloadFileTests Inherits VbFileCleanupTestBase + + + + Public Sub DownloadFile_UnauthorizedUriWithAllOptions_ExceptOnUserCancel_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + GetNetworkCredentials(DefaultUserName, password), + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + + + + + Public Sub DownloadFile_UnauthorizedUriWithUserNamePassword_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + userName:=DefaultUserName, + password:=String.Empty) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + + + + + Public Sub DownloadFile_UnauthorizedUriWithUserNamePassword_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + + + + + Public Sub DownloadFile_UnauthorizedUrlWithUserNamePassword_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + userName:=DefaultUserName, + password:=String.Empty) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + + + + + Public Sub DownloadFile_UnauthorizedUrlWithUserNamePassword_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + userName:=DefaultUserName, + password:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + Public Sub DownloadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -28,7 +165,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -36,7 +173,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsEmptyString_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -54,7 +191,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereAddressIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -72,7 +209,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -93,7 +230,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -110,7 +247,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -119,7 +256,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -144,7 +281,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -170,7 +307,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -186,7 +323,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -194,7 +331,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -210,36 +347,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) - End Using - End Sub - - - - - Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, - userName:=DefaultUserName, - password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - GetNetworkCredentials(DefaultUserName, password), - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) + .Be(webListener.FileSize) End Using End Sub @@ -247,7 +355,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -272,7 +380,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -297,7 +405,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -322,7 +430,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -345,7 +453,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -361,7 +469,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -370,7 +478,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -396,7 +504,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -414,7 +522,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -423,7 +531,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -448,7 +556,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -472,7 +580,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsExceptOnUserCancelWhereDestinationFileNameInvalidOverwriteThrows( destinationFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -498,7 +606,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -526,7 +634,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -552,7 +660,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -580,7 +688,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -609,7 +717,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -626,7 +734,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -636,7 +744,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -664,7 +772,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -694,7 +802,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -711,7 +819,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -719,7 +827,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -745,7 +853,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -762,7 +870,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -770,7 +878,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -797,7 +905,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -812,61 +920,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) - End Using - End Sub - - - - - Public Sub DownloadFile_UriWithUserNamePasswordWherePasswordWrong_Throw(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, - userName:=DefaultUserName, - password:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using - End Sub - - - - - Public Sub DownloadFile_UriWithUserNamePasswordWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, - userName:=DefaultUserName, - password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) + .Be(webListener.FileSize) End Using End Sub @@ -874,7 +928,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnly_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -885,7 +939,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -894,7 +948,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -912,7 +966,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlOnlyWhereDestinationFileNameInvalidAddressOnly_Throws(destinationFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -931,7 +985,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereDestinationFileNameInvalidOverwrite_Throws( destinationFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -956,7 +1010,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -982,7 +1036,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1007,8 +1061,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) - Dim webListener As New WebListener(ExtraLargeTestFileSize) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1GB) + Dim webListener As New WebListener(FileSizes.FileSize1GB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1030,7 +1084,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1046,7 +1100,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(LargeTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1054,7 +1108,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1070,7 +1124,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1078,7 +1132,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1103,7 +1157,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(LargeTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1128,7 +1182,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1154,7 +1208,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1170,7 +1224,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1179,7 +1233,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1197,7 +1251,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1205,7 +1259,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1251,7 +1305,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationFileNameInvalid_Throws(destinationFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1279,7 +1333,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1309,7 +1363,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1338,7 +1392,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1355,7 +1409,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1365,7 +1419,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1393,7 +1447,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1423,7 +1477,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Directory.Delete(testDirectory, recursive:=True) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1440,7 +1494,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1448,7 +1502,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1474,7 +1528,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1491,7 +1545,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(webListener.FileSize) End Using End Sub @@ -1499,7 +1553,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptionsWithAllOptionsWithAllOptionsWhereDestinationIsDirectory_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1526,7 +1580,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1541,61 +1595,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) - End Using - End Sub - - - - - Public Sub DownloadFile_UrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, - userName:=DefaultUserName, - password:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using - End Sub - - - - - Public Sub DownloadFile_UrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, - userName:=DefaultUserName, - password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) + .Be(webListener.FileSize) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index d7e5a696ce5..edcde1f6095 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -16,8 +16,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -34,8 +34,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWhereAddressIsEmptyString_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -52,8 +52,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWhereAddressIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -71,7 +71,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWhereSourceFileNameInvalidAddressOnly_Throws(sourceFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -90,7 +90,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWrongFileSize_Throw() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener(1) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -108,9 +108,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancel_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -134,9 +134,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -162,7 +162,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereSourceFileNameInvalid_Throws( sourceFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -185,8 +185,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize100MB) + Dim webListener As New WebListener(fileSize:=FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -208,8 +208,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize100MB) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -232,8 +232,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -254,8 +254,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -278,8 +278,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -300,8 +300,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -322,8 +322,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -344,9 +344,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentials_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -369,9 +369,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentials_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -394,9 +394,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsAndNetworkCredentialsTimeout0_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -419,8 +419,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -443,7 +443,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsExceptOnUserCancelWhereSourceFileNameInvalidThrows( sourceFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -467,8 +467,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereCheckFilePathTrailingSeparators_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -497,8 +497,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -523,8 +523,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -551,8 +551,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -576,8 +576,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereRootDirectoryInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -601,8 +601,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -629,7 +629,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereSourceFileNameInvalid_Throws(sourceFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -651,8 +651,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -676,8 +676,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -699,9 +699,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithUserNamePassword_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -724,9 +724,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithUserNamePasswordWherePasswordWrong_Throw(password As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -751,9 +751,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriWithUserNamePasswordWherePasswordWrong_Throws(password As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -776,8 +776,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlOnly_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -795,8 +795,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -814,7 +814,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlOnlyWhereSourceFileNameInvalidAddressOnly_Throws(sourceFileName As String) Dim testDirectory As String = CreateTempDirectory() - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -831,8 +831,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereInvalidUrlDoNotShowUI_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -858,7 +858,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereSourceFileNameInvalid_Throws( sourceFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -881,8 +881,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeOut_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize100MB) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -904,8 +904,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTimeoutNegative_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=LargeTestFileSize) - Dim webListener As New WebListener(LargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize100MB) + Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -928,8 +928,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Fail() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=ExtraLargeTestFileSize) - Dim webListener As New WebListener(ExtraLargeTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1GB) + Dim webListener As New WebListener(FileSizes.FileSize1GB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -949,8 +949,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -971,8 +971,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUrlInvalid_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -996,8 +996,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1019,7 +1019,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws() Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1040,9 +1040,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsAndNetworkCredentials_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1065,8 +1065,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsDoNotShowUI_ExceptOnUserCancelWhereInvalidUrl_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1112,8 +1112,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereDestinationIsRootDirectory_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1141,8 +1141,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereFilePathTrailingSeparatorsAreInvalid_Throw(separator As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1169,8 +1169,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1194,8 +1194,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereRootDirectoryTrailingSeparatorInvalid_Throw(root As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1222,7 +1222,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereSourceFileNameInvalid_Throws(sourceFileName As String) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1246,8 +1246,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWhereUriIsNothing_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1271,8 +1271,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptionsWithAllOptions_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) - Dim webListener As New WebListener(SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1294,9 +1294,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithUserNamePassword_Success() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() @@ -1319,9 +1319,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() @@ -1346,9 +1346,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=SmallTestFileSize) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( - fileSize:=SmallTestFileSize, + fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb index 1689e74e87c..0399b51d3a6 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/DownloadFileTestConstants.vb @@ -5,9 +5,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Module DownloadFileTestConstants Friend Const DefaultPassword As String = NameOf(DefaultPassword) Friend Const DefaultUserName As String = NameOf(DefaultUserName) - Friend Const ExtraLargeTestFileSize As Integer = 1_048_576_000 - Friend Const LargeTestFileSize As Integer = 104_857_600 - Friend Const SmallTestFileSize As Integer = 18_135 Friend Const InvalidUrlAddress As String = "invalidURL" Friend Const TestingConnectionLargeTimeout As Integer = 100_000_000 Friend Const TestingConnectionTimeout As Integer = 100_000 diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb new file mode 100644 index 00000000000..ea76f9006e4 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb @@ -0,0 +1,12 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Namespace Microsoft.VisualBasic.Forms.Tests + Friend Module EnumFileSizes + Friend Enum FileSizes As Integer + FileSize1MB = 1_048_576 + FileSize100MB = 104_857_600 + FileSize1GB = 1_048_576_000 + End Enum + End Module +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index f952126e35f..283f62b0d33 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -10,6 +10,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener Private Const BufferSize As Integer = 4096 + Private ReadOnly _address As String Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String @@ -24,7 +25,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Debug.Assert(fileSize > 0) _fileSize = fileSize _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" - Address = $"{_fileUrlPrefix}T{fileSize}" + _address = $"{_fileUrlPrefix}T{fileSize}" End Sub ''' @@ -46,6 +47,17 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub Public ReadOnly Property Address As String + Get + Return _address + End Get + End Property + + Public ReadOnly Property FileSize As Long + Get + Return _fileSize + End Get + End Property + ''' ''' This server will save a when something in the stream doesn't ''' match what is expected. These will never be visible to the user. @@ -101,89 +113,92 @@ Namespace Microsoft.VisualBasic.Forms.Tests ' Create a listener and add the prefixes. Dim listener As New HttpListener() - listener.Prefixes.Add(_fileUrlPrefix) - If _userName IsNot Nothing OrElse _password IsNot Nothing Then - listener.AuthenticationSchemes = AuthenticationSchemes.Basic - End If - listener.Start() - Dim action As Action = - Sub() - ' Start the listener to begin listening for requests. - Dim response As HttpListenerResponse = Nothing - Try - ' Note: GetContext blocks while waiting for a request. - Dim context As HttpListenerContext = listener.GetContext() - ' Create the response. - response = context.Response - - If context.User?.Identity.IsAuthenticated Then - Dim identity As HttpListenerBasicIdentity = - CType(context.User?.Identity, HttpListenerBasicIdentity) - - If String.IsNullOrWhiteSpace(identity.Name) _ - OrElse identity.Name <> _userName _ - OrElse String.IsNullOrWhiteSpace(identity.Password) _ - OrElse identity.Password <> _password Then - - response.StatusCode = HttpStatusCode.Unauthorized - Exit Try + If _fileUrlPrefix.Contains("8080") Then + + listener.Prefixes.Add(_fileUrlPrefix) + If _userName IsNot Nothing OrElse _password IsNot Nothing Then + listener.AuthenticationSchemes = AuthenticationSchemes.Basic + End If + listener.Start() + Dim action As Action = + Sub() + ' Start the listener to begin listening for requests. + Dim response As HttpListenerResponse = Nothing + Try + ' Note: GetContext blocks while waiting for a request. + Dim context As HttpListenerContext = listener.GetContext() + ' Create the response. + response = context.Response + + If context.User?.Identity.IsAuthenticated Then + Dim identity As HttpListenerBasicIdentity = + CType(context.User?.Identity, HttpListenerBasicIdentity) + + If String.IsNullOrWhiteSpace(identity.Name) _ + OrElse identity.Name <> _userName _ + OrElse String.IsNullOrWhiteSpace(identity.Password) _ + OrElse identity.Password <> _password Then + + response.StatusCode = HttpStatusCode.Unauthorized + Exit Try + End If End If - End If - ' Simulate network traffic - Thread.Sleep(millisecondsTimeout:=20) - If listener.Prefixes(0).Contains("UploadFile") Then - Dim request As HttpListenerRequest = context.Request - If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ - AndAlso request.HasEntityBody Then - - Dim formData As Dictionary(Of String, String) = GetMultipartFormData(request) - - Using bodyStream As Stream = request.InputStream - Using reader As New StreamReader( - stream:=bodyStream, - encoding:=request.ContentEncoding, - detectEncodingFromByteOrderMarks:=True, - BufferSize) + ' Simulate network traffic + Thread.Sleep(millisecondsTimeout:=20) + If listener.Prefixes(0).Contains("UploadFile") Then + Dim request As HttpListenerRequest = context.Request + If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ + AndAlso request.HasEntityBody Then + + Dim formData As Dictionary(Of String, String) = GetMultipartFormData(request) + + Using bodyStream As Stream = request.InputStream + Using reader As New StreamReader( + stream:=bodyStream, + encoding:=request.ContentEncoding, + detectEncodingFromByteOrderMarks:=True, + BufferSize) + End Using End Using - End Using - Try - Dim dataLength As String = formData(NameOf(dataLength)) - If _fileSize.ToString <> dataLength Then - ServerFaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" - End If + Try + Dim dataLength As String = formData(NameOf(dataLength)) + If _fileSize.ToString <> dataLength Then + ServerFaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" + End If - Dim fileName As String = formData("filename") - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - ServerFaultMessage = $"Filename incorrect, expected 'Testing.Txt', actual {fileName}" - End If - Catch ex As Exception - ' Ignore is case upload is cancelled - End Try + Dim fileName As String = formData("filename") + If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then + ServerFaultMessage = $"Filename incorrect, expected 'Testing.Txt', actual {fileName}" + End If + Catch ex As Exception + ' Ignore is case upload is cancelled + End Try + End If + response.StatusCode = 200 + Else + Dim responseString As String = Strings.StrDup(_fileSize, "A") + Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) + response.ContentLength64 = buffer.Length + Using output As Stream = response.OutputStream + Try + output.Write(buffer, offset:=0, count:=buffer.Length) + Catch ex As Exception + ' ignore it will be handled elsewhere + End Try + End Using End If - response.StatusCode = 200 - Else - Dim responseString As String = Strings.StrDup(_fileSize, "A") - Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) - response.ContentLength64 = buffer.Length - Using output As Stream = response.OutputStream - Try - output.Write(buffer, offset:=0, count:=buffer.Length) - Catch ex As Exception - ' ignore it will be handled elsewhere - End Try - End Using - End If - Finally - Try - response?.Close() - Catch ex As Exception + Finally + Try + response?.Close() + Catch ex As Exception + End Try + response = Nothing End Try - response = Nothing - End Try - End Sub + End Sub - Task.Run(action) + Task.Run(action) + End If Return listener End Function From 4fed93b6f13622db4cedb0d206f62ea4d78850a3 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 11 Jan 2025 20:19:26 -0800 Subject: [PATCH 079/119] Fix merge issues --- .../System/Windows/Forms/DownloadFileAsyncTests.vb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb index 15f80a1fbe8..088a8ce39f4 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileAsyncTests.vb @@ -17,7 +17,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptions_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -38,7 +38,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(FileSizes.FileSize1MB) End Using End Sub @@ -46,7 +46,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptionsClientHandlerNothing_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -72,7 +72,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_AllOptionsInvalidTimeout_Throws() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -98,7 +98,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFileAsync_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) - Dim webListener As New WebListener(SmallTestFileSize) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -114,7 +114,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() VerifySuccessfulDownload(testDirectory, destinationFileName, listener).Should() _ - .Be(SmallTestFileSize) + .Be(FileSizes.FileSize1MB) End Using End Sub From 253b185cc6c2d8c7d24a535a2f5b97a777a4d6bf Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 11 Jan 2025 23:03:05 -0800 Subject: [PATCH 080/119] Make it easier to test with real web server instead of WebListener --- .../System/Windows/Forms/UploadFileTests.vb | 4 ++-- .../System/Windows/TestUtilities/EnumFileSizes.vb | 1 + .../Windows/TestUtilities/VbFileCleanupTestBase.vb | 4 ++++ .../System/Windows/TestUtilities/WebListener.vb | 14 ++++++++------ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index edcde1f6095..515930e9df6 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -90,8 +90,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UriOnlyWrongFileSize_Throw() Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(1) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) + Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb index ea76f9006e4..bd5cbfdae28 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb @@ -4,6 +4,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Module EnumFileSizes Friend Enum FileSizes As Integer + FileSize1Byte = 1 FileSize1MB = 1_048_576 FileSize100MB = 104_857_600 FileSize1GB = 1_048_576_000 diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 18bc3f06182..b9ce9edc6d9 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -46,6 +46,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' If size = -1 no file is create but the full path is returned. ''' Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String + If filename = DefaultFileName Then + filename = $"{[Enum].GetName(GetType(FileSizes), size)}.zip" + + End If Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) If size >= 0 Then diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 283f62b0d33..9e0cabedc64 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -11,6 +11,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener Private Const BufferSize As Integer = 4096 Private ReadOnly _address As String + Private ReadOnly _fileName As String Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String @@ -23,9 +24,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) Debug.Assert(fileSize > 0) + _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip" _fileSize = fileSize _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" - _address = $"{_fileUrlPrefix}T{fileSize}" + _address = $"{_fileUrlPrefix}{_fileName}" End Sub ''' @@ -164,11 +166,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim dataLength As String = formData(NameOf(dataLength)) If _fileSize.ToString <> dataLength Then ServerFaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" - End If - - Dim fileName As String = formData("filename") - If Not fileName.Equals("Testing.Txt", StringComparison.OrdinalIgnoreCase) Then - ServerFaultMessage = $"Filename incorrect, expected 'Testing.Txt', actual {fileName}" + Else + Dim fileName As String = formData("filename") + If Not fileName.Equals(_fileName, StringComparison.OrdinalIgnoreCase) Then + ServerFaultMessage = $"Filename incorrect, expected '{_fileName}', actual {fileName}" + End If End If Catch ex As Exception ' Ignore is case upload is cancelled From 127c7efaa01bd96ae3b7292288d03057b352ebb9 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 12 Jan 2025 17:17:04 -0800 Subject: [PATCH 081/119] Make FileName for Transfer match size Allow skipping of tests that need server to support Unauthorized Access based on user reedentials. Make logic for file Upload detection not dependant on file name to support other Servers in testing. --- .../System/Windows/Forms/DownloadFileTests.vb | 148 ++++++++++-------- .../TestUtilities/VbFileCleanupTestBase.vb | 2 +- .../Windows/TestUtilities/WebListener.vb | 9 +- 3 files changed, 86 insertions(+), 73 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index fbbd040c096..a89e421e457 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -23,23 +23,25 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - GetNetworkCredentials(DefaultUserName, password), - showUI:=False, - connectionTimeout:=TestingConnectionTimeout, - overwrite:=True) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using + If webListener.PasswordErrorsIgnored Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + GetNetworkCredentials(DefaultUserName, password), + showUI:=False, + connectionTimeout:=TestingConnectionTimeout, + overwrite:=True) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End If End Sub @@ -52,21 +54,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using + If webListener.PasswordErrorsIgnored Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End If End Sub @@ -79,21 +83,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=New Uri(webListener.Address), - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using + If webListener.PasswordErrorsIgnored Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=New Uri(webListener.Address), + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End If End Sub @@ -106,21 +112,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.DownloadFile( - address:=webListener.Address, - destinationFileName, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using + If webListener.PasswordErrorsIgnored Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End If End Sub @@ -133,8 +141,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, userName:=DefaultUserName, password:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = + If webListener.PasswordErrorsIgnored Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = Sub() My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), @@ -143,11 +152,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests password) End Sub - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - VerifyFailedDownload(testDirectory, destinationFileName, listener) - End Using + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End If End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index b9ce9edc6d9..1cc3a58bf7e 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -47,7 +47,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String If filename = DefaultFileName Then - filename = $"{[Enum].GetName(GetType(FileSizes), size)}.zip" + filename = $"{[Enum].GetName(GetType(FileSizes), size).Replace("FileSize", "")}.zip" End If Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 9e0cabedc64..bb591143978 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -15,6 +15,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _password As String + Private ReadOnly _upload As Boolean Private ReadOnly _userName As String ''' @@ -24,9 +25,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) Debug.Assert(fileSize > 0) - _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip" + _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize - _fileUrlPrefix = $"http://127.0.0.1:8080/{memberName}/" + _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) + _fileUrlPrefix = $"http://127.0.0.1:8080/" _address = $"{_fileUrlPrefix}{_fileName}" End Sub @@ -66,6 +68,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' ''' A with a description of the issue or Public Property ServerFaultMessage As String + Public Property PasswordErrorsIgnored As Boolean Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -147,7 +150,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If ' Simulate network traffic Thread.Sleep(millisecondsTimeout:=20) - If listener.Prefixes(0).Contains("UploadFile") Then + If _upload Then Dim request As HttpListenerRequest = context.Request If request.HttpMethod.Equals("Post", StringComparison.OrdinalIgnoreCase) _ AndAlso request.HasEntityBody Then From fde921e5e0db2bfddadb2f4902fe710116b7acf9 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Mon, 13 Jan 2025 00:25:27 -0800 Subject: [PATCH 082/119] Fix logic for ServerThrowsPasswordErrors Add support for real File Server Testing --- .../System/Windows/Forms/DownloadFileTests.vb | 58 +++---- .../System/Windows/Forms/UploadFileTests.vb | 152 +++++++++--------- .../TestUtilities/ServerConfiguration.vb | 87 ++++++++++ .../Windows/TestUtilities/WebListener.vb | 63 +++++--- 4 files changed, 234 insertions(+), 126 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index a89e421e457..1ea7453fe34 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -21,9 +21,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) - If webListener.PasswordErrorsIgnored Then + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + If webListener.ServerThrowsPasswordErrors Then Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -52,9 +52,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=String.Empty) - If webListener.PasswordErrorsIgnored Then + serverUserName:=DefaultUserName, + serverPassword:=String.Empty) + If webListener.ServerThrowsPasswordErrors Then Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -81,9 +81,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) - If webListener.PasswordErrorsIgnored Then + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + If webListener.ServerThrowsPasswordErrors Then Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -110,9 +110,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=String.Empty) - If webListener.PasswordErrorsIgnored Then + serverUserName:=DefaultUserName, + serverPassword:=String.Empty) + If webListener.ServerThrowsPasswordErrors Then Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -139,9 +139,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) - If webListener.PasswordErrorsIgnored Then + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + If webListener.ServerThrowsPasswordErrors Then Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -241,8 +241,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -489,8 +489,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -515,8 +515,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -542,8 +542,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -916,8 +916,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1244,8 +1244,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1591,8 +1591,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 515930e9df6..48099093bab 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -27,7 +27,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -45,7 +45,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -63,7 +63,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -83,7 +83,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -101,7 +101,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.StartWith("File size mismatch") + webListener.FaultMessage.Should.StartWith("File size mismatch") End Using End Sub @@ -111,8 +111,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -125,7 +125,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -137,8 +137,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -153,7 +153,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -178,7 +178,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -225,7 +225,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -247,7 +247,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -271,7 +271,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -293,7 +293,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of UriFormatException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -315,7 +315,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -337,7 +337,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -347,8 +347,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -362,7 +362,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -372,8 +372,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -387,7 +387,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -397,8 +397,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -412,7 +412,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -434,7 +434,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of UriFormatException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -459,7 +459,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -488,7 +488,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -544,7 +544,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -567,7 +567,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -592,7 +592,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should().Throw(Of FileNotFoundException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -622,7 +622,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -644,7 +644,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.Throw(Of ArgumentNullException)() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -669,7 +669,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -692,7 +692,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -702,8 +702,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -715,7 +715,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -727,8 +727,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=String.Empty) + serverUserName:=DefaultUserName, + serverPassword:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -742,7 +742,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -754,8 +754,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -769,7 +769,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -787,7 +787,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -806,7 +806,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -824,7 +824,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -849,7 +849,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -874,7 +874,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -921,7 +921,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(SR.Network_BadConnectionTimeout)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -964,7 +964,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -989,7 +989,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1011,7 +1011,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1033,7 +1033,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.StartWith("File size mismatch") + webListener.FaultMessage.Should.StartWith("File size mismatch") End Using End Sub @@ -1043,8 +1043,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1058,7 +1058,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1083,7 +1083,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(Function(e) e.Message.StartsWith(value)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1133,7 +1133,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1162,7 +1162,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1185,7 +1185,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1215,7 +1215,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentException)() _ .Where(exceptionExpression) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1239,7 +1239,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1264,7 +1264,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of ArgumentNullException)() _ .Where(Function(e) e.Message.StartsWith(SR.General_ArgumentNullException)) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1287,7 +1287,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1297,8 +1297,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1310,7 +1310,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub testCode.Should.NotThrow() - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1322,8 +1322,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=String.Empty) + serverUserName:=DefaultUserName, + serverPassword:=String.Empty) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1337,7 +1337,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub @@ -1349,8 +1349,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) Dim webListener As New WebListener( fileSize:=FileSizes.FileSize1MB, - userName:=DefaultUserName, - password:=DefaultPassword) + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1364,7 +1364,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests testCode.Should() _ .Throw(Of WebException)() _ .WithMessage(SR.net_webstatus_Unauthorized) - webListener.ServerFaultMessage.Should.BeNull() + webListener.FaultMessage.Should.BeNull() End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb new file mode 100644 index 00000000000..00a7b22bed8 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -0,0 +1,87 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.Text.Json +Imports System.IO +Imports System.Text.Json.Serialization + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class ServerConfiguration + + Private Shared ReadOnly s_jsonFilePath As String = + Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "ServerConfiguration.JSON") + + Private ReadOnly _options As New JsonSerializerOptions() With { + .WriteIndented = True + } + + Public Sub New() + + End Sub + + Public Sub New( + fileDownloadUrlPrefix As String, + fileUploadUrlPrefix As String, + passwordErrorsIgnored As Boolean, + serverDownloadPassword As String, + serverDownloadUserName As String, + serverUploadPassword As String, + serverUploadUserName As String) + + Me.FileDownloadUrlPrefix = fileDownloadUrlPrefix + Me.FileUploadUrlPrefix = fileUploadUrlPrefix + Me.PasswordErrorsIgnored = passwordErrorsIgnored + Me.ServerDownloadPassword = serverDownloadPassword + Me.ServerDownloadUserName = serverDownloadUserName + Me.ServerUploadPassword = serverUploadPassword + Me.ServerUploadUserName = serverUploadUserName + End Sub + + Public Property FileDownloadUrlPrefix As String = "http://127.0.0.1:8080/" + Public Property FileUploadUrlPrefix As String = "http://127.0.0.1:8080/" + Public Property PasswordErrorsIgnored As Boolean + Public Property ServerDownloadPassword As String = "DefaultPassword" + Public Property ServerDownloadUserName As String = "DefaultUserName" + Public Property ServerUploadPassword As String = "DefaultPassword" + Public Property ServerUploadUserName As String = "DefaultUserName" + + Public Shared Function Load() As ServerConfiguration + If File.Exists(s_jsonFilePath) Then + Dim jsonString As String = File.ReadAllText(s_jsonFilePath) + Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString) + End If + Return New ServerConfiguration + End Function + + Public Function GetDefaultPassword(uploading As Boolean) As String + If uploading Then + Return ServerUploadPassword + Else + Return ServerDownloadPassword + End If + End Function + + Public Function GetDefaultUserName(uploading As Boolean) As String + If uploading Then + Return ServerUploadUserName + Else + Return ServerDownloadUserName + End If + End Function + + Public Function GetFileUrlPrefix(uploading As Boolean) As String + If uploading Then + Return FileUploadUrlPrefix + Else + Return FileDownloadUrlPrefix + End If + End Function + + Public Sub Save() + Dim jsonString As String = JsonSerializer.Serialize(Me, _options) + File.WriteAllText(s_jsonFilePath, jsonString) + End Sub + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index bb591143978..afedd94da0d 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -14,9 +14,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private ReadOnly _fileName As String Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String - Private ReadOnly _password As String + Private ReadOnly _serverConfigurationInstance As ServerConfiguration Private ReadOnly _upload As Boolean - Private ReadOnly _userName As String ''' ''' The name of the function that creates the server is used to establish the file to be downloaded. @@ -27,27 +26,35 @@ Namespace Microsoft.VisualBasic.Forms.Tests Debug.Assert(fileSize > 0) _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize + _serverConfigurationInstance = ServerConfiguration.Load() + _fileUrlPrefix = Get_serverConfigurationInstance().GetFileUrlPrefix(_upload) _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) - _fileUrlPrefix = $"http://127.0.0.1:8080/" - _address = $"{_fileUrlPrefix}{_fileName}" + _address = $"{Get_serverConfigurationInstance().GetFileUrlPrefix(_upload)}{_fileName}" End Sub ''' ''' Used for authenticated download. ''' ''' Passed to Me.New. - ''' Name to match for authorization. - ''' Password to match for authorization. + ''' Name to match for authorization. + ''' Password to match for authorization. ''' Passed to Me.New. Public Sub New( fileSize As Integer, - userName As String, - password As String, + serverUserName As String, + serverPassword As String, Optional memberName As String = Nothing) Me.New(fileSize, memberName) - _userName = userName - _password = password + Dim localServer As Boolean = _fileUrlPrefix.Contains("8080") + + If localServer Then + _ServerPassword = serverPassword + _ServerUerName = serverUserName + Else + _ServerPassword = Get_serverConfigurationInstance().GetDefaultPassword(_upload) + _ServerUerName = Get_serverConfigurationInstance().GetDefaultUserName(_upload) + End If End Sub Public ReadOnly Property Address As String @@ -56,19 +63,29 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Get End Property + ''' + ''' This server will save a when something in the stream doesn't + ''' match what is expected. These will never be visible to the user. + ''' + ''' A with a description of the issue or + Public Property FaultMessage As String + Public ReadOnly Property FileSize As Long Get Return _fileSize End Get End Property + Public Property ServerPassword As String + ''' - ''' This server will save a when something in the stream doesn't - ''' match what is expected. These will never be visible to the user. + ''' Some File servers do not return errors on mismatched passwords so we need to + ''' ignore tests that expect errors. ''' - ''' A with a description of the issue or - Public Property ServerFaultMessage As String - Public Property PasswordErrorsIgnored As Boolean + ''' + Public Property ServerThrowsPasswordErrors As Boolean = True + + Public Property ServerUerName As String Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -121,7 +138,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests If _fileUrlPrefix.Contains("8080") Then listener.Prefixes.Add(_fileUrlPrefix) - If _userName IsNot Nothing OrElse _password IsNot Nothing Then + If _ServerUerName IsNot Nothing OrElse _ServerPassword IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If listener.Start() @@ -140,9 +157,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests CType(context.User?.Identity, HttpListenerBasicIdentity) If String.IsNullOrWhiteSpace(identity.Name) _ - OrElse identity.Name <> _userName _ + OrElse identity.Name <> _ServerUerName _ OrElse String.IsNullOrWhiteSpace(identity.Password) _ - OrElse identity.Password <> _password Then + OrElse identity.Password <> _ServerPassword Then response.StatusCode = HttpStatusCode.Unauthorized Exit Try @@ -168,11 +185,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Try Dim dataLength As String = formData(NameOf(dataLength)) If _fileSize.ToString <> dataLength Then - ServerFaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" + FaultMessage = $"File size mismatch, expected {_fileSize} actual {dataLength}" Else Dim fileName As String = formData("filename") If Not fileName.Equals(_fileName, StringComparison.OrdinalIgnoreCase) Then - ServerFaultMessage = $"Filename incorrect, expected '{_fileName}', actual {fileName}" + FaultMessage = $"Filename incorrect, expected '{_fileName}', actual {fileName}" End If End If Catch ex As Exception @@ -207,6 +224,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return listener End Function + Public Function Get_serverConfigurationInstance() As ServerConfiguration + Return _serverConfigurationInstance + End Function + ''' ''' Parses a and gets the fileName of the uploaded file ''' and the lenght of the data file in bytes @@ -244,7 +265,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If Next Catch ex As Exception - ServerFaultMessage = "MultipartFormData format Error" + FaultMessage = "MultipartFormData format Error" End Try End Using End If From 3ed66c3925a71ce912f5f478937d1c32c43053fc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Mon, 13 Jan 2025 15:50:46 -0800 Subject: [PATCH 083/119] Add ServerConfiguratio,json file to allow manual testing using private servers --- .../Microsoft.VisualBasic.Forms.Tests.vbproj | 6 + .../System/Windows/Forms/DownloadFileTests.vb | 12 +- .../System/Windows/Forms/UploadFileTests.vb | 228 +++++++++--------- .../TestUtilities/ServerConfiguration.vb | 21 +- .../TestData/ServerConfiguration.json | 9 + .../Windows/TestUtilities/WebListener.vb | 24 +- 6 files changed, 156 insertions(+), 144 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj index 48eca2f60d4..04b45a8ab61 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj @@ -16,6 +16,12 @@ + + + PreserveNewest + + + diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 1ea7453fe34..79c4680f276 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -30,7 +30,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - GetNetworkCredentials(DefaultUserName, password), + GetNetworkCredentials(webListener.ServerUserName, password), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -249,7 +249,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True) @@ -497,7 +497,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=Nothing, destinationFileName, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -523,7 +523,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, @@ -550,7 +550,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=0, overwrite:=True, @@ -1252,7 +1252,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, overwrite:=True, diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 48099093bab..aabdd7dce61 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -13,6 +13,115 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class UploadFileTests Inherits VbFileCleanupTestBase + + + + Public Sub UploadFile_UnauthorizedUriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + New NetworkCredential(webListener.ServerUserName, password), + showUI:=False, + connectionTimeout:=TestingConnectionTimeout) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End Sub + + + + + Public Sub UploadFile_UnauthorizedUriWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End Sub + + + + + Public Sub UploadFile_UnauthorizedUrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + serverUserName:=DefaultUserName, + serverPassword:=String.Empty) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End Sub + + + + + Public Sub UploadFile_UnauthorizedUrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) + Dim testDirectory As String = CreateTempDirectory() + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) + Dim webListener As New WebListener( + fileSize:=FileSizes.FileSize1MB, + serverUserName:=DefaultUserName, + serverPassword:=DefaultPassword) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=DefaultUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End Sub + Public Sub UploadFile_UriOnly_Success() Dim testDirectory As String = CreateTempDirectory() @@ -119,7 +228,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout) End Sub @@ -129,34 +238,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - - - Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener( - fileSize:=FileSizes.FileSize1MB, - serverUserName:=DefaultUserName, - serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(webListener.Address), - New NetworkCredential(DefaultUserName, password), - showUI:=False, - connectionTimeout:=TestingConnectionTimeout) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using - End Sub - Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereSourceFileNameInvalid_Throws( @@ -355,7 +436,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=Nothing, - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -380,7 +461,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -405,7 +486,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=0, onUserCancel:=UICancelOption.ThrowException) @@ -746,33 +827,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - - - Public Sub UploadFile_UriWithUserNamePasswordWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener( - fileSize:=FileSizes.FileSize1MB, - serverUserName:=DefaultUserName, - serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(webListener.Address), - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using - End Sub - Public Sub UploadFile_UrlOnly_Success() Dim testDirectory As String = CreateTempDirectory() @@ -1051,7 +1105,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - GetNetworkCredentials(DefaultUserName, DefaultPassword), + GetNetworkCredentials(webListener.ServerUserName, webListener.ServerPassword), showUI:=False, connectionTimeout:=TestingConnectionTimeout, onUserCancel:=UICancelOption.ThrowException) @@ -1314,59 +1368,5 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - - - Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throw(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener( - fileSize:=FileSizes.FileSize1MB, - serverUserName:=DefaultUserName, - serverPassword:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=webListener.Address, - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using - End Sub - - - - - Public Sub UploadFile_UrlWithUserNamePasswordWherePasswordWrong_Throws(password As String) - Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener( - fileSize:=FileSizes.FileSize1MB, - serverUserName:=DefaultUserName, - serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(webListener.Address), - userName:=DefaultUserName, - password) - End Sub - - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using - End Sub - End Class End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index 00a7b22bed8..ec09c3e915a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -1,20 +1,13 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Text.Json Imports System.IO -Imports System.Text.Json.Serialization +Imports System.Text.Json Namespace Microsoft.VisualBasic.Forms.Tests Public Class ServerConfiguration - - Private Shared ReadOnly s_jsonFilePath As String = - Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "ServerConfiguration.JSON") - - Private ReadOnly _options As New JsonSerializerOptions() With { - .WriteIndented = True - } + Private ReadOnly _options As New JsonSerializerOptions() With {.WriteIndented = True} Public Sub New() @@ -46,9 +39,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Property ServerUploadPassword As String = "DefaultPassword" Public Property ServerUploadUserName As String = "DefaultUserName" - Public Shared Function Load() As ServerConfiguration - If File.Exists(s_jsonFilePath) Then - Dim jsonString As String = File.ReadAllText(s_jsonFilePath) + Public Shared Function ServerConfigurationLoad(jsonFilePath As String) As ServerConfiguration + If File.Exists(jsonFilePath) Then + Dim jsonString As String = File.ReadAllText(jsonFilePath) Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString) End If Return New ServerConfiguration @@ -78,9 +71,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function - Public Sub Save() + Public Sub ServerConfigurationSave(jsonFilePath As String) Dim jsonString As String = JsonSerializer.Serialize(Me, _options) - File.WriteAllText(s_jsonFilePath, jsonString) + File.WriteAllText(jsonFilePath, jsonString) End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json new file mode 100644 index 00000000000..7af6099981d --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -0,0 +1,9 @@ +{ + "FileDownloadUrlPrefix": "http://127.0.0.1:8080/", + "FileUploadUrlPrefix": "http://127.0.0.1:8080/", + "PasswordErrorsIgnored": false, + "ServerDownloadPassword": "DefaultPassword", + "ServerDownloadUserName": "DefaultUserName", + "ServerUploadPassword": "DefaultPassword", + "ServerUploadUserName": "DefaultUserName" +} \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index afedd94da0d..ef12d2f39d2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -9,6 +9,7 @@ Imports System.Threading Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener + Inherits ServerConfiguration Private Const BufferSize As Integer = 4096 Private ReadOnly _address As String Private ReadOnly _fileName As String @@ -16,6 +17,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private ReadOnly _fileUrlPrefix As String Private ReadOnly _serverConfigurationInstance As ServerConfiguration Private ReadOnly _upload As Boolean + Private Shared ReadOnly s_jsonFilePath As String = + Path.Combine(My.Application.Info.DirectoryPath, "System\Windows\TestUtilities\TestData", "ServerConfiguration.JSON") + ''' ''' The name of the function that creates the server is used to establish the file to be downloaded. @@ -26,10 +30,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests Debug.Assert(fileSize > 0) _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize - _serverConfigurationInstance = ServerConfiguration.Load() - _fileUrlPrefix = Get_serverConfigurationInstance().GetFileUrlPrefix(_upload) + _serverConfigurationInstance = ServerConfigurationLoad(s_jsonFilePath) + _fileUrlPrefix = GetServerConfigurationInstance().GetFileUrlPrefix(_upload) _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) - _address = $"{Get_serverConfigurationInstance().GetFileUrlPrefix(_upload)}{_fileName}" + _address = $"{GetServerConfigurationInstance().GetFileUrlPrefix(_upload)}{_fileName}" End Sub ''' @@ -50,10 +54,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests If localServer Then _ServerPassword = serverPassword - _ServerUerName = serverUserName + _ServerUserName = serverUserName Else - _ServerPassword = Get_serverConfigurationInstance().GetDefaultPassword(_upload) - _ServerUerName = Get_serverConfigurationInstance().GetDefaultUserName(_upload) + _ServerPassword = GetServerConfigurationInstance().GetDefaultPassword(_upload) + _ServerUserName = GetServerConfigurationInstance().GetDefaultUserName(_upload) End If End Sub @@ -85,7 +89,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Public Property ServerThrowsPasswordErrors As Boolean = True - Public Property ServerUerName As String + Public Property ServerUserName As String Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -138,7 +142,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests If _fileUrlPrefix.Contains("8080") Then listener.Prefixes.Add(_fileUrlPrefix) - If _ServerUerName IsNot Nothing OrElse _ServerPassword IsNot Nothing Then + If _ServerUserName IsNot Nothing OrElse _ServerPassword IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If listener.Start() @@ -157,7 +161,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests CType(context.User?.Identity, HttpListenerBasicIdentity) If String.IsNullOrWhiteSpace(identity.Name) _ - OrElse identity.Name <> _ServerUerName _ + OrElse identity.Name <> _ServerUserName _ OrElse String.IsNullOrWhiteSpace(identity.Password) _ OrElse identity.Password <> _ServerPassword Then @@ -224,7 +228,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return listener End Function - Public Function Get_serverConfigurationInstance() As ServerConfiguration + Public Function GetServerConfigurationInstance() As ServerConfiguration Return _serverConfigurationInstance End Function From c94a16f78f187c88f347b6357a54207d1f3029d0 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Mon, 13 Jan 2025 21:40:15 -0800 Subject: [PATCH 084/119] Fix issues found in testing on File servers --- .../MyServices/Internal/WebClientCopy.vb | 6 +- .../System/Windows/Forms/DownloadFileTests.vb | 16 +- .../System/Windows/Forms/UploadFileTests.vb | 249 ++++++++++++------ .../TestUtilities/ServerConfiguration.vb | 49 +++- .../TestData/ServerConfiguration.json | 13 +- .../Windows/TestUtilities/WebListener.vb | 55 ++-- 6 files changed, 263 insertions(+), 125 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index cc2cb53dbe7..d93fe488cfb 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -206,7 +206,7 @@ Namespace Microsoft.VisualBasic.MyServices.Internal _webClient.UploadFile(address, sourceFileName) End If Catch ex As WebException - If ex.Message.Contains("401") Then + If ex.Message.Contains("401") OrElse ex.Message.Contains("530") Then Throw New WebException(SR.net_webstatus_Unauthorized) End If If ex.Message.Contains("500") Then @@ -219,8 +219,12 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ' encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then + If _exceptionEncounteredDuringFileTransfer.Message.Contains("401") OrElse _exceptionEncounteredDuringFileTransfer.Message.Contains("530") Then + Throw New WebException(SR.net_webstatus_Unauthorized) + End If Throw _exceptionEncounteredDuringFileTransfer End If + Throw _exceptionEncounteredDuringFileTransfer End If End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 79c4680f276..8f9a081097c 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -61,7 +61,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=DefaultUserName, + userName:=webListener.ServerDownloadUserName, password) End Sub @@ -90,7 +90,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=DefaultUserName, + userName:=webListener.ServerDownloadUserName, password) End Sub @@ -119,7 +119,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=webListener.Address, destinationFileName, - userName:=DefaultUserName, + userName:=webListener.ServerDownloadUserName, password) End Sub @@ -148,7 +148,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=DefaultUserName, + userName:=webListener.ServerDownloadUserName, password) End Sub @@ -924,8 +924,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) + userName:=webListener.ServerDownloadUserName, + password:=webListener.ServerDownloadPassword) End Sub testCode.Should.NotThrow() @@ -1599,8 +1599,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=webListener.Address, destinationFileName, - userName:=DefaultUserName, - password:=DefaultPassword) + userName:=webListener.ServerDownloadUserName, + password:=webListener.ServerDownloadPassword) End Sub testCode.Should.NotThrow() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index aabdd7dce61..08fccd5019f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -23,8 +23,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, serverUserName:=DefaultUserName, serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = + If webListener.ServerThrowsPasswordErrors Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = Sub() My.Computer.Network.UploadFile( sourceFileName, @@ -34,11 +35,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End If End Sub @@ -51,21 +53,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, serverUserName:=DefaultUserName, serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(webListener.Address), - userName:=DefaultUserName, - password) - End Sub + If webListener.ServerThrowsPasswordErrors Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=webListener.ServerUserName, + password) + End Sub - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End If End Sub @@ -78,21 +82,24 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, serverUserName:=DefaultUserName, serverPassword:=String.Empty) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=webListener.Address, - userName:=DefaultUserName, - password) - End Sub + If webListener.ServerThrowsPasswordErrors Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=webListener.Address, + userName:=webListener.ServerUserName, + password) + End Sub + + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End If - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using End Sub @@ -105,21 +112,23 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize:=FileSizes.FileSize1MB, serverUserName:=DefaultUserName, serverPassword:=DefaultPassword) - Using listener As HttpListener = webListener.ProcessRequests() - Dim testCode As Action = - Sub() - My.Computer.Network.UploadFile( - sourceFileName, - address:=New Uri(webListener.Address), - userName:=DefaultUserName, - password) - End Sub + If webListener.ServerThrowsPasswordErrors Then + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.UploadFile( + sourceFileName, + address:=New Uri(webListener.Address), + userName:=webListener.ServerUserName, + password) + End Sub - testCode.Should() _ - .Throw(Of WebException)() _ - .WithMessage(SR.net_webstatus_Unauthorized) - webListener.FaultMessage.Should.BeNull() - End Using + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + webListener.FaultMessage.Should.BeNull() + End Using + End If End Sub @@ -135,8 +144,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=New Uri(webListener.Address)) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -208,9 +223,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests sourceFileName, address:=New Uri(webListener.Address)) End Sub + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.StartWith("File size mismatch") + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If - testCode.Should.NotThrow() - webListener.FaultMessage.Should.StartWith("File size mismatch") End Using End Sub @@ -327,8 +348,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionLargeTimeout) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -395,8 +422,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -417,8 +450,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -647,8 +686,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests onUserCancel:=UICancelOption.DoNothing) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -772,8 +817,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests onUserCancel:=UICancelOption.DoNothing) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -791,10 +842,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - userName:=DefaultUserName, - password:=DefaultPassword) + userName:=webListener.ServerUserName, + password:=webListener.ServerPassword) End Sub - testCode.Should.NotThrow() webListener.FaultMessage.Should.BeNull() End Using @@ -816,7 +866,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=New Uri(webListener.Address), - userName:=DefaultUserName, + userName:=webListener.ServerUserName, password) End Sub @@ -840,8 +890,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests address:=webListener.Address) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1016,9 +1072,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests showUI:=True, connectionTimeout:=TestingConnectionTimeout) End Sub - - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1064,8 +1125,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1086,8 +1153,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.StartWith("File size mismatch") + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.StartWith("File size mismatch") + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1238,8 +1311,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests onUserCancel:=UICancelOption.DoNothing) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1340,8 +1419,14 @@ Namespace Microsoft.VisualBasic.Forms.Tests onUserCancel:=UICancelOption.DoNothing) End Sub - testCode.Should.NotThrow() - webListener.FaultMessage.Should.BeNull() + If webListener.ServerAcceptsAnonymousLogin Then + testCode.Should.NotThrow() + webListener.FaultMessage.Should.BeNull() + Else + testCode.Should() _ + .Throw(Of WebException)() _ + .WithMessage(SR.net_webstatus_Unauthorized) + End If End Using End Sub @@ -1359,8 +1444,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.UploadFile( sourceFileName, address:=webListener.Address, - userName:=DefaultUserName, - password:=DefaultPassword) + userName:=webListener.ServerUserName, + password:=webListener.ServerPassword) End Sub testCode.Should.NotThrow() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index ec09c3e915a..9d342b8f35a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -3,6 +3,7 @@ Imports System.IO Imports System.Text.Json +Imports System.Text.Json.Serialization Namespace Microsoft.VisualBasic.Forms.Tests @@ -13,41 +14,51 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub + Public Sub New( fileDownloadUrlPrefix As String, fileUploadUrlPrefix As String, - passwordErrorsIgnored As Boolean, + serverDownloadAllowsAnonymousUser As Boolean, + serverDownloadAllowsPasswordErrors As Boolean, serverDownloadPassword As String, serverDownloadUserName As String, + serverUploadAllowsAnonymousUser As Boolean, + serverUploadAllowsPasswordErrors As Boolean, serverUploadPassword As String, serverUploadUserName As String) Me.FileDownloadUrlPrefix = fileDownloadUrlPrefix Me.FileUploadUrlPrefix = fileUploadUrlPrefix - Me.PasswordErrorsIgnored = passwordErrorsIgnored + Me.ServerDownloadAllowsAnonymousUser = serverDownloadAllowsAnonymousUser + Me.ServerDownloadAllowsPasswordErrors = serverDownloadAllowsPasswordErrors Me.ServerDownloadPassword = serverDownloadPassword Me.ServerDownloadUserName = serverDownloadUserName + Me.ServerUploadAllowsAnonymousUser = serverUploadAllowsAnonymousUser + Me.ServerUploadAllowsPasswordErrors = serverUploadAllowsPasswordErrors Me.ServerUploadPassword = serverUploadPassword Me.ServerUploadUserName = serverUploadUserName End Sub Public Property FileDownloadUrlPrefix As String = "http://127.0.0.1:8080/" Public Property FileUploadUrlPrefix As String = "http://127.0.0.1:8080/" - Public Property PasswordErrorsIgnored As Boolean + Public Property ServerDownloadAllowsAnonymousUser As Boolean = True + Public Property ServerDownloadAllowsPasswordErrors As Boolean Public Property ServerDownloadPassword As String = "DefaultPassword" Public Property ServerDownloadUserName As String = "DefaultUserName" + Public Property ServerUploadAllowsAnonymousUser As Boolean = True + Public Property ServerUploadAllowsPasswordErrors As Boolean Public Property ServerUploadPassword As String = "DefaultPassword" Public Property ServerUploadUserName As String = "DefaultUserName" - Public Shared Function ServerConfigurationLoad(jsonFilePath As String) As ServerConfiguration - If File.Exists(jsonFilePath) Then - Dim jsonString As String = File.ReadAllText(jsonFilePath) - Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString) + Friend Function GetAcceptsAnonymousLogin(uploading As Boolean) As Boolean + If uploading Then + Return ServerUploadAllowsAnonymousUser + Else + Return ServerDownloadAllowsAnonymousUser End If - Return New ServerConfiguration End Function - Public Function GetDefaultPassword(uploading As Boolean) As String + Friend Function GetDefaultPassword(uploading As Boolean) As String If uploading Then Return ServerUploadPassword Else @@ -55,7 +66,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function - Public Function GetDefaultUserName(uploading As Boolean) As String + Friend Function GetDefaultUserName(uploading As Boolean) As String If uploading Then Return ServerUploadUserName Else @@ -63,7 +74,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function - Public Function GetFileUrlPrefix(uploading As Boolean) As String + Friend Function GetFileUrlPrefix(uploading As Boolean) As String If uploading Then Return FileUploadUrlPrefix Else @@ -71,6 +82,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function + Friend Function GetThrowsPasswordErrors(uploading As Boolean) As Boolean + If uploading Then + Return Not ServerUploadAllowsPasswordErrors + Else + Return Not ServerDownloadAllowsPasswordErrors + End If + End Function + + Public Shared Function ServerConfigurationLoad(jsonFilePath As String) As ServerConfiguration + If File.Exists(jsonFilePath) Then + Dim jsonString As String = File.ReadAllText(jsonFilePath) + Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString) + End If + Return New ServerConfiguration + End Function + Public Sub ServerConfigurationSave(jsonFilePath As String) Dim jsonString As String = JsonSerializer.Serialize(Me, _options) File.WriteAllText(jsonFilePath, jsonString) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index 7af6099981d..cd3934e08a6 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -1,9 +1,12 @@ { "FileDownloadUrlPrefix": "http://127.0.0.1:8080/", "FileUploadUrlPrefix": "http://127.0.0.1:8080/", - "PasswordErrorsIgnored": false, - "ServerDownloadPassword": "DefaultPassword", - "ServerDownloadUserName": "DefaultUserName", - "ServerUploadPassword": "DefaultPassword", - "ServerUploadUserName": "DefaultUserName" + "ServerDownloadAllowsAnonymousUser": true, + "ServerDownloadAllowsPasswordErrors": true, + "ServerDownloadPassword": "", + "ServerDownloadUserName": "", + "ServerUploadAllowsAnonymousUser": true, + "serverUploadAllowsPasswordErrors": true, + "ServerUploadPassword": "", + "ServerUploadUserName": "" } \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index ef12d2f39d2..fabdcf1bbf8 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -11,15 +11,18 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener Inherits ServerConfiguration Private Const BufferSize As Integer = 4096 + + Private Shared ReadOnly s_jsonFilePath As String = + Path.Combine(My.Application.Info.DirectoryPath, "System\Windows\TestUtilities\TestData", "ServerConfiguration.JSON") + Private ReadOnly _address As String Private ReadOnly _fileName As String Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String Private ReadOnly _serverConfigurationInstance As ServerConfiguration + Private ReadOnly _serverPassword As String + Private ReadOnly _serverUserName As String Private ReadOnly _upload As Boolean - Private Shared ReadOnly s_jsonFilePath As String = - Path.Combine(My.Application.Info.DirectoryPath, "System\Windows\TestUtilities\TestData", "ServerConfiguration.JSON") - ''' ''' The name of the function that creates the server is used to establish the file to be downloaded. @@ -31,9 +34,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize _serverConfigurationInstance = ServerConfigurationLoad(s_jsonFilePath) - _fileUrlPrefix = GetServerConfigurationInstance().GetFileUrlPrefix(_upload) + _fileUrlPrefix = _serverConfigurationInstance.GetFileUrlPrefix(_upload) _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) - _address = $"{GetServerConfigurationInstance().GetFileUrlPrefix(_upload)}{_fileName}" + ServerAcceptsAnonymousLogin = _serverConfigurationInstance.GetAcceptsAnonymousLogin(_upload) + ServerThrowsPasswordErrors = _serverConfigurationInstance.GetThrowsPasswordErrors(_upload) + _address = $"{_serverConfigurationInstance.GetFileUrlPrefix(_upload)}{_fileName}" End Sub ''' @@ -53,11 +58,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim localServer As Boolean = _fileUrlPrefix.Contains("8080") If localServer Then - _ServerPassword = serverPassword - _ServerUserName = serverUserName + _serverPassword = serverPassword + _serverUserName = serverUserName Else - _ServerPassword = GetServerConfigurationInstance().GetDefaultPassword(_upload) - _ServerUserName = GetServerConfigurationInstance().GetDefaultUserName(_upload) + If serverPassword = DefaultPassword Then + _serverPassword = _serverConfigurationInstance.GetDefaultPassword(_upload) + Else + _serverPassword = serverPassword + End If + If serverUserName = DefaultUserName Then + _serverUserName = _serverConfigurationInstance.GetDefaultUserName(_upload) + Else + _serverUserName = serverUserName + End If End If End Sub @@ -80,7 +93,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Get End Property - Public Property ServerPassword As String + Public Property ServerAcceptsAnonymousLogin As Boolean = True + + Public ReadOnly Property ServerPassword As String + Get + Return _serverPassword + End Get + End Property ''' ''' Some File servers do not return errors on mismatched passwords so we need to @@ -89,7 +108,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Public Property ServerThrowsPasswordErrors As Boolean = True - Public Property ServerUserName As String + Public ReadOnly Property ServerUserName As String + Get + Return _serverUserName + End Get + End Property Private Shared Function GetBoundary(contentType As String) As String Dim elements As String() = contentType.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries) @@ -142,7 +165,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests If _fileUrlPrefix.Contains("8080") Then listener.Prefixes.Add(_fileUrlPrefix) - If _ServerUserName IsNot Nothing OrElse _ServerPassword IsNot Nothing Then + If _serverUserName IsNot Nothing OrElse _serverPassword IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If listener.Start() @@ -161,9 +184,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests CType(context.User?.Identity, HttpListenerBasicIdentity) If String.IsNullOrWhiteSpace(identity.Name) _ - OrElse identity.Name <> _ServerUserName _ + OrElse identity.Name <> _serverUserName _ OrElse String.IsNullOrWhiteSpace(identity.Password) _ - OrElse identity.Password <> _ServerPassword Then + OrElse identity.Password <> _serverPassword Then response.StatusCode = HttpStatusCode.Unauthorized Exit Try @@ -228,10 +251,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return listener End Function - Public Function GetServerConfigurationInstance() As ServerConfiguration - Return _serverConfigurationInstance - End Function - ''' ''' Parses a and gets the fileName of the uploaded file ''' and the lenght of the data file in bytes From b674593f631f581e5088a1c26147eaaf035066d5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 13:32:50 -0800 Subject: [PATCH 085/119] Fix upload and download tests to handle both possible cancelations exceptions in existing code. --- .../UnitTests/System/Windows/Forms/DownloadFileTests.vb | 6 ++++-- .../tests/UnitTests/System/Windows/Forms/UploadFileTests.vb | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 8f9a081097c..41ff5187f15 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1068,7 +1068,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - + '(Skip:="Manual Testing Only, cancel must be hit during testing")> Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1GB) @@ -1086,7 +1086,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests overwrite:=True) End Sub - testCode.Should.Throw(Of OperationCanceledException)() + testCode.Should() _ + .Throw(Of Exception)() _ + .Where(Function(ex) TypeOf ex Is OperationCanceledException OrElse TypeOf ex Is WebException) End Using End Sub diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 08fccd5019f..0e9b16ce247 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -1052,7 +1052,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests connectionTimeout:=TestingConnectionTimeout) End Sub - testCode.Should.Throw(Of OperationCanceledException)() + testCode.Should() _ + .Throw(Of Exception)() _ + .Where(Function(ex) TypeOf ex Is OperationCanceledException OrElse TypeOf ex Is WebException) End Using End Sub From e8b912e097a24571d703b22d96fcac3b233011bd Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 13:35:22 -0800 Subject: [PATCH 086/119] Correct skip download test on GitHub --- .../tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 41ff5187f15..f9f58f6bd5f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1068,7 +1068,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - '(Skip:="Manual Testing Only, cancel must be hit during testing")> + Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Fail() Dim testDirectory As String = CreateTempDirectory() Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1GB) From 846ae50f709ebc9530a0f8b85e661f68ab6e0f6a Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 17:26:14 -0800 Subject: [PATCH 087/119] Improve code coverage --- .../Windows/TestUtilities/ServerConfiguration.vb | 16 ++++++++-------- .../TestData/ServerConfiguration.json | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index 9d342b8f35a..8733182f39d 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -19,22 +19,22 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileDownloadUrlPrefix As String, fileUploadUrlPrefix As String, serverDownloadAllowsAnonymousUser As Boolean, - serverDownloadAllowsPasswordErrors As Boolean, + serverDownloadIgnoresPasswordErrors As Boolean, serverDownloadPassword As String, serverDownloadUserName As String, serverUploadAllowsAnonymousUser As Boolean, - serverUploadAllowsPasswordErrors As Boolean, + serverUploadIgnoresPasswordErrors As Boolean, serverUploadPassword As String, serverUploadUserName As String) Me.FileDownloadUrlPrefix = fileDownloadUrlPrefix Me.FileUploadUrlPrefix = fileUploadUrlPrefix Me.ServerDownloadAllowsAnonymousUser = serverDownloadAllowsAnonymousUser - Me.ServerDownloadAllowsPasswordErrors = serverDownloadAllowsPasswordErrors + Me.ServerDownloadIgnoresPasswordErrors = serverDownloadIgnoresPasswordErrors Me.ServerDownloadPassword = serverDownloadPassword Me.ServerDownloadUserName = serverDownloadUserName Me.ServerUploadAllowsAnonymousUser = serverUploadAllowsAnonymousUser - Me.ServerUploadAllowsPasswordErrors = serverUploadAllowsPasswordErrors + Me.ServerUploadIgnoresPasswordErrors = serverUploadIgnoresPasswordErrors Me.ServerUploadPassword = serverUploadPassword Me.ServerUploadUserName = serverUploadUserName End Sub @@ -42,11 +42,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Property FileDownloadUrlPrefix As String = "http://127.0.0.1:8080/" Public Property FileUploadUrlPrefix As String = "http://127.0.0.1:8080/" Public Property ServerDownloadAllowsAnonymousUser As Boolean = True - Public Property ServerDownloadAllowsPasswordErrors As Boolean + Public Property ServerDownloadIgnoresPasswordErrors As Boolean Public Property ServerDownloadPassword As String = "DefaultPassword" Public Property ServerDownloadUserName As String = "DefaultUserName" Public Property ServerUploadAllowsAnonymousUser As Boolean = True - Public Property ServerUploadAllowsPasswordErrors As Boolean + Public Property ServerUploadIgnoresPasswordErrors As Boolean Public Property ServerUploadPassword As String = "DefaultPassword" Public Property ServerUploadUserName As String = "DefaultUserName" @@ -84,9 +84,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Function GetThrowsPasswordErrors(uploading As Boolean) As Boolean If uploading Then - Return Not ServerUploadAllowsPasswordErrors + Return Not ServerUploadIgnoresPasswordErrors Else - Return Not ServerDownloadAllowsPasswordErrors + Return Not ServerDownloadIgnoresPasswordErrors End If End Function diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index cd3934e08a6..a4b4444b7fd 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -2,11 +2,11 @@ "FileDownloadUrlPrefix": "http://127.0.0.1:8080/", "FileUploadUrlPrefix": "http://127.0.0.1:8080/", "ServerDownloadAllowsAnonymousUser": true, - "ServerDownloadAllowsPasswordErrors": true, + "ServerDownloadIgnoresPasswordErrors": false, "ServerDownloadPassword": "", "ServerDownloadUserName": "", "ServerUploadAllowsAnonymousUser": true, - "serverUploadAllowsPasswordErrors": true, + "serverUploadAllowsIgnoresErrors": false, "ServerUploadPassword": "", "ServerUploadUserName": "" } \ No newline at end of file From abeb4f78cdcf925fb2eaa60cce31729576fd24b8 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 17:32:33 -0800 Subject: [PATCH 088/119] Fix spelling error --- .../Windows/TestUtilities/TestData/ServerConfiguration.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index a4b4444b7fd..df09989680b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -6,7 +6,7 @@ "ServerDownloadPassword": "", "ServerDownloadUserName": "", "ServerUploadAllowsAnonymousUser": true, - "serverUploadAllowsIgnoresErrors": false, + "serverUploadIgnoresPasswordErrors": false, "ServerUploadPassword": "", "ServerUploadUserName": "" } \ No newline at end of file From bb1b65a36568f43d9bbab01c0b15b7dfb1907ffe Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 17:42:29 -0800 Subject: [PATCH 089/119] Fix default values in ServerConfiguration.json --- .../TestUtilities/TestData/ServerConfiguration.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index df09989680b..6234e281fb4 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -3,10 +3,10 @@ "FileUploadUrlPrefix": "http://127.0.0.1:8080/", "ServerDownloadAllowsAnonymousUser": true, "ServerDownloadIgnoresPasswordErrors": false, - "ServerDownloadPassword": "", - "ServerDownloadUserName": "", + "ServerDownloadPassword": "DefaultPassword", + "ServerDownloadUserName": "DefaultUserName", "ServerUploadAllowsAnonymousUser": true, "serverUploadIgnoresPasswordErrors": false, - "ServerUploadPassword": "", - "ServerUploadUserName": "" + "ServerUploadPassword": "DefaultPassword", + "ServerUploadUserName": "DefaultUserName" } \ No newline at end of file From 96e783424bb1f9cd3b86c8731b5ce3710d649ede Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 14 Jan 2025 19:37:01 -0800 Subject: [PATCH 090/119] Correct default password for local testing Improve code coverage for upload tests Support more real File Server options --- .../System/Windows/Forms/DownloadFileTests.vb | 16 ++-- .../System/Windows/Forms/UploadFileTests.vb | 91 +++++++++++-------- .../TestUtilities/ServerConfiguration.vb | 8 +- .../TestData/ServerConfiguration.json | 8 +- .../Windows/TestUtilities/WebListener.vb | 20 ++-- 5 files changed, 82 insertions(+), 61 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index f9f58f6bd5f..b989bf21f55 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -61,7 +61,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=webListener.ServerDownloadUserName, + userName:=webListener.ServerUserName, password) End Sub @@ -90,7 +90,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=webListener.ServerDownloadUserName, + userName:=webListener.ServerUserName, password) End Sub @@ -119,7 +119,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=webListener.Address, destinationFileName, - userName:=webListener.ServerDownloadUserName, + userName:=webListener.ServerUserName, password) End Sub @@ -148,7 +148,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=webListener.ServerDownloadUserName, + userName:=webListener.ServerUserName, password) End Sub @@ -924,8 +924,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=New Uri(webListener.Address), destinationFileName, - userName:=webListener.ServerDownloadUserName, - password:=webListener.ServerDownloadPassword) + userName:=webListener.ServerUserName, + password:=webListener.ServerPassword) End Sub testCode.Should.NotThrow() @@ -1601,8 +1601,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests My.Computer.Network.DownloadFile( address:=webListener.Address, destinationFileName, - userName:=webListener.ServerDownloadUserName, - password:=webListener.ServerDownloadPassword) + userName:=webListener.ServerUserName, + password:=webListener.ServerPassword) End Sub testCode.Should.NotThrow() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 0e9b16ce247..694ec889a4f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -131,11 +131,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Sub - - Public Sub UploadFile_UriOnly_Success() + + + Public Sub UploadFile_UriOnly_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -211,11 +212,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriOnlyWrongFileSize_Throw() + + + Public Sub UploadFile_UriOnlyWrongFileSize_Throw(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -331,11 +333,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success() + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereTrue_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -405,11 +408,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -433,11 +437,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success() + + + Public Sub UploadFile_UriWithAllOptions_ExceptOnUserCancelWhereWhereDestinationFileExists_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -668,11 +673,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + + + Public Sub UploadFile_UriWithAllOptionsWhereOnUserCancelIsDoNothing_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -799,11 +805,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UriWithAllOptionsWithAllOptions_Success() + + + Public Sub UploadFile_UriWithAllOptionsWithAllOptions_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -877,11 +884,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlOnly_Success() + + + Public Sub UploadFile_UrlOnly_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1058,11 +1066,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success() + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereTrue_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1110,11 +1119,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success() + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereUsernameIsNothing_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1138,11 +1148,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws() + + + Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1295,11 +1306,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success() + + + Public Sub UploadFile_UrlWithAllOptionsWhereOnUserCancelIsDoNothing_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() @@ -1403,11 +1415,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub - - Public Sub UploadFile_UrlWithAllOptionsWithAllOptions_Success() + + + Public Sub UploadFile_UrlWithAllOptionsWithAllOptions_Success(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1MB) - Dim webListener As New WebListener(FileSizes.FileSize1MB) + Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = Sub() diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index 8733182f39d..1bbe741d310 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -43,12 +43,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Property FileUploadUrlPrefix As String = "http://127.0.0.1:8080/" Public Property ServerDownloadAllowsAnonymousUser As Boolean = True Public Property ServerDownloadIgnoresPasswordErrors As Boolean - Public Property ServerDownloadPassword As String = "DefaultPassword" - Public Property ServerDownloadUserName As String = "DefaultUserName" + Public Property ServerDownloadPassword As String = "" + Public Property ServerDownloadUserName As String = "" Public Property ServerUploadAllowsAnonymousUser As Boolean = True Public Property ServerUploadIgnoresPasswordErrors As Boolean - Public Property ServerUploadPassword As String = "DefaultPassword" - Public Property ServerUploadUserName As String = "DefaultUserName" + Public Property ServerUploadPassword As String = "" + Public Property ServerUploadUserName As String = "" Friend Function GetAcceptsAnonymousLogin(uploading As Boolean) As Boolean If uploading Then diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index 6234e281fb4..df09989680b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -3,10 +3,10 @@ "FileUploadUrlPrefix": "http://127.0.0.1:8080/", "ServerDownloadAllowsAnonymousUser": true, "ServerDownloadIgnoresPasswordErrors": false, - "ServerDownloadPassword": "DefaultPassword", - "ServerDownloadUserName": "DefaultUserName", + "ServerDownloadPassword": "", + "ServerDownloadUserName": "", "ServerUploadAllowsAnonymousUser": true, "serverUploadIgnoresPasswordErrors": false, - "ServerUploadPassword": "DefaultPassword", - "ServerUploadUserName": "DefaultUserName" + "ServerUploadPassword": "", + "ServerUploadUserName": "" } \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index fabdcf1bbf8..a03313a0857 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -19,6 +19,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Private ReadOnly _fileName As String Private ReadOnly _fileSize As Integer Private ReadOnly _fileUrlPrefix As String + Private ReadOnly _localServer As Boolean Private ReadOnly _serverConfigurationInstance As ServerConfiguration Private ReadOnly _serverPassword As String Private ReadOnly _serverUserName As String @@ -29,14 +30,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' ''' Is used to create the file name and the size of download. ''' Used to establish the file path to be downloaded. - Public Sub New(fileSize As Integer, Optional memberName As String = Nothing) + Public Sub New(fileSize As Integer, Optional supportAnonymousLogin As Boolean = True, Optional memberName As String = Nothing) Debug.Assert(fileSize > 0) _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize _serverConfigurationInstance = ServerConfigurationLoad(s_jsonFilePath) _fileUrlPrefix = _serverConfigurationInstance.GetFileUrlPrefix(_upload) + _localServer = _fileUrlPrefix.Contains("8080") _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) - ServerAcceptsAnonymousLogin = _serverConfigurationInstance.GetAcceptsAnonymousLogin(_upload) + If _localServer Then + ServerAcceptsAnonymousLogin = supportAnonymousLogin + Else + ServerAcceptsAnonymousLogin = _serverConfigurationInstance.GetAcceptsAnonymousLogin(_upload) + End If ServerThrowsPasswordErrors = _serverConfigurationInstance.GetThrowsPasswordErrors(_upload) _address = $"{_serverConfigurationInstance.GetFileUrlPrefix(_upload)}{_fileName}" End Sub @@ -52,12 +58,12 @@ Namespace Microsoft.VisualBasic.Forms.Tests fileSize As Integer, serverUserName As String, serverPassword As String, + Optional supportAnonymousLogin As Boolean = True, Optional memberName As String = Nothing) - Me.New(fileSize, memberName) - Dim localServer As Boolean = _fileUrlPrefix.Contains("8080") + Me.New(fileSize, supportAnonymousLogin, memberName) - If localServer Then + If _localServer Then _serverPassword = serverPassword _serverUserName = serverUserName Else @@ -165,7 +171,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests If _fileUrlPrefix.Contains("8080") Then listener.Prefixes.Add(_fileUrlPrefix) - If _serverUserName IsNot Nothing OrElse _serverPassword IsNot Nothing Then + If Not ServerAcceptsAnonymousLogin _ + OrElse _serverUserName IsNot Nothing _ + OrElse _serverPassword IsNot Nothing Then listener.AuthenticationSchemes = AuthenticationSchemes.Basic End If listener.Start() From 9faf31e69f5ed8579a2699eb47f5a93a5f980673 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 15 Jan 2025 13:45:54 -0800 Subject: [PATCH 091/119] Make CreateTempFile more robust by making size a FileSize Enum --- .../System/Windows/Forms/DownloadFileTests.vb | 12 ++++++------ .../System/Windows/Forms/FileSystemProxyTests.vb | 10 +++++----- .../System/Windows/Forms/UploadFileTests.vb | 2 +- .../System/Windows/TestUtilities/EnumFileSizes.vb | 1 + .../Windows/TestUtilities/VbFileCleanupTestBase.vb | 13 ++++++------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index b989bf21f55..db28d76dde7 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -290,7 +290,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -316,7 +316,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -340,7 +340,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UriWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -1019,7 +1019,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereFileExistsNoOverwrite_Throws() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -1095,7 +1095,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteTrue_Success() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize100MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = @@ -1119,7 +1119,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DownloadFile_UrlWithAllOptions_ExceptOnUserCancelWhereOverwriteWhereDestinationFileExists_Success() Dim testDirectory As String = CreateTempDirectory() - Dim destinationFileName As String = CreateTempFile(testDirectory, size:=1) + Dim destinationFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb index 523cf1fb5bd..7bd5b3d67ad 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileSystemProxyTests.vb @@ -96,7 +96,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DeleteDirectoryRecycleWithUICancelOptionsProxyTest() Dim sourceDirectoryName As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=1) + Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=FileSizes.FileSize1Byte) Dim data As Byte() = {4} _fileSystem.WriteAllBytes(sourceFileName, data, append:=False) File.Exists(sourceFileName).Should.BeTrue() @@ -112,7 +112,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DeleteDirectoryWithUIProxyRecycleTest() Dim sourceDirectoryName As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=1) + Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=FileSizes.FileSize1Byte) Dim data As Byte() = {4} _fileSystem.WriteAllBytes(sourceFileName, data, append:=False) File.Exists(sourceFileName).Should.BeTrue() @@ -127,7 +127,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DeleteFileWithRecycleOptionProxyTest() Dim sourceDirectoryName As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=1) + Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=FileSizes.FileSize1Byte) Dim byteArray As Byte() = {4} _fileSystem.WriteAllBytes(sourceFileName, byteArray, append:=False) File.Exists(sourceFileName).Should.BeTrue() @@ -146,7 +146,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub DeleteFileWithUIProxyTest() Dim sourceDirectoryName As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=1) + Dim sourceFileName As String = CreateTempFile(sourceDirectoryName, NameOf(sourceFileName), size:=FileSizes.FileSize1Byte) Dim data As Byte() = {4} _fileSystem.WriteAllBytes(sourceFileName, data, append:=False) File.Exists(sourceFileName).Should.BeTrue() @@ -187,7 +187,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim sourceDirectoryName As String = CreateTempDirectory() Dim fileA As String = CreateTempFile(sourceDirectoryName, NameOf(fileA)) _fileSystem.WriteAllText(fileA, "A", append:=False) - Dim fileB As String = CreateTempFile(sourceDirectoryName, NameOf(fileB), size:=1) + Dim fileB As String = CreateTempFile(sourceDirectoryName, NameOf(fileB), size:=FileSizes.FileSize1Byte) Dim fileC As String = CreateTempFile(sourceDirectoryName, NameOf(fileC)) _fileSystem.WriteAllText(fileC, "C", append:=False) Dim filenames As ReadOnlyCollection(Of String) = _fileSystem.FindInFiles( diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb index 694ec889a4f..2eb0c22020f 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/UploadFileTests.vb @@ -1152,7 +1152,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub UploadFile_UrlWithAllOptions_ExceptOnUserCancelWhereWhereUploadFailed_Throws(supportAnonymousLogin As Boolean) Dim testDirectory As String = CreateTempDirectory() - Dim sourceFileName As String = CreateTempFile(testDirectory, size:=1) + Dim sourceFileName As String = CreateTempFile(testDirectory, size:=FileSizes.FileSize1Byte) Dim webListener As New WebListener(FileSizes.FileSize1MB, supportAnonymousLogin) Using listener As HttpListener = webListener.ProcessRequests() Dim testCode As Action = diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb index bd5cbfdae28..0d864d545a0 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb @@ -4,6 +4,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Module EnumFileSizes Friend Enum FileSizes As Integer + Unknown = -1 FileSize1Byte = 1 FileSize1MB = 1_048_576 FileSize100MB = 104_857_600 diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 1cc3a58bf7e..65e2dcae009 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -36,19 +36,18 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Sub ''' - ''' If size >= 0 then create the file with size length. + ''' If size is not FileSize.Unknown then create the file with size length. ''' ''' Full path to working directory. ''' - ''' Size in bytes of the file to be created. + ''' FileSize of the file to be created. ''' ''' The full path and file name of the created file. - ''' If size = -1 no file is create but the full path is returned. + ''' If size = FileSize.Unknown no file is create but the full path is returned. ''' - Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As Integer = -1) As String - If filename = DefaultFileName Then - filename = $"{[Enum].GetName(GetType(FileSizes), size).Replace("FileSize", "")}.zip" - + Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As FileSizes = FileSizes.Unknown) As String + If filename = DefaultFileName AndAlso size <> FileSizes.Unknown Then + filename = $"{[Enum].GetName(size).Replace("FileSize", "")}.zip" End If Dim filenameWithPath As String = Path.Combine(sourceDirectoryName, filename) From 0efa368b497378a249b4f61bd2c0413581245a40 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 15 Jan 2025 16:50:47 -0800 Subject: [PATCH 092/119] Prevent merge issue --- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 2d23637c398..fcfd6afa251 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -72,12 +72,11 @@ Namespace Microsoft.VisualBasic.Devices Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=destinationFileName, paramName:=NameOf(destinationFileName)) - Dim addressTrimmed As String = address.Replace("//", "/") Return New ProgressDialog With { - .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, addressTrimmed), + .Text = VbUtils.GetResourceString(SR.ProgressDialogDownloadingTitle, address), .LabelText = VbUtils.GetResourceString( resourceKey:=SR.ProgressDialogDownloadingLabel, - addressTrimmed, + address, fullFilename) } End If From 1f5dd80ed86aec6b32e19975c3706cafedd34710 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 15 Jan 2025 17:30:46 -0800 Subject: [PATCH 093/119] Fix test failures --- .../VisualBasic/Devices/Network.DownloadFileAsync.vb | 10 ++++++---- .../VisualBasic/Devices/Network.UploadFileAsync.vb | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb index 0f350ed9c0a..61b260e8470 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFileAsync.vb @@ -314,10 +314,12 @@ Namespace Microsoft.VisualBasic.Devices End If ' Set credentials if we have any - Dim client As HttpClient = If(clientHandler Is Nothing, - New HttpClient(), - New HttpClient(clientHandler) - ) + Dim client As HttpClient + If clientHandler Is Nothing Then + client = New HttpClient() + Else + client = New HttpClient(clientHandler) + End If client.Timeout = New TimeSpan(0, 0, 0, 0, connectionTimeout) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb index 7e046144fb2..b5fc3d91b2e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFileAsync.vb @@ -70,12 +70,15 @@ Namespace Microsoft.VisualBasic.Devices Throw Catch ex As Exception - If onUserCancel = UICancelOption.ThrowException OrElse Not dialog.UserCanceledTheDialog Then + If onUserCancel = UICancelOption.ThrowException OrElse (dialog IsNot Nothing AndAlso Not dialog.UserCanceledTheDialog) Then If ex.Message.Contains("401") Then Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) End If Throw End If + If ex.Message.Contains("401") Then + Throw New WebException(SR.net_webstatus_Unauthorized) + End If Finally CloseProgressDialog(dialog) End Try From bbd01fe3c0afcfc3132e52a4a5245dfac78df3aa Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 15 Jan 2025 18:17:03 -0800 Subject: [PATCH 094/119] Cleanup VbFileCleanupTestBase --- .../TestUtilities/VbFileCleanupTestBase.vb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb index 65e2dcae009..68dabaf0c53 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/VbFileCleanupTestBase.vb @@ -9,7 +9,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public MustInherit Class VbFileCleanupTestBase Implements IDisposable - Private Shared ReadOnly s_baseTempPath As String = Path.Combine(Path.GetTempPath, "DownLoadTest9d9e3a8-7a46-4333-a0eb-4faf76994801") + Private Shared ReadOnly s_baseTempPath As String = Path.Combine( + Path.GetTempPath, + "DownLoadTest9d9e3a8-7a46-4333-a0eb-4faf76994801") Friend Const DefaultFileName As String = "Testing.Txt" Friend ReadOnly _testDirectories As New HashSet(Of String) @@ -17,7 +19,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dispose(disposing:=False) End Sub - ' The base path is system temp directory / a guaranteed unique directory based on a GUID / a temp directory based on TestName + ' The base path is the system temp directory / + ' a guaranteed unique directory based on a GUID / + ' a temp directory based on TestName Friend Shared ReadOnly Property BaseTempPath As String Get Return s_baseTempPath @@ -45,7 +49,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' The full path and file name of the created file. ''' If size = FileSize.Unknown no file is create but the full path is returned. ''' - Friend Shared Function CreateTempFile(sourceDirectoryName As String, Optional filename As String = DefaultFileName, Optional size As FileSizes = FileSizes.Unknown) As String + Friend Shared Function CreateTempFile( + sourceDirectoryName As String, + Optional filename As String = DefaultFileName, + Optional size As FileSizes = FileSizes.Unknown) As String + If filename = DefaultFileName AndAlso size <> FileSizes.Unknown Then filename = $"{[Enum].GetName(size).Replace("FileSize", "")}.zip" End If @@ -96,7 +104,10 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' ''' If >0 use line number as part of name. ''' The name of a directory that is safe to write to and is verified to exist. - Friend Function CreateTempDirectory( Optional memberName As String = Nothing, Optional lineNumber As Integer = -1) As String + Friend Function CreateTempDirectory( + Optional memberName As String = Nothing, + Optional lineNumber As Integer = -1) As String + Dim folder As String If lineNumber > 0 Then folder = Path.Combine(BaseTempPath, $"{memberName}{lineNumber}") From 74bb4b8289ac411e2514a7fcde3438cea24097b0 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 18 Jan 2025 01:38:32 -0800 Subject: [PATCH 095/119] Change Getter in ServerConfiguration to be private to avoid exposing internal implementation. Use TypeResolver to load SystemConfiguration and add sample configuration for real servers. Add tests for leading ServerConfiguration json files. --- .../Microsoft.VisualBasic.Forms.Tests.vbproj | 7 + .../Windows/Forms/ServerConfigurationTests.vb | 37 +++++ .../PrivateSetterContractResolver.vb | 33 ++++ .../TestUtilities/ServerConfiguration.vb | 143 ++++++++++++++++-- .../TestData/ServerConfiguration.json | 2 +- .../TestData/ServerConfigurationSample.JSON | 12 ++ .../Windows/TestUtilities/WebListener.vb | 6 +- 7 files changed, 218 insertions(+), 22 deletions(-) create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/PrivateSetterContractResolver.vb create mode 100644 src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfigurationSample.JSON diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj index 04b45a8ab61..d6066dceb6e 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/Microsoft.VisualBasic.Forms.Tests.vbproj @@ -16,10 +16,17 @@ + + + + PreserveNewest + + PreserveNewest + diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb new file mode 100644 index 00000000000..469335f75cc --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb @@ -0,0 +1,37 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports FluentAssertions +Imports FluentAssertions.Execution +Imports Xunit + +Namespace Microsoft.VisualBasic.Forms.Tests + + Public Class ServerConfigurationTests + + + Public Sub VerifyNew_Success(uploading As Boolean) + Dim serverConfigurationDefaults As New ServerConfiguration + Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad + serverConfigurationDefaults.GetAcceptsAnonymousLogin(uploading).Should.Be(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) + serverConfigurationDefaults.GetDefaultPassword(uploading).Should.Be(serverConfigurationLoad.GetDefaultPassword(uploading)) + serverConfigurationDefaults.GetDefaultUserName(uploading).Should.Be(serverConfigurationLoad.GetDefaultUserName(uploading)) + serverConfigurationDefaults.GetFileUrlPrefix(uploading).Should.Be(serverConfigurationLoad.GetFileUrlPrefix(uploading)) + serverConfigurationDefaults.GetThrowsPasswordErrors(uploading).Should.Be(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) + End Sub + + + Public Sub VerifyNew_Fail(uploading As Boolean) + Dim serverConfigurationDefaults As New ServerConfiguration + Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad("ServerConfigurationSample.JSON") + Using New AssertionScope() + serverConfigurationDefaults.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) + serverConfigurationDefaults.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) + serverConfigurationDefaults.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) + serverConfigurationDefaults.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) + serverConfigurationDefaults.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) + End Using + End Sub + + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/PrivateSetterContractResolver.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/PrivateSetterContractResolver.vb new file mode 100644 index 00000000000..f3156fda23c --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/PrivateSetterContractResolver.vb @@ -0,0 +1,33 @@ +' Licensed to the .NET Foundation under one or more agreements. +' The .NET Foundation licenses this file to you under the MIT license. + +Imports System.Reflection +Imports System.Text.Json +Imports System.Text.Json.Serialization.Metadata + +Namespace Microsoft.VisualBasic.Forms.Tests + Public Class PrivateSetterContractResolver + Inherits DefaultJsonTypeInfoResolver + + Public Overrides Function GetTypeInfo(type As Type, options As JsonSerializerOptions) As JsonTypeInfo + Dim jsonTypeInfo As JsonTypeInfo = MyBase.GetTypeInfo(type, options) + + If jsonTypeInfo.Kind = JsonTypeInfoKind.Object Then + For Each [property] As JsonPropertyInfo In jsonTypeInfo.Properties + [property].Set = Function(obj, value) + Dim prop As PropertyInfo = type.GetProperty( + [property].Name, + BindingFlags.Public _ + Or BindingFlags.NonPublic _ + Or BindingFlags.Instance _ + Or BindingFlags.IgnoreCase) + prop.SetValue(obj, value, Nothing) + Return True + End Function + Next + End If + + Return jsonTypeInfo + End Function + End Class +End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index 1bbe741d310..917769a363a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -8,7 +8,28 @@ Imports System.Text.Json.Serialization Namespace Microsoft.VisualBasic.Forms.Tests Public Class ServerConfiguration - Private ReadOnly _options As New JsonSerializerOptions() With {.WriteIndented = True} + Private Const JsonDefaultFileName As String = "ServerConfiguration.JSON" + + Private Shared ReadOnly s_deserializerOptions As New JsonSerializerOptions() With { + .TypeInfoResolver = New PrivateSetterContractResolver()} + + Private Shared ReadOnly s_jsonFilePathBase As String = Path.Combine( + My.Application.Info.DirectoryPath, + "System\Windows\TestUtilities\TestData") + + Private ReadOnly _serializerOptions As New JsonSerializerOptions With { + .WriteIndented = True} + + Private _fileDownloadUrlPrefix As String = "http://127.0.0.1:8080/" + Private _fileUploadUrlPrefix As String = "http://127.0.0.1:8080/" + Private _serverDownloadAllowsAnonymousUser As Boolean = True + Private _serverDownloadIgnoresPasswordErrors As Boolean + Private _serverDownloadPassword As String = "" + Private _serverDownloadUserName As String = "" + Private _serverUploadAllowsAnonymousUser As Boolean = True + Private _serverUploadIgnoresPasswordErrors As Boolean + Private _serverUploadPassword As String = "" + Private _serverUploadUserName As String = "" Public Sub New() @@ -39,16 +60,95 @@ Namespace Microsoft.VisualBasic.Forms.Tests Me.ServerUploadUserName = serverUploadUserName End Sub - Public Property FileDownloadUrlPrefix As String = "http://127.0.0.1:8080/" - Public Property FileUploadUrlPrefix As String = "http://127.0.0.1:8080/" - Public Property ServerDownloadAllowsAnonymousUser As Boolean = True + Public Property FileDownloadUrlPrefix As String + Private Get + Return _fileDownloadUrlPrefix + End Get + Set + _fileDownloadUrlPrefix = Value + End Set + End Property + + Public Property FileUploadUrlPrefix As String + Private Get + Return _fileUploadUrlPrefix + End Get + Set + _fileUploadUrlPrefix = Value + End Set + End Property + + Public Property ServerDownloadAllowsAnonymousUser As Boolean + Private Get + Return _serverDownloadAllowsAnonymousUser + End Get + Set + _serverDownloadAllowsAnonymousUser = Value + End Set + End Property + Public Property ServerDownloadIgnoresPasswordErrors As Boolean - Public Property ServerDownloadPassword As String = "" - Public Property ServerDownloadUserName As String = "" - Public Property ServerUploadAllowsAnonymousUser As Boolean = True + Private Get + Return _serverDownloadIgnoresPasswordErrors + End Get + Set + _serverDownloadIgnoresPasswordErrors = Value + End Set + End Property + + Public Property ServerDownloadPassword As String + Private Get + Return _serverDownloadPassword + End Get + Set + _serverDownloadPassword = Value + End Set + End Property + + Public Property ServerDownloadUserName As String + Private Get + Return _serverDownloadUserName + End Get + Set + _serverDownloadUserName = Value + End Set + End Property + + Public Property ServerUploadAllowsAnonymousUser As Boolean + Private Get + Return _serverUploadAllowsAnonymousUser + End Get + Set + _serverUploadAllowsAnonymousUser = Value + End Set + End Property + Public Property ServerUploadIgnoresPasswordErrors As Boolean - Public Property ServerUploadPassword As String = "" - Public Property ServerUploadUserName As String = "" + Private Get + Return _serverUploadIgnoresPasswordErrors + End Get + Set + _serverUploadIgnoresPasswordErrors = Value + End Set + End Property + + Public Property ServerUploadPassword As String + Private Get + Return _serverUploadPassword + End Get + Set + _serverUploadPassword = Value + End Set + End Property + + Public Property ServerUploadUserName As String + Private Get + Return _serverUploadUserName + End Get + Set + _serverUploadUserName = Value + End Set + End Property Friend Function GetAcceptsAnonymousLogin(uploading As Boolean) As Boolean If uploading Then @@ -90,17 +190,28 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function - Public Shared Function ServerConfigurationLoad(jsonFilePath As String) As ServerConfiguration - If File.Exists(jsonFilePath) Then - Dim jsonString As String = File.ReadAllText(jsonFilePath) - Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString) + Public Shared Function ServerConfigurationLoad(Optional jsonFileName As String = JsonDefaultFileName) As ServerConfiguration + Dim jsonFileNameWithPath As String = Path.Combine(s_jsonFilePathBase, jsonFileName) + + If File.Exists(jsonFileNameWithPath) Then + Dim jsonString As String = File.ReadAllText(jsonFileNameWithPath) + Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString, s_deserializerOptions) End If Return New ServerConfiguration End Function - Public Sub ServerConfigurationSave(jsonFilePath As String) - Dim jsonString As String = JsonSerializer.Serialize(Me, _options) - File.WriteAllText(jsonFilePath, jsonString) + Public Overrides Function Equals(obj As Object) As Boolean + Return MyBase.Equals(obj) + End Function + + Public Overrides Function GetHashCode() As Integer + Return MyBase.GetHashCode() + End Function + + Public Sub ServerConfigurationSave() + Dim jsonString As String = JsonSerializer.Serialize(Me, _serializerOptions) + Dim jsonFileNameWithPath As String = Path.Combine(s_jsonFilePathBase, JsonDefaultFileName) + File.WriteAllText(jsonFileNameWithPath, jsonString) End Sub End Class diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json index df09989680b..03191e63526 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfiguration.json @@ -6,7 +6,7 @@ "ServerDownloadPassword": "", "ServerDownloadUserName": "", "ServerUploadAllowsAnonymousUser": true, - "serverUploadIgnoresPasswordErrors": false, + "ServerUploadIgnoresPasswordErrors": false, "ServerUploadPassword": "", "ServerUploadUserName": "" } \ No newline at end of file diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfigurationSample.JSON b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfigurationSample.JSON new file mode 100644 index 00000000000..3e36d5eef58 --- /dev/null +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/ServerConfigurationSample.JSON @@ -0,0 +1,12 @@ +{ + "FileDownloadUrlPrefix": "http://someServer.net/", + "FileUploadUrlPrefix": "ftp://ftp.someServer.com/", + "ServerDownloadAllowsAnonymousUser": false, + "ServerDownloadIgnoresPasswordErrors": true, + "ServerDownloadPassword": "Anything", + "ServerDownloadUserName": "Anonymous", + "ServerUploadAllowsAnonymousUser": false, + "ServerUploadIgnoresPasswordErrors": true, + "ServerUploadPassword": "rNrKYTX9g7z3RgJRmxWuGHbeu", + "ServerUploadUserName": "defaultuser" +} diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index a03313a0857..0ac369608fd 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -11,10 +11,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Class WebListener Inherits ServerConfiguration Private Const BufferSize As Integer = 4096 - - Private Shared ReadOnly s_jsonFilePath As String = - Path.Combine(My.Application.Info.DirectoryPath, "System\Windows\TestUtilities\TestData", "ServerConfiguration.JSON") - Private ReadOnly _address As String Private ReadOnly _fileName As String Private ReadOnly _fileSize As Integer @@ -34,7 +30,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Debug.Assert(fileSize > 0) _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize - _serverConfigurationInstance = ServerConfigurationLoad(s_jsonFilePath) + _serverConfigurationInstance = ServerConfigurationLoad() _fileUrlPrefix = _serverConfigurationInstance.GetFileUrlPrefix(_upload) _localServer = _fileUrlPrefix.Contains("8080") _upload = memberName.Contains("Upload", StringComparison.InvariantCultureIgnoreCase) From 8d46198faa6d1cd2c988b147542905576cf6e7a7 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 21 Jan 2025 20:30:40 -0800 Subject: [PATCH 096/119] Add tests for ServerConfiguration --- .../Windows/Forms/ServerConfigurationTests.vb | 52 +++++++++++++------ .../TestUtilities/ServerConfiguration.vb | 39 +++++++++----- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb index 469335f75cc..baa6258458e 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb @@ -1,6 +1,7 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.IO Imports FluentAssertions Imports FluentAssertions.Execution Imports Xunit @@ -8,30 +9,47 @@ Imports Xunit Namespace Microsoft.VisualBasic.Forms.Tests Public Class ServerConfigurationTests - - - Public Sub VerifyNew_Success(uploading As Boolean) - Dim serverConfigurationDefaults As New ServerConfiguration - Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad - serverConfigurationDefaults.GetAcceptsAnonymousLogin(uploading).Should.Be(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) - serverConfigurationDefaults.GetDefaultPassword(uploading).Should.Be(serverConfigurationLoad.GetDefaultPassword(uploading)) - serverConfigurationDefaults.GetDefaultUserName(uploading).Should.Be(serverConfigurationLoad.GetDefaultUserName(uploading)) - serverConfigurationDefaults.GetFileUrlPrefix(uploading).Should.Be(serverConfigurationLoad.GetFileUrlPrefix(uploading)) - serverConfigurationDefaults.GetThrowsPasswordErrors(uploading).Should.Be(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) + + Private Shared Sub Verify(uploading As Boolean, defaultConfiguration As ServerConfiguration, testConfiguration As ServerConfiguration) + defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.Be(testConfiguration.GetAcceptsAnonymousLogin(uploading)) + defaultConfiguration.GetDefaultPassword(uploading).Should.Be(testConfiguration.GetDefaultPassword(uploading)) + defaultConfiguration.GetDefaultUserName(uploading).Should.Be(testConfiguration.GetDefaultUserName(uploading)) + defaultConfiguration.GetFileUrlPrefix(uploading).Should.Be(testConfiguration.GetFileUrlPrefix(uploading)) + defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.Be(testConfiguration.GetThrowsPasswordErrors(uploading)) End Sub + Public Sub VerifyNew_Fail(uploading As Boolean) - Dim serverConfigurationDefaults As New ServerConfiguration - Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad("ServerConfigurationSample.JSON") + Dim defaultConfiguration As New ServerConfiguration + Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad(jsonFileName:="ServerConfigurationSample.JSON") Using New AssertionScope() - serverConfigurationDefaults.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) - serverConfigurationDefaults.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) - serverConfigurationDefaults.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) - serverConfigurationDefaults.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) - serverConfigurationDefaults.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) + defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) + defaultConfiguration.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) + defaultConfiguration.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) + defaultConfiguration.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) + defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) End Using End Sub + + + Public Sub VerifyNew_Success(uploading As Boolean) + Dim defaultConfiguration As New ServerConfiguration + Dim testConfiguration As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad + Verify(uploading, defaultConfiguration, testConfiguration) + End Sub + + + + Public Sub VerifySerialization_Success(uploading As Boolean) + Dim defaultConfiguration As New ServerConfiguration + Dim jsonFilePathBase As String = Path.GetTempPath + Dim jsonFullPath As String = defaultConfiguration.ServerConfigurationSave(jsonFilePathBase) + Dim testConfiguration As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad(jsonFilePathBase) + Verify(uploading, defaultConfiguration, testConfiguration) + File.Delete(jsonFullPath) + End Sub + End Class End Namespace diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb index 917769a363a..38ad15b17d3 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/ServerConfiguration.vb @@ -60,6 +60,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Me.ServerUploadUserName = serverUploadUserName End Sub + Public Property FileDownloadUrlPrefix As String Private Get Return _fileDownloadUrlPrefix @@ -69,6 +70,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property FileUploadUrlPrefix As String Private Get Return _fileUploadUrlPrefix @@ -78,6 +80,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerDownloadAllowsAnonymousUser As Boolean Private Get Return _serverDownloadAllowsAnonymousUser @@ -87,6 +90,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerDownloadIgnoresPasswordErrors As Boolean Private Get Return _serverDownloadIgnoresPasswordErrors @@ -96,6 +100,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerDownloadPassword As String Private Get Return _serverDownloadPassword @@ -105,6 +110,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerDownloadUserName As String Private Get Return _serverDownloadUserName @@ -114,6 +120,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerUploadAllowsAnonymousUser As Boolean Private Get Return _serverUploadAllowsAnonymousUser @@ -123,6 +130,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerUploadIgnoresPasswordErrors As Boolean Private Get Return _serverUploadIgnoresPasswordErrors @@ -132,6 +140,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerUploadPassword As String Private Get Return _serverUploadPassword @@ -141,6 +150,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Public Property ServerUploadUserName As String Private Get Return _serverUploadUserName @@ -150,6 +160,13 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Set End Property + Private Shared Function GetJsonFilePath(jsonFilePathBase As String, jsonFileName As String) As String + If String.IsNullOrWhiteSpace(jsonFilePathBase) Then + jsonFilePathBase = s_jsonFilePathBase + End If + Return Path.Combine(jsonFilePathBase, jsonFileName) + End Function + Friend Function GetAcceptsAnonymousLogin(uploading As Boolean) As Boolean If uploading Then Return ServerUploadAllowsAnonymousUser @@ -190,9 +207,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Function - Public Shared Function ServerConfigurationLoad(Optional jsonFileName As String = JsonDefaultFileName) As ServerConfiguration - Dim jsonFileNameWithPath As String = Path.Combine(s_jsonFilePathBase, jsonFileName) + Public Shared Function ServerConfigurationLoad( + Optional jsonFilePathBase As String = "", + Optional jsonFileName As String = JsonDefaultFileName) As ServerConfiguration + Dim jsonFileNameWithPath As String = GetJsonFilePath(jsonFilePathBase, jsonFileName) If File.Exists(jsonFileNameWithPath) Then Dim jsonString As String = File.ReadAllText(jsonFileNameWithPath) Return JsonSerializer.Deserialize(Of ServerConfiguration)(jsonString, s_deserializerOptions) @@ -200,19 +219,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests Return New ServerConfiguration End Function - Public Overrides Function Equals(obj As Object) As Boolean - Return MyBase.Equals(obj) - End Function - - Public Overrides Function GetHashCode() As Integer - Return MyBase.GetHashCode() - End Function + Public Function ServerConfigurationSave( + Optional jsonFilePathBase As String = "", + Optional jsonFileName As String = JsonDefaultFileName) As String - Public Sub ServerConfigurationSave() + Dim jsonFileNameWithPath As String = GetJsonFilePath(jsonFilePathBase, jsonFileName) Dim jsonString As String = JsonSerializer.Serialize(Me, _serializerOptions) - Dim jsonFileNameWithPath As String = Path.Combine(s_jsonFilePathBase, JsonDefaultFileName) File.WriteAllText(jsonFileNameWithPath, jsonString) - End Sub + Return jsonFileNameWithPath + End Function End Class End Namespace From caaeb54dfca8f2b80e9a35a22ff8b8811a026809 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 22 Jan 2025 19:54:33 -0800 Subject: [PATCH 097/119] Cleanup ServerConfigurationTests --- .../Windows/Forms/ServerConfigurationTests.vb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb index baa6258458e..6ca67b5d54b 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb @@ -3,7 +3,6 @@ Imports System.IO Imports FluentAssertions -Imports FluentAssertions.Execution Imports Xunit Namespace Microsoft.VisualBasic.Forms.Tests @@ -23,13 +22,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests Public Sub VerifyNew_Fail(uploading As Boolean) Dim defaultConfiguration As New ServerConfiguration Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad(jsonFileName:="ServerConfigurationSample.JSON") - Using New AssertionScope() - defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) - defaultConfiguration.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) - defaultConfiguration.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) - defaultConfiguration.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) - defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) - End Using + defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) + defaultConfiguration.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) + defaultConfiguration.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) + defaultConfiguration.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) + defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) End Sub From b9fcf21fa0243c150e1a392a55d8df150143d985 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 22 Jan 2025 19:57:42 -0800 Subject: [PATCH 098/119] Update broken test --- .../System/Windows/Forms/ServerConfigurationTests.vb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb index 6ca67b5d54b..76bb0361468 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb @@ -17,18 +17,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.Be(testConfiguration.GetThrowsPasswordErrors(uploading)) End Sub - - - Public Sub VerifyNew_Fail(uploading As Boolean) - Dim defaultConfiguration As New ServerConfiguration - Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad(jsonFileName:="ServerConfigurationSample.JSON") - defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) - defaultConfiguration.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) - defaultConfiguration.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) - defaultConfiguration.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) - defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) - End Sub - Public Sub VerifyNew_Success(uploading As Boolean) From 4ea5f9ed9866f613f020e9940c4378823ea96ec2 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 22 Jan 2025 20:09:54 -0800 Subject: [PATCH 099/119] Fix failing test --- .../Windows/Forms/ServerConfigurationTests.vb | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb index baa6258458e..76bb0361468 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ServerConfigurationTests.vb @@ -3,7 +3,6 @@ Imports System.IO Imports FluentAssertions -Imports FluentAssertions.Execution Imports Xunit Namespace Microsoft.VisualBasic.Forms.Tests @@ -18,20 +17,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.Be(testConfiguration.GetThrowsPasswordErrors(uploading)) End Sub - - - Public Sub VerifyNew_Fail(uploading As Boolean) - Dim defaultConfiguration As New ServerConfiguration - Dim serverConfigurationLoad As ServerConfiguration = ServerConfiguration.ServerConfigurationLoad(jsonFileName:="ServerConfigurationSample.JSON") - Using New AssertionScope() - defaultConfiguration.GetAcceptsAnonymousLogin(uploading).Should.NotBe(serverConfigurationLoad.GetAcceptsAnonymousLogin(uploading)) - defaultConfiguration.GetDefaultPassword(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultPassword(uploading)) - defaultConfiguration.GetDefaultUserName(uploading).Should.NotBe(serverConfigurationLoad.GetDefaultUserName(uploading)) - defaultConfiguration.GetFileUrlPrefix(uploading).Should.NotBe(serverConfigurationLoad.GetFileUrlPrefix(uploading)) - defaultConfiguration.GetThrowsPasswordErrors(uploading).Should.NotBe(serverConfigurationLoad.GetThrowsPasswordErrors(uploading)) - End Using - End Sub - Public Sub VerifyNew_Success(uploading As Boolean) From 79c12e5476e14918aeca15751725e9b2f31d5303 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 23 Jan 2025 19:47:17 -0800 Subject: [PATCH 100/119] Restore WebClientCopy, remove incorrect fix --- .../MyServices/Internal/WebClientCopy.vb | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index d93fe488cfb..aa18bedce88 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -158,22 +158,14 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Debug.Assert((Not String.IsNullOrWhiteSpace(destinationFileName)) _ AndAlso IO.Directory.Exists(path), $"Invalid {NameOf(path)}") - Try - - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.DownloadFileAsync(address, destinationFileName) - 'returns when the download sequence is over, whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.DownloadFile(address, destinationFileName) - End If - Catch ex As WebException - If ex.Message.Contains("401") Then - Throw New WebException(SR.net_webstatus_Unauthorized) - End If - Throw New WebException(SR.net_webstatus_Timeout) - End Try + ' If we have a dialog we need to set up an async download + If _progressDialog IsNot Nothing Then + _webClient.DownloadFileAsync(address, destinationFileName) + 'returns when the download sequence is over, whether due to success, error, or being canceled + _progressDialog.ShowProgressDialog() + Else + _webClient.DownloadFile(address, destinationFileName) + End If 'Now that we are back on the main thread, throw the exception we encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then @@ -219,12 +211,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal ' encountered if the user didn't cancel. If _exceptionEncounteredDuringFileTransfer IsNot Nothing Then If _progressDialog Is Nothing OrElse Not _progressDialog.UserCanceledTheDialog Then - If _exceptionEncounteredDuringFileTransfer.Message.Contains("401") OrElse _exceptionEncounteredDuringFileTransfer.Message.Contains("530") Then - Throw New WebException(SR.net_webstatus_Unauthorized) - End If Throw _exceptionEncounteredDuringFileTransfer End If - Throw _exceptionEncounteredDuringFileTransfer End If End Sub From db6e9a5e7cfc9784fcb0e5c2399910321cedc28c Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 24 Jan 2025 20:49:24 -0800 Subject: [PATCH 101/119] Fix error message text for file upload/download operations --- src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx | 9 +++++---- .../src/Resources/xlf/SR.cs.xlf | 8 ++++---- .../src/Resources/xlf/SR.de.xlf | 8 ++++---- .../src/Resources/xlf/SR.es.xlf | 8 ++++---- .../src/Resources/xlf/SR.fr.xlf | 8 ++++---- .../src/Resources/xlf/SR.it.xlf | 8 ++++---- .../src/Resources/xlf/SR.ja.xlf | 8 ++++---- .../src/Resources/xlf/SR.ko.xlf | 8 ++++---- .../src/Resources/xlf/SR.pl.xlf | 8 ++++---- .../src/Resources/xlf/SR.pt-BR.xlf | 8 ++++---- .../src/Resources/xlf/SR.ru.xlf | 8 ++++---- .../src/Resources/xlf/SR.tr.xlf | 8 ++++---- .../src/Resources/xlf/SR.zh-Hans.xlf | 8 ++++---- .../src/Resources/xlf/SR.zh-Hant.xlf | 8 ++++---- 14 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx index 2d86b1c6b73..5979edfcd46 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx @@ -1,4 +1,5 @@ - + + From 1d94bd539824d151de961d04accafe0c35d9d952 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 26 Jan 2025 19:39:53 -0800 Subject: [PATCH 111/119] Undo changes to WebClientCopy --- .../MyServices/Internal/WebClientCopy.vb | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb index aa18bedce88..5ebced7ff68 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/WebClientCopy.vb @@ -186,26 +186,17 @@ Namespace Microsoft.VisualBasic.MyServices.Internal Debug.Assert(address IsNot Nothing, $"No {NameOf(address)}") Debug.Assert((Not String.IsNullOrWhiteSpace(sourceFileName)) _ AndAlso IO.File.Exists(sourceFileName), "Invalid file") - Try - ' If we have a dialog we need to set up an async download - If _progressDialog IsNot Nothing Then - _webClient.UploadFileAsync(address, sourceFileName) - - ' Returns when the download sequence is over, - ' whether due to success, error, or being canceled - _progressDialog.ShowProgressDialog() - Else - _webClient.UploadFile(address, sourceFileName) - End If - Catch ex As WebException - If ex.Message.Contains("401") OrElse ex.Message.Contains("530") Then - Throw New WebException(SR.net_webstatus_Unauthorized) - End If - If ex.Message.Contains("500") Then - Throw - End If - Throw New WebException(SR.net_webstatus_Timeout) - End Try + + ' If we have a dialog we need to set up an async download + If _progressDialog IsNot Nothing Then + _webClient.UploadFileAsync(address, sourceFileName) + + ' Returns when the download sequence is over, + ' whether due to success, error, or being canceled + _progressDialog.ShowProgressDialog() + Else + _webClient.UploadFile(address, sourceFileName) + End If ' Now that we are back on the main thread, throw the exception we ' encountered if the user didn't cancel. From 6a446669bf5b6e25c13af970c9ded1cd28e70a3e Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 26 Jan 2025 19:47:57 -0800 Subject: [PATCH 112/119] Update changes to SingleInstanceHe;perTests to match master --- .../System/Windows/Forms/SingleInstanceHelpersTests.vb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb index b93e5c6ef48..297c174fe58 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/SingleInstanceHelpersTests.vb @@ -61,14 +61,15 @@ Namespace Microsoft.VisualBasic.Forms.Tests Dim awaitable As ConfiguredTaskAwaitable = SendSecondInstanceArgsAsync( pipeName, args:=commandLine, - cancellationToken:=tokenSource.Token).ConfigureAwait(continueOnCapturedContext:=False) + cancellationToken:=tokenSource.Token) _ + .ConfigureAwait(continueOnCapturedContext:=False) awaitable.GetAwaiter().GetResult() Dim CancelToken As New CancellationToken Dim buffer As Byte() = New Byte(commandLine.Length) {} - Dim count As Integer = Await pipeServer.ReadAsync( - buffer:=buffer.AsMemory(start:=0, length:=commandLine.Length)) _ - .ConfigureAwait(continueOnCapturedContext:=True) + Dim count As Integer = + Await pipeServer.ReadAsync(buffer:=buffer.AsMemory(start:=0, length:=commandLine.Length)) _ + .ConfigureAwait(continueOnCapturedContext:=True) ' Ensure the result is set Do From 4674ca343ba02097be734d0a68117f40b937136a Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 26 Jan 2025 19:55:53 -0800 Subject: [PATCH 113/119] Fix a couple of comments --- .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index fcfd6afa251..a5b264c37ac 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -68,7 +68,7 @@ Namespace Microsoft.VisualBasic.Devices showUI As Boolean) As ProgressDialog If InteractiveEnvironment(showUI) Then - 'Construct the local file. This will validate the full name and path + ' Construct the local file. This will validate the full name and path Dim fullFilename As String = FileSystemUtils.NormalizeFilePath( path:=destinationFileName, paramName:=NameOf(destinationFileName)) @@ -95,7 +95,7 @@ Namespace Microsoft.VisualBasic.Devices Try Return New Uri(address) Catch ex As UriFormatException - 'Throw an exception with an error message more appropriate to our API + ' Throw an exception with an error message more appropriate to our API Throw GetArgumentExceptionWithArgName( argumentName:=NameOf(address), resourceKey:=SR.Network_InvalidUriString, From 16a9c620e8846b66b750ee9baa8593721c66cbb4 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 28 Jan 2025 17:42:59 -0800 Subject: [PATCH 114/119] Add test for missing file to be downloaded on server --- .../src/Resources/SR.resx | 59 ++++++++++--------- .../src/Resources/xlf/SR.cs.xlf | 5 ++ .../src/Resources/xlf/SR.de.xlf | 5 ++ .../src/Resources/xlf/SR.es.xlf | 5 ++ .../src/Resources/xlf/SR.fr.xlf | 5 ++ .../src/Resources/xlf/SR.it.xlf | 5 ++ .../src/Resources/xlf/SR.ja.xlf | 5 ++ .../src/Resources/xlf/SR.ko.xlf | 5 ++ .../src/Resources/xlf/SR.pl.xlf | 5 ++ .../src/Resources/xlf/SR.pt-BR.xlf | 5 ++ .../src/Resources/xlf/SR.ru.xlf | 5 ++ .../src/Resources/xlf/SR.tr.xlf | 5 ++ .../src/Resources/xlf/SR.zh-Hans.xlf | 5 ++ .../src/Resources/xlf/SR.zh-Hant.xlf | 5 ++ .../System/Windows/Forms/DownloadFileTests.vb | 20 +++++++ .../Windows/TestUtilities/EnumFileSizes.vb | 1 + .../Windows/TestUtilities/WebListener.vb | 7 ++- 17 files changed, 123 insertions(+), 29 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx index 5979edfcd46..a213f29af4f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/SR.resx @@ -1,17 +1,17 @@  - @@ -222,10 +222,13 @@ Environment variable is not defined: '{0}'. + + The remote server returned an error: (404) Not Found. + The operation has timed out. The remote server returned an error: (401) Unauthorized. - \ No newline at end of file + diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf index 80e59ce0494..a8e991392b5 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.cs.xlf @@ -177,6 +177,11 @@ Ukládání {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf index a4df8e3bc15..1f69d697049 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.de.xlf @@ -177,6 +177,11 @@ {0} wird hochgeladen + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf index da77678109f..593cc3c4fe8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.es.xlf @@ -177,6 +177,11 @@ Cargando {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf index 65862891cc9..28adb2042d7 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.fr.xlf @@ -177,6 +177,11 @@ Chargement de {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf index 6146da0a83e..ff076a8ec82 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.it.xlf @@ -177,6 +177,11 @@ Caricamento di {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf index 51fa58f5bd3..068bcfde92a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ja.xlf @@ -177,6 +177,11 @@ {0} のアップロード中 + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf index d12ca81dbdf..38992a7020a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ko.xlf @@ -177,6 +177,11 @@ {0} 업로드 중 + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf index de1e14e07cf..fa27aa35ef4 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pl.xlf @@ -177,6 +177,11 @@ Przekazywanie: {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf index f63a33f5c00..deffcc04396 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.pt-BR.xlf @@ -177,6 +177,11 @@ Carregando {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf index c544a9c4e95..f06353c3d18 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.ru.xlf @@ -177,6 +177,11 @@ Идет отправка {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf index ee197bd39d1..d8b7045e74a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.tr.xlf @@ -177,6 +177,11 @@ {0} karşıya yükleniyor + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf index 30e4a236f02..ff6d043271e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hans.xlf @@ -177,6 +177,11 @@ 正在上传 {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf index 2e1e83c4a7c..0d405fb9a8c 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf +++ b/src/Microsoft.VisualBasic.Forms/src/Resources/xlf/SR.zh-Hant.xlf @@ -177,6 +177,11 @@ 正在上傳 {0} + + The remote server returned an error: (404) Not Found. + The remote server returned an error: (404) Not Found. + + The operation has timed out. The operation has timed out. diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb index 6a967f9442e..fc27bc9d98a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/DownloadFileTests.vb @@ -1015,6 +1015,26 @@ Namespace Microsoft.VisualBasic.Forms.Tests End Using End Sub + + Public Sub DownloadFile_UrlOnlyFileDoesNotExist_Fail() + Dim testDirectory As String = CreateTempDirectory() + Dim destinationFileName As String = GetUniqueFileNameWithPath(testDirectory) + Dim webListener As New WebListener(FileSizes.FileSize0Bytes) + Using listener As HttpListener = webListener.ProcessRequests() + Dim testCode As Action = + Sub() + My.Computer.Network.DownloadFile( + address:=webListener.Address, + destinationFileName) + End Sub + + testCode.Should. + Throw(Of WebException)(). + Where(Function(e) e.Message.Equals(SR.net_webstatus_NotFound)) + VerifyFailedDownload(testDirectory, destinationFileName, listener) + End Using + End Sub + Public Sub DownloadFile_UrlOnlyWhereAddressInvalid_Throws(address As String) diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb index 0d864d545a0..d0d615d0185 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/EnumFileSizes.vb @@ -5,6 +5,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Friend Module EnumFileSizes Friend Enum FileSizes As Integer Unknown = -1 + FileSize0Bytes = 0 ' The file does not exist FileSize1Byte = 1 FileSize1MB = 1_048_576 FileSize100MB = 104_857_600 diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb index 0ac369608fd..9d6da3e410a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/WebListener.vb @@ -27,7 +27,6 @@ Namespace Microsoft.VisualBasic.Forms.Tests ''' Is used to create the file name and the size of download. ''' Used to establish the file path to be downloaded. Public Sub New(fileSize As Integer, Optional supportAnonymousLogin As Boolean = True, Optional memberName As String = Nothing) - Debug.Assert(fileSize > 0) _fileName = $"{[Enum].GetName(GetType(FileSizes), fileSize)}.zip".Replace("FileSize", "") _fileSize = fileSize _serverConfigurationInstance = ServerConfigurationLoad() @@ -196,6 +195,7 @@ Namespace Microsoft.VisualBasic.Forms.Tests Exit Try End If End If + ' Simulate network traffic Thread.Sleep(millisecondsTimeout:=20) If _upload Then @@ -229,6 +229,11 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If response.StatusCode = 200 Else + If _fileSize = 0 Then + response.StatusCode = HttpStatusCode.NotFound + Exit Try + End If + Dim responseString As String = Strings.StrDup(_fileSize, "A") Dim buffer() As Byte = Text.Encoding.UTF8.GetBytes(responseString) response.ContentLength64 = buffer.Length From ff4ce617f636046f96109ab6636a5541ef273ee0 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 28 Jan 2025 18:35:02 -0800 Subject: [PATCH 115/119] Handle File not found on server --- .../VisualBasic/MyServices/Internal/HttpClientCopy.vb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index f2cfe043a11..0c5970f3868 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -174,7 +174,8 @@ Namespace Microsoft.VisualBasic.MyServices.Internal RemoveHandler _progressDialog.UserHitCancel, AddressOf _progressDialog_UserHitCancel End If End If - + Case HttpStatusCode.NotFound + Throw New WebException(SR.net_webstatus_NotFound) Case HttpStatusCode.Unauthorized Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) Case Else From 65f863849ddeb242da15bf073529a0d4a6413c6a Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 29 Jan 2025 14:12:32 -0800 Subject: [PATCH 116/119] Improve HttpResponse handling --- .../VisualBasic/MyServices/Internal/HttpClientCopy.vb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index 0c5970f3868..3ed85089c53 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -176,8 +176,10 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End If Case HttpStatusCode.NotFound Throw New WebException(SR.net_webstatus_NotFound) - Case HttpStatusCode.Unauthorized + Case HttpStatusCode.Unauthorized, HttpStatusCode.NetworkAuthenticationRequired Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case HttpStatusCode.RequestTimeout + Throw New WebException(SR.net_webstatus_Timeout) Case Else Throw New WebException() End Select @@ -228,8 +230,10 @@ Namespace Microsoft.VisualBasic.MyServices.Internal response.EnsureSuccessStatusCode() Select Case response.StatusCode Case HttpStatusCode.OK - Case HttpStatusCode.Unauthorized + Case HttpStatusCode.Unauthorized, HttpStatusCode.NetworkAuthenticationRequired Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case HttpStatusCode.RequestTimeout + Throw New WebException(SR.net_webstatus_Timeout) Case Else Throw New WebException() End Select From 513fe713db5bd2ae9ca343a0b6336027329d0741 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 30 Jan 2025 16:58:02 -0800 Subject: [PATCH 117/119] Handle more HttpStatusCode(s) --- .../MyServices/Internal/HttpClientCopy.vb | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb index 3ed85089c53..4095b707292 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/HttpClientCopy.vb @@ -176,8 +176,15 @@ Namespace Microsoft.VisualBasic.MyServices.Internal End If Case HttpStatusCode.NotFound Throw New WebException(SR.net_webstatus_NotFound) - Case HttpStatusCode.Unauthorized, HttpStatusCode.NetworkAuthenticationRequired - Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case HttpStatusCode.Found, + HttpStatusCode.Moved, + HttpStatusCode.NotModified, + HttpStatusCode.PermanentRedirect + Throw New WebException() + Case HttpStatusCode.Forbidden, + HttpStatusCode.NetworkAuthenticationRequired, + HttpStatusCode.Unauthorized + Throw New WebException(SR.net_webstatus_Unauthorized) Case HttpStatusCode.RequestTimeout Throw New WebException(SR.net_webstatus_Timeout) Case Else @@ -230,8 +237,15 @@ Namespace Microsoft.VisualBasic.MyServices.Internal response.EnsureSuccessStatusCode() Select Case response.StatusCode Case HttpStatusCode.OK - Case HttpStatusCode.Unauthorized, HttpStatusCode.NetworkAuthenticationRequired - Throw New WebException(SR.net_webstatus_Unauthorized, WebExceptionStatus.ProtocolError) + Case HttpStatusCode.Found, + HttpStatusCode.Moved, + HttpStatusCode.NotModified, + HttpStatusCode.PermanentRedirect + Throw New WebException() + Case HttpStatusCode.Forbidden, + HttpStatusCode.NetworkAuthenticationRequired, + HttpStatusCode.Unauthorized + Throw New WebException(SR.net_webstatus_Unauthorized) Case HttpStatusCode.RequestTimeout Throw New WebException(SR.net_webstatus_Timeout) Case Else From 435a8fb7b8ea741a81f8c7c6e50820df0d421414 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 2 Mar 2025 03:01:32 -0800 Subject: [PATCH 118/119] Remove unneccessary imports --- .../WindowsFormsApplicationBase.WinFormsAppContext.vb | 1 - .../src/Microsoft/VisualBasic/Devices/Keyboard.vb | 1 - .../src/Microsoft/VisualBasic/Devices/Mouse.vb | 2 -- .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 1 - .../src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 1 - .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 2 -- .../src/Microsoft/VisualBasic/Helpers/VBInputBox.vb | 1 - .../src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb | 2 -- .../src/Microsoft/VisualBasic/Interaction.vb | 1 - .../src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb | 1 - .../src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb | 1 - .../Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb | 1 - .../tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb | 1 - .../System/Windows/Forms/CompilerServices.TestVbHost.vb | 1 - .../UnitTests/System/Windows/Forms/CompilerServicesTests.vb | 2 -- .../tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb | 1 - .../UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb | 1 - .../tests/UnitTests/System/Windows/Forms/InteractionTests.vb | 2 -- 18 files changed, 23 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb index d395e93e04e..8367a69f642 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb @@ -2,7 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Security -Imports System.Windows.Forms Namespace Microsoft.VisualBasic.ApplicationServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb index 14773681229..d5466a1b1f2 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb @@ -1,7 +1,6 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Namespace Microsoft.VisualBasic.Devices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb index 4a4b36d7635..2cbdb994715 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb @@ -1,8 +1,6 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Windows.Forms - Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils Namespace Microsoft.VisualBasic.Devices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 227f14a42d9..0ad0cd6d28b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -3,7 +3,6 @@ Imports System.Net Imports System.Threading -Imports System.Windows.Forms Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index dcf939a4809..0cf414903a1 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -4,7 +4,6 @@ Imports System.Net Imports System.Net.Http Imports System.Threading -Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index a5b264c37ac..856fd94ddaf 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -2,8 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net -Imports System.Windows.Forms -Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb index 0f91f6a9029..63168b1d557 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb @@ -4,7 +4,6 @@ Imports System.ComponentModel Imports System.Drawing Imports System.Globalization -Imports System.Windows.Forms Namespace Microsoft.VisualBasic.CompilerServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb index ade7075bddc..517288d160a 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb @@ -1,8 +1,6 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Windows.Forms - Namespace Microsoft.VisualBasic Partial Friend Module _Interaction diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb index 14004e09e8b..b52f5f9795d 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb @@ -5,7 +5,6 @@ Imports System.Runtime.InteropServices Imports System.Security Imports System.Text Imports System.Threading -Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports NativeMethods = Microsoft.VisualBasic.CompilerServices.NativeMethods diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb index fe06c7aa46a..42218e45b8f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb @@ -5,7 +5,6 @@ Imports System.ComponentModel Imports System.Globalization Imports System.IO Imports System.Text -Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index d3a5135af94..4ab8e2e77b5 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -5,7 +5,6 @@ Imports System.Collections.Specialized Imports System.ComponentModel Imports System.Drawing Imports System.IO -Imports System.Windows.Forms Imports System.Reflection.Metadata Imports System.Runtime.InteropServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb index 80650ca3691..1244dc4d0b8 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb @@ -5,7 +5,6 @@ Imports System.ComponentModel Imports System.Drawing Imports System.Globalization Imports System.Threading -Imports System.Windows.Forms Namespace Microsoft.VisualBasic.MyServices.Internal diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb index b2ae13602f0..3e858f9a102 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb @@ -3,7 +3,6 @@ Imports System.Collections.Specialized Imports System.IO -Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb index d0d072970d2..268dd1de218 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb @@ -1,7 +1,6 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Namespace Microsoft.VisualBasic.Forms.Tests diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb index 842d2aaf6ed..c7a411d57dd 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb @@ -2,8 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Option Strict Off - -Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb index 7f4e61de8fa..c9e86a65a1a 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb @@ -1,7 +1,6 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. -Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb index c1e12773b9d..a4f149bfdc3 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb @@ -5,7 +5,6 @@ Option Strict Off Imports System.IO Imports System.Text -Imports System.Windows.Forms Imports FluentAssertions Imports Microsoft.VisualBasic.Logging Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb index de95203d6cc..91946d38b1e 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb @@ -2,8 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Option Strict Off - -Imports System.Windows.Forms Imports FluentAssertions Imports Xunit From ebb4b833d3d5040122a51bc7fff4c64c63505ec2 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 13 Mar 2025 14:54:17 -1000 Subject: [PATCH 119/119] Fix merge issues --- .../WindowsFormsApplicationBase.WinFormsAppContext.vb | 1 + .../src/Microsoft/VisualBasic/Devices/Keyboard.vb | 1 + .../src/Microsoft/VisualBasic/Devices/Mouse.vb | 2 ++ .../src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb | 1 + .../src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb | 1 + .../src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb | 2 ++ .../src/Microsoft/VisualBasic/Helpers/VBInputBox.vb | 1 + .../src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb | 2 ++ .../src/Microsoft/VisualBasic/Interaction.vb | 1 + .../src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb | 1 + .../src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb | 1 + .../Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb | 1 + .../tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb | 1 + .../System/Windows/Forms/CompilerServices.TestVbHost.vb | 1 + .../UnitTests/System/Windows/Forms/CompilerServicesTests.vb | 2 ++ .../tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb | 1 + .../UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb | 1 + .../tests/UnitTests/System/Windows/Forms/InteractionTests.vb | 1 + .../ImplementITypedDataObjectInAdditionToIDataObjectAnalyzer.vb | 2 -- 19 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb index 8367a69f642..d395e93e04e 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.WinFormsAppContext.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Security +Imports System.Windows.Forms Namespace Microsoft.VisualBasic.ApplicationServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb index d5466a1b1f2..14773681229 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Keyboard.vb @@ -1,6 +1,7 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Namespace Microsoft.VisualBasic.Devices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb index 2cbdb994715..4a4b36d7635 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Mouse.vb @@ -1,6 +1,8 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.Windows.Forms + Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils Namespace Microsoft.VisualBasic.Devices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb index 0ad0cd6d28b..2290d1d9b62 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.DownloadFile.vb @@ -5,6 +5,7 @@ Imports System.Net Imports System.Threading Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal +Imports System.Windows.Forms Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb index 0cf414903a1..dcf939a4809 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/Network.UploadFile.vb @@ -4,6 +4,7 @@ Imports System.Net Imports System.Net.Http Imports System.Threading +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.FileIO Imports Microsoft.VisualBasic.MyServices.Internal diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb index 856fd94ddaf..a5b264c37ac 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Devices/NetworkUtilities.vb @@ -2,6 +2,8 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Net +Imports System.Windows.Forms +Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.MyServices.Internal Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb index 63168b1d557..0f91f6a9029 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Helpers/VBInputBox.vb @@ -4,6 +4,7 @@ Imports System.ComponentModel Imports System.Drawing Imports System.Globalization +Imports System.Windows.Forms Namespace Microsoft.VisualBasic.CompilerServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb index 517288d160a..ade7075bddc 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.InputBoxHandler.vb @@ -1,6 +1,8 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.Windows.Forms + Namespace Microsoft.VisualBasic Partial Friend Module _Interaction diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb index b52f5f9795d..14004e09e8b 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Interaction.vb @@ -5,6 +5,7 @@ Imports System.Runtime.InteropServices Imports System.Security Imports System.Text Imports System.Threading +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports NativeMethods = Microsoft.VisualBasic.CompilerServices.NativeMethods diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb index 81e4978b59f..a5496d632c3 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/Logging/FileLogTraceListener.vb @@ -5,6 +5,7 @@ Imports System.ComponentModel Imports System.Globalization Imports System.IO Imports System.Text +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb index 4ab8e2e77b5..d3a5135af94 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/ClipboardProxy.vb @@ -5,6 +5,7 @@ Imports System.Collections.Specialized Imports System.ComponentModel Imports System.Drawing Imports System.IO +Imports System.Windows.Forms Imports System.Reflection.Metadata Imports System.Runtime.InteropServices diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb index 1244dc4d0b8..80650ca3691 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/Internal/ProgressDialog.vb @@ -5,6 +5,7 @@ Imports System.ComponentModel Imports System.Drawing Imports System.Globalization Imports System.Threading +Imports System.Windows.Forms Namespace Microsoft.VisualBasic.MyServices.Internal diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb index 3e858f9a102..b2ae13602f0 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ClipboardProxyTests.vb @@ -3,6 +3,7 @@ Imports System.Collections.Specialized Imports System.IO +Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb index 268dd1de218..d0d072970d2 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServices.TestVbHost.vb @@ -1,6 +1,7 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.Windows.Forms Imports Microsoft.VisualBasic.CompilerServices Namespace Microsoft.VisualBasic.Forms.Tests diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb index c7a411d57dd..842d2aaf6ed 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/CompilerServicesTests.vb @@ -2,6 +2,8 @@ ' The .NET Foundation licenses this file to you under the MIT license. Option Strict Off + +Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb index c9e86a65a1a..7f4e61de8fa 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/ControlInvokeTests.vb @@ -1,6 +1,7 @@ ' Licensed to the .NET Foundation under one or more agreements. ' The .NET Foundation licenses this file to you under the MIT license. +Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb index 8034ee10d33..07401583f68 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/FileLogTraceListenerTests.vb @@ -5,6 +5,7 @@ Option Strict Off Imports System.IO Imports System.Text +Imports System.Windows.Forms Imports FluentAssertions Imports Microsoft.VisualBasic.Logging Imports Xunit diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb index 91946d38b1e..a6a59d61d42 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/InteractionTests.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Option Strict Off +Imports System.Windows.Forms Imports FluentAssertions Imports Xunit diff --git a/src/System.Windows.Forms.Analyzers.VisualBasic/src/Analyzers/ImplementITypedDataObject/ImplementITypedDataObjectInAdditionToIDataObjectAnalyzer.vb b/src/System.Windows.Forms.Analyzers.VisualBasic/src/Analyzers/ImplementITypedDataObject/ImplementITypedDataObjectInAdditionToIDataObjectAnalyzer.vb index 2206c25609b..8dd36d405ce 100644 --- a/src/System.Windows.Forms.Analyzers.VisualBasic/src/Analyzers/ImplementITypedDataObject/ImplementITypedDataObjectInAdditionToIDataObjectAnalyzer.vb +++ b/src/System.Windows.Forms.Analyzers.VisualBasic/src/Analyzers/ImplementITypedDataObject/ImplementITypedDataObjectInAdditionToIDataObjectAnalyzer.vb @@ -2,8 +2,6 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System.Collections.Immutable -Imports System.Runtime.InteropServices.ComTypes -Imports System.Windows.Forms.Analyzers Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.VisualBasic