From 1cd54bf21770c8e52a06102c4be8868748f71e31 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 14 Nov 2024 10:16:45 -0900 Subject: [PATCH 01/67] 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 17829151d281219e0c36e8ffcc713b5f76f081a5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 24 Nov 2024 15:23:16 -0800 Subject: [PATCH 02/67] 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 2d9209641f3962b8c885ebd24b9cde5a5df61c37 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 14:47:38 -0800 Subject: [PATCH 03/67] 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 4324b8b934dc8109f372ed7d86d47c3e3b9850ed Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 26 Nov 2024 15:09:59 -0800 Subject: [PATCH 04/67] 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 ebdc88d9e2df4f899d49f7a5519ce11edebb969e Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 15:56:46 -0800 Subject: [PATCH 05/67] 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 06/67] 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 175440ad3bb3eb0c8acd282017d558638dedd350 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 19:06:45 -0800 Subject: [PATCH 07/67] 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 f1c6e6501c88cb26ac01a388ae63ff8394550ecc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 27 Nov 2024 22:14:31 -0800 Subject: [PATCH 08/67] 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 fbd2c7cd477bac0b6ab2c6c66c69704308f61c1f Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 28 Nov 2024 15:15:53 -0800 Subject: [PATCH 09/67] 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 10/67] 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 11/67] 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 12/67] 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 7324bb1c945deb2f28306112aafd55cf027cfde2 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 12:27:19 -0800 Subject: [PATCH 13/67] 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 14/67] 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 f3cc2d00ec54d438f6954f69a9cd4af2138b64c0 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:08:01 -0800 Subject: [PATCH 15/67] 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 cdab3aab6736118a3307a23dea6a81978b835bbc Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 17 Dec 2024 13:11:46 -0800 Subject: [PATCH 16/67] 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 47f12cd3e84a26bd4eb7c706c0802f47b2bf5933 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 00:58:33 -0800 Subject: [PATCH 17/67] 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 18/67] 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 21106085ed49199652841d809ea2205e80139832 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 03:54:11 -0800 Subject: [PATCH 19/67] 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 20/67] 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 21/67] 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 22/67] 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 a7b20cc579fe14eb7c310d8c700f5cae4a00c7c8 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 17:56:06 -0800 Subject: [PATCH 23/67] 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 c633809bff424f6174eca8ad90e240b399a7866f Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 18:20:55 -0800 Subject: [PATCH 24/67] 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 4f9d051f7b2116259083cd3757dd1f42e625702a Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 19 Dec 2024 19:05:00 -0800 Subject: [PATCH 25/67] 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 354ccdf686dd1894cb00d15644dcf4e3268a1d11 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Fri, 20 Dec 2024 02:42:17 -0800 Subject: [PATCH 26/67] 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 27/67] 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 28/67] 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 29/67] 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 30/67] 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 31/67] 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 32/67] 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 8449f48244f8dc05007e2fa40b3fc0dba15f17f3 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:36:07 -0800 Subject: [PATCH 33/67] 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 6e3b58e8a4812c176f802cc92a6fceef07006dc5 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sun, 22 Dec 2024 20:51:11 -0800 Subject: [PATCH 34/67] 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 ac82c9f9bf37885f56d5ab5606bebd8666108795 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Tue, 7 Jan 2025 23:40:27 -0800 Subject: [PATCH 35/67] 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 36/67] 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 6509c30b29509965809e44d1ee2797ee57f65126 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 8 Jan 2025 19:00:25 -0800 Subject: [PATCH 37/67] 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 38/67] 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 39/67] 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 253b185cc6c2d8c7d24a535a2f5b97a777a4d6bf Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Sat, 11 Jan 2025 23:03:05 -0800 Subject: [PATCH 40/67] 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 41/67] 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 42/67] 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 43/67] 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 44/67] 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 45/67] 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 46/67] 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 47/67] 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 48/67] 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 49/67] 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 50/67] 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 51/67] 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 52/67] 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 bbd01fe3c0afcfc3132e52a4a5245dfac78df3aa Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Wed, 15 Jan 2025 18:17:03 -0800 Subject: [PATCH 53/67] 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 54/67] 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 55/67] 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 56/67] 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 57/67] 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 79c12e5476e14918aeca15751725e9b2f31d5303 Mon Sep 17 00:00:00 2001 From: Paul M Cohen Date: Thu, 23 Jan 2025 19:47:17 -0800 Subject: [PATCH 58/67] 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 59/67] 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 @@ - + + @@ -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