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:
parent
1c921cdf85
commit
3237d43b26
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue