Creating UrlUtility for IsLocalUrl, and cleaning up UrlHelper

This commit is contained in:
Youngjune Hong 2015-01-26 15:33:07 -08:00
parent 42df4cf2ed
commit 70efc5ae0a
4 changed files with 134 additions and 10 deletions

View File

@ -33,10 +33,24 @@ namespace Microsoft.AspNet.Mvc
string Content(string contentPath);
/// <summary>
/// Returns a value that indicates whether the URL is local.
/// Returns a value that indicates whether the URL is local. An URL with an absolute path is considered local
/// if it does not have a host/authority part. URLs using the virtual paths ('~/') are also local.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>true if the URL is local; otherwise, false.</returns>
/// <returns><c>true</c> if the URL is local; otherwise, <c>false</c>.</returns>
/// <example>
/// <para>
/// For example, the following URLs are considered local:
/// /Views/Default/Index.html
/// ~/Index.html
/// </para>
/// <para>
/// The following URLs are non-local:
/// ../Index.html
/// http://www.contoso.com/
/// http://localhost/Index.html
/// </para>
/// </example>
bool IsLocalUrl(string url);
/// <summary>

View File

@ -0,0 +1,39 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Mvc.Internal
{
public static class UrlUtility
{
/// <summary>
/// Returns a value that indicates whether the URL is local. An URL with an absolute path is considered local
/// if it does not have a host/authority part. URLs using the virtual paths ('~/') are also local.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns><c>true</c> if the URL is local; otherwise, <c>false</c>.</returns>
/// <example>
/// <para>
/// For example, the following URLs are considered local:
/// /Views/Default/Index.html
/// ~/Index.html
/// </para>
/// <para>
/// The following URLs are non-local:
/// ../Index.html
/// http://www.contoso.com/
/// http://localhost/Index.html
/// </para>
/// </example>
public static bool IsLocalUrl(string url)
{
return
!string.IsNullOrEmpty(url) &&
// Allows "/" or "/foo" but not "//" or "/\".
((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) ||
// Allows "~/" or "~/foo".
(url.Length > 1 && url[0] == '~' && url[1] == '/'));
}
}
}

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
@ -71,14 +72,7 @@ namespace Microsoft.AspNet.Mvc
/// <inheritdoc />
public bool IsLocalUrl(string url)
{
return
!string.IsNullOrEmpty(url) &&
// Allows "/" or "/foo" but not "//" or "/\".
((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) ||
// Allows "~/" or "~/foo".
(url.Length > 1 && url[0] == '~' && url[1] == '/'));
return UrlUtility.IsLocalUrl(url);
}
/// <inheritdoc />

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
@ -58,9 +59,13 @@ namespace Microsoft.AspNet.Mvc.Core.Test
Assert.Equal(expectedPath, path);
}
// UrlHelper.IsLocalUrl depends on the UrlUtility.IsLocalUrl method.
// To avoid duplicate tests, all the tests exercising IsLocalUrl verify
// both of UrlHelper.IsLocalUrl and UrlUtility.IsLocalUrl
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void IsLocalUrl_ReturnsFalseOnEmpty(string url)
{
// Arrange
@ -71,6 +76,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -87,6 +98,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.True(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.True(result);
}
[Theory]
@ -102,6 +119,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.True(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.True(result);
}
[Theory]
@ -118,6 +141,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -135,6 +164,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -150,6 +185,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -165,6 +206,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -179,6 +226,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -196,6 +249,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -214,6 +273,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -233,6 +298,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -250,6 +321,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Fact]