From 2c65cd280e1a06525578611b41572184a80311e6 Mon Sep 17 00:00:00 2001 From: sbohlen Date: Tue, 3 Aug 2010 16:17:25 +0000 Subject: [PATCH] SPRNET-984 -modified CreateAbsolutePath method to respect FQ urls as 'relativePath' argument --- src/Spring/Spring.Web/Util/WebUtils.cs | 5 ++ .../Spring.Web.Tests/Util/WebUtilsTests.cs | 88 ++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/Spring/Spring.Web/Util/WebUtils.cs b/src/Spring/Spring.Web/Util/WebUtils.cs index 8216bb7f..365529e7 100644 --- a/src/Spring/Spring.Web/Util/WebUtils.cs +++ b/src/Spring/Spring.Web/Util/WebUtils.cs @@ -158,6 +158,11 @@ namespace Spring.Util { if (StringUtils.HasLength(relativePath)) { + if (relativePath.ToLowerInvariant().StartsWith("http://") || relativePath.ToLowerInvariant().StartsWith("https://")) + { + return relativePath; + } + if (relativePath.StartsWith("/")) { return relativePath; diff --git a/test/Spring/Spring.Web.Tests/Util/WebUtilsTests.cs b/test/Spring/Spring.Web.Tests/Util/WebUtilsTests.cs index b10d46d9..d8edab62 100644 --- a/test/Spring/Spring.Web.Tests/Util/WebUtilsTests.cs +++ b/test/Spring/Spring.Web.Tests/Util/WebUtilsTests.cs @@ -104,29 +104,113 @@ namespace Spring.Util } [Test] - public void TestCreateAbsolutePath() + public void CreateAbsolutePath_EmptyApplicationPathAndNullRelativePath_ReturnsRootPath() { Assert.AreEqual("/", WebUtils.CreateAbsolutePath("", null)); + } + + [Test] + public void CreateAbsolutePath_RootApplicationPathAndNullRelativePath_ReturnsRootPath() + { Assert.AreEqual("/", WebUtils.CreateAbsolutePath("/", null)); + } + + [Test] + public void CreateAbsolutePath_NullRelativePath_ReturnsApplicationPath() + { Assert.AreEqual("/MyApp/", WebUtils.CreateAbsolutePath("/MyApp", null)); + } + + [Test] + public void CreateAbsolutePath_EmptyRelativePath_ReturnsApplicationPath() + { Assert.AreEqual("/MyApp/", WebUtils.CreateAbsolutePath("/MyApp", string.Empty)); + } + [Test] + public void CreateAbsolutePath_NullApplicationPath_ReturnsRelativePath() + { Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath(null, "/MyPath")); + } + + [Test] + public void CreateAbsolutePath_NullApplicationPathAndAppRootRelativePath_ReturnsRelativePath() + { Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath(null, "~/MyPath")); + } + [Test] + public void CreateAbsolutePath_EmptyApplicationPath_ReturnsRelativePath() + { Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath(string.Empty, "/MyPath")); + } + + [Test] + public void CreateAbsolutePath_EmptyApplicationPathAndAppRootRelativePath_ReturnsRelativePath() + { Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath(string.Empty, "~/MyPath")); + } - Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath("/", "MyPath")); + [Test] + public void CreateAbsolutePath_RootApplicationPath_ReturnsRelativePath() + { + Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath("/", "/MyPath")); + } + + [Test] + public void CreateAbsolutePath_RootApplicationPathAndAppRootRelativePath_ReturnsRelativePath() + { Assert.AreEqual("/MyPath", WebUtils.CreateAbsolutePath("/", "~/MyPath")); + } + [Test] + public void CreateAbsolutePath_ApplicationPathAndRelativePath_ReturnsConcatenatedPath() + { Assert.AreEqual("/MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp", "MyPath")); + } + + [Test] + public void CreateAbsolutePath_ApplicationPathAndAppRootRelativePath_ReturnsConcatenatedPath() + { Assert.AreEqual("/MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp", "~/MyPath")); + } + + [Test] + public void CreateAbsolutePath_ApplicationPathWithTrailingSlashAndRelativePath_ReturnsConcatenatedPath() + { Assert.AreEqual("/MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp/", "MyPath")); + } + + [Test] + public void CreateAbsolutePath_ApplicationPathWithTrailingSlashAndAppRootRelativePath_ReturnsConcatenatedPath() + { Assert.AreEqual("/MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp/", "~/MyPath")); + } + + [Test] + public void CreateAbsolutePath_WhenRelativePathBeginsWithApplicationPath_ReturnsConcatenatedPath() + { Assert.AreEqual("/MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp", "/MyApp/MyPath")); } + [Test] + public void CreateAbsolutePath_WhenRelativePathIsHttpQualifiedUrl_ReturnsRelativePath() + { + Assert.AreEqual("http://MyApp/MyPath", WebUtils.CreateAbsolutePath(null, "http://MyApp/MyPath")); + Assert.AreEqual("http://MyApp/MyPath", WebUtils.CreateAbsolutePath("/", "http://MyApp/MyPath")); + Assert.AreEqual("http://MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp", "http://MyApp/MyPath")); + Assert.AreEqual("http://MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp/", "http://MyApp/MyPath")); + } + + [Test] + public void CreateAbsolutePath_WhenRelativePathIsHttpsQualifiedUrl_ReturnsRelativePath() + { + Assert.AreEqual("https://MyApp/MyPath", WebUtils.CreateAbsolutePath(null, "https://MyApp/MyPath")); + Assert.AreEqual("https://MyApp/MyPath", WebUtils.CreateAbsolutePath("/", "https://MyApp/MyPath")); + Assert.AreEqual("https://MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp", "https://MyApp/MyPath")); + Assert.AreEqual("https://MyApp/MyPath", WebUtils.CreateAbsolutePath("/MyApp/", "https://MyApp/MyPath")); + } + [Test] [ExpectedException(typeof(ArgumentNullException))] public void CombineVirtualPathsDoesntAcceptNullRoot()