mirror of https://github.com/abpframework/abp.git
22 changed files with 465 additions and 28 deletions
@ -0,0 +1,62 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling; |
|||
|
|||
public class ComponentBundleUrlBuilder : IComponentBundleUrlBuilder, ITransientDependency |
|||
{ |
|||
public virtual Task<string> BuildAsync( |
|||
[NotNull] string fileName, |
|||
[CanBeNull] string? appBasePath, |
|||
[CanBeNull] string? navigationBaseUri) |
|||
{ |
|||
Check.NotNull(fileName, nameof(fileName)); |
|||
|
|||
if (IsExternalUrl(fileName)) |
|||
{ |
|||
return Task.FromResult(fileName); |
|||
} |
|||
|
|||
var pathBase = !string.IsNullOrWhiteSpace(appBasePath) |
|||
? appBasePath |
|||
: ExtractPathBaseFromNavigationBaseUri(navigationBaseUri); |
|||
|
|||
if (string.IsNullOrWhiteSpace(pathBase)) |
|||
{ |
|||
return Task.FromResult(fileName); |
|||
} |
|||
|
|||
var normalized = pathBase.EnsureEndsWith('/'); |
|||
if (normalized == "/") |
|||
{ |
|||
return Task.FromResult(fileName); |
|||
} |
|||
|
|||
if (fileName.StartsWith(normalized, StringComparison.Ordinal)) |
|||
{ |
|||
return Task.FromResult(fileName); |
|||
} |
|||
|
|||
return Task.FromResult(normalized + fileName.RemovePreFix("/")); |
|||
} |
|||
|
|||
protected virtual bool IsExternalUrl([NotNull] string fileName) |
|||
{ |
|||
return fileName.StartsWith("//", StringComparison.Ordinal) || |
|||
(fileName.Contains(':') && Uri.TryCreate(fileName, UriKind.Absolute, out _)); |
|||
} |
|||
|
|||
protected virtual string? ExtractPathBaseFromNavigationBaseUri([CanBeNull] string? navigationBaseUri) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(navigationBaseUri)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return Uri.TryCreate(navigationBaseUri, UriKind.Absolute, out var uri) |
|||
? uri.AbsolutePath |
|||
: null; |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling; |
|||
|
|||
public interface IComponentBundleUrlBuilder |
|||
{ |
|||
Task<string> BuildAsync(string fileName, string? appBasePath, string? navigationBaseUri); |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web; |
|||
|
|||
public static class MenuItemUrlHelper |
|||
{ |
|||
/// <summary>
|
|||
/// Makes an app-local menu url ("/x" or "~/x") relative to the base href, so it keeps the PathBase.
|
|||
/// Protocol-relative urls ("//host/x") point to another host and are returned as is.
|
|||
/// </summary>
|
|||
public static string? ToBaseRelativeUrl(string? url) |
|||
{ |
|||
if (url == null || url.StartsWith("//", StringComparison.Ordinal)) |
|||
{ |
|||
return url; |
|||
} |
|||
|
|||
return url.TrimStart('/', '~'); |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
{ |
|||
"role": "lib.test" |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net10.0</TargetFramework> |
|||
<RootNamespace /> |
|||
<Nullable>enable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor\Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,119 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.MudBlazor.Bundling; |
|||
|
|||
public class ComponentBundleUrlBuilder_Tests |
|||
{ |
|||
private readonly IComponentBundleUrlBuilder _builder = new ComponentBundleUrlBuilder(); |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_FileName_When_No_PathBase_Available() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: null)) |
|||
.ShouldBe("/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_FileName_When_NavigationBaseUri_Has_Root_PathBase() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/")) |
|||
.ShouldBe("/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Use_Explicit_AppBasePath_Over_NavigationBaseUri() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/explicit", navigationBaseUri: "https://localhost/from-nav/")) |
|||
.ShouldBe("/explicit/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Resolve_PathBase_From_NavigationBaseUri() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/")) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Resolve_PathBase_From_NavigationBaseUri_When_AppBasePath_Empty() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "", navigationBaseUri: "https://localhost/foo/")) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Handle_Nested_PathBase() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/bar/")) |
|||
.ShouldBe("/foo/bar/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Normalize_AppBasePath_Without_Trailing_Slash() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null)) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Duplicate_Leading_Slash() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: "/foo/", navigationBaseUri: null)) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Handle_FileName_Without_Leading_Slash() |
|||
{ |
|||
(await _builder.BuildAsync("__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null)) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_FileName_When_NavigationBaseUri_Is_Invalid() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: null, navigationBaseUri: "not-a-uri")) |
|||
.ShouldBe("/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Throw_When_FileName_Is_Null() |
|||
{ |
|||
await Should.ThrowAsync<System.ArgumentNullException>(async () => |
|||
await _builder.BuildAsync(null!, appBasePath: "/foo", navigationBaseUri: null)); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData("https://cdn.example.com/foo.css")] |
|||
[InlineData("http://cdn.example.com/foo.css")] |
|||
[InlineData("//cdn.example.com/foo.css")] |
|||
[InlineData("data:text/css;base64,Zm9vIA==")] |
|||
public async Task Should_Not_Prefix_External_Urls(string externalUrl) |
|||
{ |
|||
(await _builder.BuildAsync(externalUrl, appBasePath: "/foo", navigationBaseUri: "https://localhost/foo/")) |
|||
.ShouldBe(externalUrl); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Treat_Whitespace_AppBasePath_As_Not_Provided() |
|||
{ |
|||
(await _builder.BuildAsync("/__bundles/Global.css", appBasePath: " ", navigationBaseUri: "https://localhost/foo/")) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Be_Idempotent_For_Already_Prefixed_FileName() |
|||
{ |
|||
(await _builder.BuildAsync("/foo/__bundles/Global.css", appBasePath: "/foo", navigationBaseUri: null)) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Be_Idempotent_When_PathBase_Resolved_From_NavigationBaseUri() |
|||
{ |
|||
(await _builder.BuildAsync("/foo/__bundles/Global.css", appBasePath: null, navigationBaseUri: "https://localhost/foo/")) |
|||
.ShouldBe("/foo/__bundles/Global.css"); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web; |
|||
|
|||
public class MenuItemUrlHelper_Tests |
|||
{ |
|||
[Theory] |
|||
[InlineData("/", "")] |
|||
[InlineData("/books", "books")] |
|||
[InlineData("~/books", "books")] |
|||
[InlineData("books", "books")] |
|||
[InlineData("/identity/users?filter=a", "identity/users?filter=a")] |
|||
[InlineData("//cdn.example.com/help", "//cdn.example.com/help")] |
|||
[InlineData("https://docs.example.com/help", "https://docs.example.com/help")] |
|||
[InlineData("mailto:info@example.com", "mailto:info@example.com")] |
|||
public void Should_Make_Local_Urls_Relative_To_Base(string url, string expected) |
|||
{ |
|||
MenuItemUrlHelper.ToBaseRelativeUrl(url).ShouldBe(expected); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Return_Null_For_Null_Url() |
|||
{ |
|||
MenuItemUrlHelper.ToBaseRelativeUrl(null).ShouldBeNull(); |
|||
} |
|||
} |
|||
@ -0,0 +1,125 @@ |
|||
#nullable enable |
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc.Abstractions; |
|||
using Microsoft.AspNetCore.Mvc.Rendering; |
|||
using Microsoft.AspNetCore.Mvc.Routing; |
|||
using Microsoft.AspNetCore.Razor.TagHelpers; |
|||
using Microsoft.AspNetCore.Routing; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Security; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers; |
|||
|
|||
public class AbpTagHelperResourceService_ExternalFile_Tests |
|||
{ |
|||
[Theory] |
|||
[InlineData("/themes/basic/googlefonts.css", "", "/themes/basic/googlefonts.css")] |
|||
[InlineData("/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] |
|||
[InlineData("~/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] |
|||
[InlineData("/themes/basic/googlefonts.css", "/foo/bar", "/foo/bar/themes/basic/googlefonts.css")] |
|||
[InlineData("https://fonts.googleapis.com/css2?family=Lexend", "/foo", "https://fonts.googleapis.com/css2?family=Lexend")] |
|||
[InlineData("//cdn.example.com/foo.css", "/foo", "//cdn.example.com/foo.css")] |
|||
[InlineData("main.css", "/foo", "main.css")] |
|||
[InlineData("/foo/themes/basic/googlefonts.css", "/foo", "/foo/themes/basic/googlefonts.css")] |
|||
[InlineData("/foo/bar/themes/basic/googlefonts.css", "/foo/bar", "/foo/bar/themes/basic/googlefonts.css")] |
|||
[InlineData("/foo/themes/basic/googlefonts.css?v=1", "/foo", "/foo/themes/basic/googlefonts.css?v=1")] |
|||
[InlineData("/foobar/themes/basic/googlefonts.css", "/foo", "/foo/foobar/themes/basic/googlefonts.css")] |
|||
[InlineData("~/themes/basic/googlefonts.css", "", "/themes/basic/googlefonts.css")] |
|||
[InlineData("~/themes/basic/googlefonts.css", "/themes", "/themes/themes/basic/googlefonts.css")] |
|||
[InlineData("~/themes/basic/googlefonts.css", "/themes/basic", "/themes/basic/themes/basic/googlefonts.css")] |
|||
public void Style_Should_Resolve_Local_External_File_With_PathBase(string fileName, string pathBase, string expected) |
|||
{ |
|||
var service = new TestStyleService(); |
|||
|
|||
var output = CreateOutput(); |
|||
service.AddTag(CreateViewContext(pathBase), output, new BundleFile(fileName, true)); |
|||
|
|||
output.Content.GetContent().ShouldContain($"href=\"{expected}\""); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData("/libs/external.js", "", "/libs/external.js")] |
|||
[InlineData("/libs/external.js", "/foo", "/foo/libs/external.js")] |
|||
[InlineData("~/libs/external.js", "/foo", "/foo/libs/external.js")] |
|||
[InlineData("https://cdn.example.com/external.js", "/foo", "https://cdn.example.com/external.js")] |
|||
[InlineData("//cdn.example.com/external.js", "/foo", "//cdn.example.com/external.js")] |
|||
[InlineData("/foo/libs/external.js", "/foo", "/foo/libs/external.js")] |
|||
[InlineData("/foo/bar/libs/external.js", "/foo/bar", "/foo/bar/libs/external.js")] |
|||
[InlineData("/foobar/libs/external.js", "/foo", "/foo/foobar/libs/external.js")] |
|||
public void Script_Should_Resolve_Local_External_File_With_PathBase(string fileName, string pathBase, string expected) |
|||
{ |
|||
var service = new TestScriptService(); |
|||
|
|||
var output = CreateOutput(); |
|||
service.AddTag(CreateViewContext(pathBase), output, new BundleFile(fileName, true)); |
|||
|
|||
output.Content.GetContent().ShouldContain($"src=\"{expected}\""); |
|||
} |
|||
|
|||
private static ViewContext CreateViewContext(string pathBase) |
|||
{ |
|||
var httpContext = new DefaultHttpContext |
|||
{ |
|||
RequestServices = new ServiceCollection() |
|||
.AddSingleton<IUrlHelperFactory, UrlHelperFactory>() |
|||
.BuildServiceProvider() |
|||
}; |
|||
httpContext.Request.PathBase = new PathString(pathBase); |
|||
|
|||
return new ViewContext |
|||
{ |
|||
HttpContext = httpContext, |
|||
RouteData = new RouteData(), |
|||
ActionDescriptor = new ActionDescriptor() |
|||
}; |
|||
} |
|||
|
|||
private static TagHelperOutput CreateOutput() |
|||
{ |
|||
return new TagHelperOutput( |
|||
"abp-test", |
|||
new TagHelperAttributeList(), |
|||
(_, _) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent())); |
|||
} |
|||
|
|||
private static TagHelperContext CreateContext() |
|||
{ |
|||
return new TagHelperContext(new TagHelperAttributeList(), new Dictionary<object, object>(), "test"); |
|||
} |
|||
|
|||
private class TestTagHelper : TagHelper |
|||
{ |
|||
} |
|||
|
|||
private class TestStyleService : AbpTagHelperStyleService |
|||
{ |
|||
public TestStyleService() |
|||
: base(null!, Microsoft.Extensions.Options.Options.Create(new AbpBundlingOptions()), null!, Microsoft.Extensions.Options.Options.Create(new AbpSecurityHeadersOptions())) |
|||
{ |
|||
} |
|||
|
|||
public void AddTag(ViewContext viewContext, TagHelperOutput output, BundleFile file) |
|||
{ |
|||
AddHtmlTag(viewContext, new TestTagHelper(), CreateContext(), output, file, null); |
|||
} |
|||
} |
|||
|
|||
private class TestScriptService : AbpTagHelperScriptService |
|||
{ |
|||
public TestScriptService() |
|||
: base(null!, Microsoft.Extensions.Options.Options.Create(new AbpBundlingOptions()), null!) |
|||
{ |
|||
} |
|||
|
|||
public void AddTag(ViewContext viewContext, TagHelperOutput output, BundleFile file) |
|||
{ |
|||
AddHtmlTag(viewContext, new TestTagHelper(), CreateContext(), output, file, null); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue