1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Web.WebPages.TestUtils;
using Xunit;
namespace System.Web.WebPages.Test
{
public class UrlUtilTest
{
[Fact]
public void UrlTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/WebSite1/subfolder1/default.aspx"),
__ = Utils.CreateHttpRuntime("/WebSite1/"))
{
var vpath = "~/subfolder1/default.aspx";
var href = "~/world/test.aspx";
var expected = "/WebSite1/world/test.aspx";
Assert.Equal(expected, UrlUtil.Url(vpath, href));
Assert.Equal(expected, new MockPage() { VirtualPath = vpath }.Href(href));
}
});
}
[Fact]
public void UrlTest2()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/WebSite1/default.aspx"),
__ = Utils.CreateHttpRuntime("/WebSite1/"))
{
var vpath = "~/default.aspx";
var href = "~/world/test.aspx";
var expected = "/WebSite1/world/test.aspx";
Assert.Equal(expected, UrlUtil.Url(vpath, href));
Assert.Equal(expected, new MockPage() { VirtualPath = vpath }.Href(href));
}
});
}
[Fact]
public void UrlTest3()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/WebSite1/subfolder1/default.aspx"),
__ = Utils.CreateHttpRuntime("/WebSite1/"))
{
var vpath = "~/subfolder1/default.aspx";
var href = "world/test.aspx";
var expected = "/WebSite1/subfolder1/world/test.aspx";
Assert.Equal(expected, UrlUtil.Url(vpath, href));
Assert.Equal(expected, new MockPage() { VirtualPath = vpath }.Href(href));
}
});
}
[Fact]
public void UrlTest4()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/WebSite1/subfolder1/default.aspx"),
__ = Utils.CreateHttpRuntime("/WebSite1/"))
{
var vpath = "~/subfolder2/default.aspx";
var href = "world/test.aspx";
var expected = "/WebSite1/subfolder2/world/test.aspx";
Assert.Equal(expected, UrlUtil.Url(vpath, href));
Assert.Equal(expected, new MockPage() { VirtualPath = vpath }.Href(href));
}
});
}
[Fact]
public void BuildUrlEncodesPagePart()
{
// Arrange
var page = "This is a really bad name for a page";
var expected = "This%20is%20a%20really%20bad%20name%20for%20a%20page";
// Act
var actual = UrlUtil.BuildUrl(page);
// Assert
Assert.Equal(actual, expected);
}
[Fact]
public void BuildUrlAppendsNonAnonymousTypesToPathPortion()
{
// Arrange
object[] pathParts = new object[] { "part", Decimal.One, 1.25f };
var page = "home";
// Act
var actual = UrlUtil.BuildUrl(page, pathParts);
// Assert
Assert.Equal(actual, page + "/part/1/1.25");
}
[Fact]
public void BuildUrlEncodesAppendedPathPortion()
{
// Arrange
object[] pathParts = new object[] { "path portion", "ζ" };
var page = "home";
// Act
var actual = UrlUtil.BuildUrl(page, pathParts);
// Assert
Assert.Equal(actual, page + "/path%20portion/%ce%b6");
}
[Fact]
public void BuildUrlAppendsAnonymousObjectsToQueryString()
{
// Arrange
var page = "home";
var queryString = new { sort = "FName", dir = "desc" };
// Act
var actual = UrlUtil.BuildUrl(page, queryString);
// Assert
Assert.Equal(actual, page + "?sort=FName&dir=desc");
}
[Fact]
public void BuildUrlAppendsMultipleAnonymousObjectsToQueryString()
{
// Arrange
var page = "home";
var queryString1 = new { sort = "FName", dir = "desc" };
var queryString2 = new { view = "Activities", page = 7 };
// Act
var actual = UrlUtil.BuildUrl(page, queryString1, queryString2);
// Assert
Assert.Equal(actual, page + "?sort=FName&dir=desc&view=Activities&page=7");
}
[Fact]
public void BuildUrlEncodesQueryStringKeysAndValues()
{
// Arrange
var page = "home";
var queryString = new { ζ = "my=value&", mykey = "<π" };
// Act
var actual = UrlUtil.BuildUrl(page, queryString);
// Assert
Assert.Equal(actual, page + "?%ce%b6=my%3dvalue%26&mykey=%3c%cf%80");
}
[Fact]
public void BuildUrlGeneratesPathPartsAndQueryString()
{
// Arrange
var page = "home";
// Act
var actual = UrlUtil.BuildUrl(page, "products", new { cat = 37 }, "furniture", new { sort = "name", dir = "desc" });
// Assert
Assert.Equal(actual, page + "/products/furniture?cat=37&sort=name&dir=desc");
}
[Fact]
public void UrlAppRootTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/"),
__ = Utils.CreateHttpRuntime("/"))
{
var vpath = "~/";
var href = "~/world/test.aspx";
var expected = "/world/test.aspx";
Assert.Equal(expected, UrlUtil.Url(vpath, href));
Assert.Equal(expected, new MockPage() { VirtualPath = vpath }.Href(href));
}
});
}
[Fact]
public void UrlAnonymousObjectTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
using (IDisposable _ = Utils.CreateHttpContext("default.aspx", "http://localhost/"),
__ = Utils.CreateHttpRuntime("/"))
{
Assert.Equal("/world/test.cshtml?Prop1=value1",
UrlUtil.Url("~/world/page.cshtml", "test.cshtml", new { Prop1 = "value1" }));
Assert.Equal("/world/test.cshtml?Prop1=value1&Prop2=value2",
UrlUtil.Url("~/world/page.cshtml", "test.cshtml", new { Prop1 = "value1", Prop2 = "value2" }));
}
});
}
}
}
|