Skip to content

Commit

Permalink
[add] StaticFille tests
Browse files Browse the repository at this point in the history
[add] StaticFile operation full paths
  • Loading branch information
i4004 committed May 25, 2024
1 parent 50242fa commit 5ef01a6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/Simplify.Web.Tests/Simplify.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="StaticFiles\IO\TestFiles\TestStaticFile.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
81 changes: 81 additions & 0 deletions src/Simplify.Web.Tests/StaticFiles/IO/StaticFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using Simplify.Web.StaticFiles.IO;

namespace Simplify.Web.Tests.StaticFiles.IO;

[TestFixture]
public class StaticFileTests
{
private const string ValidRelativeFilePath = "StaticFiles/IO/TestFiles/TestStaticFile.html";

// ReSharper disable once StringLiteralTypo
private readonly IList<string> _validPaths = ["staticfiles"];

private readonly string _sitePhysicalPath = Directory.GetCurrentDirectory() + "/";

private StaticFile _staticFile = null!;

[SetUp]
public void Initialize() => _staticFile = new StaticFile(_validPaths, _sitePhysicalPath);

[Test]
public void IsValidPath_AllowedPathAndValidFile_True()
{
// Act
var result = _staticFile.IsValidPath(ValidRelativeFilePath);

// Assert
Assert.That(result, Is.True);
}

[Test]
public void IsValidPath_ValidFileButPathIsNotAllowed_False()
{
// Arrange
var staticFile = new StaticFile([], _sitePhysicalPath);

// Act
var result = staticFile.IsValidPath(ValidRelativeFilePath);

// Assert
Assert.That(result, Is.False);
}

[Test]
public void IsValidPath_AllowedPathButInvalidFile_False()
{
// Arrange
const string relativeFilePath = "StaticFiles/IO/TestFiles/NotExistent.html";

// Act
var result = _staticFile.IsValidPath(relativeFilePath);

// Assert
Assert.That(result, Is.False);
}

[Test]
public void GetLastModificationTime_ValidFile_MillisecondsTrimmed()
{
// Act
var result = _staticFile.GetLastModificationTime(ValidRelativeFilePath);

// Assert

Assert.That(result.Year, Is.Not.Zero);
Assert.That(result.Millisecond, Is.Zero);
}

[Test]
public async Task GetDataAsync_ValidFile_DataReturned()
{
// Act
var result = await _staticFile.GetDataAsync(ValidRelativeFilePath);

// Assert
Assert.That(result, Is.EqualTo("Foo"u8.ToArray()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
14 changes: 9 additions & 5 deletions src/Simplify.Web/StaticFiles/IO/StaticFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ public class StaticFile(IList<string> staticFilesPaths, string sitePhysicalPath)
/// Determines whether the relative file path is a static file route path.
/// </summary>
/// <param name="relativeFilePath">The relative file path.</param>
public bool IsValidPath(string relativeFilePath) =>
staticFilesPaths
.Where(relativeFilePath.ToLower().StartsWith)
public bool IsValidPath(string relativeFilePath)
{
relativeFilePath = relativeFilePath.ToLower();

return staticFilesPaths
.Where(relativeFilePath.StartsWith)
.Any(_ => File.Exists(sitePhysicalPath + relativeFilePath));
}

public DateTime GetLastModificationTime(string relativeFilePath) => File.GetLastWriteTimeUtc(relativeFilePath).TrimMilliseconds();
public DateTime GetLastModificationTime(string relativeFilePath) => File.GetLastWriteTimeUtc(sitePhysicalPath + relativeFilePath).TrimMilliseconds();

/// <summary>
/// Gets the file data.
Expand All @@ -42,7 +46,7 @@ public async Task<byte[]> GetDataAsync(string relativeFilePath)

await stream.ReadAsync(result, 0, (int)stream.Length);
#else
await using var stream = File.Open(relativeFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
await using var stream = File.Open(sitePhysicalPath + relativeFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

var result = new byte[stream.Length];

Expand Down

0 comments on commit 5ef01a6

Please sign in to comment.