-
Notifications
You must be signed in to change notification settings - Fork 7.8k
adds parameter sets to web cmdlets to allow for standard and non-stan… #3142
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
Changes from 1 commit
7c11001
8f25fd9
ecf7624
51fb2fb
5d6763b
f5db053
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…dard method verbs
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,14 +147,25 @@ public virtual int MaximumRedirection | |
/// <summary> | ||
/// gets or sets the Method property | ||
/// </summary> | ||
[Parameter] | ||
[Parameter(ParameterSetName = "StandardMethod")] | ||
public virtual WebRequestMethod Method | ||
{ | ||
get { return _method; } | ||
set { _method = value; } | ||
} | ||
private WebRequestMethod _method = WebRequestMethod.Default; | ||
|
||
/// <summary> | ||
/// gets or sets the CustomMethod property | ||
/// </summary> | ||
[Parameter(ParameterSetName = "CustomMethod")] | ||
public virtual string CustomMethod | ||
{ | ||
get { return _customMethod; } | ||
set { _customMethod = value; } | ||
} | ||
private string _customMethod; | ||
|
||
#endregion | ||
|
||
#region Proxy | ||
|
@@ -547,7 +558,8 @@ private Uri PrepareUri(Uri uri) | |
IDictionary bodyAsDictionary; | ||
LanguagePrimitives.TryConvertTo<IDictionary>(Body, out bodyAsDictionary); | ||
if ((null != bodyAsDictionary) | ||
&& (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get)) | ||
&& ((IsStandardMethodSet() && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get)) | ||
|| (IsCustomMethodSet() && (string.IsNullOrEmpty(CustomMethod) || CustomMethod.ToUpper() == "GET")))) | ||
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. Should be consistent and use ToUpperInvariant(). |
||
{ | ||
UriBuilder uriBuilder = new UriBuilder(uri); | ||
if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) | ||
|
@@ -626,6 +638,16 @@ private ErrorRecord GetValidationError(string msg, string errorId, params object | |
return (error); | ||
} | ||
|
||
private bool IsStandardMethodSet() | ||
{ | ||
return (ParameterSetName == "StandardMethod"); | ||
} | ||
|
||
private bool IsCustomMethodSet() | ||
{ | ||
return (ParameterSetName == "CustomMethod"); | ||
} | ||
|
||
#endregion Helper Methods | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,10 +84,22 @@ 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 | ||
if (!string.IsNullOrEmpty(CustomMethod)) | ||
{ | ||
request.Method = CustomMethod.ToUpperInvariant(); | ||
} | ||
break; | ||
} | ||
|
||
// pull in http specific properties | ||
|
@@ -248,7 +260,7 @@ internal virtual void FillRequestStream(WebRequest request) | |
request.ContentType = ContentType; | ||
} | ||
// ContentType == null | ||
else if (Method == WebRequestMethod.Post) | ||
else if (Method == WebRequestMethod.Post || (!string.IsNullOrEmpty(CustomMethod) && CustomMethod.ToUpper() == "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 |
---|---|---|
|
@@ -399,6 +399,23 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
} | ||
|
||
It "Validate StandardMethod and CustomMethod parameter sets" { | ||
|
||
#Validate that parameter sets are functioning correctly | ||
$command = "Invoke-RestMethod -Uri 'http://sandbox.lee.io/method.php' -Method GET -CustomMethod 'TEST'" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should Not BeNullOrEmpty | ||
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. You should use the 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. refactored test to utilise |
||
} | ||
|
||
It "Validate CustomMethod parameter set method is passed" { | ||
|
||
#Validate that parameter sets are functioning correctly | ||
$command = "Invoke-RestMethod -Uri 'http://sandbox.lee.io/method.php' -CustomMethod 'TEST'" | ||
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
($result.Output | ConvertFrom-Json).method | Should Be "TEST" | ||
} | ||
} | ||
|
||
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. you added code explicitly for when CustomMethod is 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. tests now added for this |
||
Describe "Invoke-RestMethod tests" -Tags "Feature" { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.