/wine-mono-0.0.4/mono/external/aspnetwebstack/test/System.Web.WebPages.Administration.Test/PageUtilsTest.cs

# · C# · 155 lines · 105 code · 25 blank · 25 comment · 0 complexity · 6bf6158ac65760d422a21cfba55a0d7b MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Web.WebPages.Administration.PackageManager;
  5. using Moq;
  6. using Xunit;
  7. namespace System.Web.WebPages.Administration.Test
  8. {
  9. public class PageUtilsTest
  10. {
  11. [Fact]
  12. public void GetFilterValueReturnsNullIfValueWasNotFound()
  13. {
  14. // Arrange
  15. var request = new Mock<HttpRequestBase>();
  16. request.Setup(c => c.QueryString).Returns(new NameValueCollection());
  17. request.Setup(c => c.Cookies).Returns(new HttpCookieCollection());
  18. // Act
  19. var value = PageUtils.GetFilterValue(request.Object, "foo", "my-key");
  20. // Assert
  21. Assert.Null(value);
  22. }
  23. [Fact]
  24. public void GetFilterValueReturnsValueFromCookieIfQueryStringDoesNotContainKey()
  25. {
  26. // Arrange
  27. const string key = "my-key";
  28. const string value = "my-cookie-value";
  29. var request = new Mock<HttpRequestBase>();
  30. request.Setup(c => c.QueryString).Returns(new NameValueCollection());
  31. var cookies = new HttpCookieCollection();
  32. var cookie = new HttpCookie("foo");
  33. cookie[key] = value;
  34. cookies.Add(cookie);
  35. request.Setup(c => c.Cookies).Returns(cookies);
  36. // Act
  37. var returnedValue = PageUtils.GetFilterValue(request.Object, "foo", key);
  38. // Assert
  39. Assert.Equal(value, returnedValue);
  40. }
  41. [Fact]
  42. public void GetFilterValueReturnsValueFromQueryString()
  43. {
  44. // Arrange
  45. const string key = "my-key";
  46. const string requestValue = "my-request-value";
  47. const string cookieValue = "my-cookie-value";
  48. var request = new Mock<HttpRequestBase>();
  49. var queryString = new NameValueCollection();
  50. queryString[key] = requestValue;
  51. request.Setup(c => c.QueryString).Returns(queryString);
  52. var cookies = new HttpCookieCollection();
  53. var cookie = new HttpCookie("foo");
  54. cookie[key] = cookieValue;
  55. request.Setup(c => c.Cookies).Returns(cookies);
  56. // Act
  57. var returnedValue = PageUtils.GetFilterValue(request.Object, "foo", key);
  58. // Assert
  59. Assert.Equal(requestValue, returnedValue);
  60. }
  61. [Fact]
  62. public void PersistFilterCreatesCookieIfItDoesNotExist()
  63. {
  64. // Arrange
  65. var cookies = new HttpCookieCollection();
  66. var response = new Mock<HttpResponseBase>();
  67. response.Setup(c => c.Cookies).Returns(cookies);
  68. // Act
  69. PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>());
  70. // Assert
  71. Assert.NotNull(cookies["my-cookie"]);
  72. }
  73. [Fact]
  74. public void PersistFilterUsesExistingCookie()
  75. {
  76. // Arrange
  77. var cookieName = "my-cookie";
  78. var cookies = new HttpCookieCollection();
  79. cookies.Add(new HttpCookie(cookieName));
  80. var response = new Mock<HttpResponseBase>();
  81. response.Setup(c => c.Cookies).Returns(cookies);
  82. // Act
  83. PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>());
  84. // Assert
  85. Assert.Equal(1, cookies.Count);
  86. }
  87. [Fact]
  88. public void PersistFilterAddsDictionaryEntriesToCookie()
  89. {
  90. // Arrange
  91. var cookies = new HttpCookieCollection();
  92. var response = new Mock<HttpResponseBase>();
  93. response.Setup(c => c.Cookies).Returns(cookies);
  94. // Act
  95. PageUtils.PersistFilter(response.Object, "my-cookie", new Dictionary<string, string>() { { "a", "b" }, { "x", "y" } });
  96. // Assert
  97. var cookie = cookies["my-cookie"];
  98. Assert.Equal(cookie["a"], "b");
  99. Assert.Equal(cookie["x"], "y");
  100. }
  101. [Fact]
  102. public void IsValidLicenseUrlReturnsTrueForHttpUris()
  103. {
  104. // Arrange
  105. var uri = new Uri("http://www.microsoft.com");
  106. // Act and Assert
  107. Assert.True(PageUtils.IsValidLicenseUrl(uri));
  108. }
  109. [Fact]
  110. public void IsValidLicenseUrlReturnsTrueForHttpsUris()
  111. {
  112. // Arrange
  113. var uri = new Uri("HTTPs://www.asp.net");
  114. // Act and Assert
  115. Assert.True(PageUtils.IsValidLicenseUrl(uri));
  116. }
  117. [Fact]
  118. public void IsValidLicenseUrlReturnsFalseForNonHttpUris()
  119. {
  120. // Arrange
  121. var jsUri = new Uri("javascript:alert('Hello world');");
  122. var fileShareUri = new Uri(@"c:\windows\system32\notepad.exe");
  123. var mailToUti = new Uri("mailto:invalid-email@microsoft.com");
  124. // Act and Assert
  125. Assert.False(PageUtils.IsValidLicenseUrl(jsUri));
  126. Assert.False(PageUtils.IsValidLicenseUrl(fileShareUri));
  127. Assert.False(PageUtils.IsValidLicenseUrl(mailToUti));
  128. }
  129. }
  130. }