Skip to content

Commit c8ccb37

Browse files
markekrausiSazonov
authored andcommitted
[Feature] Replace HttpListener Echo Tests with WebListener (#5840)
1 parent 211ee63 commit c8ccb37

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -807,55 +807,61 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
807807
#region SkipHeaderVerification Tests
808808

809809
It "Verifies Invoke-WebRequest default header handling with no errors" {
810+
$uri = Get-WebListenerUrl -Test 'Get'
810811
$headers = @{"If-Match" = "*"}
811-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8080/PowerShell?test=echo" -headers $headers
812+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers
812813

813814
$response.Error | Should BeNullOrEmpty
814-
$response.Content.Headers -contains "If-Match" | Should Be $true
815+
$response.Content.Headers."If-Match" | Should BeExactly "*"
815816
}
816817

817818
It "Verifies Invoke-WebRequest default header handling reports an error is returned for an invalid If-Match header value" {
819+
$uri = Get-WebListenerUrl -Test 'Get'
818820
$headers = @{"If-Match" = "12345"}
819-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8080/PowerShell?test=echo" -headers $headers
821+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers
820822

821823
$response.Error | Should Not BeNullOrEmpty
822824
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
823825
$response.Error.Exception.Message | Should Be "The format of value '12345' is invalid."
824826
}
825827

826828
It "Verifies Invoke-WebRequest header handling does not report an error when using -SkipHeaderValidation" {
829+
$uri = Get-WebListenerUrl -Test 'Get'
827830
$headers = @{"If-Match" = "12345"}
828-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8080/PowerShell?test=echo" -headers $headers -SkipHeaderValidation
831+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -SkipHeaderValidation
829832

830833
$response.Error | Should BeNullOrEmpty
831-
$response.Content.Headers -contains "If-Match" | Should Be $true
834+
$response.Content.Headers."If-Match" | Should BeExactly "12345"
832835
}
833836

834837
It "Verifies Invoke-WebRequest default UserAgent handling with no errors" {
838+
$uri = Get-WebListenerUrl -Test 'Get'
835839
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer
836-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8080/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
840+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
837841

838842
$response.Error | Should BeNullOrEmpty
839843
$Pattern = [regex]::Escape($UserAgent)
840-
$response.Content.UserAgent | Should Match $Pattern
844+
$response.Content.Headers."User-Agent" | Should Match $Pattern
841845
}
842846

843847
It "Verifies Invoke-WebRequest default UserAgent handling reports an error is returned for an invalid UserAgent value" {
848+
$uri = Get-WebListenerUrl -Test 'Get'
844849
$UserAgent = 'Invalid:Agent'
845-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8080/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
850+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
846851

847852
$response.Error | Should Not BeNullOrEmpty
848853
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
849854
$response.Error.Exception.Message | Should Be "The format of value 'Invalid:Agent' is invalid."
850855
}
851856

852857
It "Verifies Invoke-WebRequest UserAgent handling does not report an error when using -SkipHeaderValidation" {
858+
$uri = Get-WebListenerUrl -Test 'Get'
853859
$UserAgent = 'Invalid:Agent'
854-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8080/PowerShell?test=echo" -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-WebRequest"
860+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-WebRequest"
855861

856862
$response.Error | Should BeNullOrEmpty
857863
$Pattern = [regex]::Escape($UserAgent)
858-
$response.Content.UserAgent | Should Match $Pattern
864+
$response.Content.Headers."User-Agent" | Should Match $Pattern
859865
}
860866

861867
#endregion SkipHeaderVerification Tests
@@ -1802,55 +1808,61 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
18021808
#region SkipHeaderVerification tests
18031809

18041810
It "Verifies Invoke-RestMethod default header handling with no errors" {
1811+
$uri = Get-WebListenerUrl -Test 'Get'
18051812
$headers = @{"If-Match" = "*"}
1806-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8081/PowerShell?test=echo" -headers $headers -Cmdlet "Invoke-RestMethod"
1813+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -Cmdlet "Invoke-RestMethod"
18071814

18081815
$response.Error | Should BeNullOrEmpty
1809-
$response.Content.Headers -contains "If-Match" | Should Be $true
1816+
$response.Content.Headers."If-Match" | Should BeExactly "*"
18101817
}
18111818

18121819
It "Verifies Invoke-RestMethod default header handling reports an error is returned for an invalid If-Match header value" {
1820+
$uri = Get-WebListenerUrl -Test 'Get'
18131821
$headers = @{"If-Match" = "12345"}
1814-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8081/PowerShell?test=echo" -headers $headers -Cmdlet "Invoke-RestMethod"
1822+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -Cmdlet "Invoke-RestMethod"
18151823

18161824
$response.Error | Should Not BeNullOrEmpty
18171825
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
18181826
$response.Error.Exception.Message | Should Be "The format of value '12345' is invalid."
18191827
}
18201828

18211829
It "Verifies Invoke-RestMethod header handling does not report an error when using -SkipHeaderValidation" {
1830+
$uri = Get-WebListenerUrl -Test 'Get'
18221831
$headers = @{"If-Match" = "12345"}
1823-
$response = ExecuteRequestWithCustomHeaders -Uri "http://localhost:8081/PowerShell?test=echo" -headers $headers -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
1832+
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
18241833

18251834
$response.Error | Should BeNullOrEmpty
1826-
$response.Content.Headers -contains "If-Match" | Should Be $true
1835+
$response.Content.Headers."If-Match" | Should BeExactly "12345"
18271836
}
18281837

18291838
It "Verifies Invoke-RestMethod default UserAgent handling with no errors" {
1839+
$uri = Get-WebListenerUrl -Test 'Get'
18301840
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer
1831-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
1841+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
18321842

18331843
$response.Error | Should BeNullOrEmpty
18341844
$Pattern = [regex]::Escape($UserAgent)
1835-
$response.Content.UserAgent | Should Match $Pattern
1845+
$response.Content.Headers."User-Agent" | Should Match $Pattern
18361846
}
18371847

18381848
It "Verifies Invoke-RestMethod default UserAgent handling reports an error is returned for an invalid UserAgent value" {
1849+
$uri = Get-WebListenerUrl -Test 'Get'
18391850
$UserAgent = 'Invalid:Agent'
1840-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
1851+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
18411852

18421853
$response.Error | Should Not BeNullOrEmpty
18431854
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
18441855
$response.Error.Exception.Message | Should Be "The format of value 'Invalid:Agent' is invalid."
18451856
}
18461857

18471858
It "Verifies Invoke-RestMethod UserAgent handling does not report an error when using -SkipHeaderValidation" {
1859+
$uri = Get-WebListenerUrl -Test 'Get'
18481860
$UserAgent = 'Invalid:Agent'
1849-
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
1861+
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
18501862

18511863
$response.Error | Should BeNullOrEmpty
18521864
$Pattern = [regex]::Escape($UserAgent)
1853-
$response.Content.UserAgent | Should Match $Pattern
1865+
$response.Content.Headers."User-Agent" | Should Match $Pattern
18541866
}
18551867

18561868
#endregion SkipHeaderVerification tests

0 commit comments

Comments
 (0)