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.Tests.Testing; using Umbraco.Web; using Umbraco.Web.Common.Controllers; using Umbraco.Web.Editors; namespace Umbraco.Tests.Integration.TestServerTest { [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console, Boot = false)] public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest { [SetUp] public void SetUp() { Factory = new UmbracoWebApplicationFactory(TestDBConnectionString); Client = Factory.CreateClient(new WebApplicationFactoryClientOptions(){ AllowAutoRedirect = false }); LinkGenerator = Factory.Services.GetRequiredService(); } /// /// Get the service from the underlying container that is also used by the . /// protected T GetRequiredService() => Factory.Services.GetRequiredService(); /// /// Prepare a url before using . /// This returns the url but also sets the HttpContext.request into to use this url. /// /// The string URL of the controller action. 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) { Current.IsInitialized = false; } } } }