forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageUtilsTest.cs
More file actions
156 lines (131 loc) · 5.25 KB
/
Copy pathPageUtilsTest.cs
File metadata and controls
156 lines (131 loc) · 5.25 KB
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
// 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.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.WebPages.Administration.PackageManager;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.WebPages.Administration.Test
{
public class PageUtilsTest
{
[Fact]
public void GetFilterValueReturnsNullIfValueWasNotFound()
{
// Arrange
var request = new Mock<HttpRequestBase>();
request.Setup(c => c.QueryString).Returns(new NameValueCollection());
request.Setup(c => c.Cookies).Returns(new HttpCookieCollection());
// Act
var value = PageUtils.GetFilterValue(request.Object, "foo", "my-key");
// Assert
Assert.Null(value);
}
[Fact]
public void GetFilterValueReturnsValueFromCookieIfQueryStringDoesNotContainKey()
{
// Arrange
const string key = "my-key";
const string value = "my-cookie-value";
var request = new Mock<HttpRequestBase>();
request.Setup(c => c.QueryString).Returns(new NameValueCollection());
var cookies = new HttpCookieCollection();
var cookie = new HttpCookie("foo");
cookie[key] = value;
cookies.Add(cookie);
request.Setup(c => c.Cookies).Returns(cookies);
// Act
var returnedValue = PageUtils.GetFilterValue(request.Object, "foo", key);
// Assert
Assert.Equal(value, returnedValue);
}
[Fact]
public void GetFilterValueReturnsValueFromQueryString()
{
// Arrange
const string key = "my-key";
const string requestValue = "my-request-value";
const string cookieValue = "my-cookie-value";
var request = new Mock<HttpRequestBase>();
var queryString = new NameValueCollection();
queryString[key] = requestValue;
request.Setup(c => c.QueryString).Returns(queryString);
var cookies = new HttpCookieCollection();
var cookie = new HttpCookie("foo");
cookie[key] = cookieValue;
request.Setup(c => c.Cookies).Returns(cookies);
// Act
var returnedValue = PageUtils.GetFilterValue(request.Object, "foo", key);
// Assert
Assert.Equal(requestValue, returnedValue);
}
[Fact]
public void PersistFilterCreatesCookieIfItDoesNotExist()
{
// Arrange
var cookies = new HttpCookieCollection();
var response = new Mock<HttpResponseBase>();
response.Setup(c => c.Cookies).Returns(cookies);
// Act
PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>());
// Assert
Assert.NotNull(cookies["my-cookie"]);
}
[Fact]
public void PersistFilterUsesExistingCookie()
{
// Arrange
var cookieName = "my-cookie";
var cookies = new HttpCookieCollection();
cookies.Add(new HttpCookie(cookieName));
var response = new Mock<HttpResponseBase>();
response.Setup(c => c.Cookies).Returns(cookies);
// Act
PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>());
// Assert
Assert.Single(cookies);
}
[Fact]
public void PersistFilterAddsDictionaryEntriesToCookie()
{
// Arrange
var cookies = new HttpCookieCollection();
var response = new Mock<HttpResponseBase>();
response.Setup(c => c.Cookies).Returns(cookies);
// Act
PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>() { { "a", "b" }, { "x", "y" } });
// Assert
var cookie = cookies["my-cookie"];
Assert.Equal("b", cookie["a"]);
Assert.Equal("y", cookie["x"]);
}
[Fact]
public void IsValidLicenseUrlReturnsTrueForHttpUris()
{
// Arrange
var uri = new Uri("http://www.microsoft.com");
// Act and Assert
Assert.True(PageUtils.IsValidLicenseUrl(uri));
}
[Fact]
public void IsValidLicenseUrlReturnsTrueForHttpsUris()
{
// Arrange
var uri = new Uri("HTTPs://www.asp.net");
// Act and Assert
Assert.True(PageUtils.IsValidLicenseUrl(uri));
}
[Fact]
public void IsValidLicenseUrlReturnsFalseForNonHttpUris()
{
// Arrange
var jsUri = new Uri("javascript:alert('Hello world');");
var fileShareUri = new Uri(@"c:\windows\system32\notepad.exe");
var mailToUti = new Uri("mailto:invalid-email@microsoft.com");
// Act and Assert
Assert.False(PageUtils.IsValidLicenseUrl(jsUri));
Assert.False(PageUtils.IsValidLicenseUrl(fileShareUri));
Assert.False(PageUtils.IsValidLicenseUrl(mailToUti));
}
}
}