Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"

}
#>

Expand All @@ -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
Expand Down Expand Up @@ -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" {
Expand Down