[Fixes #2929] Moving IsLocalUrl implementation to UrlHelper
This commit is contained in:
parent
a0e0df87de
commit
dd94d54e1d
|
|
@ -1,39 +0,0 @@
|
||||||
// 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.
|
|
||||||
|
|
||||||
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] == '/'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,7 +6,6 @@ using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Mvc.Actions;
|
using Microsoft.AspNet.Mvc.Actions;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
|
|
@ -87,9 +86,16 @@ namespace Microsoft.AspNet.Mvc
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsLocalUrl(string url)
|
public virtual bool IsLocalUrl(string url)
|
||||||
{
|
{
|
||||||
return UrlUtility.IsLocalUrl(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] == '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Internal;
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Microsoft.AspNet.Mvc.Actions;
|
using Microsoft.AspNet.Mvc.Actions;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
|
|
@ -66,9 +65,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Assert.Equal(expectedPath, path);
|
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]
|
[Theory]
|
||||||
[InlineData(null)]
|
[InlineData(null)]
|
||||||
[InlineData("")]
|
[InlineData("")]
|
||||||
|
|
@ -83,12 +79,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -105,12 +95,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(result);
|
Assert.True(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -126,12 +110,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(result);
|
Assert.True(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.True(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -148,12 +126,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -171,12 +143,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -192,12 +158,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -213,12 +173,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -233,12 +187,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -256,12 +204,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -280,12 +222,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -305,12 +241,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -328,12 +258,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
|
|
||||||
// Arrange & Act
|
|
||||||
result = UrlUtility.IsLocalUrl(url);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.False(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue