mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add option -NoProxy to WebCmdlet
This commit is contained in:
committed by
Mike Richmond
parent
64230566f8
commit
a770ecd686
+3
-1
@@ -17,6 +17,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// gets or sets the parameter Method
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "StandardMethod")]
|
||||
[Parameter(ParameterSetName = "StandardMethodNoProxy")]
|
||||
public override WebRequestMethod Method
|
||||
{
|
||||
get { return base.Method; }
|
||||
@@ -26,7 +27,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// gets or sets the parameter CustomMethod
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "CustomMethod")]
|
||||
[Parameter(Mandatory=true,ParameterSetName = "CustomMethod")]
|
||||
[Parameter(Mandatory=true,ParameterSetName = "CustomMethodNoProxy")]
|
||||
[Alias("CM")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public override string CustomMethod
|
||||
|
||||
+20
-4
@@ -148,6 +148,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// gets or sets the Method property
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "StandardMethod")]
|
||||
[Parameter(ParameterSetName = "StandardMethodNoProxy")]
|
||||
public virtual WebRequestMethod Method
|
||||
{
|
||||
get { return _method; }
|
||||
@@ -158,7 +159,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// gets or sets the CustomMethod property
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "CustomMethod")]
|
||||
[Parameter(Mandatory=true,ParameterSetName = "CustomMethod")]
|
||||
[Parameter(Mandatory=true,ParameterSetName = "CustomMethodNoProxy")]
|
||||
[Alias("CM")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public virtual string CustomMethod
|
||||
@@ -170,25 +172,39 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
#endregion
|
||||
|
||||
#region NoProxy
|
||||
|
||||
/// <summary>
|
||||
/// gets or sets the NoProxy property
|
||||
/// </summary>
|
||||
[Parameter(Mandatory=true,ParameterSetName = "CustomMethodNoProxy")]
|
||||
[Parameter(Mandatory=true,ParameterSetName = "StandardMethodNoProxy")]
|
||||
public virtual SwitchParameter NoProxy { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Proxy
|
||||
|
||||
/// <summary>
|
||||
/// gets or sets the Proxy property
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
[Parameter(ParameterSetName = "StandardMethod")]
|
||||
[Parameter(ParameterSetName = "CustomMethod")]
|
||||
public virtual Uri Proxy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// gets or sets the ProxyCredential property
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
[Parameter(ParameterSetName = "StandardMethod")]
|
||||
[Parameter(ParameterSetName = "CustomMethod")]
|
||||
[Credential]
|
||||
public virtual PSCredential ProxyCredential { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// gets or sets the ProxyUseDefaultCredentials property
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
[Parameter(ParameterSetName = "StandardMethod")]
|
||||
[Parameter(ParameterSetName = "CustomMethod")]
|
||||
public virtual SwitchParameter ProxyUseDefaultCredentials { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
+10
-2
@@ -105,7 +105,11 @@ namespace Microsoft.PowerShell.Commands
|
||||
handler.Credentials = WebSession.Credentials;
|
||||
}
|
||||
|
||||
if (WebSession.Proxy != null)
|
||||
if (NoProxy)
|
||||
{
|
||||
handler.UseProxy = false;
|
||||
}
|
||||
else if (WebSession.Proxy != null)
|
||||
{
|
||||
handler.Proxy = WebSession.Proxy;
|
||||
}
|
||||
@@ -155,13 +159,17 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
Uri requestUri = PrepareUri(uri);
|
||||
HttpMethod httpMethod = null;
|
||||
|
||||
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
case "StandardMethodNoProxy":
|
||||
goto case "StandardMethod";
|
||||
case "StandardMethod":
|
||||
// set the method if the parameter was provided
|
||||
httpMethod = GetHttpMethod(Method);
|
||||
break;
|
||||
case "CustomMethodNoProxy":
|
||||
goto case "CustomMethod";
|
||||
case "CustomMethod":
|
||||
if (!string.IsNullOrEmpty(CustomMethod))
|
||||
{
|
||||
|
||||
+9
-1
@@ -79,13 +79,19 @@ namespace Microsoft.PowerShell.Commands
|
||||
request.Credentials = WebSession.Credentials;
|
||||
}
|
||||
|
||||
if (null != WebSession.Proxy)
|
||||
if (NoProxy)
|
||||
{
|
||||
handler.UseProxy = false;
|
||||
}
|
||||
else if (WebSession.Proxy != null)
|
||||
{
|
||||
request.Proxy = WebSession.Proxy;
|
||||
}
|
||||
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
case "StandardMethodNoProxy":
|
||||
goto case "StandardMethod";
|
||||
case "StandardMethod":
|
||||
if (WebRequestMethod.Default != Method)
|
||||
{
|
||||
@@ -93,6 +99,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
request.Method = Method.ToString().ToUpperInvariant();
|
||||
}
|
||||
break;
|
||||
case "CustomMethodNoProxy":
|
||||
goto case "CustomMethod";
|
||||
case "CustomMethod":
|
||||
// set the method if the parameter was provided
|
||||
request.Method = CustomMethod.ToUpperInvariant();
|
||||
|
||||
@@ -247,7 +247,66 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-WebRequest error with -Proxy and -NoProxy option" {
|
||||
|
||||
$command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
$testCase = @(
|
||||
@{ proxy_address = "http://localhost:8080"; name = 'http_proxy'; protocol = 'http' }
|
||||
@{ proxy_address = "http://localhost:8080"; name = 'https_proxy'; protocol = 'https' }
|
||||
)
|
||||
|
||||
It "Validate Invoke-WebRequest error with -Proxy option set - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
$command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5 -Proxy '${proxy_address}'"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-WebRequest error with environment proxy set - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
# Configure the environment variable.
|
||||
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
|
||||
|
||||
$command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-WebRequest returns User-Agent where -NoProxy with envirionment proxy set - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
# Configure the environment variable.
|
||||
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
|
||||
|
||||
$command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/headers' -TimeoutSec 5 -NoProxy"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
ValidateResponse -response $result
|
||||
|
||||
# Validate response content
|
||||
$jsonContent = $result.Output.Content | ConvertFrom-Json
|
||||
$jsonContent.headers.'Accept-Encoding' | Should Match "gzip, ?deflate"
|
||||
$jsonContent.headers.Host | Should Match "httpbin.org"
|
||||
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
|
||||
}
|
||||
|
||||
It "Invoke-WebRequest validate timeout option" {
|
||||
|
||||
$command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:5 -TimeoutSec 10"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
# Perform the following operation for Invoke-WebRequest
|
||||
@@ -451,6 +510,32 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
|
||||
$result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
if ($env:http_proxy) {
|
||||
$savedHttpProxy = $env:http_proxy
|
||||
$copiedHttpProxy = $true
|
||||
}
|
||||
|
||||
if ($env:https_proxy) {
|
||||
$savedHttpsProxy = $env:https_proxy
|
||||
$copiedHttpsProxy = $true
|
||||
}
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
if ($copiedHttpProxy) {
|
||||
$env:http_proxy = $savedHttpProxy
|
||||
} else {
|
||||
$env:http_proxy = $null
|
||||
}
|
||||
|
||||
if ($copiedHttpsProxy) {
|
||||
$env:https_proxy = $savedHttpsProxy
|
||||
} else {
|
||||
$env:https_proxy = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Invoke-RestMethod tests" -Tags "Feature" {
|
||||
@@ -524,7 +609,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
|
||||
$result.headers.'Accept-Encoding' | Should Match "gzip, deflate"
|
||||
$result.headers.Host | Should Match "httpbin.org"
|
||||
$result.headers.'User-Agent' | Should Match "WindowsPowerShell"
|
||||
|
||||
}
|
||||
#>
|
||||
|
||||
@@ -534,7 +618,54 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-RestMethod error with -Proxy and -NoProxy option" {
|
||||
|
||||
$command = "Invoke-RestMethod -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
|
||||
}
|
||||
|
||||
$testCase = @(
|
||||
@{ proxy_address = "http://localhost:8080"; name = 'http_proxy'; protocol = 'http' }
|
||||
@{ proxy_address = "http://localhost:8080"; name = 'https_proxy'; protocol = 'https' }
|
||||
)
|
||||
|
||||
It "Validate Invoke-RestMethod error with -Proxy option - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
$command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/' -Proxy '${proxy_address}' -TimeoutSec 2"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-RestMethod error with environment proxy set - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
# Configure the environment variable.
|
||||
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
|
||||
|
||||
$command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
|
||||
}
|
||||
|
||||
It "Validate Invoke-RestMethod returns User-Agent with option -NoProxy when environment proxy set - '<name>'" -TestCases $testCase {
|
||||
param($proxy_address, $name, $protocol)
|
||||
|
||||
# Configure the environment variable.
|
||||
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
|
||||
|
||||
$command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/user-agent' -TimeoutSec 5 -NoProxy"
|
||||
|
||||
$result = ExecuteWebCommand -command $command
|
||||
|
||||
# Validate response
|
||||
$result.Output.'User-Agent' | Should Match "WindowsPowerShell"
|
||||
}
|
||||
|
||||
# Perform the following operation for Invoke-RestMethod
|
||||
@@ -744,7 +875,33 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
|
||||
# need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same
|
||||
$result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message
|
||||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
|
||||
}
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
if ($env:http_proxy) {
|
||||
$savedHttpProxy = $env:http_proxy
|
||||
$copiedHttpProxy = $true
|
||||
}
|
||||
|
||||
if ($env:https_proxy) {
|
||||
$savedHttpsProxy = $env:https_proxy
|
||||
$copiedHttpsProxy = $true
|
||||
}
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
if ($copiedHttpProxy) {
|
||||
$env:http_proxy = $savedHttpProxy
|
||||
} else {
|
||||
$env:http_proxy = $null
|
||||
}
|
||||
|
||||
if ($copiedHttpsProxy) {
|
||||
$env:https_proxy = $savedHttpsProxy
|
||||
} else {
|
||||
$env:https_proxy = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" {
|
||||
|
||||
Reference in New Issue
Block a user