Skip to content

Commit

Permalink
Add ResponseHeadersVariable Parameter to Invoke-RestMethod (#4888)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
markekraus authored and adityapatwardhan committed Sep 25, 2017
1 parent 1c921cd commit 3237d43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Expand Up @@ -60,6 +60,13 @@ public int MaximumFollowRelLink
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
Expand Down
Expand Up @@ -95,6 +95,12 @@ internal override void ProcessResponse(HttpResponseMessage response)
{
StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
}

if (!String.IsNullOrEmpty(ResponseHeadersVariable))
{
PSVariableIntrinsics vi = SessionState.PSVariable;
vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
}
}
}

Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 3237d43

Please sign in to comment.