mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
Commit migrated from https://github.com/dotnet/corefx/commit/6943fc7c0e92359e6335a618f6ff5ee8402af4ac
795 lines
34 KiB
C#
795 lines
34 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.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net.Security;
|
|
using System.Net.Sockets;
|
|
using System.Net.Test.Common;
|
|
using System.Security.Authentication;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
public sealed class SocketsHttpHandler_HttpProtocolTests : HttpProtocolTests
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpProtocolTests_Dribble : HttpProtocolTests_Dribble
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientTest : HttpClientTest
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_DiagnosticsTest : DiagnosticsTest
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientEKUTest : HttpClientEKUTest
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_DangerousAcceptAllCertificatesValidator_Test : HttpClientHandler_DangerousAcceptAllCertificatesValidator_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_ClientCertificates_Test : HttpClientHandler_ClientCertificates_Test
|
|
{
|
|
public SocketsHttpHandler_HttpClientHandler_ClientCertificates_Test(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_DefaultProxyCredentials_Test : HttpClientHandler_DefaultProxyCredentials_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_MaxConnectionsPerServer_Test : HttpClientHandler_MaxConnectionsPerServer_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_ServerCertificates_Test : HttpClientHandler_ServerCertificates_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_PostScenarioTest : PostScenarioTest
|
|
{
|
|
public SocketsHttpHandler_PostScenarioTest(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_ResponseStreamTest : ResponseStreamTest
|
|
{
|
|
public SocketsHttpHandler_ResponseStreamTest(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_SslProtocols_Test : HttpClientHandler_SslProtocols_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_SchSendAuxRecordHttpTest : SchSendAuxRecordHttpTest
|
|
{
|
|
public SocketsHttpHandler_SchSendAuxRecordHttpTest(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientMiniStress : HttpClientMiniStress
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandlerTest : HttpClientHandlerTest
|
|
{
|
|
public SocketsHttpHandler_HttpClientHandlerTest(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_DefaultCredentialsTest : DefaultCredentialsTest
|
|
{
|
|
public SocketsHttpHandler_DefaultCredentialsTest(ITestOutputHelper output) : base(output) { }
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_IdnaProtocolTests : IdnaProtocolTests
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
protected override bool SupportsIdna => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpRetryProtocolTests : HttpRetryProtocolTests
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpCookieProtocolTests : HttpCookieProtocolTests
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test : HttpClientHandler_Cancellation_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_MaxResponseHeadersLength_Test : HttpClientHandler_MaxResponseHeadersLength_Test
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_DuplexCommunication_Test : HttpClientTestBase
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
|
|
[Fact]
|
|
public async Task SendBytesBackAndForthBetweenClientAndServer_Success()
|
|
{
|
|
using (HttpClient client = CreateHttpClient())
|
|
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
|
{
|
|
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
|
listener.Listen(1);
|
|
var ep = (IPEndPoint)listener.LocalEndPoint;
|
|
|
|
var clientToServerStream = new ProducerConsumerStream();
|
|
clientToServerStream.WriteByte(0);
|
|
|
|
var reqMsg = new HttpRequestMessage
|
|
{
|
|
RequestUri = new Uri($"http://{ep.Address}:{ep.Port}/"),
|
|
Content = new StreamContent(clientToServerStream),
|
|
};
|
|
Task<HttpResponseMessage> req = client.SendAsync(reqMsg, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
using (Socket server = await listener.AcceptAsync())
|
|
using (var serverStream = new NetworkStream(server, ownsSocket: false))
|
|
{
|
|
// Skip request headers.
|
|
while (true)
|
|
{
|
|
if (serverStream.ReadByte() == '\r')
|
|
{
|
|
serverStream.ReadByte();
|
|
break;
|
|
}
|
|
while (serverStream.ReadByte() != '\r') { }
|
|
serverStream.ReadByte();
|
|
}
|
|
|
|
// Send response headers.
|
|
await server.SendAsync(
|
|
new ArraySegment<byte>(Encoding.ASCII.GetBytes($"HTTP/1.1 200 OK\r\nConnection: close\r\nDate: {DateTimeOffset.UtcNow:R}\r\n\r\n")),
|
|
SocketFlags.None);
|
|
|
|
HttpResponseMessage resp = await req;
|
|
Stream serverToClientStream = await resp.Content.ReadAsStreamAsync();
|
|
|
|
// Communication should now be open between the client and server.
|
|
// Ping pong bytes back and forth.
|
|
for (byte i = 0; i < 100; i++)
|
|
{
|
|
// Send a byte from the client to the server. The server will receive
|
|
// the byte as a chunk.
|
|
if (i > 0) clientToServerStream.WriteByte(i); // 0 was already seeded when the stream was created above
|
|
Assert.Equal('1', serverStream.ReadByte());
|
|
Assert.Equal('\r', serverStream.ReadByte());
|
|
Assert.Equal('\n', serverStream.ReadByte());
|
|
Assert.Equal(i, serverStream.ReadByte());
|
|
Assert.Equal('\r', serverStream.ReadByte());
|
|
Assert.Equal('\n', serverStream.ReadByte());
|
|
|
|
// Send a byte from the server to the client. The client will receive
|
|
// the byte on its own, with HttpClient stripping away the chunk encoding.
|
|
serverStream.WriteByte(i);
|
|
Assert.Equal(i, serverToClientStream.ReadByte());
|
|
}
|
|
|
|
clientToServerStream.DoneWriting();
|
|
server.Shutdown(SocketShutdown.Send);
|
|
Assert.Equal(-1, clientToServerStream.ReadByte());
|
|
}
|
|
}
|
|
}
|
|
|
|
private sealed class ProducerConsumerStream : Stream
|
|
{
|
|
private readonly BlockingCollection<byte[]> _buffers = new BlockingCollection<byte[]>();
|
|
private ArraySegment<byte> _remaining;
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
if (count > 0)
|
|
{
|
|
byte[] tmp = new byte[count];
|
|
Buffer.BlockCopy(buffer, offset, tmp, 0, count);
|
|
_buffers.Add(tmp);
|
|
}
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
if (count > 0)
|
|
{
|
|
if (_remaining.Count == 0)
|
|
{
|
|
if (!_buffers.TryTake(out byte[] tmp, Timeout.Infinite))
|
|
{
|
|
return 0;
|
|
}
|
|
_remaining = new ArraySegment<byte>(tmp, 0, tmp.Length);
|
|
}
|
|
|
|
if (_remaining.Count <= count)
|
|
{
|
|
count = _remaining.Count;
|
|
Buffer.BlockCopy(_remaining.Array, _remaining.Offset, buffer, offset, count);
|
|
_remaining = default(ArraySegment<byte>);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(_remaining.Array, _remaining.Offset, buffer, offset, count);
|
|
_remaining = new ArraySegment<byte>(_remaining.Array, _remaining.Offset + count, _remaining.Count - count);
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
public void DoneWriting() => _buffers.CompleteAdding();
|
|
|
|
public override bool CanRead => true;
|
|
public override bool CanSeek => false;
|
|
public override bool CanWrite => true;
|
|
public override long Length => throw new NotImplementedException();
|
|
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
public override void Flush() { }
|
|
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
|
|
public override void SetLength(long value) => throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_ConnectionUpgrade_Test : HttpClientTestBase
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
|
|
[Fact]
|
|
public async Task UpgradeConnection_Success()
|
|
{
|
|
await LoopbackServer.CreateServerAsync(async (server, url) =>
|
|
{
|
|
using (HttpClient client = CreateHttpClient())
|
|
{
|
|
// We need to use ResponseHeadersRead here, otherwise we will hang trying to buffer the response body.
|
|
Task<HttpResponseMessage> getResponseTask = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
|
await server.AcceptConnectionAsync(async connection =>
|
|
{
|
|
Task<List<string>> serverTask = connection.ReadRequestHeaderAndSendCustomResponseAsync($"HTTP/1.1 101 Switching Protocols\r\nDate: {DateTimeOffset.UtcNow:R}\r\n\r\n");
|
|
|
|
await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask);
|
|
|
|
using (Stream clientStream = await (await getResponseTask).Content.ReadAsStreamAsync())
|
|
{
|
|
Assert.True(clientStream.CanWrite);
|
|
Assert.True(clientStream.CanRead);
|
|
Assert.False(clientStream.CanSeek);
|
|
|
|
TextReader clientReader = new StreamReader(clientStream);
|
|
TextWriter clientWriter = new StreamWriter(clientStream) { AutoFlush = true };
|
|
TextReader serverReader = connection.Reader;
|
|
TextWriter serverWriter = connection.Writer;
|
|
|
|
const string helloServer = "hello server";
|
|
const string helloClient = "hello client";
|
|
const string goodbyeServer = "goodbye server";
|
|
const string goodbyeClient = "goodbye client";
|
|
|
|
clientWriter.WriteLine(helloServer);
|
|
Assert.Equal(helloServer, serverReader.ReadLine());
|
|
serverWriter.WriteLine(helloClient);
|
|
Assert.Equal(helloClient, clientReader.ReadLine());
|
|
clientWriter.WriteLine(goodbyeServer);
|
|
Assert.Equal(goodbyeServer, serverReader.ReadLine());
|
|
serverWriter.WriteLine(goodbyeClient);
|
|
Assert.Equal(goodbyeClient, clientReader.ReadLine());
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_HttpClientHandler_ConnectionPooling_Test : HttpClientTestBase
|
|
{
|
|
protected override bool UseSocketsHttpHandler => true;
|
|
|
|
// TODO: Currently the subsequent tests sometimes fail/hang with WinHttpHandler / CurlHandler.
|
|
// In theory they should pass with any handler that does appropriate connection pooling.
|
|
// We should understand why they sometimes fail there and ideally move them to be
|
|
// used by all handlers this test project tests.
|
|
|
|
[Fact]
|
|
public async Task MultipleIterativeRequests_SameConnectionReused()
|
|
{
|
|
using (HttpClient client = CreateHttpClient())
|
|
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
|
{
|
|
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
|
listener.Listen(1);
|
|
var ep = (IPEndPoint)listener.LocalEndPoint;
|
|
var uri = new Uri($"http://{ep.Address}:{ep.Port}/");
|
|
|
|
string responseBody =
|
|
"HTTP/1.1 200 OK\r\n" +
|
|
$"Date: {DateTimeOffset.UtcNow:R}\r\n" +
|
|
"Content-Length: 0\r\n" +
|
|
"\r\n";
|
|
|
|
Task<string> firstRequest = client.GetStringAsync(uri);
|
|
using (Socket server = await listener.AcceptAsync())
|
|
using (var serverStream = new NetworkStream(server, ownsSocket: false))
|
|
using (var serverReader = new StreamReader(serverStream))
|
|
{
|
|
while (!string.IsNullOrWhiteSpace(await serverReader.ReadLineAsync()));
|
|
await server.SendAsync(new ArraySegment<byte>(Encoding.ASCII.GetBytes(responseBody)), SocketFlags.None);
|
|
await firstRequest;
|
|
|
|
Task<Socket> secondAccept = listener.AcceptAsync(); // shouldn't complete
|
|
|
|
Task<string> additionalRequest = client.GetStringAsync(uri);
|
|
while (!string.IsNullOrWhiteSpace(await serverReader.ReadLineAsync()));
|
|
await server.SendAsync(new ArraySegment<byte>(Encoding.ASCII.GetBytes(responseBody)), SocketFlags.None);
|
|
await additionalRequest;
|
|
|
|
Assert.False(secondAccept.IsCompleted, $"Second accept should never complete");
|
|
}
|
|
}
|
|
}
|
|
|
|
[OuterLoop("Incurs a delay")]
|
|
[Fact]
|
|
public async Task ServerDisconnectsAfterInitialRequest_SubsequentRequestUsesDifferentConnection()
|
|
{
|
|
using (HttpClient client = CreateHttpClient())
|
|
{
|
|
await LoopbackServer.CreateServerAsync(async (server, uri) =>
|
|
{
|
|
// Make multiple requests iteratively.
|
|
for (int i = 0; i < 2; i++)
|
|
{
|
|
Task<string> request = client.GetStringAsync(uri);
|
|
await server.AcceptConnectionSendResponseAndCloseAsync();
|
|
await request;
|
|
|
|
if (i == 0)
|
|
{
|
|
await Task.Delay(2000); // give client time to see the closing before next connect
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ServerSendsConnectionClose_SubsequentRequestUsesDifferentConnection()
|
|
{
|
|
using (HttpClient client = CreateHttpClient())
|
|
{
|
|
await LoopbackServer.CreateServerAsync(async (server, uri) =>
|
|
{
|
|
string responseBody =
|
|
"HTTP/1.1 200 OK\r\n" +
|
|
$"Date: {DateTimeOffset.UtcNow:R}\r\n" +
|
|
"Content-Length: 0\r\n" +
|
|
"Connection: close\r\n" +
|
|
"\r\n";
|
|
|
|
// Make first request.
|
|
Task<string> request1 = client.GetStringAsync(uri);
|
|
await server.AcceptConnectionAsync(async connection1 =>
|
|
{
|
|
await connection1.ReadRequestHeaderAndSendCustomResponseAsync(responseBody);
|
|
await request1;
|
|
|
|
// Make second request and expect it to be served from a different connection.
|
|
Task<string> request2 = client.GetStringAsync(uri);
|
|
await server.AcceptConnectionAsync(async connection2 =>
|
|
{
|
|
await connection2.ReadRequestHeaderAndSendCustomResponseAsync(responseBody);
|
|
await request2;
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("PooledConnectionLifetime")]
|
|
[InlineData("PooledConnectionIdleTimeout")]
|
|
public async Task SmallConnectionTimeout_SubsequentRequestUsesDifferentConnection(string timeoutPropertyName)
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
switch (timeoutPropertyName)
|
|
{
|
|
case "PooledConnectionLifetime": handler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(1); break;
|
|
case "PooledConnectionIdleTimeout": handler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(1); break;
|
|
default: throw new ArgumentOutOfRangeException(nameof(timeoutPropertyName));
|
|
}
|
|
|
|
using (HttpClient client = new HttpClient(handler))
|
|
{
|
|
await LoopbackServer.CreateServerAsync(async (server, uri) =>
|
|
{
|
|
// Make first request.
|
|
Task<string> request1 = client.GetStringAsync(uri);
|
|
await server.AcceptConnectionAsync(async connection =>
|
|
{
|
|
await connection.ReadRequestHeaderAndSendResponseAsync();
|
|
await request1;
|
|
|
|
// Wait a small amount of time before making the second request, to give the first request time to timeout.
|
|
await Task.Delay(100);
|
|
|
|
// Make second request and expect it to be served from a different connection.
|
|
Task<string> request2 = client.GetStringAsync(uri);
|
|
await server.AcceptConnectionAsync(async connection2 =>
|
|
{
|
|
await connection2.ReadRequestHeaderAndSendResponseAsync();
|
|
await request2;
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public sealed class SocketsHttpHandler_PublicAPIBehavior_Test
|
|
{
|
|
private static async Task IssueRequestAsync(HttpMessageHandler handler)
|
|
{
|
|
using (var c = new HttpMessageInvoker(handler, disposeHandler: false))
|
|
await Assert.ThrowsAnyAsync<Exception>(() =>
|
|
c.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri("/shouldquicklyfail", UriKind.Relative)), default));
|
|
}
|
|
|
|
[Fact]
|
|
public void AllowAutoRedirect_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.True(handler.AllowAutoRedirect);
|
|
|
|
handler.AllowAutoRedirect = true;
|
|
Assert.True(handler.AllowAutoRedirect);
|
|
|
|
handler.AllowAutoRedirect = false;
|
|
Assert.False(handler.AllowAutoRedirect);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AutomaticDecompression_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(DecompressionMethods.None, handler.AutomaticDecompression);
|
|
|
|
handler.AutomaticDecompression = DecompressionMethods.GZip;
|
|
Assert.Equal(DecompressionMethods.GZip, handler.AutomaticDecompression);
|
|
|
|
handler.AutomaticDecompression = DecompressionMethods.Deflate;
|
|
Assert.Equal(DecompressionMethods.Deflate, handler.AutomaticDecompression);
|
|
|
|
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
|
Assert.Equal(DecompressionMethods.GZip | DecompressionMethods.Deflate, handler.AutomaticDecompression);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void CookieContainer_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
CookieContainer container = handler.CookieContainer;
|
|
Assert.Same(container, handler.CookieContainer);
|
|
|
|
var newContainer = new CookieContainer();
|
|
handler.CookieContainer = newContainer;
|
|
Assert.Same(newContainer, handler.CookieContainer);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Credentials_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Null(handler.Credentials);
|
|
|
|
var newCredentials = new NetworkCredential("username", "password");
|
|
handler.Credentials = newCredentials;
|
|
Assert.Same(newCredentials, handler.Credentials);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DefaultProxyCredentials_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Null(handler.DefaultProxyCredentials);
|
|
|
|
var newCredentials = new NetworkCredential("username", "password");
|
|
handler.DefaultProxyCredentials = newCredentials;
|
|
Assert.Same(newCredentials, handler.DefaultProxyCredentials);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void MaxAutomaticRedirections_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(50, handler.MaxAutomaticRedirections);
|
|
|
|
handler.MaxAutomaticRedirections = int.MaxValue;
|
|
Assert.Equal(int.MaxValue, handler.MaxAutomaticRedirections);
|
|
|
|
handler.MaxAutomaticRedirections = 1;
|
|
Assert.Equal(1, handler.MaxAutomaticRedirections);
|
|
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxAutomaticRedirections = 0);
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxAutomaticRedirections = -1);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void MaxConnectionsPerServer_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(int.MaxValue, handler.MaxConnectionsPerServer);
|
|
|
|
handler.MaxConnectionsPerServer = int.MaxValue;
|
|
Assert.Equal(int.MaxValue, handler.MaxConnectionsPerServer);
|
|
|
|
handler.MaxConnectionsPerServer = 1;
|
|
Assert.Equal(1, handler.MaxConnectionsPerServer);
|
|
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxConnectionsPerServer = 0);
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxConnectionsPerServer = -1);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void MaxResponseHeadersLength_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(64, handler.MaxResponseHeadersLength);
|
|
|
|
handler.MaxResponseHeadersLength = int.MaxValue;
|
|
Assert.Equal(int.MaxValue, handler.MaxResponseHeadersLength);
|
|
|
|
handler.MaxResponseHeadersLength = 1;
|
|
Assert.Equal(1, handler.MaxResponseHeadersLength);
|
|
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxResponseHeadersLength = 0);
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxResponseHeadersLength = -1);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PreAuthenticate_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.False(handler.PreAuthenticate);
|
|
|
|
handler.PreAuthenticate = false;
|
|
Assert.False(handler.PreAuthenticate);
|
|
|
|
handler.PreAuthenticate = true;
|
|
Assert.True(handler.PreAuthenticate);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PooledConnectionIdleTimeout_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(TimeSpan.FromMinutes(2), handler.PooledConnectionIdleTimeout);
|
|
|
|
handler.PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan;
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, handler.PooledConnectionIdleTimeout);
|
|
|
|
handler.PooledConnectionIdleTimeout = TimeSpan.FromSeconds(0);
|
|
Assert.Equal(TimeSpan.FromSeconds(0), handler.PooledConnectionIdleTimeout);
|
|
|
|
handler.PooledConnectionIdleTimeout = TimeSpan.FromSeconds(1);
|
|
Assert.Equal(TimeSpan.FromSeconds(1), handler.PooledConnectionIdleTimeout);
|
|
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.PooledConnectionIdleTimeout = TimeSpan.FromSeconds(-2));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PooledConnectionLifetime_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, handler.PooledConnectionLifetime);
|
|
|
|
handler.PooledConnectionLifetime = Timeout.InfiniteTimeSpan;
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, handler.PooledConnectionLifetime);
|
|
|
|
handler.PooledConnectionLifetime = TimeSpan.FromSeconds(0);
|
|
Assert.Equal(TimeSpan.FromSeconds(0), handler.PooledConnectionLifetime);
|
|
|
|
handler.PooledConnectionLifetime = TimeSpan.FromSeconds(1);
|
|
Assert.Equal(TimeSpan.FromSeconds(1), handler.PooledConnectionLifetime);
|
|
|
|
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => handler.PooledConnectionLifetime = TimeSpan.FromSeconds(-2));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Properties_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
IDictionary<string, object> props = handler.Properties;
|
|
Assert.NotNull(props);
|
|
Assert.Empty(props);
|
|
|
|
props.Add("hello", "world");
|
|
Assert.Equal(1, props.Count);
|
|
Assert.Equal("world", props["hello"]);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Proxy_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.Null(handler.Proxy);
|
|
|
|
var proxy = new WebProxy();
|
|
handler.Proxy = proxy;
|
|
Assert.Same(proxy, handler.Proxy);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void SslOptions_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
SslClientAuthenticationOptions options = handler.SslOptions;
|
|
Assert.NotNull(options);
|
|
|
|
Assert.True(options.AllowRenegotiation);
|
|
Assert.Null(options.ApplicationProtocols);
|
|
Assert.Equal(X509RevocationMode.NoCheck, options.CertificateRevocationCheckMode);
|
|
Assert.Null(options.ClientCertificates);
|
|
Assert.Equal(SslProtocols.None, options.EnabledSslProtocols);
|
|
Assert.Equal(EncryptionPolicy.RequireEncryption, options.EncryptionPolicy);
|
|
Assert.Null(options.LocalCertificateSelectionCallback);
|
|
Assert.Null(options.RemoteCertificateValidationCallback);
|
|
Assert.Null(options.TargetHost);
|
|
|
|
Assert.Same(options, handler.SslOptions);
|
|
|
|
var newOptions = new SslClientAuthenticationOptions();
|
|
handler.SslOptions = newOptions;
|
|
Assert.Same(newOptions, handler.SslOptions);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void UseCookies_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.True(handler.UseCookies);
|
|
|
|
handler.UseCookies = true;
|
|
Assert.True(handler.UseCookies);
|
|
|
|
handler.UseCookies = false;
|
|
Assert.False(handler.UseCookies);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void UseProxy_GetSet_Roundtrips()
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Assert.True(handler.UseProxy);
|
|
|
|
handler.UseProxy = false;
|
|
Assert.False(handler.UseProxy);
|
|
|
|
handler.UseProxy = true;
|
|
Assert.True(handler.UseProxy);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public async Task AfterDisposeSendAsync_GettersUsable_SettersThrow(bool dispose)
|
|
{
|
|
using (var handler = new SocketsHttpHandler())
|
|
{
|
|
Type expectedExceptionType;
|
|
if (dispose)
|
|
{
|
|
handler.Dispose();
|
|
expectedExceptionType = typeof(ObjectDisposedException);
|
|
}
|
|
else
|
|
{
|
|
await IssueRequestAsync(handler);
|
|
expectedExceptionType = typeof(InvalidOperationException);
|
|
}
|
|
|
|
Assert.True(handler.AllowAutoRedirect);
|
|
Assert.Equal(DecompressionMethods.None, handler.AutomaticDecompression);
|
|
Assert.NotNull(handler.CookieContainer);
|
|
Assert.Null(handler.Credentials);
|
|
Assert.Null(handler.DefaultProxyCredentials);
|
|
Assert.Equal(50, handler.MaxAutomaticRedirections);
|
|
Assert.Equal(int.MaxValue, handler.MaxConnectionsPerServer);
|
|
Assert.Equal(64, handler.MaxResponseHeadersLength);
|
|
Assert.False(handler.PreAuthenticate);
|
|
Assert.Equal(TimeSpan.FromMinutes(2), handler.PooledConnectionIdleTimeout);
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, handler.PooledConnectionLifetime);
|
|
Assert.NotNull(handler.Properties);
|
|
Assert.Null(handler.Proxy);
|
|
Assert.NotNull(handler.SslOptions);
|
|
Assert.True(handler.UseCookies);
|
|
Assert.True(handler.UseProxy);
|
|
|
|
Assert.Throws(expectedExceptionType, () => handler.AllowAutoRedirect = false);
|
|
Assert.Throws(expectedExceptionType, () => handler.AutomaticDecompression = DecompressionMethods.GZip);
|
|
Assert.Throws(expectedExceptionType, () => handler.CookieContainer = new CookieContainer());
|
|
Assert.Throws(expectedExceptionType, () => handler.Credentials = new NetworkCredential("anotheruser", "anotherpassword"));
|
|
Assert.Throws(expectedExceptionType, () => handler.DefaultProxyCredentials = new NetworkCredential("anotheruser", "anotherpassword"));
|
|
Assert.Throws(expectedExceptionType, () => handler.MaxAutomaticRedirections = 2);
|
|
Assert.Throws(expectedExceptionType, () => handler.MaxConnectionsPerServer = 2);
|
|
Assert.Throws(expectedExceptionType, () => handler.MaxResponseHeadersLength = 2);
|
|
Assert.Throws(expectedExceptionType, () => handler.PreAuthenticate = false);
|
|
Assert.Throws(expectedExceptionType, () => handler.PooledConnectionIdleTimeout = TimeSpan.FromSeconds(2));
|
|
Assert.Throws(expectedExceptionType, () => handler.PooledConnectionLifetime = TimeSpan.FromSeconds(2));
|
|
Assert.Throws(expectedExceptionType, () => handler.Proxy = new WebProxy());
|
|
Assert.Throws(expectedExceptionType, () => handler.SslOptions = new SslClientAuthenticationOptions());
|
|
Assert.Throws(expectedExceptionType, () => handler.UseCookies = false);
|
|
Assert.Throws(expectedExceptionType, () => handler.UseProxy = false);
|
|
}
|
|
}
|
|
}
|
|
}
|