Skip to content

Commit b043b44

Browse files
0x4c6565mirichmo
authored andcommitted
adds parameter sets to web cmdlets to allow for standard and non-stan… (PowerShell#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
1 parent 4a0afd8 commit b043b44

9 files changed

Lines changed: 131 additions & 13 deletions

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ public partial class InvokeRestMethodCommand
1616
/// <summary>
1717
/// gets or sets the parameter Method
1818
/// </summary>
19-
[Parameter]
19+
[Parameter(ParameterSetName = "StandardMethod")]
2020
public override WebRequestMethod Method
2121
{
2222
get { return base.Method; }
2323
set { base.Method = value; }
2424
}
2525

26+
/// <summary>
27+
/// gets or sets the parameter CustomMethod
28+
/// </summary>
29+
[Parameter(ParameterSetName = "CustomMethod")]
30+
[Alias("CM")]
31+
[ValidateNotNullOrEmpty]
32+
public override string CustomMethod
33+
{
34+
get { return base.CustomMethod; }
35+
set { base.CustomMethod = value; }
36+
}
37+
2638
#endregion Parameters
2739

2840
#region Helper Methods

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,27 @@ public virtual int MaximumRedirection
147147
/// <summary>
148148
/// gets or sets the Method property
149149
/// </summary>
150-
[Parameter]
150+
[Parameter(ParameterSetName = "StandardMethod")]
151151
public virtual WebRequestMethod Method
152152
{
153153
get { return _method; }
154154
set { _method = value; }
155155
}
156156
private WebRequestMethod _method = WebRequestMethod.Default;
157157

158+
/// <summary>
159+
/// gets or sets the CustomMethod property
160+
/// </summary>
161+
[Parameter(ParameterSetName = "CustomMethod")]
162+
[Alias("CM")]
163+
[ValidateNotNullOrEmpty]
164+
public virtual string CustomMethod
165+
{
166+
get { return _customMethod; }
167+
set { _customMethod = value; }
168+
}
169+
private string _customMethod;
170+
158171
#endregion
159172

160173
#region Proxy
@@ -547,7 +560,8 @@ private Uri PrepareUri(Uri uri)
547560
IDictionary bodyAsDictionary;
548561
LanguagePrimitives.TryConvertTo<IDictionary>(Body, out bodyAsDictionary);
549562
if ((null != bodyAsDictionary)
550-
&& (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get))
563+
&& ((IsStandardMethodSet() && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get))
564+
|| (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET")))
551565
{
552566
UriBuilder uriBuilder = new UriBuilder(uri);
553567
if (uriBuilder.Query != null && uriBuilder.Query.Length > 1)
@@ -626,6 +640,16 @@ private ErrorRecord GetValidationError(string msg, string errorId, params object
626640
return (error);
627641
}
628642

643+
private bool IsStandardMethodSet()
644+
{
645+
return (ParameterSetName == "StandardMethod");
646+
}
647+
648+
private bool IsCustomMethodSet()
649+
{
650+
return (ParameterSetName == "CustomMethod");
651+
}
652+
629653
#endregion Helper Methods
630654
}
631655
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.PowerShell.Commands
1818
/// Intended to work against the wide spectrum of "RESTful" web services
1919
/// currently deployed across the web.
2020
/// </summary>
21-
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034")]
21+
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034", DefaultParameterSetName = "StandardMethod")]
2222
public partial class InvokeRestMethodCommand : WebRequestPSCmdlet
2323
{
2424
#region Virtual Method Overrides

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.PowerShell.Commands
1515
/// The Invoke-RestMethod command
1616
/// This command makes an HTTP or HTTPS request to a web server and returns the results.
1717
/// </summary>
18-
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035")]
18+
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035", DefaultParameterSetName = "StandardMethod")]
1919
public class InvokeWebRequestCommand : WebRequestPSCmdlet
2020
{
2121
#region Virtual Method Overrides

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,22 @@ internal virtual HttpClient GetHttpClient()
154154
internal virtual HttpRequestMessage GetRequest(Uri uri)
155155
{
156156
Uri requestUri = PrepareUri(uri);
157-
HttpMethod httpMethod = GetHttpMethod(Method);
157+
HttpMethod httpMethod = null;
158+
159+
switch (ParameterSetName)
160+
{
161+
case "StandardMethod":
162+
// set the method if the parameter was provided
163+
httpMethod = GetHttpMethod(Method);
164+
break;
165+
case "CustomMethod":
166+
if (!string.IsNullOrEmpty(CustomMethod))
167+
{
168+
// set the method if the parameter was provided
169+
httpMethod = new HttpMethod(CustomMethod.ToString().ToUpperInvariant());
170+
}
171+
break;
172+
}
158173

159174
// create the base WebRequest object
160175
var request = new HttpRequestMessage(httpMethod, requestUri);
@@ -229,7 +244,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
229244
//request
230245
}
231246
// ContentType == null
232-
else if (Method == WebRequestMethod.Post)
247+
else if (Method == WebRequestMethod.Post || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST"))
233248
{
234249
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
235250
string contentType = null;

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FullClr/InvokeRestMethodCommand.FullClr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Microsoft.PowerShell.Commands
1919
/// Intended to work against the wide spectrum of "RESTful" web services
2020
/// currently deployed across the web.
2121
/// </summary>
22-
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034")]
22+
[Cmdlet(VerbsLifecycle.Invoke, "RestMethod", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217034", DefaultParameterSetName = "StandardMethod")]
2323
public partial class InvokeRestMethodCommand : WebRequestPSCmdlet
2424
{
2525
#region Virtual Method Overrides

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FullClr/InvokeWebRequestCommand.FullClr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.PowerShell.Commands
1313
/// The Invoke-RestMethod command
1414
/// This command makes an HTTP or HTTPS request to a web server and returns the results.
1515
/// </summary>
16-
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035")]
16+
[Cmdlet(VerbsLifecycle.Invoke, "WebRequest", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217035", DefaultParameterSetName = "StandardMethod")]
1717
public class InvokeWebRequestCommand : WebRequestPSCmdlet
1818
{
1919
#region Virtual Method Overrides

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FullClr/WebRequestPSCmdlet.FullClr.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,19 @@ internal virtual WebRequest GetRequest(Uri uri)
8484
request.Proxy = WebSession.Proxy;
8585
}
8686

87-
// set the method if the parameter was provided
88-
if (WebRequestMethod.Default != Method)
87+
switch (ParameterSetName)
8988
{
90-
request.Method = Method.ToString().ToUpperInvariant();
89+
case "StandardMethod":
90+
if (WebRequestMethod.Default != Method)
91+
{
92+
// set the method if the parameter was provided
93+
request.Method = Method.ToString().ToUpperInvariant();
94+
}
95+
break;
96+
case "CustomMethod":
97+
// set the method if the parameter was provided
98+
request.Method = CustomMethod.ToUpperInvariant();
99+
break;
91100
}
92101

93102
// pull in http specific properties
@@ -248,7 +257,8 @@ internal virtual void FillRequestStream(WebRequest request)
248257
request.ContentType = ContentType;
249258
}
250259
// ContentType == null
251-
else if (Method == WebRequestMethod.Post)
260+
else if ((IsStandardMethodSet() && Method == WebRequestMethod.Post)
261+
|| (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST"))
252262
{
253263
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
254264
if (String.IsNullOrEmpty(request.ContentType))

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,35 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
400400
$result.Error | Should BeNullOrEmpty
401401
}
402402

403+
It "Validate Invoke-WebRequest StandardMethod and CustomMethod parameter sets" {
404+
405+
#Validate that parameter sets are functioning correctly
406+
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
407+
{ Invoke-WebRequest -Uri 'http://http.lee.io/method' -Method GET -CustomMethod TEST } | ShouldBeErrorId $errorId
408+
}
409+
410+
It "Validate Invoke-WebRequest CustomMethod method is used" {
411+
412+
$command = "Invoke-WebRequest -Uri 'http://http.lee.io/method' -CustomMethod TEST"
413+
$result = ExecuteWebCommand -command $command
414+
$result.Error | Should BeNullOrEmpty
415+
($result.Output.Content | ConvertFrom-Json).output.method | Should Be "TEST"
416+
}
417+
418+
It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" {
419+
420+
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'"
421+
$result = ExecuteWebCommand -command $command
422+
($result.Output.Content | ConvertFrom-Json).form.testparam | Should Be "testvalue"
423+
}
424+
425+
It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" {
426+
427+
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/get' -CustomMethod GET -Body @{'testparam'='testvalue'}"
428+
$result = ExecuteWebCommand -command $command
429+
($result.Output.Content | ConvertFrom-Json).args.testparam | Should Be "testvalue"
430+
}
431+
403432
It "Validate Invoke-WebRequest returns HTTP errors in exception" {
404433

405434
$command = "Invoke-WebRequest -Uri http://httpbin.org/status/418"
@@ -649,6 +678,34 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
649678
$result.Error | Should BeNullOrEmpty
650679
}
651680

681+
It "Validate Invoke-RestMethod StandardMethod and CustomMethod parameter sets" {
682+
683+
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
684+
{ Invoke-RestMethod -Uri 'http://http.lee.io/method' -Method GET -CustomMethod TEST } | ShouldBeErrorId $errorId
685+
}
686+
687+
It "Validate CustomMethod method is used" {
688+
689+
$command = "Invoke-RestMethod -Uri 'http://http.lee.io/method' -CustomMethod TEST"
690+
$result = ExecuteWebCommand -command $command
691+
$result.Error | Should BeNullOrEmpty
692+
$result.Output.output.method | Should Be "TEST"
693+
}
694+
695+
It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" {
696+
697+
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'"
698+
$result = ExecuteWebCommand -command $command
699+
$result.Output.form.testparam | Should Be "testvalue"
700+
}
701+
702+
It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" {
703+
704+
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/get' -CustomMethod GET -Body @{'testparam'='testvalue'}"
705+
$result = ExecuteWebCommand -command $command
706+
$result.Output.args.testparam | Should Be "testvalue"
707+
}
708+
652709
It "Invoke-RestMethod supports request that returns plain text response." {
653710

654711
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/encoding/utf8'"

0 commit comments

Comments
 (0)