Skip to content

Commit

Permalink
adds parameter sets to web cmdlets to allow for standard and non-stan… (
Browse files Browse the repository at this point in the history
#3142)

* adds parameter sets to web cmdlets to allow for standard and non-standard method verbs

* add CoreCLI implementation

* Adds CM alias and notnullempty for CustomMethod parameter

* Add tests for Invoke-[WebRequest|RestMethod] CustomMethod parameter

* Fix webcmdlet tests - incorrect parameter name
  • Loading branch information
0x4c6565 authored and mirichmo committed Mar 6, 2017
1 parent 4a0afd8 commit b043b44
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 13 deletions.
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")]
[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 @@ -154,7 +154,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 @@ -229,7 +244,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)
{
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 @@ -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).form.testparam | Should Be "testvalue"
}

It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" {

$command = "Invoke-WebRequest -Uri 'http://httpbin.org/get' -CustomMethod GET -Body @{'testparam'='testvalue'}"
$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"
Expand Down Expand Up @@ -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.form.testparam | Should Be "testvalue"
}

It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" {

$command = "Invoke-RestMethod -Uri 'http://httpbin.org/get' -CustomMethod 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'"
Expand Down

0 comments on commit b043b44

Please sign in to comment.