-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Add option -NoProxy to WebCmdlet #3447
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
Conversation
@TheFlyingCorpse, |
Can you add a test? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test
/// gets or sets the NoProxy property | ||
/// </summary> | ||
[Parameter] | ||
public virtual SwitchParameter NoProxy { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a good reason to make the property/parameter virtual?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I copied what was already used in that file for the other switches present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should -noproxy
and -proxy*
parameters be in different parametersets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a good idea, I'll add a few more tests and make that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it is better not to do different parameter sets because we will not be able to do:
function Foo($NoProxy) {
Invoke-WebRequest -NoProxy:$NoProxy -Proxy "http://proxy.server.com"
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, it seems like these should be mutually exclusive (thus different parametersets). PowerShell doesn't work like Unix cmdlines where last one wins (in which case, the above example would be -Proxy overrides -NoProxy despite $true or $false). I've typically used splatting to construct parameters dynamically. I think if we want something more terse, we should introduce a more general approach as a language construct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point taken, updating now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused about the switch, all the more so in a separate parameter set. I would prefer not to complicate and just make -Proxy $null
7bcc221
to
af53884
Compare
Tests added, if you want me to, I can add scenarios to verify it fails when proxy is defined to the made up proxy as a countermeasure to
|
Probably enough: Invoke-WebRequest -Uri http://httpbin.org/headers -Proxy http://httpbin.org -NoProxy - return expected result
Invoke-WebRequest -Uri http://httpbin.org/headers -Proxy http://httpbin.org -NoProxy:$false - return empty content |
af53884
to
1d9f2de
Compare
Updated tests quite a bit. I did not split the parameters up to different parametersets as @iSazonov made a great case for why it ought not to be split up. |
It "Validate Invoke-WebRequest error for -MaximumRedirection with Environment http_proxy set" { | ||
|
||
# Store the current proxy. | ||
$currentProxy = [System.Environment]::GetEnvironmentVariable("http_proxy") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may be cleaner to put storing and restoring of http_proxy
(if defined) in BeforeEach{} and AfterEach{} blocks rather than having repeated code.
|
||
# Store the current proxy. | ||
$currentProxy = [System.Environment]::GetEnvironmentVariable("http_proxy") | ||
[System.Environment]::SetEnvironmentVariable("http_proxy","http://localhost:8080") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to rely on:
$savedProxy = $env:http_proxy
...
$env:http_proxy = "http://localhost:8080"
...
$env:http_proxy = $savedProxy
@@ -247,7 +247,115 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |||
|
|||
$result = ExecuteWebCommand -command $command | |||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" | |||
} | |||
|
|||
It "Validate Invoke-WebRequest returns User-Agent where -NoProxy with -Proxy set" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we agree that -NoProxy
and -Proxy
are mutually exclusive, then this test should validate you get an AmbiguousParameterSet,Microsoft.PowerShell.Commands.NewPSSessionCommand
exception
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/redirect/3' -MaximumRedirection 2 -TimeoutSec 5" | ||
|
||
$result = ExecuteWebCommand -command $command | ||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear to me what this test is intended to validate. Since localhost:8080
is not a proxy, no redirection will actually happen and the error returned is that it can't contact the proxy.
[System.Environment]::SetEnvironmentVariable("http_proxy",$currentProxy) | ||
} | ||
|
||
It "Validate Invoke-WebRequest error for -MaximumRedirection with Environment https_proxy set" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, not clear to me why -MaximumRedirection
is interesting here as a test with invalid proxy
1d9f2de
to
4da3e63
Compare
Updated, incorporated ParameterSetName's and updated the tests to reflect this change. To be able to make the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given all the discussion in #3418, I'm explicitly giving this one my 👍
|
||
It "Validate Invoke-WebRequest returns User-Agent where -NoProxy with Environment http_proxy Set" { | ||
|
||
$env:http_proxy="http://localhost:8080" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the http and https tests are essentially the same, I think it may be better to use Pester's TestCase feature. See a sample here:
PowerShell/test/powershell/Language/Parser/LanguageAndParser.TestFollowup.Tests.ps1
Line 147 in 02b5f35
$testCase = @( |
Note: I didn't find a good way with the mock to have a dynamic proxy when using |
4da3e63
to
1d31ab4
Compare
This commit adds a
-NoProxy
parameter to the WebCmdlet, used by bothInvoke-WebRequest
andInvoke-RestMethod
. The fix is intended to allow for bypassing a DefaultWebProxy, such as a system configured one for direct requests.References #3418
I did not figure out where I can update the help files relevant to this, I'd appreciate being pointed in the right direction for this.