Refactor UmbracoContext to inject IHttpAccessor instead of HttpContext
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -75,9 +75,9 @@ namespace Umbraco.Tests.Cache.PublishedCache
|
||||
publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).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<IUserService>(), globalSettings, IOHelper),
|
||||
umbracoSettings,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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<HttpContextBase>();
|
||||
var umbracoContextFactory = factory.GetInstance<IUmbracoContextFactory>();
|
||||
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(httpContext);
|
||||
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
||||
var umbracoContext = umbracoContextReference.UmbracoContext;
|
||||
|
||||
// assert that there is no published document
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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<HttpContextBase>(),
|
||||
httpContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper),
|
||||
TestObjects.GetUmbracoSettings(), new List<IUrlProvider>(), Enumerable.Empty<IMediaUrlProvider>(), 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<HttpContextBase>(),
|
||||
httpContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper),
|
||||
TestObjects.GetUmbracoSettings(), new List<IUrlProvider>(), Enumerable.Empty<IMediaUrlProvider>(), globalSettings,
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace Umbraco.Tests.Templates
|
||||
mediaUrlProvider: mediaUrlProvider.Object,
|
||||
umbracoContextAccessor: umbracoContextAccessor);
|
||||
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>()))
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext())
|
||||
{
|
||||
var mediaCache = Mock.Get(reference.UmbracoContext.Media);
|
||||
mediaCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(media.Object);
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Umbraco.Tests.Templates
|
||||
mediaUrlProvider: mediaUrlProvider.Object,
|
||||
umbracoContextAccessor: umbracoContextAccessor);
|
||||
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>()))
|
||||
using (var reference = umbracoContextFactory.EnsureUmbracoContext())
|
||||
{
|
||||
var contentCache = Mock.Get(reference.UmbracoContext.Content);
|
||||
contentCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(publishedContent.Object);
|
||||
|
||||
@@ -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<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
|
||||
@@ -154,7 +155,7 @@ namespace Umbraco.Tests.TestHelpers.ControllerTesting
|
||||
urlHelper.Setup(provider => provider.GetUrl(It.IsAny<IUmbracoContext>(), It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<Uri>()))
|
||||
.Returns(UrlInfo.Url("/hello/world/1234"));
|
||||
|
||||
var membershipHelper = new MembershipHelper(umbCtx.HttpContext, Mock.Of<IPublishedMemberCache>(), Mock.Of<MembersMembershipProvider>(), Mock.Of<RoleProvider>(), Mock.Of<IMemberService>(), Mock.Of<IMemberTypeService>(), Mock.Of<IPublicAccessService>(), AppCaches.Disabled, Mock.Of<ILogger>(), new MockShortStringHelper(), Mock.Of<IEntityService>());
|
||||
var membershipHelper = new MembershipHelper(httpContext, Mock.Of<IPublishedMemberCache>(), Mock.Of<MembersMembershipProvider>(), Mock.Of<RoleProvider>(), Mock.Of<IMemberService>(), Mock.Of<IMemberTypeService>(), Mock.Of<IPublicAccessService>(), AppCaches.Disabled, Mock.Of<ILogger>(), new MockShortStringHelper(), Mock.Of<IEntityService>());
|
||||
|
||||
var umbHelper = new UmbracoHelper(Mock.Of<IPublishedContent>(),
|
||||
Mock.Of<ITagQuery>(),
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<HttpContextBase>();
|
||||
|
||||
httpContextMock.Setup(x => x.DisposeOnPipelineCompleted(It.IsAny<IDisposable>()))
|
||||
.Returns(Mock.Of<ISubscriptionToken>());
|
||||
|
||||
httpContextBase = httpContextMock.Object;
|
||||
}
|
||||
|
||||
var mock = new Mock<IHttpContextAccessor>();
|
||||
|
||||
mock.Setup(x => x.HttpContext).Returns(httpContextBase);
|
||||
|
||||
return mock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +111,6 @@ namespace Umbraco.Tests.TestHelpers
|
||||
/// <remarks>This should be the minimum Umbraco context.</remarks>
|
||||
public IUmbracoContext GetUmbracoContextMock(IUmbracoContextAccessor accessor = null)
|
||||
{
|
||||
var httpContext = Mock.Of<HttpContextBase>();
|
||||
|
||||
var publishedSnapshotMock = new Mock<IPublishedSnapshot>();
|
||||
publishedSnapshotMock.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
|
||||
@@ -127,7 +126,7 @@ namespace Umbraco.Tests.TestHelpers
|
||||
|
||||
if (accessor == null) accessor = new TestUmbracoContextAccessor();
|
||||
|
||||
var httpContextAccessor = Mock.Of<IHttpContextAccessor>();
|
||||
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<IHttpContextAccessor>();
|
||||
|
||||
var httpContext = UmbracoContextFactory.EnsureHttpContext(httpContextBase);
|
||||
|
||||
mock.Setup(x => x.HttpContext).Returns(httpContext);
|
||||
|
||||
return mock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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<IUserService>(),
|
||||
Factory.GetInstance<IGlobalSettings>(), IOHelper),
|
||||
|
||||
@@ -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<IUrlProvider>();
|
||||
if (mediaUrlProvider == null) mediaUrlProvider = Mock.Of<IMediaUrlProvider>();
|
||||
if (umbracoContextAccessor == null) umbracoContextAccessor = new TestUmbracoContextAccessor();
|
||||
if (httpContextAccessor == null) httpContextAccessor = TestHelper.GetHttpContextAccessor();
|
||||
|
||||
var contentCache = new Mock<IPublishedContentCache>();
|
||||
var mediaCache = new Mock<IPublishedMediaCache>();
|
||||
@@ -33,7 +35,7 @@ namespace Umbraco.Tests.Testing.Objects
|
||||
var snapshotService = new Mock<IPublishedSnapshotService>();
|
||||
snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(snapshot.Object);
|
||||
|
||||
var httpContextAccessor = Mock.Of<IHttpContextAccessor>();
|
||||
|
||||
|
||||
var umbracoContextFactory = new UmbracoContextFactory(
|
||||
umbracoContextAccessor,
|
||||
|
||||
@@ -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<IMemberService>());
|
||||
Composition.Register(_ => Mock.Of<IMemberTypeService>());
|
||||
@@ -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<IProfilingLogger>();
|
||||
var memberService = Mock.Of<IMemberService>();
|
||||
var memberTypeService = Mock.Of<IMemberTypeService>();
|
||||
|
||||
@@ -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<ParameterEditorCollection>();
|
||||
|
||||
|
||||
Composition.RegisterUnique<IHttpContextAccessor>(TestObjects.GetHttpContextAccessor());
|
||||
Composition.RegisterUnique<IHttpContextAccessor>(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)
|
||||
|
||||
@@ -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<IHttpContextAccessor>();
|
||||
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
|
||||
|
||||
var umbracoContextFactory = new UmbracoContextFactory(
|
||||
Current.UmbracoContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
@@ -77,7 +78,7 @@ namespace Umbraco.Tests.Web.Mvc
|
||||
TestHelper.IOHelper,
|
||||
httpContextAccessor);
|
||||
|
||||
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>());
|
||||
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<IHttpContextAccessor>();
|
||||
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<HttpContextBase>());
|
||||
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<IHttpContextAccessor>();
|
||||
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<HttpContextBase>());
|
||||
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<IHttpContextAccessor>();
|
||||
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<HttpContextBase>());
|
||||
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
||||
var umbCtx = umbracoContextReference.UmbracoContext;
|
||||
|
||||
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx);
|
||||
|
||||
@@ -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<HttpContextBase>());
|
||||
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<HttpContextBase>());
|
||||
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<IPublishedSnapshotService>();
|
||||
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<HttpContextBase>());
|
||||
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<IWebRoutingSection>(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<HttpContextBase>());
|
||||
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
||||
var umbracoContext = umbracoContextReference.UmbracoContext;
|
||||
|
||||
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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<IHttpContextAccessor>();
|
||||
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
|
||||
|
||||
var umbCtx = new UmbracoContext(
|
||||
Mock.Of<HttpContextBase>(),
|
||||
httpContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
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<IHttpContextAccessor>();
|
||||
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
|
||||
|
||||
var umbCtx = new UmbracoContext(
|
||||
Mock.Of<HttpContextBase>(),
|
||||
httpContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
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<IHttpContextAccessor>();
|
||||
var httpContextAccessor = TestHelper.GetHttpContextAccessor();
|
||||
|
||||
var umbCtx = new UmbracoContext(
|
||||
Mock.Of<HttpContextBase>(),
|
||||
httpContextAccessor,
|
||||
Mock.Of<IPublishedSnapshotService>(),
|
||||
new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper),
|
||||
TestObjects.GetUmbracoSettings(),
|
||||
|
||||
@@ -26,7 +26,6 @@ namespace Umbraco.Web
|
||||
/// // use umbracoContext...
|
||||
/// }
|
||||
/// </example>
|
||||
/// <param name="httpContext">An optional http context.</param>
|
||||
UmbracoContextReference EnsureUmbracoContext(HttpContextBase httpContext = null);
|
||||
UmbracoContextReference EnsureUmbracoContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace Umbraco.Web
|
||||
/// </summary>
|
||||
public class UmbracoContext : DisposableObjectSlim, IDisposeOnRequestEnd, IUmbracoContext
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IIOHelper _ioHelper;
|
||||
private readonly Lazy<IPublishedSnapshot> _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
|
||||
/// </summary>
|
||||
public IPublishedRequest PublishedRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Exposes the HttpContext for the current request
|
||||
/// </summary>
|
||||
public HttpContextBase HttpContext { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the variation context accessor.
|
||||
/// </summary>
|
||||
@@ -268,7 +264,7 @@ namespace Umbraco.Web
|
||||
{
|
||||
try
|
||||
{
|
||||
return HttpContext.Request;
|
||||
return _httpContextAccessor.HttpContext.Request;
|
||||
}
|
||||
catch (HttpException)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user