﻿using Arch.CFramework.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web;
//using Moq;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

namespace Arch.CFramework.Utility.Test
{
    /// <summary>
    ///This is a test class for IPFuncTest and is intended
    ///to contain all IPFuncTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IPHelperTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Compare
        ///</summary>
        [TestMethod()]
        public void CompareTest()
        {
            string ipAddress1 = "192.168.83.52";
            string ipAddress2 = "192.168.83.57";
            int expected = -1;
            int actual = IPHelper.Compare( ipAddress1, ipAddress2 );
            Assert.AreEqual( expected, actual );

            ipAddress1 = "192.168.83.57";
            expected = 0;
            actual = IPHelper.Compare( ipAddress1, ipAddress2 );
            Assert.AreEqual( expected, actual );

            ipAddress1 = "192.168.83.68";
            expected = 1;
            actual = IPHelper.Compare( ipAddress1, ipAddress2 );
            Assert.AreEqual( expected, actual );

            ipAddress1 = "192.168.83.168";
            expected = 1;
            actual = IPHelper.Compare( ipAddress1, ipAddress2 );
            Assert.AreEqual( expected, actual );
        }

        ///// <summary>
        /////A test for GetRealUserHostAddress
        /////</summary>
        //[TestMethod()]
        //[HostType("ASP.NET")]
        //[UrlToTest("http://www.ctrip.com")]
        //public void GetUserHostAddressTest()
        //{
        //    //var realRequest = new HttpRequest( string.Empty, "http://www.ctrip.com", string.Empty );
        //    //var mockRequest = new Mock<HttpRequestWrapper>( realRequest ) { CallBase = true };
        //    //mockRequest
        //    //    .Setup( r => r.AppRelativeCurrentExecutionFilePath )
        //    //    .Returns( "~" + realRequest.CurrentExecutionFilePath );
        //    //var mockContext = new Mock<HttpContextBase>();
        //    //mockContext.Setup( c => c.Request ).Returns( mockRequest.Object );

        //    //Route route = new Route( realRequest.Url.ToString(), null );
        //    //RouteData routeData = route.GetRouteData( mockContext.Object );
        //    ////var realContext = new HttpContext( mockContext.Object, routeData );


        //    string expected = "127.0.0.1";
        //    string actual = IPHelper.GetUserHostAddress();
        //    Assert.AreEqual( expected, actual );
        //}

        /// <summary>
        ///A test for IsInRange
        ///</summary>
        [TestMethod()]
        public void IsInRangeTest()
        {
            string ipAddress = "192.168.83.45";
            string ipRangeLowerLimit = "192.168.0.0";
            string ipRangeUpperLimit = "192.168.255.100";
            bool actual = IPHelper.IsInRange( ipAddress, ipRangeLowerLimit, ipRangeUpperLimit );
            Assert.IsTrue( actual );

            ipAddress = "172.145.0.45";
            actual = IPHelper.IsInRange( ipAddress, ipRangeLowerLimit, ipRangeUpperLimit );
            Assert.IsFalse( actual );
        }
    }
}
