Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,9 @@ private HttpMethod GetHttpMethod(WebRequestMethod method)
// and PreserveAuthorizationOnRedirect is NOT set.
internal virtual HttpClient GetHttpClient(bool handleRedirect)
{
// By default the HttpClientHandler will automatically decompress GZip and Deflate content
HttpClientHandler handler = new();
handler.CookieContainer = WebSession.Cookies;
handler.AutomaticDecompression = DecompressionMethods.All;

// set the credentials used by this request
if (WebSession.UseDefaultCredentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.IO;
using System.IO.Compression;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Net.Http;
Expand Down Expand Up @@ -468,21 +467,6 @@ internal static byte[] EncodeToBytes(string str)
internal static Stream GetResponseStream(HttpResponseMessage response)
{
Stream responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
var contentEncoding = response.Content.Headers.ContentEncoding;

// HttpClient by default will automatically decompress GZip and Deflate content.
// We keep this decompression logic here just in case.
if (contentEncoding != null && contentEncoding.Count > 0)
{
if (contentEncoding.Contains("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
else if (contentEncoding.Contains("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
}

return responseStream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
# Note: These are the supported options, but we do not have a web service to test them all.
It "Invoke-WebRequest supports request that returns <DataEncoding>-encoded data." -TestCases @(
@{ DataEncoding = "gzip"}
@{ DataEncoding = "deflate"}
@{ DataEncoding = "gzip" }
@{ DataEncoding = "deflate" }
) {
param($dataEncoding)
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
Expand All @@ -598,7 +598,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
ValidateResponse -response $result

# Validate response content
$result.Output.Headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding
# The content should be de-compressed, and otherwise converting from JSON will fail.
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.Headers.Host | Should -BeExactly $uri.Authority
}
Expand Down Expand Up @@ -2246,15 +2246,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
# Note: These are the supported options, but we do not have a web service to test them all.
It "Invoke-RestMethod supports request that returns <DataEncoding>-encoded data." -TestCases @(
@{ DataEncoding = "gzip"}
@{ DataEncoding = "deflate"}
@{ DataEncoding = "gzip" }
@{ DataEncoding = "deflate" }
) {
param($dataEncoding)
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
$result = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
$result = Invoke-RestMethod -Uri $uri

# Validate response content
$headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding
# The content should be de-compressed. Otherwise, the above 'Invoke-RestMethod' would have thrown because converting to JSON internally would fail.
$result.Headers.Host | Should -BeExactly $uri.Authority
}

Expand Down