Exemple #1
0
        public void TestArgumentExceptions()
        {
            var credentials = new NetworkCredential("user", "password");
            var proxy       = new HttpsProxyClient("http.proxy.com", 0, credentials);

            Assert.Throws <ArgumentNullException> (() => new HttpsProxyClient(null, 1080));
            Assert.Throws <ArgumentException> (() => new HttpsProxyClient(string.Empty, 1080));
            Assert.Throws <ArgumentOutOfRangeException> (() => new HttpsProxyClient(proxy.ProxyHost, -1));
            Assert.Throws <ArgumentNullException> (() => new HttpsProxyClient(proxy.ProxyHost, 1080, null));

            Assert.AreEqual(1080, proxy.ProxyPort);
            Assert.AreEqual("http.proxy.com", proxy.ProxyHost);
            Assert.AreEqual(credentials, proxy.ProxyCredentials);

            Assert.Throws <ArgumentNullException> (() => proxy.Connect(null, 443));
            Assert.Throws <ArgumentNullException> (() => proxy.Connect(null, 443, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 443));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 443, ConnectTimeout));

            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 443));
            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 443, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 443));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 443, ConnectTimeout));

            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 0, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 0));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 0, ConnectTimeout));

            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 80, -ConnectTimeout));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 80, -ConnectTimeout));
        }
Exemple #2
0
        public async Task TestMethodNotAllowedAsync()
        {
            var    proxy  = new HttpsProxyClient("www.google.com", 443);
            Stream stream = null;

            try {
                stream = await proxy.ConnectAsync("www.google.com", 443);

                Assert.Fail("www.google.com is not an HTTP proxy, so CONNECT should have failed.");
            } catch (ProxyProtocolException ex) {
                // This is expected since this proxy does not support Socks4a
                Assert.AreEqual("Failed to connect to www.google.com:443: HTTP/1.1 405 Method Not Allowed", ex.Message);
            } catch (TimeoutException) {
                Assert.Inconclusive("Timed out.");
            } catch (Exception ex) {
                Assert.Fail(ex.Message);
            } finally {
                stream?.Dispose();
            }
        }
Exemple #3
0
        public async Task TestConnectWithCredentialsAsync()
        {
            using (var server = new HttpProxyListener(certificate)) {
                server.Start(IPAddress.Loopback, 0);

                var credentials = new NetworkCredential("username", "password");
                var proxy       = new HttpsProxyClient(server.IPAddress.ToString(), server.Port, credentials)
                {
                    ServerCertificateValidationCallback = (s, c, ch, e) => true,
                    CheckCertificateRevocation          = false
                };
                Stream stream = null;

                try {
                    stream = await proxy.ConnectAsync("www.google.com", 80, ConnectTimeout);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    stream?.Dispose();
                }
            }
        }