File: StartPageTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,740 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (570 lines) | stat: -rw-r--r-- 21,431 bytes parent folder | download | duplicates (9)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Reflection;
using System.Web.Caching;
using System.Web.Compilation;
using System.Web.Hosting;
using System.Web.Profile;
using System.Web.WebPages.TestUtils;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.WebPages.Test
{
    public class StartPageTest
    {
        // The page ~/_pagestart.cshtml does the following:
        // this is the init page
        // 
        // The page ~/index.cshtml does the following:
        // hello world
        // Expected result:
        // this is the init page hello world
        [Fact]
        public void InitPageBasicTest()
        {
            var init = Utils.CreateStartPage(p =>
                                             p.Write("this is the init page "));
            var page = Utils.CreatePage(p =>
                                        p.Write("hello world"));

            init.ChildPage = page;

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("this is the init page hello world", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // this is the init page
        // 
        // The page ~/folder1/index.cshtml does the following:
        // hello world
        // Expected result:
        // this is the init page hello world
        [Fact]
        public void InitSubfolderTest()
        {
            var init = Utils.CreateStartPage(p =>
                                             p.Write("this is the init page "));
            var page = Utils.CreatePage(p =>
                                        p.Write("hello world"), "~/folder1/index.cshtml");

            init.ChildPage = page;

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("this is the init page hello world", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // PageData["Title"] = "InitPage";
        // Layout = "Layout.cshtml";
        // this is the init page
        // 
        // The page ~/index.cshtml does the following:
        // PageData["Title"] = "IndexCshtmlPage"
        // hello world
        //
        // The layout page ~/Layout.cshtml does the following:
        // layout start
        // @PageData["Title"]
        // @RenderBody()
        // layout end
        //
        // Expected result:
        // layout start IndexCshtmlPage this is the init page hello world layout end
        [Fact]
        public void InitPageLayoutTest()
        {
            var init = Utils.CreateStartPage(p =>
            {
                p.Layout = "Layout.cshtml";
                p.Write(" this is the init page ");
                Assert.Equal("~/Layout.cshtml", p.Layout);
            });
            var page = Utils.CreatePage(p =>
            {
                p.PageData["Title"] = "IndexCshtmlPage";
                p.Write("hello world");
            });
            var layoutPage = Utils.CreatePage(p =>
            {
                p.Write("layout start ");
                p.Write(p.PageData["Title"]);
                p.WriteLiteral(p.RenderBody());
                p.Write(" layout end");
            }, "~/Layout.cshtml");

            init.ChildPage = page;
            Utils.AssignObjectFactoriesAndDisplayModeProvider(init, page, layoutPage);

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("layout start IndexCshtmlPage this is the init page hello world layout end", result);
        }

        // _pagestart.cshtml sets the LayoutPage to be null
        [Fact]
        public void InitPageNullLayoutPageTest()
        {
            var init1 = Utils.CreateStartPage(
                p =>
                {
                    p.Layout = "~/Layout.cshtml";
                    p.WriteLiteral("<init1>");
                    p.RunPage();
                    p.WriteLiteral("</init1>");
                });
            var init2path = "~/folder1/_pagestart.cshtml";
            var init2 = Utils.CreateStartPage(
                p =>
                {
                    p.Layout = null;
                    p.WriteLiteral("<init2>");
                    p.RunPage();
                    p.WriteLiteral("</init2>");
                }, init2path);
            var page = Utils.CreatePage(p =>
                                        p.Write("hello world"), "~/folder1/index.cshtml");
            var layoutPage = Utils.CreatePage(p =>
                                              p.Write("layout page"), "~/Layout.cshtml");

            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layoutPage, init1, init2);

            init1.ChildPage = init2;
            init2.ChildPage = page;

            var result = Utils.RenderWebPage(page, init1);
            Assert.Equal("<init1><init2>hello world</init2></init1>", result);
        }

        // _pagestart.cshtml sets the LayoutPage, but page sets it to null
        [Fact]
        public void PageSetsNullLayoutPageTest()
        {
            var init1 = Utils.CreateStartPage(
                p =>
                {
                    p.Layout = "~/Layout.cshtml";
                    p.WriteLiteral("<init1>");
                    p.RunPage();
                    p.WriteLiteral("</init1>");
                });
            var layoutPage = Utils.CreatePage(p =>
                                              p.Write("layout page"), "~/Layout.cshtml");
            var page = Utils.CreatePage(p =>
            {
                p.Layout = null;
                p.Write("hello world");
            });
            Utils.AssignObjectFactoriesAndDisplayModeProvider(init1, layoutPage, page);
            init1.ChildPage = page;
            var result = Utils.RenderWebPage(page, init1);
            Assert.Equal("<init1>hello world</init1>", result);
        }

        [Fact]
        public void PageSetsEmptyLayoutPageTest()
        {
            var init1 = Utils.CreateStartPage(
                p =>
                {
                    p.Layout = "~/Layout.cshtml";
                    p.WriteLiteral("<init1>");
                    p.RunPage();
                    p.WriteLiteral("</init1>");
                });
            var layoutPage = Utils.CreatePage(p =>
                                              p.Write("layout page"), "~/Layout.cshtml");
            var page = Utils.CreatePage(p =>
            {
                p.Layout = "";
                p.Write("hello world");
            });
            Utils.AssignObjectFactoriesAndDisplayModeProvider(init1, layoutPage, page);
            init1.ChildPage = page;
            var result = Utils.RenderWebPage(page, init1);
            Assert.Equal("<init1>hello world</init1>", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // init page start
        // @RunPage()
        // init page end
        // 
        // The page ~/index.cshtml does the following:
        // hello world
        //
        // Expected result:
        // init page start hello world init page end
        [Fact]
        public void RunPageTest()
        {
            var init = Utils.CreateStartPage(
                p =>
                {
                    p.Write("init page start ");
                    p.RunPage();
                    p.Write(" init page end");
                });
            var page = Utils.CreatePage(p =>
                                        p.Write("hello world"));

            init.ChildPage = page;

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("init page start hello world init page end", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // <init1>
        // @RunPage()
        // </init1>
        // 
        // The page ~/folder1/_pagestart.cshtml does the following:
        // <init2>
        // @RunPage()
        // </init2>
        // 
        // The page ~/folder1/index.cshtml does the following:
        // hello world
        //
        // Expected result:
        // <init1><init2>hello world</init2></init1>
        [Fact]
        public void NestedRunPageTest()
        {
            var init1 = Utils.CreateStartPage(
                p =>
                {
                    p.WriteLiteral("<init1>");
                    p.RunPage();
                    p.WriteLiteral("</init1>");
                });
            var init2path = "~/folder1/_pagestart.cshtml";
            var init2 = Utils.CreateStartPage(
                p =>
                {
                    p.WriteLiteral("<init2>");
                    p.RunPage();
                    p.WriteLiteral("</init2>");
                }, init2path);
            var page = Utils.CreatePage(p =>
                                        p.Write("hello world"), "~/folder1/index.cshtml");

            init1.ChildPage = init2;
            init2.ChildPage = page;

            var result = Utils.RenderWebPage(page, init1);
            Assert.Equal("<init1><init2>hello world</init2></init1>", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // PageData["key1"] = "value1";
        // 
        // The page ~/folder1/_pagestart.cshtml does the following:
        // PageData["key2"] = "value2";
        // 
        // The page ~/folder1/index.cshtml does the following:
        // @PageData["key1"] @PageData["key2"] @PageData["key3"]
        //
        // Expected result:
        // value1 value2
        [Fact]
        public void PageDataTest()
        {
            var init1 = Utils.CreateStartPage(p => p.PageData["key1"] = "value1");
            var init2path = "~/folder1/_pagestart.cshtml";
            var init2 = Utils.CreateStartPage(p => p.PageData["key2"] = "value2", init2path);
            var page = Utils.CreatePage(
                p =>
                {
                    p.Write(p.PageData["key1"]);
                    p.Write(" ");
                    p.Write(p.PageData["key2"]);
                },
                "~/folder1/index.cshtml");

            init1.ChildPage = init2;
            init2.ChildPage = page;

            var result = Utils.RenderWebPage(page, init1);
            Assert.Equal("value1 value2", result);
        }

        // The page ~/_pagestart.cshtml does the following:
        // init page
        // @RenderPage("subpage.cshtml", "init_data");
        //
        // The page ~/subpage.cshtml does the following:
        // subpage
        // @PageData[0]
        //
        // The page ~/index.cshtml does the following:
        // hello world
        //
        // Expected result:
        // init page subpage init_data hello world
        [Fact]
        public void RenderPageTest()
        {
            var init = Utils.CreateStartPage(
                p =>
                {
                    p.Write("init page ");
                    p.Write(p.RenderPage("subpage.cshtml", "init_data"));
                });
            var subpagePath = "~/subpage.cshtml";
            var subpage = Utils.CreatePage(
                p =>
                {
                    p.Write("subpage ");
                    p.Write(p.PageData[0]);
                }, subpagePath);
            var page = Utils.CreatePage(p =>
                                        p.Write(" hello world"));

            init.ChildPage = page;
            Utils.AssignObjectFactoriesAndDisplayModeProvider(init, page, subpage);

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("init page subpage init_data hello world", result);
        }

        [Fact]
        // The page ~/_pagestart.cshtml does the following:
        // <init>
        // @{ 
        //     try {
        //         RunPage();
        //     } catch (Exception e) {
        //         Write("Exception: " + e.Message);
        //     }
        // }
        // </init>
        //
        // The page ~/index.cshtml does the following:
        // hello world
        // @{throw new InvalidOperation("exception from index.cshtml");}
        //
        // Expected result:
        // <init>hello world Exception: exception from index.cshtml</init>
        public void InitCatchExceptionTest()
        {
            var init = Utils.CreateStartPage(
                p =>
                {
                    p.WriteLiteral("<init>");
                    try
                    {
                        p.RunPage();
                    }
                    catch (Exception e)
                    {
                        p.Write("Exception: " + e.Message);
                    }
                    p.WriteLiteral("</init>");
                });
            var page = Utils.CreatePage(
                p =>
                {
                    p.WriteLiteral("hello world ");
                    throw new InvalidOperationException("exception from index.cshtml");
                });

            init.ChildPage = page;

            var result = Utils.RenderWebPage(page, init);
            Assert.Equal("<init>hello world Exception: exception from index.cshtml</init>", result);
        }

        public class MockInitPage : MockStartPage
        {
            internal object GetBuildManager()
            {
                return typeof(BuildManager).GetField("_theBuildManager", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
            }
        }

        // Simulate a site that is nested, eg /subfolder1/website1
        [Fact]
        public void ExecuteWithinInitTest()
        {
            AppDomainUtils.RunInSeparateAppDomain(() =>
            {
                Utils.CreateHttpRuntime("/subfolder1/website1");
                new HostingEnvironment();
                var stringSet = Activator.CreateInstance(typeof(BuildManager).Assembly.GetType("System.Web.Util.StringSet"), true);
                typeof(BuildManager).GetField("_forbiddenTopLevelDirectories", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(new MockInitPage().GetBuildManager(), stringSet);
                ;

                var init = new MockInitPage()
                {
                    VirtualPath = "~/_pagestart.cshtml",
                    ExecuteAction = p => { },
                };
                var page = Utils.CreatePage(p => { });

                Utils.AssignObjectFactoriesAndDisplayModeProvider(page, init);

                var result = Utils.RenderWebPage(page);
            });
        }

        [Fact]
        public void SetGetPropertiesTest()
        {
            var init = new MockInitPage();
            var page = new MockPage();
            init.ChildPage = page;

            // Context
            var context = new Mock<HttpContextBase>().Object;
            init.Context = context;
            Assert.Equal(context, init.Context);
            Assert.Equal(context, page.Context);

            // Profile/Request/Response/Server/Cache/Session/Application
            var profile = new Mock<ProfileBase>().Object;
            var request = new Mock<HttpRequestBase>().Object;
            var response = new Mock<HttpResponseBase>().Object;
            var server = new Mock<HttpServerUtilityBase>().Object;
            var cache = new Cache();
            var app = new Mock<HttpApplicationStateBase>().Object;
            var session = new Mock<HttpSessionStateBase>().Object;

            var contextMock = new Mock<HttpContextBase>();
            contextMock.Setup(c => c.Profile).Returns(profile);
            contextMock.Setup(c => c.Request).Returns(request);
            contextMock.Setup(c => c.Response).Returns(response);
            contextMock.Setup(c => c.Cache).Returns(cache);
            contextMock.Setup(c => c.Server).Returns(server);
            contextMock.Setup(c => c.Application).Returns(app);
            contextMock.Setup(c => c.Session).Returns(session);

            context = contextMock.Object;
            page.Context = context;
            Assert.Same(profile, init.Profile);
            Assert.Same(request, init.Request);
            Assert.Same(response, init.Response);
            Assert.Same(cache, init.Cache);
            Assert.Same(server, init.Server);
            Assert.Same(session, init.Session);
            Assert.Same(app, init.AppState);
        }

        [Fact]
        public void GetDirectoryTest()
        {
            var initPage = new Mock<StartPage>().Object;
            Assert.Equal("/website1/", initPage.GetDirectory("/website1/default.cshtml"));
            Assert.Equal("~/", initPage.GetDirectory("~/default.cshtml"));
            Assert.Equal("/", initPage.GetDirectory("/website1/"));
            Assert.Equal(null, initPage.GetDirectory("/"));
        }

        [Fact]
        public void GetStartPageReturnsStartPageFromCurrentDirectoryIfExists()
        {
            // Arrange
            var initPage = Utils.CreateStartPage(p => p.Write("<init>"), "~/subdir/_pagestart.vbhtml");
            var page = Utils.CreatePage(p => p.Write("test"), "~/subdir/_index.cshtml");
            var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);

            // Act
            var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });

            // Assert
            Assert.Equal(initPage, result);
        }

        [Fact]
        public void GetStartPageReturnsStartPageFromParentDirectoryIfStartPageDoesNotExistInCurrentDirectory()
        {
            // Arrange
            var initPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.vbhtml");
            var page = Utils.CreatePage(null, "~/subdir/subsubdir/test.cshtml");
            var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);

            // Act
            var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });

            // Assert
            Assert.Equal(initPage, result);
        }

        [Fact]
        public void GetStartPageCreatesChainOfStartPages()
        {
            // Arrange
            var subInitPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.vbhtml");
            var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
            var page = Utils.CreatePage(null, "~/subdir/subsubdir/subsubsubdir/test.cshtml");
            var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage, subInitPage);

            // Act
            var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });

            // Assert
            Assert.Equal(initPage, result);
            Assert.Equal(subInitPage, (result as StartPage).ChildPage);
        }

        [Fact]
        public void GetStartPageReturnsStartPageFromRoot()
        {
            // Arrange
            var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
            var page = Utils.CreatePage(null, "~/subdir/subsubdir/subsubsubdir/subsubsubsubdir/why-does-this-remind-me-of-a-movie-title.cshtml");
            var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage);

            // Act
            var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });

            // Assert
            Assert.Equal(initPage, result);
        }

        [Fact]
        public void GetStartPageUsesBothFileNamesAndExtensionsWhenDeterminingStartPage()
        {
            // Arrange
            var subInitPage = Utils.CreateStartPage(null, "~/subdir/_pagestart.jshtml");
            var initPage = Utils.CreateStartPage(null, "~/_pagestart.vbhtml");
            var page = Utils.CreatePage(null, "~/subdir/test.cshtml");
            var objectFactory = Utils.AssignObjectFactoriesAndDisplayModeProvider(page, initPage, subInitPage);

            // Act
            var result = StartPage.GetStartPage(page, objectFactory, null, WebPageHttpHandler.StartPageFileName, new string[] { "cshtml", "vbhtml" });

            // Assert
            Assert.Equal(initPage, result);
        }

        [Fact]
        public void GetStartPage_ThrowsOnNullPage()
        {
            Assert.ThrowsArgumentNull(() => StartPage.GetStartPage(null, "name", new[] { "cshtml" }), "page");
        }

        [Fact]
        public void GetStartPage_ThrowsOnNullFileName()
        {
            var page = Utils.CreatePage(p => p.Write("test"));
            Assert.ThrowsArgumentNullOrEmptyString(() => StartPage.GetStartPage(page, null, new[] { "cshtml" }), "fileName");
        }

        [Fact]
        public void GetStartPage_ThrowsOnEmptyFileName()
        {
            var page = Utils.CreatePage(p => p.Write("test"));
            Assert.ThrowsArgumentNullOrEmptyString(() => StartPage.GetStartPage(page, String.Empty, new[] { "cshtml" }), "fileName");
        }

        [Fact]
        public void GetStartPage_ThrowsOnNullSupportedExtensions()
        {
            var page = Utils.CreatePage(p => p.Write("test"));
            Assert.ThrowsArgumentNull(() => StartPage.GetStartPage(page, "name", null), "supportedExtensions");
        }
    }
}