Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Web;
using System.Web.WebPages;
using Moq;
using Xunit;
namespace Microsoft.WebPages.Test.Helpers
{
public class HttpContextExtensionsTest
{
class RedirectData
{
public string RequestUrl { get; set; }
public string RedirectUrl { get; set; }
}
private static HttpContextBase GetContextForRedirectLocal(RedirectData data)
{
Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
contextMock.Setup(context => context.Request.Url).Returns(new Uri(data.RequestUrl));
contextMock.Setup(context => context.Response.Redirect(It.IsAny<string>())).Callback((string url) => data.RedirectUrl = url);
return contextMock.Object;
}
[Fact]
public void RedirectLocalWithNullGoesToRootTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("");
Assert.Equal("~/", data.RedirectUrl);
}
[Fact]
public void RedirectLocalWithEmptyStringGoesToRootTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("");
Assert.Equal("~/", data.RedirectUrl);
}
[Fact]
public void RedirectLocalWithNonLocalGoesToRootTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("");
Assert.Equal("~/", data.RedirectUrl);
}
[Fact]
public void RedirectLocalWithDifferentHostGoesToRootTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("http://bar");
Assert.Equal("~/", data.RedirectUrl);
}
[Fact]
public void RedirectLocalOnSameHostTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("http://foo/bar/baz");
Assert.Equal("~/", data.RedirectUrl);
context.RedirectLocal("http://foo/bar/baz/woot.htm");
Assert.Equal("~/", data.RedirectUrl);
}
[Fact]
public void RedirectLocalRelativeTest()
{
RedirectData data = new RedirectData() { RequestUrl = "http://foo" };
var context = GetContextForRedirectLocal(data);
context.RedirectLocal("/bar");
Assert.Equal("/bar", data.RedirectUrl);
context.RedirectLocal("/bar/hey.you");
Assert.Equal("/bar/hey.you", data.RedirectUrl);
}
}
}

View File

