#2295 - Disposing the read stream after computing file hash.
This commit is contained in:
parent
50ea1fb3d2
commit
6e9533ce9c
|
|
@ -77,8 +77,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
|
||||||
{
|
{
|
||||||
using (var sha256 = SHA256.Create())
|
using (var sha256 = SHA256.Create())
|
||||||
{
|
{
|
||||||
var hash = sha256.ComputeHash(fileInfo.CreateReadStream());
|
using (var readStream = fileInfo.CreateReadStream())
|
||||||
return WebEncoders.Base64UrlEncode(hash);
|
{
|
||||||
|
var hash = sha256.ComputeHash(readStream);
|
||||||
|
return WebEncoders.Base64UrlEncode(hash);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,38 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
|
||||||
Assert.Equal(expected, result);
|
Assert.Equal(expected, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verifies if the stream is closed after reading.
|
||||||
|
[Fact]
|
||||||
|
public void AddsVersionToFiles_DoesNotLockFileAfterReading()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello World!"));
|
||||||
|
var mockFile = new Mock<IFileInfo>();
|
||||||
|
mockFile.SetupGet(f => f.Exists).Returns(true);
|
||||||
|
mockFile
|
||||||
|
.Setup(m => m.CreateReadStream())
|
||||||
|
.Returns(stream);
|
||||||
|
|
||||||
|
var mockFileProvider = new Mock<IFileProvider>();
|
||||||
|
mockFileProvider.Setup(fp => fp.GetFileInfo(It.IsAny<string>()))
|
||||||
|
.Returns(mockFile.Object);
|
||||||
|
|
||||||
|
var hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||||
|
hostingEnvironment.Setup(h => h.WebRootFileProvider).Returns(mockFileProvider.Object);
|
||||||
|
|
||||||
|
var fileVersionProvider = new FileVersionProvider(
|
||||||
|
hostingEnvironment.Object.WebRootFileProvider,
|
||||||
|
GetMockCache(),
|
||||||
|
GetRequestPathBase());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = fileVersionProvider.AddFileVersionToPath("/hello/world");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.False(stream.CanRead);
|
||||||
|
Assert.Throws<ObjectDisposedException>(() => fileVersionProvider.AddFileVersionToPath("/hello/world"));
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("/testApp/hello/world", true, "/testApp")]
|
[InlineData("/testApp/hello/world", true, "/testApp")]
|
||||||
[InlineData("/testApp/foo/bar/hello/world", true, "/testApp/foo/bar")]
|
[InlineData("/testApp/foo/bar/hello/world", true, "/testApp/foo/bar")]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue