2025-03-21 18:02:31 +01:00
|
|
|
using System.Net;
|
2021-06-30 14:36:08 +02:00
|
|
|
using System.Reflection;
|
2021-02-02 14:48:01 +11:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2023-09-12 14:16:27 +02:00
|
|
|
using Umbraco.Cms.Tests.Common.Attributes;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.TestServerTest;
|
2021-06-30 14:36:08 +02:00
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
2021-02-10 14:21:48 +01:00
|
|
|
using Umbraco.Cms.Web.Website.Controllers;
|
2021-06-30 14:36:08 +02:00
|
|
|
using Umbraco.Extensions;
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.Website.Routing;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
2025-03-21 18:02:31 +01:00
|
|
|
internal sealed class SurfaceControllerTests : UmbracoTestServerTestBase
|
2021-02-02 14:48:01 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public async Task Auto_Routes_For_Default_Action()
|
2021-02-02 14:48:01 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var url = PrepareSurfaceControllerUrl<TestSurfaceController>(x => x.Index());
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Act
|
|
|
|
|
var response = await Client.GetAsync(url);
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var body = await response.Content.ReadAsStringAsync();
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
|
}
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public async Task Auto_Routes_For_Custom_Action()
|
|
|
|
|
{
|
|
|
|
|
var url = PrepareSurfaceControllerUrl<TestSurfaceController>(x => x.News());
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Act
|
|
|
|
|
var response = await Client.GetAsync(url);
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var body = await response.Content.ReadAsStringAsync();
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
|
|
|
|
|
}
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
2023-09-12 14:16:27 +02:00
|
|
|
[LongRunning]
|
2022-06-21 08:09:38 +02:00
|
|
|
public async Task Plugin_Controller_Routes_By_Area()
|
|
|
|
|
{
|
|
|
|
|
// Create URL manually, because PrepareSurfaceController URl will prepare whatever the controller is routed as
|
|
|
|
|
var controllerType = typeof(TestPluginController);
|
|
|
|
|
var pluginAttribute =
|
|
|
|
|
CustomAttributeExtensions.GetCustomAttribute<PluginControllerAttribute>(controllerType, false);
|
|
|
|
|
var controllerName = ControllerExtensions.GetControllerName(controllerType);
|
|
|
|
|
var url = $"/umbraco/{pluginAttribute?.AreaName}/{controllerName}";
|
|
|
|
|
PrepareUrl(url);
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var response = await Client.GetAsync(url);
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var body = await response.Content.ReadAsStringAsync();
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
2021-02-02 14:48:01 +11:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Test controllers must be non-nested, else we need to jump through some hoops with custom
|
|
|
|
|
// IApplicationFeatureProvider<ControllerFeature>
|
|
|
|
|
// For future notes if we want this, some example code of this is here
|
|
|
|
|
// https://tpodolak.com/blog/2020/06/22/asp-net-core-adding-controllers-directly-integration-tests/
|
|
|
|
|
public class TestSurfaceController : SurfaceController
|
|
|
|
|
{
|
|
|
|
|
public TestSurfaceController(
|
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory,
|
|
|
|
|
ServiceContext services,
|
|
|
|
|
AppCaches appCaches,
|
|
|
|
|
IProfilingLogger profilingLogger,
|
|
|
|
|
IPublishedUrlProvider publishedUrlProvider)
|
|
|
|
|
: base(
|
|
|
|
|
umbracoContextAccessor,
|
|
|
|
|
databaseFactory,
|
|
|
|
|
services,
|
|
|
|
|
appCaches,
|
|
|
|
|
profilingLogger,
|
|
|
|
|
publishedUrlProvider)
|
2021-02-02 14:48:01 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IActionResult Index() => Ok();
|
2021-02-02 14:48:01 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IActionResult News() => NoContent();
|
|
|
|
|
}
|
2021-06-30 14:36:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[PluginController("TestArea")]
|
|
|
|
|
public class TestPluginController : SurfaceController
|
|
|
|
|
{
|
|
|
|
|
public TestPluginController(
|
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory,
|
|
|
|
|
ServiceContext services,
|
|
|
|
|
AppCaches appCaches,
|
|
|
|
|
IProfilingLogger profilingLogger,
|
|
|
|
|
IPublishedUrlProvider publishedUrlProvider)
|
|
|
|
|
: base(
|
|
|
|
|
umbracoContextAccessor,
|
|
|
|
|
databaseFactory,
|
|
|
|
|
services,
|
|
|
|
|
appCaches,
|
|
|
|
|
profilingLogger,
|
|
|
|
|
publishedUrlProvider)
|
2021-06-30 14:36:08 +02:00
|
|
|
{
|
|
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
public IActionResult Index() => Ok();
|
2021-02-02 14:48:01 +11:00
|
|
|
}
|