Files
acceptance-tests
data
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Properties
AnalyticsTest.cs
BingTest.cs
FacebookTest.cs
FileUploadTest.cs
GamerCardTest.cs
GravatarTest.cs
LinkShareTest.cs
MapsTest.cs
Microsoft.Web.Helpers.Test.csproj
PreAppStartCodeTest.cs
ReCaptchaTest.cs
ThemesTest.cs
TwitterTest.cs
UrlBuilderTest.cs
VideoTest.cs
packages.config
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
ikdasm
ikvm
linker
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
ikvm-native
libgc
llvm
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

416 lines
14 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.WebPages;
using Moq;
using Xunit;
namespace Microsoft.Web.Helpers.Test
{
public class UrlBuilderTest
{
private static TestVirtualPathUtility _virtualPathUtility = new TestVirtualPathUtility();
[Fact]
public void UrlBuilderUsesPathAsIsIfPathIsAValidUri()
{
// Arrange
var pagePath = "http://www.test.com/page-path";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal("http://www.test.com/page-path", builder.Path);
}
[Fact]
public void UrlBuilderUsesQueryComponentsIfPathIsAValidUri()
{
// Arrange
var pagePath = "http://www.test.com/page-path.vbhtml?param=value&baz=biz";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal("http://www.test.com/page-path.vbhtml", builder.Path);
Assert.Equal("?param=value&baz=biz", builder.QueryString);
}
[Fact]
public void UrlBuilderUsesUsesObjectParameterAsQueryString()
{
// Arrange
var pagePath = "http://www.test.com/page-path.vbhtml?param=value&baz=biz";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal("http://www.test.com/page-path.vbhtml", builder.Path);
Assert.Equal("?param=value&baz=biz", builder.QueryString);
}
[Fact]
public void UrlBuilderAppendsObjectParametersToPathWithQueryString()
{
// Arrange
var pagePath = "http://www.test.com/page-path.vbhtml?param=value&baz=biz";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, new { param2 = "param2val" });
// Assert
Assert.Equal("http://www.test.com/page-path.vbhtml", builder.Path);
Assert.Equal("?param=value&baz=biz&param2=param2val", builder.QueryString);
}
[Fact]
public void UrlBuilderWithVirtualPathUsesVirtualPathUtility()
{
// Arrange
var pagePath = "~/page";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal("page", builder.Path);
}
[Fact]
public void UrlBuilderDoesNotResolvePathIfContextIsNull()
{
// Arrange
var pagePath = "~/foo/bar";
// Act
var builder = new UrlBuilder(null, _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal(pagePath, builder.Path);
}
[Fact]
public void UrlBuilderSetsPathToNullIfContextIsNullAndPagePathIsNotSpecified()
{
// Act
var builder = new UrlBuilder(null, null, null, null);
// Assert
Assert.Null(builder.Path);
Assert.True(String.IsNullOrEmpty(builder.ToString()));
}
[Fact]
public void UrlBuilderWithVirtualPathAppendsObjectAsQueryString()
{
// Arrange
var pagePath = "~/page";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, new { Foo = "bar", baz = "qux" });
// Assert
Assert.Equal("page", builder.Path);
Assert.Equal("?Foo=bar&baz=qux", builder.QueryString);
}
[Fact]
public void UrlBuilderWithVirtualPathExtractsQueryStringParameterFromPath()
{
// Arrange
var pagePath = "~/dir/page?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
// Assert
Assert.Equal("dir/page", builder.Path);
Assert.Equal("?someparam=value", builder.QueryString);
}
[Fact]
public void UrlBuilderWithVirtualPathAndQueryStringAppendsObjectAsQueryStringParams()
{
// Arrange
var pagePath = "~/dir/page?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, new { someotherparam = "value2" });
// Assert
Assert.Equal("dir/page", builder.Path);
Assert.Equal("?someparam=value&someotherparam=value2", builder.QueryString);
}
[Fact]
public void AddPathAddsPathPortionToRelativeUrl()
{
// Arrange
var pagePath = "~/dir/page?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo").AddPath("bar/baz");
// Assert
Assert.Equal("dir/page/foo/bar/baz", builder.Path);
Assert.Equal("?someparam=value", builder.QueryString);
}
[Fact]
public void AddPathEncodesPathParams()
{
// Arrange
var pagePath = "~/dir/page?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo bar").AddPath("baz biz", "qux");
// Assert
Assert.Equal("dir/page/foo%20bar/baz%20biz/qux", builder.Path);
}
[Fact]
public void AddPathAddsPathPortionToAbsoluteUrl()
{
// Arrange
var pagePath = "http://some-site/dir/page?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo").AddPath("bar/baz");
// Assert
Assert.Equal("http://some-site/dir/page/foo/bar/baz", builder.Path);
Assert.Equal("?someparam=value", builder.QueryString);
}
[Fact]
public void AddPathWithParamsArrayAddsPathPortionToRelativeUrl()
{
// Arrange
var pagePath = "~/dir/page/?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo", "bar", "baz").AddPath("qux");
// Assert
Assert.Equal("dir/page/foo/bar/baz/qux", builder.Path);
Assert.Equal("?someparam=value", builder.QueryString);
}
[Fact]
public void AddPathEnsuresSlashesAreNotRepeated()
{
// Arrange
var pagePath = "~/dir/page/";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo").AddPath("/bar/").AddPath("/baz");
// Assert
Assert.Equal("dir/page/foo/bar/baz", builder.Path);
}
[Fact]
public void AddPathWithParamsEnsuresSlashAreNotRepeated()
{
// Arrange
var pagePath = "~/dir/page/";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo", "/bar/", "/baz").AddPath("qux/");
// Assert
Assert.Equal("dir/page/foo/bar/baz/qux/", builder.Path);
}
[Fact]
public void AddPathWithParamsArrayAddsPathPortionToAbsoluteUrl()
{
// Arrange
var pagePath = "http://www.test.com/dir/page/?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddPath("foo", "bar", "baz").AddPath("qux");
// Assert
Assert.Equal("http://www.test.com/dir/page/foo/bar/baz/qux", builder.Path);
Assert.Equal("?someparam=value", builder.QueryString);
}
[Fact]
public void UrlBuilderEncodesParameters()
{
// Arrange
var pagePath = "~/dir/page/?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, new { Λ = "λ" });
builder.AddParam(new { π = "is not a lie" }).AddParam("Π", "maybe a lie");
// Assert
Assert.Equal("?someparam=value&%ce%9b=%ce%bb&%cf%80=is+not+a+lie&%ce%a0=maybe+a+lie", builder.QueryString);
}
[Fact]
public void AddParamAddsParamToQueryString()
{
// Arrange
var pagePath = "http://www.test.com/dir/page/?someparam=value";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddParam("foo", "bar");
// Assert
Assert.Equal("?someparam=value&foo=bar", builder.QueryString);
}
[Fact]
public void AddParamAddsQuestionMarkToQueryStringIfFirstParam()
{
// Arrange
var pagePath = "~/dir/page";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddParam("foo", "bar").AddParam(new { baz = "qux", biz = "quark" });
// Assert
Assert.Equal("?foo=bar&baz=qux&biz=quark", builder.QueryString);
}
[Fact]
public void AddParamIgnoresParametersWithEmptyKey()
{
// Arrange
var pagePath = "~/dir/page";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddParam("", "bar").AddParam(new { baz = "", biz = "quark" }).AddParam("qux", null).AddParam(null, "somevalue");
// Assert
Assert.Equal("?baz=&biz=quark&qux=", builder.QueryString);
}
[Fact]
public void ToStringConcatsPathAndQueryString()
{
// Arrange
var pagePath = "~/dir/page";
// Act
var builder = new UrlBuilder(GetContext(), _virtualPathUtility, pagePath, null);
builder.AddParam("foo", "bar").AddParam(new { baz = "qux", biz = "quark" });
// Assert
Assert.Equal("dir/page?foo=bar&baz=qux&biz=quark", builder.ToString());
}
[Fact]
public void UrlBuilderWithRealVirtualPathUtilityTest()
{
// Arrange
var pagePath = "~/world/test.aspx";
try
{
// Act
CreateHttpContext("default.aspx", "http://localhost/WebSite1/subfolder1/default.aspx");
CreateHttpRuntime("/WebSite1/");
var builder = new UrlBuilder(pagePath, null);
// Assert
Assert.Equal(@"/WebSite1/world/test.aspx", builder.Path);
}
finally
{
RestoreHttpRuntime();
}
}
private static HttpContextBase GetContext(params string[] virtualPaths)
{
var httpContext = new Mock<HttpContextBase>();
var table = new Hashtable();
httpContext.SetupGet(c => c.Items).Returns(table);
foreach (var item in virtualPaths)
{
var page = new Mock<ITemplateFile>();
page.SetupGet(p => p.TemplateInfo).Returns(new TemplateFileInfo(item));
TemplateStack.Push(httpContext.Object, page.Object);
}
return httpContext.Object;
}
private class TestVirtualPathUtility : VirtualPathUtilityBase
{
public override string Combine(string basePath, string relativePath)
{
return basePath + '/' + relativePath.TrimStart('~', '/');
}
public override string ToAbsolute(string virtualPath)
{
return virtualPath.TrimStart('~', '/');
}
}
internal static void CreateHttpRuntime(string appVPath)
{
var runtime = new HttpRuntime();
var appDomainAppVPathField = typeof(HttpRuntime).GetField("_appDomainAppVPath", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
appDomainAppVPathField.SetValue(runtime, CreateVirtualPath(appVPath));
GetTheRuntime().SetValue(null, runtime);
var appDomainIdField = typeof(HttpRuntime).GetField("_appDomainId", BindingFlags.NonPublic | BindingFlags.Instance);
appDomainIdField.SetValue(runtime, "test");
}
internal static FieldInfo GetTheRuntime()
{
return typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static);
}
internal static void RestoreHttpRuntime()
{
GetTheRuntime().SetValue(null, null);
}
// E.g. "default.aspx", "http://localhost/WebSite1/subfolder1/default.aspx"
internal static void CreateHttpContext(string filename, string url)
{
var request = new HttpRequest(filename, url, null);
var httpContext = new HttpContext(request, new HttpResponse(new StringWriter(new StringBuilder())));
HttpContext.Current = httpContext;
}
internal static void RestoreHttpContext()
{
HttpContext.Current = null;
}
internal static object CreateVirtualPath(string path)
{
var vPath = typeof(Page).Assembly.GetType("System.Web.VirtualPath");
var method = vPath.GetMethod("CreateNonRelativeTrailingSlash", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
return method.Invoke(null, new object[] { path });
}
}
}