示例#1
0
        public async Task TestExceptionalAsync(string scheme, string host, bool useAuth, ICredentials?credentials, string exceptionMessage)
        {
            using LoopbackSocksServer proxy = useAuth ? LoopbackSocksServer.Create("DOTNET", "424242") : LoopbackSocksServer.Create();
            using HttpClientHandler handler = CreateHttpClientHandler();
            using HttpClient client         = CreateHttpClient(handler);

            handler.Proxy = new WebProxy($"{scheme}://127.0.0.1:{proxy.Port}")
            {
                Credentials = credentials
            };

            HttpRequestMessage request = CreateRequest(HttpMethod.Get, new Uri($"http://{host}/"), UseVersion, exactVersion: true);

            // SocksException is not public
            var ex = await Assert.ThrowsAnyAsync <IOException>(() => client.SendAsync(TestAsync, request));

            Assert.Equal(exceptionMessage, ex.Message);
            Assert.Equal("SocksException", ex.GetType().Name);
        }
示例#2
0
        public async Task TestLoopbackAsync(string scheme, bool useSsl, bool useAuth, string host)
        {
            if (useSsl && UseVersion == HttpVersion.Version20 && !PlatformDetection.SupportsAlpn)
            {
                return;
            }

            await LoopbackServerFactory.CreateClientAndServerAsync(
                async uri =>
            {
                using LoopbackSocksServer proxy = useAuth ? LoopbackSocksServer.Create("DOTNET", "424242") : LoopbackSocksServer.Create();
                using HttpClientHandler handler = CreateHttpClientHandler();
                using HttpClient client         = CreateHttpClient(handler);

                handler.Proxy = new WebProxy($"{scheme}://127.0.0.1:{proxy.Port}");
                handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;

                if (useAuth)
                {
                    handler.Proxy.Credentials = new NetworkCredential("DOTNET", "424242");
                }

                uri = new UriBuilder(uri)
                {
                    Host = host
                }.Uri;

                HttpRequestMessage request = CreateRequest(HttpMethod.Get, uri, UseVersion, exactVersion: true);

                using HttpResponseMessage response = await client.SendAsync(TestAsync, request);
                string responseString = await response.Content.ReadAsStringAsync();
                Assert.Equal("Echo", responseString);
            },
                async server => await server.HandleRequestAsync(content: "Echo"),
                options : new GenericLoopbackOptions
            {
                UseSsl  = useSsl,
                Address = host == "::1" ? IPAddress.IPv6Loopback : IPAddress.Loopback
            });
        }