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

            Assert.Throws <ArgumentNullException> (() => new HttpProxyClient(null, 1080));
            Assert.Throws <ArgumentException> (() => new HttpProxyClient(string.Empty, 1080));
            Assert.Throws <ArgumentOutOfRangeException> (() => new HttpProxyClient(proxy.ProxyHost, -1));
            Assert.Throws <ArgumentNullException> (() => new HttpProxyClient(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, 80));
            Assert.Throws <ArgumentNullException> (() => proxy.Connect(null, 80, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 80));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 80, ConnectTimeout));

            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 80));
            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 80, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 80));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 80, 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));
        }
Example #2
0
        public void TestMethodNotAllowed()
        {
            var    proxy  = new HttpProxyClient("www.google.com", 80);
            Stream stream = null;

            try {
                stream = proxy.Connect("www.google.com", 80);
                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:80: 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();
            }
        }
Example #3
0
        public void TestConnectWithCredentials()
        {
            using (var server = new HttpProxyListener()) {
                server.Start(IPAddress.Loopback, 0);

                var    credentials = new NetworkCredential("username", "password");
                var    proxy       = new HttpProxyClient(server.IPAddress.ToString(), server.Port, credentials);
                Stream stream      = null;

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