using System; using System.Linq.Expressions; using System.Net.Http; using System.Reflection; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; using Umbraco.Composing; using Umbraco.Extensions; using Umbraco.Tests.Integration.Testing; using Umbraco.Web; using Umbraco.Web.Common.Controllers; using Umbraco.Web.Editors; namespace Umbraco.Tests.Integration.TestServerTest { [TestFixture] public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest { [SetUp] public void SetUp() { Factory = new UmbracoWebApplicationFactory(TestDBConnectionString); Client = Factory.CreateClient(new WebApplicationFactoryClientOptions(){ AllowAutoRedirect = false }); LinkGenerator = Factory.Services.GetRequiredService(); } protected T GetRequiredService() => Factory.Services.GetRequiredService(); protected string PrepareUrl(Expression> methodSelector) where T : UmbracoApiController { var url = LinkGenerator.GetUmbracoApiService(methodSelector); var umbracoContextFactory = GetRequiredService(); var httpContextAccessor = GetRequiredService(); httpContextAccessor.HttpContext = new DefaultHttpContext { Request = { Scheme = "https", Host = new HostString("localhost", 80), Path = url, QueryString = new QueryString(string.Empty) } }; umbracoContextFactory.EnsureUmbracoContext(); return url; } protected HttpClient Client { get; set; } protected LinkGenerator LinkGenerator { get; set; } protected UmbracoWebApplicationFactory Factory { get; set; } [TearDown] public void TearDown() { Factory.Dispose(); if (Current.IsInitialized) { typeof(Current).GetProperty(nameof(Current.IsInitialized), BindingFlags.Public | BindingFlags.Static).SetValue(null, false); } } } }