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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.WebPages.TestUtils;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class ApplicationStartPageTest
{
[Fact]
public void StartPageBasicTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var page = new ApplicationStartPageTest().CreateStartPage(p =>
{
p.AppState["x"] = "y";
p.WriteLiteral("test");
});
page.ExecuteInternal();
Assert.Equal("y", page.ApplicationState["x"]);
Assert.Equal("test", ApplicationStartPage.Markup.ToHtmlString());
});
}
[Fact]
public void StartPageDynamicAppStateBasicTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var page = new ApplicationStartPageTest().CreateStartPage(p =>
{
p.App.x = "y";
p.WriteLiteral("test");
});
page.ExecuteInternal();
Assert.Equal("y", page.ApplicationState["x"]);
Assert.Equal("y", page.App["x"]);
Assert.Equal("y", page.App.x);
Assert.Equal("test", ApplicationStartPage.Markup.ToHtmlString());
});
}
[Fact]
public void ExceptionTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var msg = "This is an error message";
var e = new InvalidOperationException(msg);
var page = new ApplicationStartPageTest().CreateStartPage(p => { throw e; });
var ex = Assert.Throws<HttpException>(() => page.ExecuteStartPage());
Assert.Equal(msg, ex.InnerException.Message);
Assert.Equal(e, ApplicationStartPage.Exception);
});
}
[Fact]
public void HtmlEncodeTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
// Set HideRequestResponse to true to simulate the condition in IIS 7/7.5
var context = new HttpContext(new HttpRequest("default.cshtml", "http://localhost/default.cshtml", null), new HttpResponse(new StringWriter(new StringBuilder())));
var hideRequestResponse = typeof(HttpContext).GetField("HideRequestResponse", BindingFlags.NonPublic | BindingFlags.Instance);
hideRequestResponse.SetValue(context, true);
HttpContext.Current = context;
var page = new ApplicationStartPageTest().CreateStartPage(p => { p.Write("test"); });
page.ExecuteStartPage();
});
}
[Fact]
public void GetVirtualPathTest()
{
var page = new MockStartPage();
Assert.Equal(ApplicationStartPage.StartPageVirtualPath, page.VirtualPath);
}
[Fact]
public void SetVirtualPathTest()
{
var page = new MockStartPage();
Assert.Throws<NotSupportedException>(() => { page.VirtualPath = "~/hello.cshtml"; });
}
[Fact]
public void ExecuteStartPageTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var startPage = new MockStartPage() { ExecuteAction = p => p.AppState["x"] = "y" };
var objectFactory = GetMockVirtualPathFactory(startPage);
ApplicationStartPage.ExecuteStartPage(new WebPageHttpApplication(),
p => { },
objectFactory,
new string[] { "cshtml", "vbhtml" });
Assert.Equal("y", startPage.ApplicationState["x"]);
});
}
[Fact]
public void ExecuteStartPageDynamicAppStateTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var startPage = new MockStartPage() { ExecuteAction = p => p.App.x = "y" };
var objectFactory = GetMockVirtualPathFactory(startPage);
ApplicationStartPage.ExecuteStartPage(new WebPageHttpApplication(),
p => { },
objectFactory,
new string[] { "cshtml", "vbhtml" });
Assert.Equal("y", startPage.ApplicationState["x"]);
Assert.Equal("y", startPage.App.x);
Assert.Equal("y", startPage.App["x"]);
});
}
public class MockStartPage : ApplicationStartPage
{
public Action<ApplicationStartPage> ExecuteAction { get; set; }
public HttpApplicationStateBase ApplicationState = new HttpApplicationStateWrapper(Activator.CreateInstance(typeof(HttpApplicationState), true) as HttpApplicationState);
public override void Execute()
{
ExecuteAction(this);
}
public override HttpApplicationStateBase AppState
{
get { return ApplicationState; }
}
public void ExecuteStartPage()
{
ExecuteStartPage(new WebPageHttpApplication(),
p => { },
GetMockVirtualPathFactory(this),
new string[] { "cshtml", "vbhtml" });
}
}
public MockStartPage CreateStartPage(Action<ApplicationStartPage> action)
{
var startPage = new MockStartPage() { ExecuteAction = action };
return startPage;
}
public sealed class WebPageHttpApplication : HttpApplication
{
}
private static IVirtualPathFactory GetMockVirtualPathFactory(ApplicationStartPage page)
{
var mockFactory = new Mock<IVirtualPathFactory>();
mockFactory.Setup(c => c.Exists(It.IsAny<string>())).Returns<string>(_ => true);
mockFactory.Setup(c => c.CreateInstance(It.IsAny<string>())).Returns<string>(_ => page);
return mockFactory.Object;
}
}
}
|