Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ private static StreamContent GetMultipartStreamContent(object fieldName, Stream
/// <param name="file">The file to use for the <see cref="StreamContent"/></param>
private static StreamContent GetMultipartFileContent(object fieldName, FileInfo file)
{
StreamContent result = GetMultipartStreamContent(fieldName: fieldName, stream: new FileStream(file.FullName, FileMode.Open));
StreamContent result = GetMultipartStreamContent(fieldName: fieldName, stream: new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read));

result.Headers.ContentDisposition.FileName = file.Name;
result.Headers.ContentDisposition.FileNameStar = file.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,26 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Files[0].Content | Should -Match $file1Contents
}

It "Verifies Invoke-WebRequest -Form supports read-only file values" {
# Create a read-only test file
$readOnlyFile = Join-Path $TestDrive "readonly-test.txt"
"ReadOnly test content" | Out-File -FilePath $readOnlyFile -Encoding utf8
Set-ItemProperty -Path $readOnlyFile -Name IsReadOnly -Value $true

$form = @{TestFile = [System.IO.FileInfo]$readOnlyFile}
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response.Content | ConvertFrom-Json

$result.Headers.'Content-Type' | Should -Match 'multipart/form-data'
$result.Files.Count | Should -Be 1

$result.Files[0].Name | Should -BeExactly "TestFile"
$result.Files[0].FileName | Should -BeExactly "readonly-test.txt"
$result.Files[0].ContentType | Should -BeExactly 'application/octet-stream'
$result.Files[0].Content | Should -Match "ReadOnly test content"
}

It "Verifies Invoke-WebRequest -Form sets Content-Disposition FileName and FileNameStar." {
$ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("attachment")
$ContentDisposition.FileName = $fileName
Expand Down Expand Up @@ -3568,6 +3588,25 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Files[0].Content | Should -Match $file1Contents
}

It "Verifies Invoke-RestMethod -Form supports read-only file values" {
# Create a read-only test file
$readOnlyFile = Join-Path $TestDrive "readonly-test.txt"
"ReadOnly test content" | Out-File -FilePath $readOnlyFile -Encoding utf8
Set-ItemProperty -Path $readOnlyFile -Name IsReadOnly -Value $true

$form = @{TestFile = [System.IO.FileInfo]$readOnlyFile}
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'

$result.Headers.'Content-Type' | Should -Match 'multipart/form-data'
$result.Files.Count | Should -Be 1

$result.Files[0].Name | Should -Be "TestFile"
$result.Files[0].FileName | Should -Be "readonly-test.txt"
$result.Files[0].ContentType | Should -Be 'application/octet-stream'
$result.Files[0].Content | Should -Match "ReadOnly test content"
}

It "Verifies Invoke-RestMethod -Form sets Content-Disposition FileName and FileNameStar." {
$ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("attachment")
$ContentDisposition.FileName = $fileName
Expand Down
Loading