2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Reflection;
|
2020-08-31 11:31:56 +02:00
|
|
|
|
using Examine;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Composing;
|
2020-08-31 11:31:56 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
|
|
|
|
|
using Umbraco.Examine;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2020-06-30 20:11:39 +02:00
|
|
|
|
using Umbraco.Tests.Testing;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
using Umbraco.Web.Common.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration.TestServerTest
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2020-06-30 20:11:39 +02:00
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console, Boot = false)]
|
2020-06-17 16:39:28 +02:00
|
|
|
|
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest
|
|
|
|
|
|
{
|
2020-08-31 11:31:56 +02:00
|
|
|
|
|
2020-06-17 16:39:28 +02:00
|
|
|
|
[SetUp]
|
|
|
|
|
|
public void SetUp()
|
|
|
|
|
|
{
|
|
|
|
|
|
Factory = new UmbracoWebApplicationFactory(TestDBConnectionString);
|
|
|
|
|
|
Client = Factory.CreateClient(new WebApplicationFactoryClientOptions(){
|
|
|
|
|
|
AllowAutoRedirect = false
|
|
|
|
|
|
});
|
|
|
|
|
|
LinkGenerator = Factory.Services.GetRequiredService<LinkGenerator>();
|
2020-08-31 11:31:56 +02:00
|
|
|
|
|
|
|
|
|
|
ExecuteExamineIndexOperationsInSync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ExecuteExamineIndexOperationsInSync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var examineManager = Factory.Services.GetRequiredService<IExamineManager>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var index in examineManager.Indexes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index is UmbracoExamineIndex umbracoExamineIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
umbracoExamineIndex.ProcessNonAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-17 16:39:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-06 12:55:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the service from the underlying container that is also used by the <see cref="Client"/>.
|
|
|
|
|
|
/// </summary>
|
2020-06-17 16:39:28 +02:00
|
|
|
|
protected T GetRequiredService<T>() => Factory.Services.GetRequiredService<T>();
|
2020-07-06 12:55:23 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Prepare a url before using <see cref="Client"/>.
|
|
|
|
|
|
/// This returns the url but also sets the HttpContext.request into to use this url.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>The string URL of the controller action.</returns>
|
2020-06-17 16:39:28 +02:00
|
|
|
|
protected string PrepareUrl<T>(Expression<Func<T, object>> methodSelector)
|
|
|
|
|
|
where T : UmbracoApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
var url = LinkGenerator.GetUmbracoApiService<T>(methodSelector);
|
|
|
|
|
|
|
|
|
|
|
|
var umbracoContextFactory = GetRequiredService<IUmbracoContextFactory>();
|
|
|
|
|
|
var httpContextAccessor = GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
2020-06-18 14:40:30 +02:00
|
|
|
|
|
|
|
|
|
|
Factory.Dispose();
|
2020-06-17 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
if (Current.IsInitialized)
|
|
|
|
|
|
{
|
2020-07-06 13:01:25 +02:00
|
|
|
|
Current.IsInitialized = false;
|
2020-06-17 16:39:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|