Add ResponseHeadersVariable Parameter to Invoke-RestMethod (#4888)

* Add ResponseHeadersVariable to Invoke-RestMethod

* Add Tests for -ResponseHeadersVariable

* [Feature] Run Feature Tests

* Remove test to address PR feedback

* Alais HV -> RHV per request

* [Feature] Rerun CI

* [Feature] Remove Superfluous Assertion
This commit is contained in:
Mark Kraus 2017-09-25 12:11:12 -05:00 committed by Aditya Patwardhan
parent 1c921cdf85
commit 3237d43b26
3 changed files with 33 additions and 0 deletions

View file

@ -60,6 +60,13 @@ namespace Microsoft.PowerShell.Commands
set { base._maximumFollowRelLink = value; }
}
/// <summary>
/// Gets or sets the ResponseHeadersVariable property.
/// </summary>
[Parameter]
[Alias("RHV")]
public string ResponseHeadersVariable { get; set; }
#endregion Parameters
#region Helper Methods

View file

@ -95,6 +95,12 @@ namespace Microsoft.PowerShell.Commands
{
StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
}
if (!String.IsNullOrEmpty(ResponseHeadersVariable))
{
PSVariableIntrinsics vi = SessionState.PSVariable;
vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
}
}
}

View file

@ -2055,6 +2055,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
#endregion charset encoding tests
Context 'Invoke-RestMethod ResponseHeadersVariable Tests' {
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable" {
$uri = Get-WebListenerUrl -Test '/'
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8'
$headers.Server | Should Be 'Kestrel'
}
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable overwriting existing variable" {
$uri = Get-WebListenerUrl -Test '/'
$headers = 'prexisting'
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
$headers | Should Not Be 'prexisting'
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8'
$headers.Server | Should Be 'Kestrel'
}
}
BeforeEach {
if ($env:http_proxy) {
$savedHttpProxy = $env:http_proxy