mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 08:49:04 -10:00
Commit migrated from https://github.com/dotnet/corefx/commit/b26e2a66e5c41c3263caf5fdaa8a1811dd360e33
261 lines
11 KiB
C#
261 lines
11 KiB
C#
// 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<object[]> DecompressedResponse_MethodSpecified_DecompressedContentReturned_MemberData()
|
|
{
|
|
foreach (bool specifyAllMethods in new[] { false, true })
|
|
{
|
|
yield return new object[]
|
|
{
|
|
"deflate",
|
|
new Func<Stream, Stream>(s => new DeflateStream(s, CompressionLevel.Optimal, leaveOpen: true)),
|
|
specifyAllMethods ? DecompressionMethods.Deflate : DecompressionMethods.All
|
|
};
|
|
yield return new object[]
|
|
{
|
|
"gzip",
|
|
new Func<Stream, Stream>(s => new GZipStream(s, CompressionLevel.Optimal, leaveOpen: true)),
|
|
specifyAllMethods ? DecompressionMethods.GZip : DecompressionMethods.All
|
|
};
|
|
yield return new object[]
|
|
{
|
|
"br",
|
|
new Func<Stream, Stream>(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<Stream, Stream> 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<byte>(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<object[]> DecompressedResponse_MethodNotSpecified_OriginalContentReturned_MemberData()
|
|
{
|
|
yield return new object[]
|
|
{
|
|
"deflate",
|
|
new Func<Stream, Stream>(s => new DeflateStream(s, CompressionLevel.Optimal, leaveOpen: true)),
|
|
DecompressionMethods.None
|
|
};
|
|
yield return new object[]
|
|
{
|
|
"gzip",
|
|
new Func<Stream, Stream>(s => new GZipStream(s, CompressionLevel.Optimal, leaveOpen: true)),
|
|
DecompressionMethods.Brotli
|
|
};
|
|
yield return new object[]
|
|
{
|
|
"br",
|
|
new Func<Stream, Stream>(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<Stream, Stream> 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<byte>(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<HttpResponseMessage> clientTask = client.GetAsync(url);
|
|
Task<List<string>> serverTask = server.AcceptConnectionSendResponseAndCloseAsync();
|
|
await TaskTimeoutExtensions.WhenAllOrAnyFailed(new Task[] { clientTask, serverTask });
|
|
|
|
List<string> 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);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|