async uri =>
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
+ requestMessage.Version = UseVersion;
+ requestMessage.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
using HttpClientHandler handler = CreateHttpClientHandler();
handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
};
using HttpClient client = CreateHttpClient(handler);
+ client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
string response = await client.GetStringAsync(uri);
Assert.Equal("foo", response);
socketsHandler.ConnectCallback = (context, token) => new ValueTask<Stream>(clientStream);
using HttpClient client = CreateHttpClient(handler);
+ client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
string response = await client.GetStringAsync($"{(options.UseSsl ? "https" : "http")}://nowhere.invalid/foo");
Assert.Equal("foo", response);
};
using HttpClient client = CreateHttpClient(handler);
+ client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
Task<string> clientTask = client.GetStringAsync($"{(options.UseSsl ? "https" : "http")}://{guid}/foo");
Assert.Equal("foo", response);
}
- [Fact]
- public async Task ConnectCallback_ConnectionPrefix_Success()
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task ConnectCallback_ConnectionPrefix_Success(bool useSsl)
{
+ GenericLoopbackOptions options = new GenericLoopbackOptions() { UseSsl = useSsl };
+
byte[] RequestPrefix = Encoding.UTF8.GetBytes("request prefix\r\n");
byte[] ResponsePrefix = Encoding.UTF8.GetBytes("response prefix\r\n");
listenSocket.Listen();
using HttpClientHandler handler = CreateHttpClientHandler();
+ handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
var socketsHandler = (SocketsHttpHandler)GetUnderlyingSocketsHttpHandler(handler);
socketsHandler.ConnectCallback = async (context, token) =>
{
};
using HttpClient client = CreateHttpClient(handler);
+ client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
- Task<string> clientTask = client.GetStringAsync($"http://nowhere.invalid/foo");
+ Task<string> clientTask = client.GetStringAsync($"{(options.UseSsl ? "https" : "http")}://nowhere.invalid/foo");
Socket serverSocket = await listenSocket.AcceptAsync();
Stream serverStream = new NetworkStream(serverSocket, ownsSocket: true);
await serverStream.WriteAsync(ResponsePrefix);
- using GenericLoopbackConnection loopbackConnection = await LoopbackServerFactory.CreateConnectionAsync(socket: null, serverStream);
+ using GenericLoopbackConnection loopbackConnection = await LoopbackServerFactory.CreateConnectionAsync(socket: null, serverStream, options);
await loopbackConnection.InitializeConnectionAsync();
HttpRequestData requestData = await loopbackConnection.ReadRequestDataAsync();
Assert.Equal("foo", response);
}
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task ConnectCallback_ExceptionDuringCallback_ThrowsHttpRequestExceptionWithInnerException(bool useSsl)
+ {
+ Exception e = new Exception("hello!");
+
+ using HttpClientHandler handler = CreateHttpClientHandler();
+ handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
+ var socketsHandler = (SocketsHttpHandler)GetUnderlyingSocketsHttpHandler(handler);
+ socketsHandler.ConnectCallback = (context, token) =>
+ {
+ throw e;
+ };
+
+ using HttpClient client = CreateHttpClient(handler);
+ client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
+
+ HttpRequestException hre = await Assert.ThrowsAnyAsync<HttpRequestException>(async () => await client.GetAsync($"{(useSsl ? "https" : "http")}://nowhere.invalid/foo"));
+ Assert.Equal(e, hre.InnerException);
+ }
+
private static bool PlatformSupportsUnixDomainSockets => Socket.OSSupportsUnixDomainSockets;
}
public sealed class SocketsHttpHandlerTest_ConnectCallback_Http2 : SocketsHttpHandlerTest_ConnectCallback
{
public SocketsHttpHandlerTest_ConnectCallback_Http2(ITestOutputHelper output) : base(output) { }
+ protected override Version UseVersion => HttpVersion.Version20;
}
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.SupportsAlpn))]