* IWebContext: Add Request body reading methods and properties.
* IWebContext: Add support for HttpOnly, and Secure cookies.
* Use file-scoped namespaces.
This commit is contained in:
2025-07-28 16:11:54 +02:00
parent 6dc19e8bbd
commit 9a480f5528
5 changed files with 110 additions and 49 deletions

View File

@@ -86,6 +86,27 @@ public class AspnetCoreWebContext : IWebContext
}
}
public string? RequestContentType => _context.Request.ContentType;
public long? RequestContentLength => _context.Request.ContentLength;
public byte[]? RequestReadBin()
{
if ((_context.Request.ContentLength ?? 0) == 0) { return null; }
byte[] content = new byte[_context.Request.ContentLength??0];
int pendingRead = (int) (_context.Request.ContentLength ?? 0);
int currentOffset = 0;
while (pendingRead > 0)
{
int readCount = _context.Request.Body.ReadAsync(content, currentOffset, pendingRead).GetAwaiter().GetResult();
currentOffset += readCount;
pendingRead -= readCount;
}
return content;
}
public void ResponseWrite(string text) { _context.Response.WriteAsync(text).GetAwaiter().GetResult(); }
public void ResponseWriteBin(byte[] content)
@@ -97,12 +118,18 @@ public class AspnetCoreWebContext : IWebContext
public void ResponseRedirect(string url) { _context.Response.Redirect(url); }
public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null)
public void AddResponseCookie(
string cookieName,
string value,
DateTime? expiration = null,
bool httpOnly = false,
bool secure = false
)
{
_context.Response.Cookies.Append(
key: cookieName,
value: value,
options: new CookieOptions { Expires = expiration, }
options: new CookieOptions { Expires = expiration, HttpOnly = httpOnly, Secure = secure, }
);
}

View File

@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Http;
namespace VAR.WebFormsCore.AspNetCore.Code
{
namespace VAR.WebFormsCore.AspNetCore.Code;
public static class ExtensionMethods
{
#region IHeaderDictionary
@@ -15,4 +15,3 @@ namespace VAR.WebFormsCore.AspNetCore.Code
#endregion IHeaderDictionary
}
}

View File

@@ -5,7 +5,16 @@ namespace VAR.WebFormsCore.Tests.Fakes;
public class FakeWebContext : IWebContext
{
public FakeWebContext(string requestMethod = "GET") { RequestMethod = requestMethod; }
public FakeWebContext(
string requestMethod = "GET",
string? requestContentType = null,
long? requestContentLength = null
)
{
RequestMethod = requestMethod;
RequestContentType = requestContentType;
RequestContentLength = requestContentLength;
}
public string RequestPath => string.Empty;
@@ -19,6 +28,15 @@ public class FakeWebContext : IWebContext
public Dictionary<string, string?> RequestForm { get; } = new();
public string? RequestContentType { get; }
public long? RequestContentLength { get; }
public byte[]? RequestReadBin()
{
throw new NotImplementedException();
}
public List<WritePackage> FakeWritePackages { get; } = new();
public void ResponseWrite(string text) { FakeWritePackages.Add(new WritePackage { Text = text, }); }
@@ -36,7 +54,13 @@ public class FakeWebContext : IWebContext
SetResponseHeader("location", url);
}
public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null)
public void AddResponseCookie(
string cookieName,
string value,
DateTime? expiration = null,
bool httpOnly = false,
bool secure = false
)
{
throw new NotImplementedException();
}

View File

@@ -12,11 +12,23 @@ public interface IWebContext
Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> RequestForm { get; }
string? RequestContentType { get; }
long? RequestContentLength { get; }
byte[]? RequestReadBin();
void ResponseWrite(string text);
void ResponseWriteBin(byte[] content);
void ResponseFlush();
void ResponseRedirect(string url);
void AddResponseCookie(string cookieName, string value, DateTime? expiration = null);
void AddResponseCookie(
string cookieName,
string value,
DateTime? expiration = null,
bool httpOnly = false,
bool secure = false
);
void DelResponseCookie(string cookieName);
bool ResponseHasStarted { get; }

View File

@@ -3,8 +3,8 @@ using System.IO;
using System.Text;
using VAR.WebFormsCore.Code;
namespace VAR.WebFormsCore.Controls
{
namespace VAR.WebFormsCore.Controls;
public class HtmlForm : Control
{
private readonly string _method = "post";
@@ -43,4 +43,3 @@ namespace VAR.WebFormsCore.Controls
return sbAction.ToString();
}
}
}