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
adds parameter sets to web cmdlets to allow for standard and non-stan… #3142
Changes from 5 commits
7c11001
8f25fd9
ecf7624
51fb2fb
5d6763b
f5db053
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,10 +84,19 @@ internal virtual WebRequest GetRequest(Uri uri) | |
request.Proxy = WebSession.Proxy; | ||
} | ||
|
||
// set the method if the parameter was provided | ||
if (WebRequestMethod.Default != Method) | ||
switch (ParameterSetName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had this simplified to this. Since CustomMethod will always be null or a real string (param validation) checking for $null should be enough. // set the custom method if the parameter was provided
if(CustomMethod != null)
{
request.Method = CustomMethod.ToUpperInvariant();
}
else
{
// set the method if the parameter was provided
if (WebRequestMethod.Default != Method)
{
request.Method = Method.ToString().ToUpperInvariant();
}
} |
||
{ | ||
request.Method = Method.ToString().ToUpperInvariant(); | ||
case "StandardMethod": | ||
if (WebRequestMethod.Default != Method) | ||
{ | ||
// set the method if the parameter was provided | ||
request.Method = Method.ToString().ToUpperInvariant(); | ||
} | ||
break; | ||
case "CustomMethod": | ||
// set the method if the parameter was provided | ||
request.Method = CustomMethod.ToUpperInvariant(); | ||
break; | ||
} | ||
|
||
// pull in http specific properties | ||
|
@@ -248,7 +257,8 @@ internal virtual void FillRequestStream(WebRequest request) | |
request.ContentType = ContentType; | ||
} | ||
// ContentType == null | ||
else if (Method == WebRequestMethod.Post) | ||
else if ((IsStandardMethodSet() && Method == WebRequestMethod.Post) | ||
|| (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST")) | ||
{ | ||
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST | ||
if (String.IsNullOrEmpty(request.ContentType)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -400,6 +400,35 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
$result.Error | Should BeNullOrEmpty | ||
} | ||
|
||
It "Validate Invoke-WebRequest StandardMethod and CustomMethod parameter sets" { | ||
|
||
#Validate that parameter sets are functioning correctly | ||
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" | ||
{ Invoke-WebRequest -Uri 'http://http.lee.io/method' -Method GET -CustomMethod TEST } | ShouldBeErrorId $errorId | ||
} | ||
|
||
It "Validate Invoke-WebRequest CustomMethod method is used" { | ||
|
||
$command = "Invoke-WebRequest -Uri 'http://http.lee.io/method' -CustomMethod TEST" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
($result.Output.Content | ConvertFrom-Json).output.method | Should Be "TEST" | ||
} | ||
|
||
It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" { | ||
|
||
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'" | ||
$result = ExecuteWebCommand -command $command | ||
($result.Output.Content | ConvertFrom-Json).headers.'Content-Type' | Should Be "application/x-www-form-urlencoded" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you're sending a specific body, seems like you should just check it was returned correctly in the response: $result.Output.form.testvalue | Should Be "testvalue" |
||
} | ||
|
||
It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" { | ||
|
||
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/get' -Method GET -Body @{'testparam'='testvalue'}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Completely missed that! Fixed |
||
$result = ExecuteWebCommand -command $command | ||
($result.Output.Content | ConvertFrom-Json).args.testparam | Should Be "testvalue" | ||
} | ||
|
||
It "Validate Invoke-WebRequest returns HTTP errors in exception" { | ||
|
||
$command = "Invoke-WebRequest -Uri http://httpbin.org/status/418" | ||
|
@@ -649,6 +678,34 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
$result.Error | Should BeNullOrEmpty | ||
} | ||
|
||
It "Validate Invoke-RestMethod StandardMethod and CustomMethod parameter sets" { | ||
|
||
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" | ||
{ Invoke-RestMethod -Uri 'http://http.lee.io/method' -Method GET -CustomMethod TEST } | ShouldBeErrorId $errorId | ||
} | ||
|
||
It "Validate CustomMethod method is used" { | ||
|
||
$command = "Invoke-RestMethod -Uri 'http://http.lee.io/method' -CustomMethod TEST" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
$result.Output.output.method | Should Be "TEST" | ||
} | ||
|
||
It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" { | ||
|
||
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Output.headers.'Content-Type' | Should Be "application/x-www-form-urlencoded" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you're sending a specific body, seems like you should just check it was returned correctly in the response: $result.Output.form.testvalue | Should Be "testvalue" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've now refactored the tests for this |
||
} | ||
|
||
It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" { | ||
|
||
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/get' -Method GET -Body @{'testparam'='testvalue'}" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Output.args.testparam | Should Be "testvalue" | ||
} | ||
|
||
It "Invoke-RestMethod supports request that returns plain text response." { | ||
|
||
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/encoding/utf8'" | ||
|
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 can make this validate on input and removed the checks for
IsNullOrEmpty
. Also adding an alias now would be nice.