PhysicalFileResult throws NotSupported on path not rooted

This commit is contained in:
unknown 2015-10-09 16:54:49 -07:00 committed by ryanbrandenburg
parent 32645e93c8
commit 6a4da5d795
4 changed files with 14 additions and 5 deletions

View File

@ -87,7 +87,7 @@ namespace Microsoft.AspNet.Mvc
{ {
if (!Path.IsPathRooted(FileName)) if (!Path.IsPathRooted(FileName))
{ {
throw new FileNotFoundException(Resources.FormatFileResult_InvalidPath(FileName), FileName); throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName));
} }
var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>(); var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();

View File

@ -650,6 +650,11 @@ namespace Microsoft.AspNet.Mvc.Core
return string.Format(CultureInfo.CurrentCulture, GetString("FileResult_InvalidPath"), p0); return string.Format(CultureInfo.CurrentCulture, GetString("FileResult_InvalidPath"), p0);
} }
internal static string FormatFileResult_PathNotRooted(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("FileResult_PathNotRooted"), p0);
}
/// <summary> /// <summary>
/// The input was not valid. /// The input was not valid.
/// </summary> /// </summary>

View File

@ -315,4 +315,8 @@
<data name="HttpResponseStreamWriter_InvalidBufferSize" xml:space="preserve"> <data name="HttpResponseStreamWriter_InvalidBufferSize" xml:space="preserve">
<value>The byte buffer must have a length of at least '{0}' to be used with a char buffer of size '{1}' and encoding '{2}'. Use '{3}.{4}' to compute the correct size for the byte buffer.</value> <value>The byte buffer must have a length of at least '{0}' to be used with a char buffer of size '{1}' and encoding '{2}'. Use '{3}.{4}' to compute the correct size for the byte buffer.</value>
</data> </data>
<data name="FileResult_PathNotRooted" xml:space="preserve">
<value>Path '{0}' was not rooted.</value>
<comment>{0} is the path which wasn't rooted</comment>
</data>
</root> </root>

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -131,19 +132,18 @@ namespace Microsoft.AspNet.Mvc
[InlineData("..\\TestFiles\\SubFolder/SubFolderTestFile.txt")] [InlineData("..\\TestFiles\\SubFolder/SubFolderTestFile.txt")]
[InlineData("~/SubFolder/SubFolderTestFile.txt")] [InlineData("~/SubFolder/SubFolderTestFile.txt")]
[InlineData("~/SubFolder\\SubFolderTestFile.txt")] [InlineData("~/SubFolder\\SubFolderTestFile.txt")]
public async Task ExecuteAsync_ThrowsFileNotFound_ForNonRootedPaths(string path) public async Task ExecuteAsync_ThrowsNotSupported_ForNonRootedPaths(string path)
{ {
// Arrange // Arrange
var result = new TestPhysicalFileResult(path, "text/plain"); var result = new TestPhysicalFileResult(path, "text/plain");
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()); var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
var expectedMessage = "Could not find file: " + path; var expectedMessage = $"Path '{path}' was not rooted.";
// Act // Act
var ex = await Assert.ThrowsAsync<FileNotFoundException>(() => result.ExecuteResultAsync(context)); var ex = await Assert.ThrowsAsync<NotSupportedException>(() => result.ExecuteResultAsync(context));
// Assert // Assert
Assert.Equal(expectedMessage, ex.Message); Assert.Equal(expectedMessage, ex.Message);
Assert.Equal(path, ex.FileName);
} }
[Theory] [Theory]