New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option -NoProxy to WebCmdlet #3447

Merged
merged 1 commit into from Apr 1, 2017
Jump to file or symbol
Failed to load files and symbols.
+201 −10
Diff settings

Always

Just for now

@@ -17,6 +17,7 @@ public partial class InvokeRestMethodCommand
/// 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 @@ public override WebRequestMethod Method
/// <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
@@ -148,6 +148,7 @@ public virtual int MaximumRedirection
/// gets or sets the Method property
/// </summary>
[Parameter(ParameterSetName = "StandardMethod")]
[Parameter(ParameterSetName = "StandardMethodNoProxy")]
public virtual WebRequestMethod Method
{
get { return _method; }
@@ -158,7 +159,8 @@ public virtual WebRequestMethod Method
/// <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 @@ public virtual string CustomMethod
#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
@@ -105,7 +105,11 @@ internal virtual HttpClient GetHttpClient()
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 @@ internal virtual HttpRequestMessage GetRequest(Uri uri)
{
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))
{
@@ -79,20 +79,28 @@ internal virtual WebRequest GetRequest(Uri uri)
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)
{
// set the method if the parameter was provided
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" {
ProTip! Use n and p to navigate between commits in a pull request.