public void RedirectProcessorHandlesLowercasedRedirects()
        {
            var configuration =
                TestData.TestData.DefaultConfiguration;
            var urlParser      = new UrlParser();
            var urlFormatter   = new UrlFormatter();
            var redirectParser = new RedirectParser(
                configuration,
                urlParser,
                urlFormatter);

            // create redirect processor
            var testHttpClient =
                new TestHttpClient();
            var redirectProcessor = new RedirectProcessor(
                configuration,
                new UrlHelper(
                    configuration,
                    urlParser,
                    urlFormatter),
                testHttpClient,
                urlParser,
                urlFormatter,
                new RedirectHelper(
                    configuration,
                    urlParser,
                    urlFormatter));

            // parse redirects with uppercased old url
            var parsedRedirects = TestData.TestData.GetParsedRedirects(
                configuration,
                new[]
            {
                new Redirect
                {
                    OldUrl = "/Url1",
                    NewUrl = "/url2"
                }
            })
                                  .ToList();

            // simulate uppercased url redirects to lowercased url
            testHttpClient.Responses[
                parsedRedirects[0].OldUrl.Formatted] =
                new HttpResponse
            {
                StatusCode = 301,
                Headers    = new Dictionary <string, string>
                {
                    { "Location", "/url1" }
                }
            };

            // preload parsed redirects
            redirectProcessor.PreloadParsedRedirects(
                parsedRedirects);

            // process redirects
            var processedRedirect = new ProcessedRedirect
            {
                ParsedRedirect = parsedRedirects[0]
            };

            redirectProcessor.Process(
                processedRedirect);

            // verify
            var urlResponseResult = processedRedirect
                                    .Results
                                    .FirstOrDefault(x => x.Type == ResultTypes.UrlResponse);

            Assert.IsNotNull(urlResponseResult);
            Assert.AreEqual(
                "http://www.test.local/url2",
                urlResponseResult.Url);
        }
        public void RedirectProcessorRedirectsOldUrlWithReplaceType()
        {
            var configuration =
                TestData.TestData.DefaultConfiguration;
            var urlParser      = new UrlParser();
            var urlFormatter   = new UrlFormatter();
            var redirectParser = new RedirectParser(
                configuration,
                urlParser,
                urlFormatter);

            // create redirect processor
            var testHttpClient =
                new TestHttpClient();
            var redirectProcessor = new RedirectProcessor(
                configuration,
                new UrlHelper(
                    configuration,
                    urlParser,
                    urlFormatter),
                testHttpClient,
                urlParser,
                urlFormatter,
                new RedirectHelper(
                    configuration,
                    urlParser,
                    urlFormatter));

            // parse redirect with old url replace type
            var parsedRedirects = TestData.TestData.GetParsedRedirects(
                configuration,
                new[]
            {
                new Redirect
                {
                    OldUrl       = "/url1",
                    NewUrl       = "/url2",
                    RedirectType = RedirectType.Replace
                }
            })
                                  .ToList();

            // parse redirects to verify replace redirect works
            var parsedRedirects2 = TestData.TestData.GetParsedRedirects(
                configuration,
                new[]
            {
                new Redirect
                {
                    OldUrl = "/url1/replace/redirect",
                    NewUrl = "/url2/replace/redirect"
                }
            })
                                   .ToList();

            // preload parsed redirects
            redirectProcessor.PreloadParsedRedirects(
                parsedRedirects);

            var processedRedirects = new []
            {
                new ProcessedRedirect
                {
                    ParsedRedirect = parsedRedirects[0]
                },
                new ProcessedRedirect
                {
                    ParsedRedirect = parsedRedirects2[0]
                }
            };

            // process redirects
            foreach (var processedRedirect in processedRedirects)
            {
                redirectProcessor.Process(
                    processedRedirect);
            }

            // verify
            var urlResponseResult = processedRedirects[1]
                                    .Results
                                    .FirstOrDefault(x => x.Type == ResultTypes.UrlResponse);

            Assert.IsNotNull(urlResponseResult);
            Assert.AreEqual(
                "http://www.test.local/url2/replace/redirect",
                urlResponseResult.Url);
        }
        public void RedirectProcessorCachesResponse()
        {
            var configuration =
                TestData.TestData.DefaultConfiguration;
            var urlParser      = new UrlParser();
            var urlFormatter   = new UrlFormatter();
            var redirectParser = new RedirectParser(
                configuration,
                urlParser,
                urlFormatter);

            // create redirect processor
            var testHttpClient =
                new TestHttpClient();
            var redirectProcessor = new RedirectProcessor(
                configuration,
                new UrlHelper(
                    configuration,
                    urlParser,
                    urlFormatter),
                testHttpClient,
                urlParser,
                urlFormatter,
                new RedirectHelper(
                    configuration,
                    urlParser,
                    urlFormatter));

            // create and parse redirects
            var redirects = new List <IRedirect>
            {
                new Redirect
                {
                    OldUrl = "/url1",
                    NewUrl = "/url3"
                },
                new Redirect
                {
                    OldUrl = "/url2",
                    NewUrl = "/url3"
                }
            };
            var parsedRedirects = new List <IParsedRedirect>();

            foreach (var redirect in redirects)
            {
                parsedRedirects.Add(
                    redirectParser.ParseRedirect(
                        redirect));
            }

            // preload parsed redirects
            redirectProcessor.PreloadParsedRedirects(
                parsedRedirects);

            // verify controlled http client doesn't have any responses
            Assert.AreEqual(
                0,
                testHttpClient.Responses.Count);

            // process redirects and verify responses are cached by overriding responses
            UrlResponseResult urlResponseResult = null;
            var processedRedirects = new List <IProcessedRedirect>();

            foreach (var parsedRedirect in parsedRedirects)
            {
                var processedRedirect = new ProcessedRedirect
                {
                    ParsedRedirect = parsedRedirect
                };

                redirectProcessor.Process(
                    processedRedirect);

                // get url response result, if url response result is null and
                // controlled http client has a response for old url
                if (urlResponseResult == null &&
                    testHttpClient.Responses.ContainsKey(
                        parsedRedirect.NewUrl.Formatted))
                {
                    urlResponseResult = processedRedirect.Results
                                        .FirstOrDefault(r => r.Type.Equals(
                                                            ResultTypes.UrlResponse)) as UrlResponseResult;
                }
                else
                {
                    // override response with forbidden status code
                    testHttpClient.Responses[
                        parsedRedirect.NewUrl.Formatted] =
                        new HttpResponse
                    {
                        StatusCode = 401
                    };
                }
            }

            // verify url response result for /url3 has status code ok and not forbidden
            Assert.IsNotNull(
                urlResponseResult);
            Assert.AreEqual(
                "http://www.test.local/url3",
                urlResponseResult.Url);
            Assert.AreEqual(
                404,
                urlResponseResult.StatusCode
                );
        }