Exemplo n.º 1
0
        public HttpConnection(IHostInput hostInput)
        {
            if (hostInput == null)
            {
                throw new ArgumentNullException(nameof(hostInput));
            }
            else if (string.IsNullOrEmpty(hostInput.Address))
            {
                throw new ArgumentException(nameof(hostInput.Address));
            }
            else if (hostInput.TimeOut.TotalMilliseconds <= 0 || hostInput.TimeOut.TotalMilliseconds > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(hostInput.TimeOut));
            }
            else
            {
                IHostInputCheck inputCheck = new HostInputValidate();

                if (!inputCheck.Check(hostInput.Address, Constant.HttpUrlOrIpCheckPattern))
                {
                    throw new FormatException(nameof(hostInput.Address));
                }

                HostInput = hostInput;
            }
        }
Exemplo n.º 2
0
        public void HostInputCheckHostOrPatternNullFailTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.ThrowsException <ArgumentNullException>(() => hostInputCheck.Check(null, Constant.HttpUrlOrIpCheckPattern));
            Assert.ThrowsException <ArgumentNullException>(() => hostInputCheck.Check("https://ya.ru", null));
        }
Exemplo n.º 3
0
        public TcpConnection(IHostInput hostInput)
        {
            if (hostInput == null)
            {
                throw new ArgumentNullException(nameof(hostInput));
            }
            else if (string.IsNullOrEmpty(hostInput.Address))
            {
                throw new ArgumentException(nameof(hostInput.Address));
            }
            else if (hostInput.TimeOut.TotalMilliseconds <= 0 || hostInput.TimeOut.TotalMilliseconds > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(hostInput.TimeOut));
            }
            else
            {
                var inputParser = new HostInputValidate();
                (_ip, _port) = inputParser.Parse(hostInput.Address);

                if (_port <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(_port));
                }

                HostInput = hostInput;
                _socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
        }
Exemplo n.º 4
0
        public void TcpHostInputParseFormatFailTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.ThrowsException <FormatException>(() => hostInputCheck.Parse("173.194.216.113"));
            Assert.ThrowsException <FormatException>(() => hostInputCheck.Parse("173.194.216:80"));
            Assert.ThrowsException <FormatException>(() => hostInputCheck.Parse("173.194.216.113:"));
        }
Exemplo n.º 5
0
        public void IcmpHostInputCheckFormatFailTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.IsFalse(hostInputCheck.Check("google", Constant.IcmpUrlOrIpCheckPattern));

            Assert.IsFalse(hostInputCheck.Check("173.194.216", Constant.IcmpUrlOrIpCheckPattern));
        }
Exemplo n.º 6
0
        public void IcmpHostInputSuccessTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.IsTrue(hostInputCheck.Check("google.com", Constant.IcmpUrlOrIpCheckPattern));

            Assert.IsTrue(hostInputCheck.Check("173.194.216.113", Constant.IcmpUrlOrIpCheckPattern));
        }
Exemplo n.º 7
0
        public void TcpHostInputParseSuccessTest()
        {
            var hostInputCheck = new HostInputValidate();

            (var ip, var port) = hostInputCheck.Parse("173.194.216.113:1234");

            Assert.AreEqual(ip, "173.194.216.113");
            Assert.AreEqual(port, 1234);
        }
Exemplo n.º 8
0
        public void HttpHostInputCheckFormatFailTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.IsFalse(hostInputCheck.Check("https:google.com", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsFalse(hostInputCheck.Check("https://google.com:", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsFalse(hostInputCheck.Check("https://173.194.216", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsFalse(hostInputCheck.Check("asd", Constant.HttpUrlOrIpCheckPattern));
        }
Exemplo n.º 9
0
        public void HttpHostInputCheckSuccessTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.IsTrue(hostInputCheck.Check("https://google.com", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsTrue(hostInputCheck.Check("https://google.com:80", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsTrue(hostInputCheck.Check("https://173.194.216.113", Constant.HttpUrlOrIpCheckPattern));

            Assert.IsTrue(hostInputCheck.Check("https://173.194.216.113:80", Constant.HttpUrlOrIpCheckPattern));
        }
Exemplo n.º 10
0
        public void TcpHostInputSuccessTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.IsTrue(hostInputCheck.Check("173.194.216.113", Constant.TcpIpCheckPattern));
        }
Exemplo n.º 11
0
        public void HostInputParseHostNullFailTest()
        {
            var hostInputCheck = new HostInputValidate();

            Assert.ThrowsException <ArgumentNullException>(() => hostInputCheck.Parse(null));
        }