Skip to content
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

Merged
merged 6 commits into from Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,13 +16,25 @@ public partial class InvokeRestMethodCommand
/// <summary>
/// gets or sets the parameter Method
/// </summary>
[Parameter]
[Parameter(ParameterSetName = "StandardMethod")]
public override WebRequestMethod Method
{
get { return base.Method; }
set { base.Method = value; }
}

/// <summary>
/// gets or sets the parameter CustomMethod
/// </summary>
[Parameter(ParameterSetName = "CustomMethod")]
Copy link
Contributor

@thezim thezim Feb 13, 2017

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.

[ValidateNotNullOrEmpty]
[Alias("CM")]

[Alias("CM")]
[ValidateNotNullOrEmpty]
public override string CustomMethod
{
get { return base.CustomMethod; }
set { base.CustomMethod = value; }
}

#endregion Parameters

#region Helper Methods
Expand Down
Expand Up @@ -147,14 +147,27 @@ 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")]
[Alias("CM")]
[ValidateNotNullOrEmpty]
public virtual string CustomMethod
{
get { return _customMethod; }
set { _customMethod = value; }
}
private string _customMethod;

#endregion

#region Proxy
Expand Down Expand Up @@ -547,7 +560,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() && CustomMethod.ToUpperInvariant() == "GET")))
{
UriBuilder uriBuilder = new UriBuilder(uri);
if (uriBuilder.Query != null && uriBuilder.Query.Length > 1)
Expand Down Expand Up @@ -626,6 +640,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
}
}
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.PowerShell.Commands
/// Intended to work against the wide spectrum of "RESTful" web services
/// currently deployed across the web.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034")]
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034", DefaultParameterSetName = "StandardMethod")]
public partial class InvokeRestMethodCommand : WebRequestPSCmdlet
{
#region Virtual Method Overrides
Expand Down
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.PowerShell.Commands
/// The Invoke-RestMethod command
/// This command makes an HTTP or HTTPS request to a web server and returns the results.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035")]
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035", DefaultParameterSetName = "StandardMethod")]
public class InvokeWebRequestCommand : WebRequestPSCmdlet
{
#region Virtual Method Overrides
Expand Down
Expand Up @@ -133,7 +133,22 @@ internal virtual HttpClient GetHttpClient()
internal virtual HttpRequestMessage GetRequest(Uri uri)
{
Uri requestUri = PrepareUri(uri);
HttpMethod httpMethod = GetHttpMethod(Method);
HttpMethod httpMethod = null;

switch (ParameterSetName)
{
case "StandardMethod":
// set the method if the parameter was provided
httpMethod = GetHttpMethod(Method);
break;
case "CustomMethod":
if (!string.IsNullOrEmpty(CustomMethod))
{
// set the method if the parameter was provided
httpMethod = new HttpMethod(CustomMethod.ToString().ToUpperInvariant());
}
break;
}

// create the base WebRequest object
var request = new HttpRequestMessage(httpMethod, requestUri);
Expand Down Expand Up @@ -208,7 +223,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
//request
}
// ContentType == null
else if (Method == WebRequestMethod.Post)
else if (Method == WebRequestMethod.Post || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST"))
{
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
string contentType = null;
Expand Down
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.PowerShell.Commands
/// Intended to work against the wide spectrum of "RESTful" web services
/// currently deployed across the web.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034")]
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034", DefaultParameterSetName = "StandardMethod")]
public partial class InvokeRestMethodCommand : WebRequestPSCmdlet
{
#region Virtual Method Overrides
Expand Down
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.PowerShell.Commands
/// The Invoke-RestMethod command
/// This command makes an HTTP or HTTPS request to a web server and returns the results.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035")]
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035", DefaultParameterSetName = "StandardMethod")]
public class InvokeWebRequestCommand : WebRequestPSCmdlet
{
#region Virtual Method Overrides
Expand Down
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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))
Expand Down
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored test to utilise ShouldBeErrorId

}

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"
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you added code explicitly for when CustomMethod is get or post. You should add tests for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests now added for this

Describe "Invoke-RestMethod tests" -Tags "Feature" {
Expand Down