Repository: PowerShell/PowerShell
File: test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 (PowerShell)

Code before:
        [string]
        $Uri,

        [switch]
        $UseBasicParsing
    )
    $result = @{Output = $null; Error = $null; Encoding = $null; Content = $null}
    $debugPreferenceSave = $DebugPreference
    $DebugPreference = 'Continue'
    try {
        $debugFile = Join-Path $TestDrive -ChildPath ExecuteRestMethod.debug.txt
        $result.Output = Invoke-RestMethod -Uri $Uri -UseBasicParsing:$UseBasicParsing.IsPresent -Debug 5>$debugFile
        $result.Content = $result.Output

        if (Test-Path -Path $debugFile) {
            $result.Debug = Get-Content -Path $debugFile
            foreach ($item in $result.Debug) {
                $line = $item.Trim()
                if ($line.StartsWith($debugEncodingPrefix)) {
                    $encodingName = $item.SubString($EncodingPrefix.Length).Split('(')[0].Trim()
                    $result.Encoding = [System.Text.Encoding]::GetEncoding($encodingName)
                    break
                }
            }
            if ($null -eq $result.Encoding) {
                throw "Encoding not found in verbose output. Lines: $($result.Debug.Count) Content:$($result.Debug)"
            }
        }

        if ($null -eq $result.Debug) {
            throw "No debug output was found"
        }
    } catch {
        $result.Error = $_ | Select-Object * | Out-String
    } finally {
        $DebugPreference = $debugPreferenceSave
        if (Test-Path -Path $debugFile) {
            Remove-Item -Path $debugFile -ErrorAction SilentlyContinue
        }
    }

    return $result
}

function GetMultipartBody {
    param (
        [Switch]
        $String,

        [Switch]
        $File

Review comment (bug):
Now it is debug output.
```suggestion
                throw "Encoding not found in debug output. Lines: $($result.Debug.Count) Content:$($result.Debug)"
```

Code after:
        [string]
        $Uri,

        [switch]
        $UseBasicParsing
    )
    $result = @{Output = $null; Error = $null; Encoding = $null; Content = $null}
    $debugPreferenceSave = $DebugPreference
    $DebugPreference = 'Continue'
    try {
        $debugFile = Join-Path $TestDrive -ChildPath ExecuteRestMethod.debug.txt
        $result.Output = Invoke-RestMethod -Uri $Uri -UseBasicParsing:$UseBasicParsing.IsPresent 5>$debugFile
        $result.Content = $result.Output

        # Invoke-RestMethod does not return encoding as part of an object, only in debug output, so we parse the debug output for the purposes of verifying the test.
        # This debug output is defined in InvokeRestMethodCommand.Common.cs ProcessResponse()
        if (Test-Path -Path $debugFile) {
            $result.Debug = Get-Content -Path $debugFile
            foreach ($item in $result.Debug) {
                $line = $item.Trim()
                if ($line.StartsWith($debugEncodingPrefix)) {
                    $encodingName = [int]::Parse($item.SubString($EncodingPrefix.Length).Split('CodePage: ')[1].Trim())
                    $result.Encoding = [System.Text.Encoding]::GetEncoding($encodingName)
                    break
                }
            }
            if ($null -eq $result.Encoding) {
                throw "Encoding not found in debug output. Lines: $($result.Debug.Count) Content:$($result.Debug)"
            }
        }

        if ($null -eq $result.Debug) {
            throw "No debug output was found"
        }
    } catch {
        $result.Error = $_ | Select-Object * | Out-String
    } finally {
        $DebugPreference = $debugPreferenceSave
        if (Test-Path -Path $debugFile) {
            Remove-Item -Path $debugFile -ErrorAction SilentlyContinue
        }
    }

    return $result
}

function GetMultipartBody {
    param (
        [Switch]
        $String,