Adding arg null or empty checks to RazorViewEngine.FindView &

FindPartialView
This commit is contained in:
Pranav K 2014-11-02 18:33:56 -08:00
parent 74da350086
commit d5515bfbb6
2 changed files with 42 additions and 3 deletions

View File

@ -3,10 +3,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.PageExecutionInstrumentation;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Mvc.Razor
@ -69,15 +69,25 @@ namespace Microsoft.AspNet.Mvc.Razor
/// <inheritdoc />
public ViewEngineResult FindView([NotNull] ActionContext context,
[NotNull] string viewName)
string viewName)
{
if (string.IsNullOrEmpty(viewName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(viewName));
}
return CreateViewEngineResult(context, viewName, partial: false);
}
/// <inheritdoc />
public ViewEngineResult FindPartialView([NotNull] ActionContext context,
[NotNull] string partialViewName)
string partialViewName)
{
if (string.IsNullOrEmpty(partialViewName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(partialViewName));
}
return CreateViewEngineResult(context, partialViewName, partial: true);
}
@ -191,6 +201,7 @@ namespace Microsoft.AspNet.Mvc.Razor
private static bool IsSpecificPath(string name)
{
Debug.Assert(!string.IsNullOrEmpty(name));
return name[0] == '~' || name[0] == '/';
}
}

View File

@ -8,6 +8,7 @@ using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.PipelineCore;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Testing;
using Moq;
using Xunit;
@ -37,6 +38,19 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
}
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void FindView_ThrowsIfViewNameIsNullOrEmpty(string viewName)
{
// Arrange
var viewEngine = CreateViewEngine();
var context = GetActionContext(_controllerTestContext);
// Act & Assert
ExceptionAssert.ThrowsArgumentNullOrEmpty(() => viewEngine.FindView(context, viewName), "viewName");
}
[Theory]
[MemberData(nameof(InvalidViewNameValues))]
public void FindView_WithFullPathReturnsNotFound_WhenPathDoesNotMatchExtension(string viewName)
@ -69,6 +83,20 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
Assert.False(result.Success);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void FindPartialView_ThrowsIfViewNameIsNullOrEmpty(string partialViewName)
{
// Arrange
var viewEngine = CreateViewEngine();
var context = GetActionContext(_controllerTestContext);
// Act & Assert
ExceptionAssert.ThrowsArgumentNullOrEmpty(() => viewEngine.FindPartialView(context, partialViewName),
"partialViewName");
}
[Theory]
[MemberData(nameof(InvalidViewNameValues))]
public void FindPartialView_WithFullPathReturnsNotFound_WhenPathDoesNotMatchExtension(string partialViewName)