[Static Web Assets] Allow assets with empty base paths (#17414)

This commit is contained in:
Javier Calvarro Nelson 2020-01-15 11:45:21 -08:00 committed by Artak
parent cba11d7e22
commit b4d2f9aa52
2 changed files with 39 additions and 0 deletions

View File

@ -46,6 +46,11 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
{
var modifiedSub = NormalizePath(subpath);
if (BasePath == "/")
{
return InnerProvider.GetDirectoryContents(modifiedSub);
}
if (StartsWithBasePath(modifiedSub, out var physicalPath))
{
return InnerProvider.GetDirectoryContents(physicalPath.Value);
@ -67,6 +72,11 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
{
var modifiedSub = NormalizePath(subpath);
if (BasePath == "/")
{
return InnerProvider.GetFileInfo(subpath);
}
if (!StartsWithBasePath(modifiedSub, out var physicalPath))
{
return new NotFoundFileInfo(subpath);

View File

@ -117,6 +117,35 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
Assert.True(provider.GetFileInfo("/_content/Static Web Assets.txt").Exists);
}
[Fact]
public void GetDirectoryContents_HandlesEmptyBasePath()
{
// Arrange
var provider = new StaticWebAssetsFileProvider("/",
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));
// Act
var directory = provider.GetDirectoryContents("/Static Web/");
// Assert
Assert.Collection(directory,
file =>
{
Assert.Equal("Static Web.txt", file.Name);
});
}
[Fact]
public void StaticWebAssetsFileProviderWithEmptyBasePath_FindsFile()
{
// Arrange & Act
var provider = new StaticWebAssetsFileProvider("/",
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));
// Assert
Assert.True(provider.GetFileInfo("/Static Web Assets.txt").Exists);
}
[Fact]
public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
{