예제 #1
0
        public void UrlsWithDifferentSchemeAreNotIdentical()
        {
            // create configuration without force http host patterns
            var configuration = TestData.TestData.DefaultConfiguration;

            configuration.ForceHttpHostPatterns = new List <string>();
            var urlFormatter = new UrlFormatter();
            var urlParser    = new UrlParser();
            var urlHelper    = new UrlHelper(
                configuration,
                urlParser,
                urlFormatter);

            // create urls
            var rawUrl1    = "http://www.test.local/url1";
            var rawUrl2    = "https://www.test.local/url1";
            var parsedUrl1 = urlParser.Parse(
                rawUrl1,
                configuration.DefaultUrl);
            var parsedUrl2 = urlParser.Parse(
                rawUrl2,
                configuration.DefaultUrl);
            var url1 = new Url
            {
                Raw       = rawUrl1,
                Parsed    = parsedUrl1,
                Formatted = urlFormatter.Format(
                    parsedUrl1)
            };
            var url2 = new Url
            {
                Raw       = rawUrl2,
                Parsed    = parsedUrl2,
                Formatted = urlFormatter.Format(
                    parsedUrl2)
            };

            // verify urls are identical using force http host pattern and one url has https scheme
            Assert.AreEqual(
                false,
                urlHelper.AreIdentical(
                    url1.Formatted,
                    url2.Formatted));
        }
        static int Main(string[] args)
        {
            // write webredirects title
            System.Console.ForegroundColor = ConsoleColor.Cyan;
            System.Console.WriteLine(
                string.Format(
                    "FirstRealize App WebRedirects v{0}",
                    Assembly.GetExecutingAssembly().GetName().Version));
            System.Console.WriteLine(string.Empty);

            if (args.Length == 0)
            {
                Usage();
                return(1);
            }

            // expand environment variables in arguments
            args = args.Select(x =>
                               Environment.ExpandEnvironmentVariables(x))
                   .ToArray();

            var argumentParser =
                new ArgumentParser(args);

            // parse arguments
            string configurationFile = argumentParser
                                       .ParseArgumentValue("^(-c|--config)");

            // write error, if configuration file argument is not defined
            if (string.IsNullOrWhiteSpace(configurationFile))
            {
                System.Console.WriteLine(
                    "ERROR: Configuration file argument is not defined");
                Usage();
                return(1);
            }

            // write progessing redirects
            System.Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine(
                string.Format(
                    "Reading configuration file '{0}'",
                    configurationFile));

            // write error, if configuration file doesn't exist
            if (!File.Exists(configurationFile))
            {
                System.Console.WriteLine(
                    "ERROR: Configuration file '{0}' doesn't exist",
                    configurationFile);
                Usage();
                return(1);
            }

            // load configuration file
            Configuration configuration;

            using (var configurationJsonReader = new ConfigurationJsonReader())
            {
                configuration = configurationJsonReader
                                .ReadConfiguationFile(configurationFile) as Configuration;
            }

            // write read configuration file done
            System.Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine("Done");
            System.Console.WriteLine(string.Empty);

            if (string.IsNullOrWhiteSpace(configuration.OutputDir))
            {
                configuration.OutputDir = Path.GetDirectoryName(
                    configurationFile);
            }

            // TODO: Apply if needed
            //ServicePointManager.SecurityProtocol =
            //	SecurityProtocolType.Tls12;

            // create http client depending use test http client
            var         urlParser = new UrlParser();
            IHttpClient httpClient;

            if (configuration.UseTestHttpClient)
            {
                httpClient = new TestHttpClient();
            }
            else
            {
                httpClient = new HttpClient(
                    configuration,
                    urlParser);
            }

            // create redirect engine
            var urlFormatter = new UrlFormatter();
            var urlHelper    = new UrlHelper(
                configuration,
                urlParser,
                urlFormatter);
            var redirectParser = new RedirectParser(
                configuration,
                urlParser,
                urlFormatter);
            var redirectEngine = new RedirectEngine(
                configuration,
                urlHelper,
                urlParser,
                urlFormatter,
                redirectParser,
                httpClient);

            // create processed redirect validator
            var processedRedirectValidator =
                new ProcessedRedirectValidator(
                    configuration,
                    urlHelper);

            // handle processed redirect event to show progress
            redirectEngine.RedirectProcessed += (o, e) =>
            {
                System.Console.ForegroundColor = ConsoleColor.Green;
                var result = ".";
                if (e.ProcessedRedirect.Results.Any(
                        r => r.Type.Equals(ResultTypes.UnknownErrorResult)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "X";
                }
                else if (e.ProcessedRedirect.Results.Any(
                             r => r.Type.Equals(ResultTypes.ExcludedRedirect)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "%";
                }
                else if (e.ProcessedRedirect.Results.Any(
                             r => r.Type.Equals(ResultTypes.InvalidResult)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "?";
                }
                else if (e.ProcessedRedirect.Results.Any(
                             r => r.Type.Equals(ResultTypes.IdenticalResult)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "=";
                }
                else if (e.ProcessedRedirect.Results.Any(
                             r => r.Type.Equals(ResultTypes.CyclicRedirect)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "O";
                }
                else if (e.ProcessedRedirect.Results.Any(
                             r => r.Type.Equals(ResultTypes.TooManyRedirects)))
                {
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    result = "*";
                }
                else
                {
                    var urlResponseResult = e
                                            .ProcessedRedirect
                                            .Results
                                            .OfType <UrlResponseResult>()
                                            .FirstOrDefault(r => r.Type.Equals(ResultTypes.UrlResponse));
                    if (urlResponseResult != null && !urlHelper.AreIdentical(
                            e.ProcessedRedirect.ParsedRedirect.NewUrl.Formatted,
                            urlResponseResult.Url))
                    {
                        System.Console.ForegroundColor = ConsoleColor.Yellow;
                        result = "!";
                    }
                }

                System.Console.Write(result);
            };

            // run redirect engine
            System.Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("Processing redirects");

            var redirectProcessingResult =
                redirectEngine.Run();

            System.Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine(string.Empty);
            System.Console.WriteLine("Done");
            System.Console.WriteLine(string.Empty);

            if (configuration.Mode == Mode.Process)
            {
                Reports(
                    configuration.OutputDir,
                    redirectProcessingResult,
                    processedRedirectValidator);
            }

            return(0);
        }