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

Code before:

    It "Invoke-WebRequest returns headers dictionary" {
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri '$uri'"

        $result = ExecuteWebCommand -command $command
        ValidateResponse -response $result

        # Validate response content
        $jsonContent = $result.Output.Content | ConvertFrom-Json
        $jsonContent.headers.Host | Should -Be $Uri.Authority
    }

    It "Invoke-WebRequest with blank ContentType succeeds" {
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri '$uri' -ContentType ''"

        $result = ExecuteWebCommand -command $command

        # Validate response
        ValidateResponse -response $result

        $result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty
    }

    It "Invoke-WebRequest -Debug shows request information" {
        $uri = Get-WebListenerUrl -Test 'Post'
        $testBody = 'TestData'
        $debugFile = Join-Path $TestDrive -ChildPath WebCmdlet-$(New-Guid).debug.txt

        Invoke-WebRequest -Uri $uri -Method Post -Body $testBody -Debug -DebugRequestContent 5>$debugFile
        $debugOutput = Get-Content $debugFile
        $debugOutput[0] | Should -Match "Method: POST, RequestUri: '$($uri)'"
    }

    It "Invoke-WebRequest -DebugRequestContent shows request content for text data" {
        $uri = Get-WebListenerUrl -Test 'Post'
        $testBody = 'TestData'
        $debugFile = Join-Path $TestDrive -ChildPath WebCmdlet-$(New-Guid).debug.txt

        Invoke-WebRequest -Uri $uri -Method Post -Body $testBody -Debug -DebugRequestContent 5>$debugFile
        $debugOutput = Get-Content $debugFile
        $debugOutput[8]  | Should -Match "=== REQUEST CONTENT BEGIN ==="
        $debugOutput[9]  | Should -Match $testBody
        $debugOutput[10] | Should -Match "=== REQUEST CONTENT END ==="
    }

    It "Validate Invoke-WebRequest -DisableKeepAlive" {
        # Operation options
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri $uri -DisableKeepAlive"

Review comment (bug):
A debug output is not public contract. So we have no need to add tests. We will check the PR manually.
(We already have generic test that debug output works.)

Code after:

    It "Invoke-WebRequest returns headers dictionary" {
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri '$uri'"

        $result = ExecuteWebCommand -command $command
        ValidateResponse -response $result

        # Validate response content
        $jsonContent = $result.Output.Content | ConvertFrom-Json
        $jsonContent.headers.Host | Should -Be $Uri.Authority
    }

    It "Invoke-WebRequest with blank ContentType succeeds" {
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri '$uri' -ContentType ''"

        $result = ExecuteWebCommand -command $command

        # Validate response
        ValidateResponse -response $result

        $result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty
    }

    It "Validate Invoke-WebRequest -DisableKeepAlive" {
        # Operation options
        $uri = Get-WebListenerUrl -Test 'Get'
        $command = "Invoke-WebRequest -Uri $uri -DisableKeepAlive"

        $result = ExecuteWebCommand -command $command
        ValidateResponse -response $result

        $result.Output.Headers.Connection | Should -Be "Close"
    }

    It "Validate Invoke-WebRequest -HttpVersion '<httpVersion>'" -Skip:(!$IsWindows) -TestCases @(
        @{ httpVersion = '1.1'},
        @{ httpVersion = '2'}
    ) {
        param($httpVersion)

        if(Test-IsWinServer2012R2 -and $httpVersion -eq '2') {
            Set-ItResult -Skipped -Because "HTTP/2 is not supported on Windows Server 2012R2"
            return
        }

        # Operation options
        $uri = Get-WebListenerUrl -Test 'Get' -Https
        $command = "Invoke-WebRequest -Uri $uri -HttpVersion $httpVersion -SkipCertificateCheck"