@@ -0,0 +1,132 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Web;
using System.Web.WebPages;
using Moq;
using Xunit;
namespace Microsoft.WebPages.Test.Helpers
{
public class HttpRequestExtensionsTest
{
private static HttpRequestBase GetRequestForIsUrlLocalToHost(string url)
{
Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
contextMock.Setup(context => context.Request.Url).Returns(new Uri(url));
return contextMock.Object.Request;
}
[Fact]
public void IsUrlLocalToHost_ReturnsFalseOnEmpty()
{
var request = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(request.IsUrlLocalToHost(null));
Assert.False(request.IsUrlLocalToHost(String.Empty));
}
[Fact]
public void IsUrlLocalToHost_AcceptsRootedUrls()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.True(helper.IsUrlLocalToHost("/foo.html"));
Assert.True(helper.IsUrlLocalToHost("/www.hackerz.com"));
Assert.True(helper.IsUrlLocalToHost("/"));
}
[Fact]
public void IsUrlLocalToHost_AcceptsApplicationRelativeUrls()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.True(helper.IsUrlLocalToHost("~/"));
Assert.True(helper.IsUrlLocalToHost("~/foobar.html"));
}
[Fact]
public void IsUrlLocalToHost_RejectsRelativeUrls()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("foobar.html"));
Assert.False(helper.IsUrlLocalToHost("../foobar.html"));
Assert.False(helper.IsUrlLocalToHost("fold/foobar.html"));
}
[Fact]
public void IsUrlLocalToHost_RejectValidButUnsafeRelativeUrls()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("http:/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("hTtP:foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http:/www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("HtTpS:/www.hackerz.com"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsOnTheSameHost()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("http://www.mysite.com/appDir/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http://WWW.MYSITE.COM"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsOnLocalHost()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("http://localhost/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http://127.0.0.1/foobar.html"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsOnTheSameHostButDifferentScheme()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("https://www.mysite.com/"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsOnDifferentHost()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("http://www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("https://www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("hTtP://www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("HtTpS://www.hackerz.com"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsWithTooManySchemeSeparatorCharacters()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("http://///www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("https://///www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("HtTpS://///www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http:///www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http:////www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("http://///www.hackerz.com/foobar.html"));
}
[Fact]
public void IsUrlLocalToHost_RejectsUrlsWithMissingSchemeName()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost("//www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("//www.hackerz.com?"));
Assert.False(helper.IsUrlLocalToHost("//www.hackerz.com:80"));
Assert.False(helper.IsUrlLocalToHost("//www.hackerz.com/foobar.html"));
Assert.False(helper.IsUrlLocalToHost("///www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost("//////www.hackerz.com"));
}
[Fact]
public void IsUrlLocalToHost_RejectsInvalidUrls()
{
var helper = GetRequestForIsUrlLocalToHost("http://www.mysite.com/");
Assert.False(helper.IsUrlLocalToHost(@"http:\\www.hackerz.com"));
Assert.False(helper.IsUrlLocalToHost(@"http:\\www.hackerz.com\"));
Assert.False(helper.IsUrlLocalToHost(@"/\"));
Assert.False(helper.IsUrlLocalToHost(@"/\foo"));
}
}
}

View File

@@ -0,0 +1,130 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.WebPages;
using Moq;
using Xunit;
namespace Microsoft.WebPages.Test.Helpers
{
public class HttpResponseExtensionsTest
{
HttpResponseBase _response;
string _redirectUrl;
StringBuilder _output;
Stream _outputStream;
public HttpResponseExtensionsTest()
{
_output = new StringBuilder();
_outputStream = new MemoryStream();
Mock<HttpResponseBase> mockResponse = new Mock<HttpResponseBase>();
mockResponse.SetupProperty(response => response.StatusCode);
mockResponse.SetupProperty(response => response.ContentType);
mockResponse.Setup(response => response.Redirect(It.IsAny<string>())).Callback((string url) => _redirectUrl = url);
mockResponse.Setup(response => response.Write(It.IsAny<string>())).Callback((string str) => _output.Append(str));
mockResponse.Setup(response => response.OutputStream).Returns(_outputStream);
mockResponse.Setup(response => response.OutputStream).Returns(_outputStream);
mockResponse.Setup(response => response.Output).Returns(new StringWriter(_output));
_response = mockResponse.Object;
}
[Fact]
public void SetStatusWithIntTest()
{
int status = 200;
_response.SetStatus(status);
Assert.Equal(status, _response.StatusCode);
}
[Fact]
public void SetStatusWithHttpStatusCodeTest()
{
HttpStatusCode status = HttpStatusCode.Forbidden;
_response.SetStatus(status);
Assert.Equal((int)status, _response.StatusCode);
}
[Fact]
public void WriteBinaryTest()
{
string foo = "I am a string, please don't mangle me!";
_response.WriteBinary(ASCIIEncoding.ASCII.GetBytes(foo));
_outputStream.Flush();
_outputStream.Position = 0;
StreamReader reader = new StreamReader(_outputStream);
Assert.Equal(foo, reader.ReadToEnd());
}
[Fact]
public void WriteBinaryWithMimeTypeTest()
{
string foo = "I am a string, please don't mangle me!";
string mimeType = "mime/foo";
_response.WriteBinary(ASCIIEncoding.ASCII.GetBytes(foo), mimeType);
_outputStream.Flush();
_outputStream.Position = 0;
StreamReader reader = new StreamReader(_outputStream);
Assert.Equal(foo, reader.ReadToEnd());
Assert.Equal(mimeType, _response.ContentType);
}
[Fact]
public void OutputCacheSetsExpirationTimeBasedOnCurrentContext()
{
// Arrange
var timestamp = new DateTime(2011, 1, 1, 0, 0, 0);
var context = new Mock<HttpContextBase>();
context.SetupGet(c => c.Timestamp).Returns(timestamp);
var response = new Mock<HttpResponseBase>().Object;
var cache = new Mock<HttpCachePolicyBase>();
cache.Setup(c => c.SetCacheability(It.Is<HttpCacheability>(p => p == HttpCacheability.Public))).Verifiable();
cache.Setup(c => c.SetExpires(It.Is<DateTime>(p => p == timestamp.AddSeconds(20)))).Verifiable();
cache.Setup(c => c.SetMaxAge(It.Is<TimeSpan>(p => p == TimeSpan.FromSeconds(20)))).Verifiable();
cache.Setup(c => c.SetValidUntilExpires(It.Is<bool>(p => p == true))).Verifiable();
cache.Setup(c => c.SetLastModified(It.Is<DateTime>(p => p == timestamp))).Verifiable();
cache.Setup(c => c.SetSlidingExpiration(It.Is<bool>(p => p == false))).Verifiable();
// Act
ResponseExtensions.OutputCache(context.Object, cache.Object, 20, false, null, null, null, HttpCacheability.Public);
// Assert
cache.VerifyAll();
}
[Fact]
public void OutputCacheSetsVaryByValues()
{
// Arrange
var timestamp = new DateTime(2011, 1, 1, 0, 0, 0);
var context = new Mock<HttpContextBase>();
context.SetupGet(c => c.Timestamp).Returns(timestamp);
var response = new Mock<HttpResponseBase>().Object;
var varyByParams = new HttpCacheVaryByParams();
var varyByHeader = new HttpCacheVaryByHeaders();
var varyByContentEncoding = new HttpCacheVaryByContentEncodings();
var cache = new Mock<HttpCachePolicyBase>();
cache.SetupGet(c => c.VaryByParams).Returns(varyByParams);
cache.SetupGet(c => c.VaryByHeaders).Returns(varyByHeader);
cache.SetupGet(c => c.VaryByContentEncodings).Returns(varyByContentEncoding);
// Act
ResponseExtensions.OutputCache(context.Object, cache.Object, 20, false, new[] { "foo" }, new[] { "bar", "bar2" },
new[] { "baz", "baz2" }, HttpCacheability.Public);
// Assert
Assert.Equal(varyByParams["foo"], true);
Assert.Equal(varyByHeader["bar"], true);
Assert.Equal(varyByHeader["bar2"], true);
Assert.Equal(varyByContentEncoding["baz"], true);
Assert.Equal(varyByContentEncoding["baz2"], true);
}
}
}

View File

@@ -0,0 +1,253 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Web.TestUtil;
using Xunit;
using Xunit.Extensions;
namespace System.Web.WebPages.Test
{
public class StringExtensionsTest
{
[Fact]
public void IsIntTests()
{
Assert.False("1.3".IsInt());
Assert.False(".13".IsInt());
Assert.False("0.0".IsInt());
Assert.False("12345678900123456".IsInt());
Assert.False("gooblygook".IsInt());
Assert.True("0".IsInt());
Assert.True("123456".IsInt());
Assert.True(Int32.MaxValue.ToString().IsInt());
Assert.True(Int32.MinValue.ToString().IsInt());
Assert.False(((string)null).IsInt());
}
[Fact]
public void AsIntBasicTests()
{
Assert.Equal(-123, "-123".AsInt());
Assert.Equal(12345, "12345".AsInt());
Assert.Equal(0, "0".AsInt());
}
[Fact]
public void AsIntDefaultTests()
{
// Illegal values default to 0
Assert.Equal(0, "-100000000000000000000000".AsInt());
// Illegal values default to 0
Assert.Equal(0, "adlfkj".AsInt());
Assert.Equal(-1, "adlfkj".AsInt(-1));
Assert.Equal(-1, "-100000000000000000000000".AsInt(-1));
}
[Fact]
public void IsDecimalTests()
{
Assert.True("1.3".IsDecimal());
Assert.True(".13".IsDecimal());
Assert.True("0.0".IsDecimal());
Assert.True("12345678900123456".IsDecimal());
Assert.True("0".IsDecimal());
Assert.True("123456".IsDecimal());
Assert.True(decimal.MaxValue.ToString().IsDecimal());
Assert.True(decimal.MinValue.ToString().IsDecimal());
Assert.False("gooblygook".IsDecimal());
Assert.False("..0".IsDecimal());
Assert.False(((string)null).IsDecimal());
}
[Fact]
public void AsDecimalBasicTests()
{
Assert.Equal(-123m, "-123".AsDecimal());
Assert.Equal(9.99m, "9.99".AsDecimal());
Assert.Equal(0m, "0".AsDecimal());
Assert.Equal(-1.1111m, "-1.1111".AsDecimal());
}
[Fact]
public void AsDecimalDefaultTests()
{
// Illegal values default to 0
Assert.Equal(0m, "abc".AsDecimal());
Assert.Equal(-1.11m, "adlfkj".AsDecimal(-1.11m));
}
[Fact]
public void AsDecimalUsesCurrentCulture()
{
decimal value = 12345.00M;
using (new CultureReplacer("ar-DZ"))
{
Assert.Equal(value.ToString(CultureInfo.CurrentCulture), "12345.00");
Assert.Equal(value.ToString(), "12345.00");
}
using (new CultureReplacer("bg-BG"))
{
Assert.Equal(value.ToString(CultureInfo.CurrentCulture), "12345,00");
Assert.Equal(value.ToString(), "12345,00");
}
}
[Fact]
public void IsAndAsDecimalsUsesCurrentCulture()
{
// Pretty identical to the earlier test case. This was a post on the forums, making sure it works.
using (new CultureReplacer(culture: "lt-LT"))
{
Assert.False("1.2".IsDecimal());
Assert.True("1,2".IsDecimal());
Assert.Equal(1.2M, "1,2".AsDecimal());
Assert.Equal(0, "1.2".AsDecimal());
}
}
[Fact]
public void IsFloatTests()
{
Assert.True("1.3".IsFloat());
Assert.True(".13".IsFloat());
Assert.True("0.0".IsFloat());
Assert.True("12345678900123456".IsFloat());
Assert.True("0".IsFloat());
Assert.True("123456".IsFloat());
Assert.True(float.MaxValue.ToString().IsFloat());
Assert.True(float.MinValue.ToString().IsFloat());
Assert.True(float.NegativeInfinity.ToString().IsFloat());
Assert.True(float.PositiveInfinity.ToString().IsFloat());
Assert.False("gooblygook".IsFloat());
Assert.False(((string)null).IsFloat());
}
[Fact]
public void AsFloatBasicTests()
{
Assert.Equal(-123f, "-123".AsFloat());
Assert.Equal(9.99f, "9.99".AsFloat());
Assert.Equal(0f, "0".AsFloat());
Assert.Equal(-1.1111f, "-1.1111".AsFloat());
}
[Fact]
public void AsFloatDefaultTests()
{
// Illegal values default to 0
Assert.Equal(0f, "abc".AsFloat());
Assert.Equal(-1.11f, "adlfkj".AsFloat(-1.11f));
}
[Fact]
public void IsDateTimeTests()
{
using (new CultureReplacer())
{
Assert.True("Sat, 01 Nov 2008 19:35:00 GMT".IsDateTime());
Assert.True("1/5/1979".IsDateTime());
Assert.False("0".IsDateTime());
Assert.True(DateTime.MaxValue.ToString().IsDateTime());
Assert.True(DateTime.MinValue.ToString().IsDateTime());
Assert.True(new DateTime(2010, 12, 21).ToUniversalTime().ToString().IsDateTime());
Assert.False("gooblygook".IsDateTime());
Assert.False(((string)null).IsDateTime());
}
}
/// <remarks>Tests for bug 153439</remarks>
[Fact]
public void IsDateTimeUsesLocalCulture()
{
using (new CultureReplacer(culture: "en-gb"))
{
Assert.True(new DateTime(2010, 12, 21).ToString().IsDateTime());
Assert.True(new DateTime(2010, 12, 11).ToString().IsDateTime());
Assert.True("2010/01/01".IsDateTime());
Assert.True("12/01/2010".IsDateTime());
Assert.True("12/12/2010".IsDateTime());
Assert.True("13/12/2010".IsDateTime());
Assert.True("2010-12-01".IsDateTime());
Assert.True("2010-12-13".IsDateTime());
Assert.False("12/13/2010".IsDateTime());
Assert.False("13/13/2010".IsDateTime());
Assert.False("2010-13-12".IsDateTime());
}
}
[Fact]
public void AsDateTimeBasicTests()
{
using (new CultureReplacer())
{
Assert.Equal(DateTime.Parse("1/14/1979"), "1/14/1979".AsDateTime());
Assert.Equal(DateTime.Parse("Sat, 01 Nov 2008 19:35:00 GMT"), "Sat, 01 Nov 2008 19:35:00 GMT".AsDateTime());
}
}
[Theory]
[InlineData(new object[] { "en-us" })]
[InlineData(new object[] { "en-gb" })]
[InlineData(new object[] { "ug" })]
[InlineData(new object[] { "lt-LT" })]
public void AsDateTimeDefaultTests(string culture)
{
using (new CultureReplacer(culture))
{
// Illegal values default to MinTime
Assert.Equal(DateTime.MinValue, "1".AsDateTime());
DateTime defaultV = new DateTime(1979, 01, 05);
Assert.Equal(defaultV, "adlfkj".AsDateTime(defaultV));
Assert.Equal(defaultV, "Jn 69".AsDateTime(defaultV));
}
}
[Theory]
[InlineData(new object[] { "en-us" })]
[InlineData(new object[] { "en-gb" })]
[InlineData(new object[] { "lt-LT" })]
public void IsDateTimeDefaultTests(string culture)
{
using (new CultureReplacer(culture))
{
var dateTime = new DateTime(2011, 10, 25, 10, 10, 00);
Assert.True(dateTime.ToShortDateString().IsDateTime());
Assert.True(dateTime.ToString().IsDateTime());
Assert.True(dateTime.ToLongDateString().IsDateTime());
}
}
[Fact]
public void IsBoolTests()
{
Assert.True("TRUE".IsBool());
Assert.True("TRUE ".IsBool());
Assert.True("false".IsBool());
Assert.False("falsey".IsBool());
Assert.False("gooblygook".IsBool());
Assert.False("".IsBool());
Assert.False(((string)null).IsBool());
}
[Fact]
public void AsBoolTests()
{
Assert.True("TRuE".AsBool());
Assert.False("False".AsBool());
Assert.False("Die".AsBool(false));
Assert.True("true!".AsBool(true));
Assert.False("".AsBool());
Assert.False(((string)null).AsBool());
Assert.True("".AsBool(true));
Assert.True(((string)null).AsBool(true));
}
}
}