[TestCase("http://domain1.com/en/bar/nil", "en-US", 100111)] // ok, alias includes "en/"
        public void Lookup_By_Url_Alias_And_Domain(string inputUrl, string expectedCulture, int expectedNode)
        {
            SetDomains1();

            var routingContext = GetRoutingContext(inputUrl);
            var url = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url
            var pcr = new PublishedContentRequest(url, routingContext);
            // must lookup domain
            pcr.Engine.FindDomain();

            if (expectedNode > 0)
                Assert.AreEqual(expectedCulture, pcr.Culture.Name);

            var finder = new ContentFinderByUrlAlias();
            var result = finder.TryFindContent(pcr);

            if (expectedNode > 0)
            {
                Assert.IsTrue(result);
                Assert.AreEqual(pcr.PublishedContent.Id, expectedNode);
            }
            else
            {
                Assert.IsFalse(result);
            }
        }
        public void Lookup_By_Url_Alias(string urlAsString, int nodeMatch)
        {
            var routingContext = GetRoutingContext(urlAsString);
            var url = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url
            var docRequest = new PublishedContentRequest(url, routingContext);
            var lookup = new ContentFinderByUrlAlias();

            var result = lookup.TryFindContent(docRequest);

            Assert.IsTrue(result);
            Assert.AreEqual(docRequest.PublishedContent.Id, nodeMatch);
        }
        public static IContentFinder SubsituteFinder(INotFoundHandler handler)
        {
            IContentFinder finder = null;

            if (handler is global::umbraco.SearchForAlias)
            {
                finder = new ContentFinderByUrlAlias();
            }
            else if (handler is global::umbraco.SearchForProfile)
            {
                finder = new ContentFinderByProfile();
            }
            else if (handler is global::umbraco.SearchForTemplate)
            {
                finder = new ContentFinderByNiceUrlAndTemplate();
            }
            else if (handler is global::umbraco.handle404)
            {
                finder = new ContentFinderByLegacy404();
            }

            return(finder);
        }
        void HandlePageNotFound(PublishedContentRequest docRequest)
        {
            var url = NotFoundHandlerHelper.GetLegacyUrlForNotFoundHandlers();

            LogHelper.Debug <ContentFinderByNotFoundHandlers>("Running for legacy url='{0}'.", () => url);

            foreach (var handler in GetNotFoundHandlers())
            {
                IContentFinder finder      = null;
                var            handlerName = handler.GetType().FullName;

                LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}'.", () => handlerName);

                // replace with our own implementation
                if (handler is global::umbraco.SearchForAlias)
                {
                    finder = new ContentFinderByUrlAlias();
                }
                else if (handler is global::umbraco.SearchForProfile)
                {
                    finder = new ContentFinderByProfile();
                }
                else if (handler is global::umbraco.SearchForTemplate)
                {
                    finder = new ContentFinderByNiceUrlAndTemplate();
                }
                else if (handler is global::umbraco.handle404)
                {
                    finder = new ContentFinderByLegacy404();
                }

                if (finder != null)
                {
                    var finderName = finder.GetType().FullName;
                    LogHelper.Debug <ContentFinderByNotFoundHandlers>("Replace handler '{0}' by new finder '{1}'.", () => handlerName, () => finderName);
                    if (finder.TryFindContent(docRequest))
                    {
                        // do NOT set docRequest.PublishedContent again here as
                        // it would clear any template that the finder might have set
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Finder '{0}' found node with id={1}.", () => finderName, () => docRequest.PublishedContent.Id);
                        if (docRequest.Is404)
                        {
                            LogHelper.Debug <ContentFinderByNotFoundHandlers>("Finder '{0}' set status to 404.", () => finderName);
                        }

                        // if we found a document, break, don't look at more handler -- we're done
                        break;
                    }

                    // if we did not find a document, continue, look at other handlers
                    continue;
                }

                // else it's a legacy handler, run

                if (handler.Execute(url) && handler.redirectID > 0)
                {
                    var redirectId = handler.redirectID;
                    docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectId);

                    if (!docRequest.HasPublishedContent)
                    {
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' found node with id={1} which is not valid.", () => handlerName, () => redirectId);
                        break;
                    }

                    LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' found valid node with id={1}.", () => handlerName, () => redirectId);

                    if (docRequest.RoutingContext.UmbracoContext.HttpContext.Response.StatusCode == 404)
                    {
                        LogHelper.Debug <ContentFinderByNotFoundHandlers>("Handler '{0}' set status code to 404.", () => handlerName);
                        docRequest.Is404 = true;
                    }

                    //// check for caching
                    //if (handler.CacheUrl)
                    //{
                    //    if (url.StartsWith("/"))
                    //        url = "/" + url;

                    //    var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
                    //    var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
                    //    SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));

                    //    HttpContext.Current.Trace.Write("NotFoundHandler",
                    //        string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
                    //}

                    // if we found a document, break, don't look at more handler -- we're done
                    break;
                }

                // if we did not find a document, continue, look at other handlers
            }
        }
		void HandlePageNotFound(PublishedContentRequest docRequest)
        {			
			var url = NotFoundHandlerHelper.GetLegacyUrlForNotFoundHandlers();
            LogHelper.Debug<ContentFinderByNotFoundHandlers>("Running for legacy url='{0}'.", () => url);

            foreach (var handler in GetNotFoundHandlers())
            {
                IContentFinder finder = null;
                var handlerName = handler.GetType().FullName;

                LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}'.", () => handlerName);

                // replace with our own implementation
                if (handler is global::umbraco.SearchForAlias)
                    finder = new ContentFinderByUrlAlias();
                else if (handler is global::umbraco.SearchForProfile)
                    finder = new ContentFinderByProfile();
                else if (handler is global::umbraco.SearchForTemplate)
                    finder = new ContentFinderByNiceUrlAndTemplate();
                else if (handler is global::umbraco.handle404)
                    finder = new ContentFinderByLegacy404();

                if (finder != null)
                {
                    var finderName = finder.GetType().FullName;
                    LogHelper.Debug<ContentFinderByNotFoundHandlers>("Replace handler '{0}' by new finder '{1}'.", () => handlerName, () => finderName);
                    if (finder.TryFindContent(docRequest))
                    {
                        // do NOT set docRequest.PublishedContent again here as 
                        // it would clear any template that the finder might have set
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Finder '{0}' found node with id={1}.", () => finderName, () => docRequest.PublishedContent.Id);
                        if (docRequest.Is404)
                            LogHelper.Debug<ContentFinderByNotFoundHandlers>("Finder '{0}' set status to 404.", () => finderName);

                        // if we found a document, break, don't look at more handler -- we're done
                        break;
                    }

                    // if we did not find a document, continue, look at other handlers
                    continue;
                }

                // else it's a legacy handler, run

				if (handler.Execute(url) && handler.redirectID > 0)
				{
				    var redirectId = handler.redirectID;
                    docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectId);

                    if (!docRequest.HasPublishedContent)
                    {
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' found node with id={1} which is not valid.", () => handlerName, () => redirectId);
                        break;
                    }

                    LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' found valid node with id={1}.", () => handlerName, () => redirectId);

                    if (docRequest.RoutingContext.UmbracoContext.HttpContext.Response.StatusCode == 404)
                    {
                        LogHelper.Debug<ContentFinderByNotFoundHandlers>("Handler '{0}' set status code to 404.", () => handlerName);
                        docRequest.Is404 = true;
                    }

                    //// check for caching
                    //if (handler.CacheUrl)
                    //{
                    //    if (url.StartsWith("/"))
                    //        url = "/" + url;

                    //    var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
                    //    var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
                    //    SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));

                    //    HttpContext.Current.Trace.Write("NotFoundHandler",
                    //        string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
                    //}

                    // if we found a document, break, don't look at more handler -- we're done
                    break;
                }

                // if we did not find a document, continue, look at other handlers
            }
        }