diff --git a/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs b/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs index fda3683064..8edef0faaa 100644 --- a/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs @@ -10,6 +10,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.Membership; using Umbraco.Core.Serialization; using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; using Umbraco.Web; @@ -153,7 +154,7 @@ namespace Umbraco.Tests.Cache }; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( new TestUmbracoContextAccessor(), diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs index 48bf22d96e..0178b416af 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs @@ -75,9 +75,9 @@ namespace Umbraco.Tests.Cache.PublishedCache publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny())).Returns(publishedShapshot); var httpContext = _httpContextFactory.HttpContext; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext); _umbracoContext = new UmbracoContext( - httpContext, + httpContextAccessor, publishedSnapshotService.Object, new WebSecurity(httpContextAccessor, Mock.Of(), globalSettings, IOHelper), umbracoSettings, diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs index 614cbfa393..efc01856fd 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs @@ -69,9 +69,9 @@ namespace Umbraco.Tests.PublishedContent var httpContext = GetHttpContextFactory("http://umbraco.local/", routeData).HttpContext; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext); var umbracoContext = new UmbracoContext( - httpContext, + httpContextAccessor, publishedSnapshotService.Object, new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), diff --git a/src/Umbraco.Tests/Runtimes/StandaloneTests.cs b/src/Umbraco.Tests/Runtimes/StandaloneTests.cs index b0e0c1138b..6811f9f8de 100644 --- a/src/Umbraco.Tests/Runtimes/StandaloneTests.cs +++ b/src/Umbraco.Tests/Runtimes/StandaloneTests.cs @@ -193,10 +193,8 @@ namespace Umbraco.Tests.Runtimes Assert.AreEqual("test", content.Name); // need an UmbracoCOntext to access the cache - // FIXME: not exactly pretty, should not depend on HttpContext - var httpContext = Mock.Of(); var umbracoContextFactory = factory.GetInstance(); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(httpContext); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbracoContext = umbracoContextReference.UmbracoContext; // assert that there is no published document diff --git a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs index 8ec190a2eb..ac76b2ac2b 100644 --- a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs @@ -117,10 +117,10 @@ namespace Umbraco.Tests.Scoping var service = PublishedSnapshotService as PublishedSnapshotService; var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContext = new UmbracoContext( - httpContext, + httpContextAccessor, service, new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), umbracoSettings ?? SettingsForTests.GetDefaultUmbracoSettings(), diff --git a/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs b/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs index 3dabcc556f..bd0443585c 100644 --- a/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs +++ b/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs @@ -7,6 +7,7 @@ using Microsoft.Owin; using Moq; using NUnit.Framework; using Umbraco.Core; +using Umbraco.Tests.TestHelpers; using Umbraco.Web.Composing; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; @@ -28,10 +29,10 @@ namespace Umbraco.Tests.Security //should force app ctx to show not-configured ConfigurationManager.AppSettings.Set(Constants.AppSettings.ConfigurationStatus, ""); - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContext = new UmbracoContext( - Mock.Of(), + httpContextAccessor, Mock.Of(), new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), globalSettings, @@ -49,10 +50,10 @@ namespace Umbraco.Tests.Security [Test] public void ShouldAuthenticateRequest_When_Configured() { - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var globalSettings = TestObjects.GetGlobalSettings(); var umbCtx = new UmbracoContext( - Mock.Of(), + httpContextAccessor, Mock.Of(), new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), globalSettings, diff --git a/src/Umbraco.Tests/Templates/HtmlImageSourceParserTests.cs b/src/Umbraco.Tests/Templates/HtmlImageSourceParserTests.cs index 141a0a916d..23dde26069 100644 --- a/src/Umbraco.Tests/Templates/HtmlImageSourceParserTests.cs +++ b/src/Umbraco.Tests/Templates/HtmlImageSourceParserTests.cs @@ -78,7 +78,7 @@ namespace Umbraco.Tests.Templates mediaUrlProvider: mediaUrlProvider.Object, umbracoContextAccessor: umbracoContextAccessor); - using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of())) + using (var reference = umbracoContextFactory.EnsureUmbracoContext()) { var mediaCache = Mock.Get(reference.UmbracoContext.Media); mediaCache.Setup(x => x.GetById(It.IsAny())).Returns(media.Object); diff --git a/src/Umbraco.Tests/Templates/HtmlLocalLinkParserTests.cs b/src/Umbraco.Tests/Templates/HtmlLocalLinkParserTests.cs index 5560e39152..f5e32711bc 100644 --- a/src/Umbraco.Tests/Templates/HtmlLocalLinkParserTests.cs +++ b/src/Umbraco.Tests/Templates/HtmlLocalLinkParserTests.cs @@ -73,7 +73,7 @@ namespace Umbraco.Tests.Templates mediaUrlProvider: mediaUrlProvider.Object, umbracoContextAccessor: umbracoContextAccessor); - using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of())) + using (var reference = umbracoContextFactory.EnsureUmbracoContext()) { var contentCache = Mock.Get(reference.UmbracoContext.Content); contentCache.Setup(x => x.GetById(It.IsAny())).Returns(publishedContent.Object); diff --git a/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs b/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs index 9365c710b6..442f50aa3a 100644 --- a/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs +++ b/src/Umbraco.Tests/TestHelpers/ControllerTesting/TestControllerActivatorBase.cs @@ -137,7 +137,8 @@ namespace Umbraco.Tests.TestHelpers.ControllerTesting var umbracoContextAccessor = Umbraco.Web.Composing.Current.UmbracoContextAccessor; - var umbCtx = new UmbracoContext(httpContext, + var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext); + var umbCtx = new UmbracoContext(httpContextAccessor, publishedSnapshotService.Object, webSecurity.Object, Mock.Of(section => section.WebRouting == Mock.Of(routingSection => routingSection.UrlProviderMode == "Auto")), @@ -154,7 +155,7 @@ namespace Umbraco.Tests.TestHelpers.ControllerTesting urlHelper.Setup(provider => provider.GetUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(UrlInfo.Url("/hello/world/1234")); - var membershipHelper = new MembershipHelper(umbCtx.HttpContext, Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), AppCaches.Disabled, Mock.Of(), new MockShortStringHelper(), Mock.Of()); + var membershipHelper = new MembershipHelper(httpContext, Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), Mock.Of(), AppCaches.Disabled, Mock.Of(), new MockShortStringHelper(), Mock.Of()); var umbHelper = new UmbracoHelper(Mock.Of(), Mock.Of(), diff --git a/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs b/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs index 45f7042e31..cc14a7a023 100644 --- a/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs +++ b/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs @@ -109,12 +109,13 @@ namespace Umbraco.Tests.TestHelpers requestContextMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object); - if (routeData != null) + + if (routeData is null) { - requestContextMock.Setup(x => x.RouteData).Returns(routeData); + routeData = new RouteData(); } - + requestContextMock.Setup(x => x.RouteData).Returns(routeData); } } diff --git a/src/Umbraco.Tests/TestHelpers/TestHelper.cs b/src/Umbraco.Tests/TestHelpers/TestHelper.cs index a5ab3efe82..5fa87fe661 100644 --- a/src/Umbraco.Tests/TestHelpers/TestHelper.cs +++ b/src/Umbraco.Tests/TestHelpers/TestHelper.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading; +using System.Web; using Moq; using NUnit.Framework; using Umbraco.Core; @@ -343,5 +344,24 @@ namespace Umbraco.Tests.TestHelpers { return new DictionaryAppCache(); } + + public static IHttpContextAccessor GetHttpContextAccessor(HttpContextBase httpContextBase = null) + { + if (httpContextBase is null) + { + var httpContextMock = new Mock(); + + httpContextMock.Setup(x => x.DisposeOnPipelineCompleted(It.IsAny())) + .Returns(Mock.Of()); + + httpContextBase = httpContextMock.Object; + } + + var mock = new Mock(); + + mock.Setup(x => x.HttpContext).Returns(httpContextBase); + + return mock.Object; + } } } diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs index d25b07722f..776a0f94a4 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs @@ -111,7 +111,6 @@ namespace Umbraco.Tests.TestHelpers /// This should be the minimum Umbraco context. public IUmbracoContext GetUmbracoContextMock(IUmbracoContextAccessor accessor = null) { - var httpContext = Mock.Of(); var publishedSnapshotMock = new Mock(); publishedSnapshotMock.Setup(x => x.Members).Returns(Mock.Of()); @@ -127,7 +126,7 @@ namespace Umbraco.Tests.TestHelpers if (accessor == null) accessor = new TestUmbracoContextAccessor(); - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( accessor, @@ -142,7 +141,7 @@ namespace Umbraco.Tests.TestHelpers TestHelper.IOHelper, httpContextAccessor); - return umbracoContextFactory.EnsureUmbracoContext(httpContext).UmbracoContext; + return umbracoContextFactory.EnsureUmbracoContext().UmbracoContext; } public IUmbracoSettingsSection GetUmbracoSettings() @@ -341,15 +340,6 @@ namespace Umbraco.Tests.TestHelpers #endregion - public IHttpContextAccessor GetHttpContextAccessor(HttpContextBase httpContextBase = null) - { - var mock = new Mock(); - var httpContext = UmbracoContextFactory.EnsureHttpContext(httpContextBase); - - mock.Setup(x => x.HttpContext).Returns(httpContext); - - return mock.Object; - } } } diff --git a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs index f85a1e6332..450128c29f 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs @@ -159,14 +159,7 @@ namespace Umbraco.Tests.TestHelpers return @"Datasource=|DataDirectory|UmbracoNPocoTests.sdf;Flush Interval=1;"; } - protected FakeHttpContextFactory GetHttpContextFactory(string url, RouteData routeData = null) - { - var factory = routeData != null - ? new FakeHttpContextFactory(url, routeData) - : new FakeHttpContextFactory(url); - return factory; - } /// /// Creates the SqlCe database if required @@ -380,9 +373,9 @@ namespace Umbraco.Tests.TestHelpers } var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext); var umbracoContext = new UmbracoContext( - httpContext, + httpContextAccessor, service, new WebSecurity(httpContextAccessor, Factory.GetInstance(), Factory.GetInstance(), IOHelper), diff --git a/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs b/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs index 8ea1f4fbcf..816409b1cc 100644 --- a/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs +++ b/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs @@ -18,12 +18,14 @@ namespace Umbraco.Tests.Testing.Objects { public static IUmbracoContextFactory Create(IGlobalSettings globalSettings = null, IUrlProvider urlProvider = null, IMediaUrlProvider mediaUrlProvider = null, - IUmbracoContextAccessor umbracoContextAccessor = null) + IUmbracoContextAccessor umbracoContextAccessor = null, + IHttpContextAccessor httpContextAccessor = null) { if (globalSettings == null) globalSettings = SettingsForTests.GenerateMockGlobalSettings(); if (urlProvider == null) urlProvider = Mock.Of(); if (mediaUrlProvider == null) mediaUrlProvider = Mock.Of(); if (umbracoContextAccessor == null) umbracoContextAccessor = new TestUmbracoContextAccessor(); + if (httpContextAccessor == null) httpContextAccessor = TestHelper.GetHttpContextAccessor(); var contentCache = new Mock(); var mediaCache = new Mock(); @@ -33,7 +35,7 @@ namespace Umbraco.Tests.Testing.Objects var snapshotService = new Mock(); snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny())).Returns(snapshot.Object); - var httpContextAccessor = Mock.Of(); + var umbracoContextFactory = new UmbracoContextFactory( umbracoContextAccessor, diff --git a/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs b/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs index b712bcafc8..33a5fca070 100644 --- a/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs +++ b/src/Umbraco.Tests/Testing/TestingTests/MockTests.cs @@ -57,8 +57,6 @@ namespace Umbraco.Tests.Testing.TestingTests [Test] public void Can_Mock_Umbraco_Helper() { - var umbracoContext = TestObjects.GetUmbracoContextMock(); - // unless we can inject them in MembershipHelper, we need need this Composition.Register(_ => Mock.Of()); Composition.Register(_ => Mock.Of()); @@ -98,8 +96,6 @@ namespace Umbraco.Tests.Testing.TestingTests [Test] public void Can_Mock_UmbracoApiController_Dependencies_With_Injected_UmbracoMapper() { - var umbracoContext = TestObjects.GetUmbracoContextMock(); - var logger = Mock.Of(); var memberService = Mock.Of(); var memberTypeService = Mock.Of(); diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 4b21e11ff4..481beb37f3 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Reflection; +using System.Web.Routing; using System.Xml.Linq; using Examine; using Moq; @@ -447,11 +448,20 @@ namespace Umbraco.Tests.Testing Composition.RegisterUnique(); - Composition.RegisterUnique(TestObjects.GetHttpContextAccessor()); + Composition.RegisterUnique(TestHelper.GetHttpContextAccessor(GetHttpContextFactory("/").HttpContext)); } #endregion + protected FakeHttpContextFactory GetHttpContextFactory(string url, RouteData routeData = null) + { + var factory = routeData != null + ? new FakeHttpContextFactory(url, routeData) + : new FakeHttpContextFactory(url); + + return factory; + } + #region Initialize protected virtual void InitializeApplication(bool withApplication) diff --git a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs index 54e45b5532..ef6136939a 100644 --- a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs @@ -63,7 +63,8 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); + var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of(), @@ -77,7 +78,7 @@ namespace Umbraco.Tests.Web.Mvc TestHelper.IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); @@ -95,7 +96,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -110,7 +111,7 @@ namespace Umbraco.Tests.Web.Mvc TestHelper.IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); @@ -128,7 +129,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -143,7 +144,7 @@ namespace Umbraco.Tests.Web.Mvc TestHelper.IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); @@ -161,7 +162,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -176,7 +177,7 @@ namespace Umbraco.Tests.Web.Mvc TestHelper.IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index c4972b16db..b0393999e8 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -39,7 +39,7 @@ namespace Umbraco.Tests.Web.Mvc public void Can_Construct_And_Get_Result() { var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -54,7 +54,7 @@ namespace Umbraco.Tests.Web.Mvc IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); @@ -70,7 +70,7 @@ namespace Umbraco.Tests.Web.Mvc public void Umbraco_Context_Not_Null() { var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -85,7 +85,7 @@ namespace Umbraco.Tests.Web.Mvc IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbCtx = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); @@ -104,7 +104,7 @@ namespace Umbraco.Tests.Web.Mvc content.Setup(x => x.Id).Returns(2); var publishedSnapshotService = new Mock(); var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -119,7 +119,7 @@ namespace Umbraco.Tests.Web.Mvc IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); @@ -145,7 +145,7 @@ namespace Umbraco.Tests.Web.Mvc { var webRoutingSettings = Mock.Of(section => section.UrlProviderMode == "Auto"); var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -160,7 +160,7 @@ namespace Umbraco.Tests.Web.Mvc IOHelper, httpContextAccessor); - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); + var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); var umbracoContext = umbracoContextReference.UmbracoContext; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); diff --git a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs index 780ad63156..eaa892ac45 100644 --- a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs @@ -435,11 +435,11 @@ namespace Umbraco.Tests.Web.Mvc var http = GetHttpContextFactory(url, routeData).HttpContext; - var httpContextAccessor = TestObjects.GetHttpContextAccessor(http); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(http); var globalSettings = TestObjects.GetGlobalSettings(); var ctx = new UmbracoContext( - http, + httpContextAccessor, _service, new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), diff --git a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs index db4dc4b705..fb04ee49bb 100644 --- a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs +++ b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs @@ -7,6 +7,7 @@ using System.Web.Routing; using Moq; using NUnit.Framework; using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Tests.Testing; using Umbraco.Tests.Testing.Objects.Accessors; @@ -26,10 +27,10 @@ namespace Umbraco.Tests.Web [Test] public void RouteDataExtensions_GetUmbracoContext() { - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbCtx = new UmbracoContext( - Mock.Of(), + httpContextAccessor, Mock.Of(), new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), @@ -48,10 +49,10 @@ namespace Umbraco.Tests.Web [Test] public void ControllerContextExtensions_GetUmbracoContext_From_RouteValues() { - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbCtx = new UmbracoContext( - Mock.Of(), + httpContextAccessor, Mock.Of(), new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), @@ -80,10 +81,10 @@ namespace Umbraco.Tests.Web [Test] public void ControllerContextExtensions_GetUmbracoContext_From_Current() { - var httpContextAccessor = Mock.Of(); + var httpContextAccessor = TestHelper.GetHttpContextAccessor(); var umbCtx = new UmbracoContext( - Mock.Of(), + httpContextAccessor, Mock.Of(), new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), diff --git a/src/Umbraco.Web/IUmbracoContextFactory.cs b/src/Umbraco.Web/IUmbracoContextFactory.cs index 37e7fa0880..2a6dd50618 100644 --- a/src/Umbraco.Web/IUmbracoContextFactory.cs +++ b/src/Umbraco.Web/IUmbracoContextFactory.cs @@ -26,7 +26,6 @@ namespace Umbraco.Web /// // use umbracoContext... /// } /// - /// An optional http context. - UmbracoContextReference EnsureUmbracoContext(HttpContextBase httpContext = null); + UmbracoContextReference EnsureUmbracoContext(); } -} \ No newline at end of file +} diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 8112b32263..cc5da6e4a6 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -18,6 +18,7 @@ namespace Umbraco.Web /// public class UmbracoContext : DisposableObjectSlim, IDisposeOnRequestEnd, IUmbracoContext { + private readonly IHttpContextAccessor _httpContextAccessor; private readonly IGlobalSettings _globalSettings; private readonly IIOHelper _ioHelper; private readonly Lazy _publishedSnapshot; @@ -28,7 +29,7 @@ namespace Umbraco.Web // internal for unit tests // otherwise it's used by EnsureContext above // warn: does *not* manage setting any IUmbracoContextAccessor - internal UmbracoContext(HttpContextBase httpContext, + internal UmbracoContext(IHttpContextAccessor httpContextAccessor, IPublishedSnapshotService publishedSnapshotService, IWebSecurity webSecurity, IUmbracoSettingsSection umbracoSettings, @@ -38,13 +39,14 @@ namespace Umbraco.Web IVariationContextAccessor variationContextAccessor, IIOHelper ioHelper) { - if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); + if (httpContextAccessor == null) throw new ArgumentNullException(nameof(httpContextAccessor)); if (publishedSnapshotService == null) throw new ArgumentNullException(nameof(publishedSnapshotService)); if (webSecurity == null) throw new ArgumentNullException(nameof(webSecurity)); if (umbracoSettings == null) throw new ArgumentNullException(nameof(umbracoSettings)); if (urlProviders == null) throw new ArgumentNullException(nameof(urlProviders)); if (mediaUrlProviders == null) throw new ArgumentNullException(nameof(mediaUrlProviders)); VariationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor)); + _httpContextAccessor = httpContextAccessor; _globalSettings = globalSettings ?? throw new ArgumentNullException(nameof(globalSettings)); _ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper)); @@ -56,11 +58,10 @@ namespace Umbraco.Web // // all in all, this context may be disposed more than once, but DisposableObject ensures that // it is ok and it will be actually disposed only once. - httpContext.DisposeOnPipelineCompleted(this); + httpContextAccessor.HttpContext.DisposeOnPipelineCompleted(this); ObjectCreated = DateTime.Now; UmbracoRequestId = Guid.NewGuid(); - HttpContext = httpContext; Security = webSecurity; // beware - we cannot expect a current user here, so detecting preview mode must be a lazy thing @@ -141,11 +142,6 @@ namespace Umbraco.Web /// public IPublishedRequest PublishedRequest { get; set; } - /// - /// Exposes the HttpContext for the current request - /// - public HttpContextBase HttpContext { get; } - /// /// Gets the variation context accessor. /// @@ -268,7 +264,7 @@ namespace Umbraco.Web { try { - return HttpContext.Request; + return _httpContextAccessor.HttpContext.Request; } catch (HttpException) { diff --git a/src/Umbraco.Web/UmbracoContextFactory.cs b/src/Umbraco.Web/UmbracoContextFactory.cs index 38c7d03835..8751ed441a 100644 --- a/src/Umbraco.Web/UmbracoContextFactory.cs +++ b/src/Umbraco.Web/UmbracoContextFactory.cs @@ -55,7 +55,7 @@ namespace Umbraco.Web _httpContextAccessor = httpContextAccessor; } - private IUmbracoContext CreateUmbracoContext(HttpContextBase httpContext) + private IUmbracoContext CreateUmbracoContext() { // make sure we have a variation context if (_variationContextAccessor.VariationContext == null) @@ -71,37 +71,23 @@ namespace Umbraco.Web var webSecurity = new WebSecurity(_httpContextAccessor, _userService, _globalSettings, _ioHelper); - return new UmbracoContext(httpContext, _publishedSnapshotService, webSecurity, _umbracoSettings, _urlProviders, _mediaUrlProviders, _globalSettings, _variationContextAccessor, _ioHelper); + return new UmbracoContext(_httpContextAccessor, _publishedSnapshotService, webSecurity, _umbracoSettings, _urlProviders, _mediaUrlProviders, _globalSettings, _variationContextAccessor, _ioHelper); } /// - public UmbracoContextReference EnsureUmbracoContext(HttpContextBase httpContext = null) + public UmbracoContextReference EnsureUmbracoContext() { var currentUmbracoContext = _umbracoContextAccessor.UmbracoContext; if (currentUmbracoContext != null) return new UmbracoContextReference(false, _umbracoContextAccessor); - httpContext = EnsureHttpContext(httpContext); - - var umbracoContext = CreateUmbracoContext(httpContext); + var umbracoContext = CreateUmbracoContext(); _umbracoContextAccessor.UmbracoContext = umbracoContext; return new UmbracoContextReference(true, _umbracoContextAccessor); } - public static HttpContextBase EnsureHttpContext(HttpContextBase httpContext = null) - { - if (Thread.GetDomain().GetData(".appPath") is null || Thread.GetDomain().GetData(".appVPath") is null) - { - return httpContext ?? new HttpContextWrapper(HttpContext.Current ?? - new HttpContext(new SimpleWorkerRequest("", "", "null.aspx", "", NullWriterInstance))); - } - return httpContext ?? new HttpContextWrapper(HttpContext.Current ?? - new HttpContext(new SimpleWorkerRequest("null.aspx", "", NullWriterInstance))); - } - - // dummy TextWriter that does not write private class NullWriter : TextWriter { diff --git a/src/Umbraco.Web/UmbracoInjectedModule.cs b/src/Umbraco.Web/UmbracoInjectedModule.cs index b8fb6d7449..3c40b42026 100644 --- a/src/Umbraco.Web/UmbracoInjectedModule.cs +++ b/src/Umbraco.Web/UmbracoInjectedModule.cs @@ -82,7 +82,7 @@ namespace Umbraco.Web // TODO: should we move this to after we've ensured we are processing a routable page? // ensure there's an UmbracoContext registered for the current request // registers the context reference so its disposed at end of request - var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext(httpContext); + var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext(); httpContext.DisposeOnPipelineCompleted(umbracoContextReference); }