From 61fce0efb76accb4f6aacc3adb0953325c035046 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Tue, 30 May 2023 02:41:53 +0200 Subject: [PATCH] Tests: Add tests for Page. --- .../Fakes/FakeWebContext.cs | 9 +- VAR.WebFormsCore.Tests/Pages/PageTests.cs | 122 ++++++++++++++++++ VAR.WebFormsCore/Pages/Page.cs | 1 - 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 VAR.WebFormsCore.Tests/Pages/PageTests.cs diff --git a/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs b/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs index f2b8004..00326a4 100644 --- a/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs +++ b/VAR.WebFormsCore.Tests/Fakes/FakeWebContext.cs @@ -55,7 +55,14 @@ public class FakeWebContext : IWebContext throw new NotImplementedException(); } - public bool ResponseHasStarted => false; + private bool _responseHasStarted; + + public void FakeSetResponseHasStarted(bool responseHasStarted) + { + _responseHasStarted = responseHasStarted; + } + + public bool ResponseHasStarted => _responseHasStarted; public int ResponseStatusCode { get; set; } = 200; diff --git a/VAR.WebFormsCore.Tests/Pages/PageTests.cs b/VAR.WebFormsCore.Tests/Pages/PageTests.cs new file mode 100644 index 0000000..d262b5a --- /dev/null +++ b/VAR.WebFormsCore.Tests/Pages/PageTests.cs @@ -0,0 +1,122 @@ +using VAR.WebFormsCore.Pages; +using VAR.WebFormsCore.Tests.Fakes; +using Xunit; + +namespace VAR.WebFormsCore.Tests.Pages; + +public class PageTests +{ + [Fact] + public void ProcessRequest__Empty__Empty() + { + FakeWebContext fakeWebContext = new(); + Page page = new(); + + page.ProcessRequest(fakeWebContext); + + Assert.Equal(200, fakeWebContext.ResponseStatusCode); + Assert.Equal("text/html", fakeWebContext.ResponseContentType); + string result = fakeWebContext.FakeWritePackages.ToString(""); + Assert.Equal(string.Empty, result); + } + + [Fact] + public void ProcessRequest__SendResponseOnPreInit__NullContentType() + { + FakeWebContext fakeWebContext = new(); + Page page = new(); + page.PreInit += (_, _) => { fakeWebContext.FakeSetResponseHasStarted(true); }; + + page.ProcessRequest(fakeWebContext); + + Assert.Null(fakeWebContext.ResponseContentType); + } + + [Fact] + public void ProcessRequest__SendResponseOnInit__NullContentType() + { + FakeWebContext fakeWebContext = new(); + Page page = new(); + page.Init += (_, _) => { fakeWebContext.FakeSetResponseHasStarted(true); }; + + page.ProcessRequest(fakeWebContext); + + Assert.Null(fakeWebContext.ResponseContentType); + } + + [Fact] + public void ProcessRequest__SendResponseOnLoad__NullContentType() + { + FakeWebContext fakeWebContext = new(); + Page page = new(); + page.Load += (_, _) => { fakeWebContext.FakeSetResponseHasStarted(true); }; + + page.ProcessRequest(fakeWebContext); + + Assert.Null(fakeWebContext.ResponseContentType); + } + + [Fact] + public void ProcessRequest__SendResponseOnPreRender__NullContentType() + { + FakeWebContext fakeWebContext = new(); + Page page = new(); + page.PreRender += (_, _) => { fakeWebContext.FakeSetResponseHasStarted(true); }; + + page.ProcessRequest(fakeWebContext); + + Assert.Null(fakeWebContext.ResponseContentType); + } + + [Fact] + public void ProcessRequest__AbortIfIsPostbackOnGet__Empty() + { + FakeWebContext fakeWebContext = new(requestMethod: "GET"); + Page page = new(); + page.Load += (_, _) => + { + if (page.IsPostBack) + { + fakeWebContext.FakeSetResponseHasStarted(true); + } + }; + + page.ProcessRequest(fakeWebContext); + + Assert.Equal(200, fakeWebContext.ResponseStatusCode); + Assert.Equal("text/html", fakeWebContext.ResponseContentType); + string result = fakeWebContext.FakeWritePackages.ToString(""); + Assert.Equal(string.Empty, result); + } + + [Fact] + public void ProcessRequest__AbortIfIsPostbackOnPost__NullContentType() + { + FakeWebContext fakeWebContext = new(requestMethod: "POST"); + Page page = new(); + page.Load += (_, _) => + { + if (page.IsPostBack) + { + fakeWebContext.FakeSetResponseHasStarted(true); + } + }; + + page.ProcessRequest(fakeWebContext); + + Assert.Null(fakeWebContext.ResponseContentType); + } + + [Fact] + public void ProcessRequest__ThrowException__NullContentType() + { + FakeWebContext fakeWebContext = new(requestMethod: "POST"); + Page page = new(); + page.Load += (_, _) => throw new Exception("Test"); + + page.ProcessRequest(fakeWebContext); + + Assert.Equal(200, fakeWebContext.ResponseStatusCode); + Assert.Equal("text/html", fakeWebContext.ResponseContentType); + } +} \ No newline at end of file diff --git a/VAR.WebFormsCore/Pages/Page.cs b/VAR.WebFormsCore/Pages/Page.cs index 4edc6e1..18a1454 100644 --- a/VAR.WebFormsCore/Pages/Page.cs +++ b/VAR.WebFormsCore/Pages/Page.cs @@ -55,7 +55,6 @@ public class Page : Control, IHttpHandler Render(stringWriter); if (context.ResponseHasStarted) { return; } - //context.SetResponseHeader("Content-Type", "text/html"); context.ResponseContentType = "text/html"; byte[] byteObject = Utf8Encoding.GetBytes(stringWriter.ToString()); context.ResponseWriteBin(byteObject);