PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/System.Web.WebPages.Test/WebPage/StartPageTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 568 lines | 391 code | 60 blank | 117 comment | 0 complexity | 53009999b70c9ae3cab23438ec8e94a8 MD5 | raw file
  1. using System.Reflection;
  2. using System.Web.Caching;
  3. using System.Web.Compilation;
  4. using System.Web.Hosting;
  5. using System.Web.Profile;
  6. using System.Web.WebPages.TestUtils;
  7. using Moq;
  8. using Xunit;
  9. using Assert = Microsoft.TestCommon.AssertEx;
  10. namespace System.Web.WebPages.Test
  11. {
  12. public class StartPageTest
  13. {
  14. // The page ~/_pagestart.cshtml does the following:
  15. // this is the init page
  16. //
  17. // The page ~/index.cshtml does the following:
  18. // hello world
  19. // Expected result:
  20. // this is the init page hello world
  21. [Fact]
  22. public void InitPageBasicTest()
  23. {
  24. var init = Utils.CreateStartPage(p =>
  25. p.Write("this is the init page "));
  26. var page = Utils.CreatePage(p =>
  27. p.Write("hello world"));
  28. init.ChildPage = page;
  29. var result = Utils.RenderWebPage(page, init);
  30. Assert.Equal("this is the init page hello world", result);
  31. }
  32. // The page ~/_pagestart.cshtml does the following:
  33. // this is the init page
  34. //
  35. // The page ~/folder1/index.cshtml does the following:
  36. // hello world
  37. // Expected result:
  38. // this is the init page hello world
  39. [Fact]
  40. public void InitSubfolderTest()
  41. {
  42. var init = Utils.CreateStartPage(p =>
  43. p.Write("this is the init page "));
  44. var page = Utils.CreatePage(p =>
  45. p.Write("hello world"), "~/folder1/index.cshtml");
  46. init.ChildPage = page;
  47. var result = Utils.RenderWebPage(page, init);
  48. Assert.Equal("this is the init page hello world", result);
  49. }
  50. // The page ~/_pagestart.cshtml does the following:
  51. // PageData["Title"] = "InitPage";
  52. // Layout = "Layout.cshtml";
  53. // this is the init page
  54. //
  55. // The page ~/index.cshtml does the following:
  56. // PageData["Title"] = "IndexCshtmlPage"
  57. // hello world
  58. //
  59. // The layout page ~/Layout.cshtml does the following:
  60. // layout start
  61. // @PageData["Title"]
  62. // @RenderBody()
  63. // layout end
  64. //
  65. // Expected result:
  66. // layout start IndexCshtmlPage this is the init page hello world layout end
  67. [Fact]
  68. public void InitPageLayoutTest()
  69. {
  70. var init = Utils.CreateStartPage(p =>
  71. {
  72. p.Layout = "Layout.cshtml";
  73. p.Write(" this is the init page ");
  74. Assert.Equal("~/Layout.cshtml", p.Layout);
  75. });
  76. var page = Utils.CreatePage(p =>
  77. {
  78. p.PageData["Title"] = "IndexCshtmlPage";
  79. p.Write("hello world");
  80. });
  81. var layoutPage = Utils.CreatePage(p =>
  82. {
  83. p.Write("layout start ");
  84. p.Write(p.PageData["Title"]);
  85. p.WriteLiteral(p.RenderBody());
  86. p.Write(" layout end");
  87. }, "~/Layout.cshtml");
  88. init.ChildPage = page;
  89. Utils.AssignObjectFactoriesAndDisplayModeProvider(init, page, layoutPage);
  90. var result = Utils.RenderWebPage(page, init);
  91. Assert.Equal("layout start IndexCshtmlPage this is the init page hello world layout end", result);
  92. }
  93. // _pagestart.cshtml sets the LayoutPage to be null
  94. [Fact]
  95. public void InitPageNullLayoutPageTest()
  96. {
  97. var init1 = Utils.CreateStartPage(
  98. p =>
  99. {
  100. p.Layout = "~/Layout.cshtml";
  101. p.WriteLiteral("<init1>");
  102. p.RunPage();
  103. p.WriteLiteral("</init1>");
  104. });
  105. var init2path = "~/folder1/_pagestart.cshtml";
  106. var init2 = Utils.CreateStartPage(
  107. p =>
  108. {
  109. p.Layout = null;
  110. p.WriteLiteral("<init2>");
  111. p.RunPage();
  112. p.WriteLiteral("</init2>");
  113. }, init2path);
  114. var page = Utils.CreatePage(p =>
  115. p.Write("hello world"), "~/folder1/index.cshtml");
  116. var layoutPage = Utils.CreatePage(p =>
  117. p.Write("layout page"), "~/Layout.cshtml");
  118. Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layoutPage, init1, init2);
  119. init1.ChildPage = init2;
  120. init2.ChildPage = page;
  121. var result = Utils.RenderWebPage(page, init1);
  122. Assert.Equal("<init1><init2>hello world</init2></init1>", result);
  123. }
  124. // _pagestart.cshtml sets the LayoutPage, but page sets it to null
  125. [Fact]
  126. public void PageSetsNullLayoutPageTest()
  127. {
  128. var init1 = Utils.CreateStartPage(
  129. p =>
  130. {
  131. p.Layout = "~/Layout.cshtml";
  132. p.WriteLiteral("<init1>");
  133. p.RunPage();
  134. p.WriteLiteral("</init1>");
  135. });
  136. var layoutPage = Utils.CreatePage(p =>
  137. p.Write("layout page"), "~/Layout.cshtml");
  138. var page = Utils.CreatePage(p =>
  139. {
  140. p.Layout = null;
  141. p.Write("hello world");
  142. });
  143. Utils.AssignObjectFactoriesAndDisplayModeProvider(init1, layoutPage, page);
  144. init1.ChildPage = page;
  145. var result = Utils.RenderWebPage(page, init1);
  146. Assert.Equal("<init1>hello world</init1>", result);
  147. }
  148. [Fact]
  149. public void PageSetsEmptyLayoutPageTest()
  150. {
  151. var init1 = Utils.CreateStartPage(
  152. p =>
  153. {
  154. p.Layout = "~/Layout.cshtml";
  155. p.WriteLiteral("<init1>");
  156. p.RunPage();
  157. p.WriteLiteral("</init1>");
  158. });
  159. var layoutPage = Utils.CreatePage(p =>
  160. p.Write("layout page"), "~/Layout.cshtml");
  161. var page = Utils.CreatePage(p =>
  162. {
  163. p.Layout = "";
  164. p.Write("hello world");
  165. });
  166. Utils.AssignObjectFactoriesAndDisplayModeProvider(init1, layoutPage, page);
  167. init1.ChildPage = page;
  168. var result = Utils.RenderWebPage(page, init1);
  169. Assert.Equal("<init1>hello world</init1>", result);
  170. }
  171. // The page ~/_pagestart.cshtml does the following:
  172. // init page start
  173. // @RunPage()
  174. // init page end
  175. //
  176. // The page ~/index.cshtml does the following:
  177. // hello world
  178. //
  179. // Expected result:
  180. // init page start hello world init page end
  181. [Fact]
  182. public void RunPageTest()
  183. {
  184. var init = Utils.CreateStartPage(
  185. p =>
  186. {
  187. p.Write("init page start ");
  188. p.RunPage();
  189. p.Write(" init page end");
  190. });
  191. var page = Utils.CreatePage(p =>
  192. p.Write("hello world"));
  193. init.ChildPage = page;
  194. var result = Utils.RenderWebPage(page, init);
  195. Assert.Equal("init page start hello world init page end", result);
  196. }
  197. // The page ~/_pagestart.cshtml does the following:
  198. // <init1>
  199. // @RunPage()
  200. // </init1>
  201. //
  202. // The page ~/folder1/_pagestart.cshtml does the following:
  203. // <init2>
  204. // @RunPage()
  205. // </init2>
  206. //
  207. // The page ~/folder1/index.cshtml does the following:
  208. // hello world
  209. //
  210. // Expected result:
  211. // <init1><init2>hello world</init2></init1>
  212. [Fact]
  213. public void NestedRunPageTest()
  214. {
  215. var init1 = Utils.CreateStartPage(
  216. p =>
  217. {
  218. p.WriteLiteral("<init1>");
  219. p.RunPage();
  220. p.WriteLiteral("</init1>");
  221. });
  222. var init2path = "~/folder1/_pagestart.cshtml";
  223. var init2 = Utils.CreateStartPage(
  224. p =>
  225. {
  226. p.WriteLiteral("<init2>");
  227. p.RunPage();
  228. p.WriteLiteral("</init2>");
  229. }, init2path);
  230. var page = Utils.CreatePage(p =>
  231. p.Write("hello world"), "~/folder1/index.cshtml");
  232. init1.ChildPage = init2;
  233. init2.ChildPage = page;
  234. var result = Utils.RenderWebPage(page, init1);
  235. Assert.Equal("<init1><init2>hello world</init2></init1>", result);
  236. }
  237. // The page ~/_pagestart.cshtml does the following:
  238. // PageData["key1"] = "value1";
  239. //
  240. // The page ~/folder1/_pagestart.cshtml does the following:
  241. // PageData["key2"] = "value2";
  242. //
  243. // The page ~/folder1/index.cshtml does the following:
  244. // @PageData["key1"] @PageData["key2"] @PageData["key3"]
  245. //
  246. // Expected result:
  247. // value1 value2
  248. [Fact]
  249. public void PageDataTest()
  250. {
  251. var init1 = Utils.CreateStartPage(p => p.PageData["key1"] = "value1");
  252. var init2path = "~/folder1/_pagestart.cshtml";
  253. var init2 = Utils.CreateStartPage(p => p.PageData["key2"] = "value2", init2path);
  254. var page = Utils.CreatePage(
  255. p =>
  256. {
  257. p.Write(p.PageData["key1"]);
  258. p.Write(" ");
  259. p.Write(p.PageData["key2"]);
  260. },
  261. "~/folder1/index.cshtml");
  262. init1.ChildPage = init2;
  263. init2.ChildPage = page;
  264. var result = Utils.RenderWebPage(page, init1);
  265. Assert.Equal("value1 value2", result);
  266. }
  267. // The page ~/_pagestart.cshtml does the following:
  268. // init page
  269. // @RenderPage("subpage.cshtml", "init_data");
  270. //
  271. // The page ~/subpage.cshtml does the following:
  272. // subpage
  273. // @PageData[0]
  274. //
  275. // The page ~/index.cshtml does the following:
  276. // hello world
  277. //
  278. // Expected result:
  279. // init page subpage init_data hello world
  280. [Fact]
  281. public void RenderPageTest()
  282. {
  283. var init = Utils.CreateStartPage(
  284. p =>
  285. {
  286. p.Write("init page ");
  287. p.Write(p.RenderPage("subpage.cshtml", "init_data"));
  288. });
  289. var subpagePath = "~/subpage.cshtml";
  290. var subpage = Utils.CreatePage(
  291. p =>
  292. {
  293. p.Write("subpage ");
  294. p.Write(p.PageData[0]);
  295. }, subpagePath);
  296. var page = Utils.CreatePage(p =>
  297. p.Write(" hello world"));
  298. init.ChildPage = page;
  299. Utils.AssignObjectFactoriesAndDisplayModeProvider(init, page, subpage);
  300. var result = Utils.RenderWebPage(page, init);
  301. Assert.Equal("init page subpage init_data hello world", result);
  302. }
  303. [Fact]
  304. // The page ~/_pagestart.cshtml does the following:
  305. // <init>
  306. // @{
  307. // try {
  308. // RunPage();
  309. // } catch (Exception e) {
  310. // Write("Exception: " + e.Message);
  311. // }
  312. // }
  313. // </init>
  314. //
  315. // The page ~/index.cshtml does the following:
  316. // hello world
  317. // @{throw new InvalidOperation("exception from index.cshtml");}
  318. //
  319. // Expected result:
  320. // <init>hello world Exception: exception from index.cshtml</init>
  321. public void InitCatchExceptionTest()
  322. {
  323. var init = Utils.CreateStartPage(
  324. p =>
  325. {
  326. p.WriteLiteral("<init>");
  327. try
  328. {
  329. p.RunPage();
  330. }
  331. catch (Exception e)
  332. {
  333. p.Write("Exception: " + e.Message);
  334. }
  335. p.WriteLiteral("</init>");
  336. });
  337. var page = Utils.CreatePage(
  338. p =>
  339. {
  340. p.WriteLiteral("hello world ");
  341. throw new InvalidOperationException("exception from index.cshtml");
  342. });
  343. init.ChildPage = page;
  344. var result = Utils.RenderWebPage(page, init);
  345. Assert.Equal("<init>hello world Exception: exception from index.cshtml</init>", result);
  346. }
  347. public class MockInitPage : MockStartPage
  348. {
  349. internal object GetBuildManager()
  350. {
  351. return typeof(BuildManager).GetField("_theBuildManager", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
  352. }
  353. }
  354. // Simulate a site that is nested, eg /subfolder1/website1
  355. [Fact]
  356. public void ExecuteWithinInitTest()
  357. {
  358. AppDomainUtils.RunInSeparateAppDomain(() =>
  359. {
  360. Utils.CreateHttpRuntime("/subfolder1/website1");
  361. new HostingEnvironment();
  362. var stringSet = Activator.CreateInstance(typeof(BuildManager).Assembly.GetType("System.Web.Util.StringSet"), true);
  363. typeof(BuildManager).GetField("_forbiddenTopLevelDirectories", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(new MockInitPage().GetBuildManager(), stringSet);
  364. ;
  365. var init = new MockInitPage()
  366. {
  367. VirtualPath = "~/_pagestart.cshtml",
  368. ExecuteAction = p => { },
  369. };
  370. var page = Utils.CreatePage(p => { });
  371. Utils.AssignObjectFactoriesAndDisplayModeProvider(page, init);
  372. var result = Utils.RenderWebPage(page);
  373. });
  374. }
  375. [Fact]
  376. public void SetGetPropertiesTest()
  377. {
  378. var init = new MockInitPage();
  379. var page = new MockPage();
  380. init.ChildPage = page;
  381. // Context
  382. var context = new Mock<HttpContextBase>().Object;
  383. init.Context = context;
  384. Assert.Equal(context, init.Context);
  385. Assert.Equal(context, page.Context);
  386. // Profile/Request/Response/Server/Cache/Session/Application
  387. var profile = new Mock<ProfileBase>().Object;
  388. var request = new Mock<HttpRequestBase>().Object;
  389. var response = new Mock<HttpResponseBase>().Object;
  390. var server = new Mock<HttpServerUtilityBase>().Object;
  391. var cache = new Cache();
  392. var app = new Mock<HttpApplicationStateBase>().Object;
  393. var session = new Mock<HttpSessionStateBase>().Object;
  394. var contextMock = new Mock<HttpContextBase>();
  395. contextMock.Setup(c => c.Profile).Returns(profile);
  396. contextMock.Setup(c => c.Request).Returns(request);
  397. contextMock.Setup(c => c.Response).Returns(response);
  398. contextMock.Setup(c => c.Cache).Returns(cache);
  399. contextMock.Setup(c => c.Server).Returns(server);
  400. contextMock.Setup(c => c.Application).Returns(app);
  401. contextMock.Setup(c => c.Session).Returns(session);
  402. context = contextMock.Object;
  403. page.Context = context;
  404. Assert.Same(profile, init.Profile);
  405. Assert.Same(request, init.Request);
  406. Assert.Same(response, init.Response);
  407. Assert.Same(cache, init.Cache);
  408. Assert.Same(server, init.Server);
  409. Assert.Same(session, init.Session);
  410. Assert.Same(app, init.AppState);
  411. }
  412. [Fact]
  413. public void GetDirectoryTest()
  414. {
  415. var initPage = new Mock<StartPage>().Object;
  416. Assert.Equal("/website1/", initPage.GetDirectory("/website1/default.cshtml"));
  417. Assert.Equal("~/", initPage.GetDirectory("~/default.cshtml"));
  418. Assert.Equal("/", initPage.GetDirectory("/website1/"));
  419. Assert.Equal(null, initPage.GetDirectory("/"));
  420. }
  421. [Fact]
  422. public void GetStartPageReturnsStartPageFromCurrentDirectoryIfExists()
  423. {
  424. // Arrange
  425. var initPage = Utils.CreateStartPage(p => p.Write("<init>"), "~/subdir/_pagestart.vbhtml");
  426. var page = Utils.CreatePage(p => p.Write("test"), "~/subdir/_index.cshtml");
  427. var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);
  428. // Act
  429. var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });
  430. // Assert
  431. Assert.Equal(initPage, result);
  432. }
  433. [Fact]
  434. public void GetStartPageReturnsStartPageFromParentDirectoryIfStartPageDoesNotExistInCurrentDirectory()
  435. {
  436. // Arrange
  437. var initPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.vbhtml");
  438. var page = Utils.CreatePage(null, "~/subdir/subsubdir/test.cshtml");
  439. var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);
  440. // Act
  441. var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });
  442. // Assert
  443. Assert.Equal(initPage, result);
  444. }
  445. [Fact]
  446. public void GetStartPageCreatesChainOfStartPages()
  447. {
  448. // Arrange
  449. var subInitPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.vbhtml");
  450. var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
  451. var page = Utils.CreatePage(null, "~/subdir/subsubdir/subsubsubdir/test.cshtml");
  452. var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage, subInitPage);
  453. // Act
  454. var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });
  455. // Assert
  456. Assert.Equal(initPage, result);
  457. Assert.Equal(subInitPage, (result as StartPage).ChildPage);
  458. }
  459. [Fact]
  460. public void GetStartPageReturnsStartPageFromRoot()
  461. {
  462. // Arrange
  463. var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
  464. var page = Utils.CreatePage(null, "~/subdir/subsubdir/subsubsubdir/subsubsubsubdir/why-does-this-remind-me-of-a-movie-title.cshtml");
  465. var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);
  466. // Act
  467. var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });
  468. // Assert
  469. Assert.Equal(initPage, result);
  470. }
  471. [Fact]
  472. public void GetStartPageUsesBothFileNamesAndExtensionsWhenDeterminingStartPage()
  473. {
  474. // Arrange
  475. var subInitPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.jshtml");
  476. var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
  477. var page = Utils.CreatePage(null, "~/subdir/test.cshtml");
  478. var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage, subInitPage);
  479. // Act
  480. var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });
  481. // Assert
  482. Assert.Equal(initPage, result);
  483. }
  484. [Fact]
  485. public void GetStartPage_ThrowsOnNullPage()
  486. {
  487. Assert.ThrowsArgumentNull(() => StartPage.GetStartPage(null, "name", new[] { "cshtml" }), "page");
  488. }
  489. [Fact]
  490. public void GetStartPage_ThrowsOnNullFileName()
  491. {
  492. var page = Utils.CreatePage(p => p.Write("test"));
  493. Assert.ThrowsArgumentNullOrEmptyString(() => StartPage.GetStartPage(page, null, new[] { "cshtml" }), "fileName");
  494. }
  495. [Fact]
  496. public void GetStartPage_ThrowsOnEmptyFileName()
  497. {
  498. var page = Utils.CreatePage(p => p.Write("test"));
  499. Assert.ThrowsArgumentNullOrEmptyString(() => StartPage.GetStartPage(page, String.Empty, new[] { "cshtml" }), "fileName");
  500. }
  501. [Fact]
  502. public void GetStartPage_ThrowsOnNullSupportedExtensions()
  503. {
  504. var page = Utils.CreatePage(p => p.Write("test"));
  505. Assert.ThrowsArgumentNull(() => StartPage.GetStartPage(page, "name", null), "supportedExtensions");
  506. }
  507. }
  508. }