using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using Umbraco.Tests.TestHelpers; using Umbraco.Web.PublishedCache.LegacyXmlCache; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic.language; using Umbraco.Web.Routing; using System.Configuration; namespace Umbraco.Tests.Routing { [TestFixture] public class UrlsWithNestedDomains : BaseRoutingTest { // in the case of nested domains more than 1 url may resolve to a document // but only one route can be cached - the 'canonical' route ie the route // using the closest domain to the node - here we test that if we request // a non-canonical route, it is not cached / the cache is not polluted [Test] public void DoNotPolluteCache() { SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains SettingsForTests.UseDomainPrefixes = false; InitializeLanguagesAndDomains(); SetDomains1(); RoutingContext routingContext; string url = "http://domain1.com/1001-1/1001-1-1"; // get the nice url for 100111 routingContext = GetRoutingContext(url); Assert.AreEqual("http://domain2.com/1001-1-1/", routingContext.UrlProvider.GetUrl(100111, true)); // check that the proper route has been cached var cache = routingContext.UmbracoContext.ContentCache.InnerCache as PublishedContentCache; if (cache == null) throw new Exception("Unsupported IPublishedContentCache, only the legacy one is supported."); var cachedRoutes = cache.RoutesCache.GetCachedRoutes(); Assert.AreEqual("10011/1001-1-1", cachedRoutes[100111]); // route a rogue url url = "http://domain1.com/1001-1/1001-1-1"; var uri = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url var pcr = new PublishedContentRequest(uri, routingContext); pcr.Engine.FindDomain(); Assert.IsTrue(pcr.HasDomain); // check that it's been routed var lookup = new ContentFinderByNiceUrl(); var result = lookup.TryFindContent(pcr); Assert.IsTrue(result); Assert.AreEqual(100111, pcr.PublishedContent.Id); // has the cache been polluted? cachedRoutes = cache.RoutesCache.GetCachedRoutes(); Assert.AreEqual("10011/1001-1-1", cachedRoutes[100111]); // no //Assert.AreEqual("1001/1001-1/1001-1-1", cachedRoutes[100111]); // yes // what's the nice url now? Assert.AreEqual("http://domain2.com/1001-1-1/", routingContext.UrlProvider.GetUrl(100111)); // good //Assert.AreEqual("http://domain1.com/1001-1/1001-1-1", routingContext.NiceUrlProvider.GetNiceUrl(100111, true)); // bad } public override void Initialize() { base.Initialize(); // ensure we can create them although the content is not in the database TestHelper.DropForeignKeys("umbracoDomains"); } protected override void FreezeResolution() { SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper()); base.FreezeResolution(); } void InitializeLanguagesAndDomains() { var domains = Domain.GetDomains(); foreach (var d in domains) d.Delete(); var langs = Language.GetAllAsList(); foreach (var l in langs.Skip(1)) l.Delete(); Language.MakeNew("fr-FR"); } void SetDomains1() { var langEn = Language.GetByCultureCode("en-US"); var langFr = Language.GetByCultureCode("fr-FR"); Domain.MakeNew("http://domain1.com/", 1001, langEn.id); Domain.MakeNew("http://domain2.com/", 10011, langEn.id); } protected override string GetXmlContent(int templateId) { return @" ]> This is some content]]> This is some content]]> This is some content]]> This is some content]]> "; } } }