// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Net.Test.Common; using System.Text.RegularExpressions; using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; namespace System.Net.Http.Functional.Tests { public abstract class HttpClientHandler_Decompression_Test : HttpClientHandlerTestBase { private readonly ITestOutputHelper _output; public static readonly object[][] CompressedServers = System.Net.Test.Common.Configuration.Http.CompressedServers; public HttpClientHandler_Decompression_Test(ITestOutputHelper output) { _output = output; } public static IEnumerable DecompressedResponse_MethodSpecified_DecompressedContentReturned_MemberData() { foreach (bool specifyAllMethods in new[] { false, true }) { yield return new object[] { "deflate", new Func(s => new DeflateStream(s, CompressionLevel.Optimal, leaveOpen: true)), specifyAllMethods ? DecompressionMethods.Deflate : DecompressionMethods.All }; yield return new object[] { "gzip", new Func(s => new GZipStream(s, CompressionLevel.Optimal, leaveOpen: true)), specifyAllMethods ? DecompressionMethods.GZip : DecompressionMethods.All }; yield return new object[] { "br", new Func(s => new BrotliStream(s, CompressionLevel.Optimal, leaveOpen: true)), specifyAllMethods ? DecompressionMethods.Brotli : DecompressionMethods.All }; } } [Theory] [MemberData(nameof(DecompressedResponse_MethodSpecified_DecompressedContentReturned_MemberData))] public async Task DecompressedResponse_MethodSpecified_DecompressedContentReturned( string encodingName, Func compress, DecompressionMethods methods) { if (!UseSocketsHttpHandler && encodingName == "br") { // Brotli only supported on SocketsHttpHandler. return; } var expectedContent = new byte[12345]; new Random(42).NextBytes(expectedContent); await LoopbackServer.CreateClientAndServerAsync(async uri => { using (HttpClientHandler handler = CreateHttpClientHandler()) using (var client = new HttpClient(handler)) { handler.AutomaticDecompression = methods; Assert.Equal(expectedContent, await client.GetByteArrayAsync(uri)); } }, async server => { await server.AcceptConnectionAsync(async connection => { await connection.ReadRequestHeaderAsync(); await connection.Writer.WriteAsync($"HTTP/1.1 200 OK\r\nContent-Encoding: {encodingName}\r\n\r\n"); using (Stream compressedStream = compress(connection.Stream)) { await compressedStream.WriteAsync(expectedContent); } }); }); } public static IEnumerable DecompressedResponse_MethodNotSpecified_OriginalContentReturned_MemberData() { yield return new object[] { "deflate", new Func(s => new DeflateStream(s, CompressionLevel.Optimal, leaveOpen: true)), DecompressionMethods.None }; yield return new object[] { "gzip", new Func(s => new GZipStream(s, CompressionLevel.Optimal, leaveOpen: true)), DecompressionMethods.Brotli }; yield return new object[] { "br", new Func(s => new BrotliStream(s, CompressionLevel.Optimal, leaveOpen: true)), DecompressionMethods.Deflate | DecompressionMethods.GZip }; } [Theory] [MemberData(nameof(DecompressedResponse_MethodNotSpecified_OriginalContentReturned_MemberData))] public async Task DecompressedResponse_MethodNotSpecified_OriginalContentReturned( string encodingName, Func compress, DecompressionMethods methods) { if (IsCurlHandler && encodingName == "br") { // 'Content-Encoding' response header with Brotli causes error // with some Linux distros of libcurl. return; } var expectedContent = new byte[12345]; new Random(42).NextBytes(expectedContent); var compressedContentStream = new MemoryStream(); using (Stream s = compress(compressedContentStream)) { await s.WriteAsync(expectedContent); } byte[] compressedContent = compressedContentStream.ToArray(); await LoopbackServer.CreateClientAndServerAsync(async uri => { using (HttpClientHandler handler = CreateHttpClientHandler()) using (var client = new HttpClient(handler)) { handler.AutomaticDecompression = methods; Assert.Equal(compressedContent, await client.GetByteArrayAsync(uri)); } }, async server => { await server.AcceptConnectionAsync(async connection => { await connection.ReadRequestHeaderAsync(); await connection.Writer.WriteAsync($"HTTP/1.1 200 OK\r\nContent-Encoding: {encodingName}\r\n\r\n"); await connection.Stream.WriteAsync(compressedContent); }); }); } [OuterLoop("Uses external servers")] [Theory, MemberData(nameof(CompressedServers))] public async Task GetAsync_SetAutomaticDecompression_ContentDecompressed(Uri server) { HttpClientHandler handler = CreateHttpClientHandler(); handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (var client = new HttpClient(handler)) { using (HttpResponseMessage response = await client.GetAsync(server)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); _output.WriteLine(responseContent); TestHelper.VerifyResponseBody( responseContent, response.Content.Headers.ContentMD5, false, null); } } } [OuterLoop("Uses external server")] [Theory, MemberData(nameof(CompressedServers))] public async Task GetAsync_SetAutomaticDecompression_HeadersRemoved(Uri server) { HttpClientHandler handler = CreateHttpClientHandler(); handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (var client = new HttpClient(handler)) using (HttpResponseMessage response = await client.GetAsync(server, HttpCompletionOption.ResponseHeadersRead)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.False(response.Content.Headers.Contains("Content-Encoding"), "Content-Encoding unexpectedly found"); Assert.False(response.Content.Headers.Contains("Content-Length"), "Content-Length unexpectedly found"); } } [Theory] #if netcoreapp [InlineData(DecompressionMethods.Brotli, "br", "")] [InlineData(DecompressionMethods.Brotli, "br", "br")] [InlineData(DecompressionMethods.Brotli, "br", "gzip")] [InlineData(DecompressionMethods.Brotli, "br", "gzip, deflate")] #endif [InlineData(DecompressionMethods.GZip, "gzip", "")] [InlineData(DecompressionMethods.Deflate, "deflate", "")] [InlineData(DecompressionMethods.GZip | DecompressionMethods.Deflate, "gzip, deflate", "")] [InlineData(DecompressionMethods.GZip, "gzip", "gzip")] [InlineData(DecompressionMethods.Deflate, "deflate", "deflate")] [InlineData(DecompressionMethods.GZip, "gzip", "deflate")] [InlineData(DecompressionMethods.GZip, "gzip", "br")] [InlineData(DecompressionMethods.Deflate, "deflate", "gzip")] [InlineData(DecompressionMethods.Deflate, "deflate", "br")] [InlineData(DecompressionMethods.GZip | DecompressionMethods.Deflate, "gzip, deflate", "gzip, deflate")] public async Task GetAsync_SetAutomaticDecompression_AcceptEncodingHeaderSentWithNoDuplicates( DecompressionMethods methods, string encodings, string manualAcceptEncodingHeaderValues) { if (IsCurlHandler) { // Skip these tests on CurlHandler, dotnet/corefx #29905. return; } if (!UseSocketsHttpHandler && (encodings.Contains("br") || manualAcceptEncodingHeaderValues.Contains("br"))) { // Brotli encoding only supported on SocketsHttpHandler. return; } await LoopbackServer.CreateServerAsync(async (server, url) => { HttpClientHandler handler = CreateHttpClientHandler(); handler.AutomaticDecompression = methods; using (var client = new HttpClient(handler)) { if (!string.IsNullOrEmpty(manualAcceptEncodingHeaderValues)) { client.DefaultRequestHeaders.Add("Accept-Encoding", manualAcceptEncodingHeaderValues); } Task clientTask = client.GetAsync(url); Task> serverTask = server.AcceptConnectionSendResponseAndCloseAsync(); await TaskTimeoutExtensions.WhenAllOrAnyFailed(new Task[] { clientTask, serverTask }); List requestLines = await serverTask; string requestLinesString = string.Join("\r\n", requestLines); _output.WriteLine(requestLinesString); Assert.InRange(Regex.Matches(requestLinesString, "Accept-Encoding").Count, 1, 1); Assert.InRange(Regex.Matches(requestLinesString, encodings).Count, 1, 1); if (!string.IsNullOrEmpty(manualAcceptEncodingHeaderValues)) { Assert.InRange(Regex.Matches(requestLinesString, manualAcceptEncodingHeaderValues).Count, 1, 1); } using (HttpResponseMessage response = await clientTask) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } }); } } }