Tests: Add tests for Page.

This commit is contained in:
2023-05-30 02:41:53 +02:00
parent bde23a865b
commit 61fce0efb7
3 changed files with 130 additions and 2 deletions

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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);