diff --git a/Lombiq.Hosting.MediaTheme.Bridge/Helpers/RequestUrlPrefixRemover.cs b/Lombiq.Hosting.MediaTheme.Bridge/Helpers/RequestUrlPrefixRemover.cs new file mode 100644 index 0000000..fdf3b5c --- /dev/null +++ b/Lombiq.Hosting.MediaTheme.Bridge/Helpers/RequestUrlPrefixRemover.cs @@ -0,0 +1,19 @@ +using OrchardCore.Environment.Shell; +using System; + +namespace Lombiq.Hosting.MediaTheme.Bridge.Helpers; + +internal static class RequestUrlPrefixRemover +{ + public static string RemoveIfHasPrefix(string path, ShellSettings shellSettings) + { + var requestUrlPrefix = shellSettings.RequestUrlPrefix; + if (string.IsNullOrEmpty(requestUrlPrefix) || + !path.StartsWith("/" + requestUrlPrefix, StringComparison.OrdinalIgnoreCase)) + { + return path; + } + + return path[(requestUrlPrefix.Length + 1)..]; + } +} diff --git a/Lombiq.Hosting.MediaTheme.Bridge/Middlewares/MediaThemeAssetUrlRewritingMiddleware.cs b/Lombiq.Hosting.MediaTheme.Bridge/Middlewares/MediaThemeAssetUrlRewritingMiddleware.cs index cfb1182..d5bd554 100644 --- a/Lombiq.Hosting.MediaTheme.Bridge/Middlewares/MediaThemeAssetUrlRewritingMiddleware.cs +++ b/Lombiq.Hosting.MediaTheme.Bridge/Middlewares/MediaThemeAssetUrlRewritingMiddleware.cs @@ -1,4 +1,5 @@ using Lombiq.Hosting.MediaTheme.Bridge.Constants; +using Lombiq.Hosting.MediaTheme.Bridge.Helpers; using Microsoft.AspNetCore.Http; using OrchardCore.Environment.Shell; using OrchardCore.FileStorage; @@ -41,13 +42,7 @@ public async Task InvokeAsync( string assetUrl; if (!context.IsDevelopment() || await mediaFileStore.FileExistsAsync(mediaPath)) { - assetUrl = mediaFileStore.MapPathToPublicUrl(mediaPath); - - if (!string.IsNullOrEmpty(shellSettings.RequestUrlPrefix) && - assetUrl.StartsWith("/" + shellSettings.RequestUrlPrefix, StringComparison.OrdinalIgnoreCase)) - { - assetUrl = assetUrl[(shellSettings.RequestUrlPrefix.Length + 1)..]; - } + assetUrl = RequestUrlPrefixRemover.RemoveIfHasPrefix(mediaFileStore.MapPathToPublicUrl(mediaPath), shellSettings); } else { diff --git a/Lombiq.Hosting.MediaTheme.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs b/Lombiq.Hosting.MediaTheme.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs index 422611d..ed092cc 100644 --- a/Lombiq.Hosting.MediaTheme.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs +++ b/Lombiq.Hosting.MediaTheme.Tests.UI/Extensions/TestCaseUITestContextExtensions.cs @@ -8,25 +8,29 @@ namespace Lombiq.Hosting.MediaTheme.Tests.UI.Extensions; public static class TestCaseUITestContextExtensions { - public static async Task TestMediaThemeDeployedBehaviorAsync(this UITestContext context) + public static async Task TestMediaThemeDeployedBehaviorAsync(this UITestContext context, string tenantPrefix = null) { await context.ExecuteMediaThemeSampleRecipeDirectlyAsync(); await context.GoToMediaThemeTestContentPageAsync(); - AssertElements(context, "v"); + AssertElements(context, "v", tenantPrefix); } - public static async Task TestMediaThemeLocalBehaviorAsync(this UITestContext context) + public static async Task TestMediaThemeLocalBehaviorAsync(this UITestContext context, string tenantPrefix = null) { await context.SetThemeDirectlyAsync("Lombiq.Hosting.MediaTheme.Tests.Theme"); await context.GoToHomePageAsync(onlyIfNotAlreadyThere: false); - AssertElements(context, "mediatheme"); + AssertElements(context, "mediatheme", tenantPrefix); } - private static void AssertElements(UITestContext context, string cacheBustingParameterName) + private static void AssertElements(UITestContext context, string cacheBustingParameterName, string tenantPrefix) { - context.Exists(By.XPath($"//head//link[contains(@href, '/mediatheme/example.css?{cacheBustingParameterName}=')]").Hidden()); + context.Exists(By.XPath( + $"//head//link[contains(@href, '{GetTenantUrlPrefix(tenantPrefix)}/mediatheme/example.css?{cacheBustingParameterName}=')]").Hidden()); context.Exists(By.XPath("//p[contains(., 'This is an example template hosted in Media Theme.')]")); - context.Exists(By.XPath("//img[contains(@src, '/mediatheme/example.png')]")); - context.Exists(By.XPath($"//img[contains(@src, '/mediatheme/example2.png?{cacheBustingParameterName}=')]")); + context.Exists(By.XPath($"//img[contains(@src, '{GetTenantUrlPrefix(tenantPrefix)}/mediatheme/example.png')]")); + context.Exists(By.XPath($"//img[contains(@src, '{GetTenantUrlPrefix(tenantPrefix)}/mediatheme/example2.png?{cacheBustingParameterName}=')]")); } + + private static string GetTenantUrlPrefix(string tenantName) => + string.IsNullOrEmpty(tenantName) ? string.Empty : "/" + tenantName; }