Enhance dictionary safety with SafeGet, SafeSet, and SafeRemove methods. Adjust IWebContext and related tests to support nullable value types in dictionary operations. Refactor GetRequestParameter for clarity and consistency.

This commit is contained in:
2025-07-29 02:05:28 +02:00
parent bab8898841
commit b7b7c472af
12 changed files with 55 additions and 34 deletions

View File

@@ -34,16 +34,16 @@ public class AspnetCoreWebContext : IWebContext
} }
private Dictionary<string, string>? _requestCookies; private Dictionary<string, string?>? _requestCookies;
public Dictionary<string, string> RequestCookies public Dictionary<string, string?> RequestCookies
{ {
get get
{ {
if (_requestCookies == null) if (_requestCookies == null)
{ {
_requestCookies = _context.Request.Cookies _requestCookies = _context.Request.Cookies
.ToDictionary(p => p.Key, p => p.Value); .ToDictionary(p => p.Key, p => (string?)p.Value);
} }
return _requestCookies; return _requestCookies;

View File

@@ -38,7 +38,7 @@ public class ExtensionMethodsTests
FakeWebContext fakeWebContext = new(); FakeWebContext fakeWebContext = new();
string key = "Key"; string key = "Key";
string value = "Value"; string value = "Value";
fakeWebContext.RequestQuery.Add(key, value); fakeWebContext.RequestQuery.SafeSet(key, value);
string result = fakeWebContext.GetRequestParameter(key); string result = fakeWebContext.GetRequestParameter(key);
@@ -52,7 +52,7 @@ public class ExtensionMethodsTests
FakeWebContext fakeWebContext = new(requestMethod: "POST"); FakeWebContext fakeWebContext = new(requestMethod: "POST");
string key = "Key"; string key = "Key";
string value = "Value"; string value = "Value";
fakeWebContext.RequestForm.Add(key, value); fakeWebContext.RequestForm.SafeSet(key, value);
string result = fakeWebContext.GetRequestParameter(key); string result = fakeWebContext.GetRequestParameter(key);
@@ -67,7 +67,7 @@ public class ExtensionMethodsTests
string keyInvalid = "KeyInvalid"; string keyInvalid = "KeyInvalid";
string key = "Key"; string key = "Key";
string value = "Value"; string value = "Value";
fakeWebContext.RequestQuery.Add(keyInvalid, value); fakeWebContext.RequestQuery.SafeSet(keyInvalid, value);
string result = fakeWebContext.GetRequestParameter(key); string result = fakeWebContext.GetRequestParameter(key);
@@ -82,7 +82,7 @@ public class ExtensionMethodsTests
string keyInvalid = "KeyInvalid"; string keyInvalid = "KeyInvalid";
string key = "Key"; string key = "Key";
string value = "Value"; string value = "Value";
fakeWebContext.RequestForm.Add(keyInvalid, value); fakeWebContext.RequestForm.SafeSet(keyInvalid, value);
string result = fakeWebContext.GetRequestParameter(key); string result = fakeWebContext.GetRequestParameter(key);

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
@@ -56,7 +57,7 @@ public class ButtonTests
button.Click += (o, _) => { result = (o as Button)?.CommandArgument; }; button.Click += (o, _) => { result = (o as Button)?.CommandArgument; };
page.Controls.Add(button); page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked"); fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
Assert.Equal(commandArgument, result); Assert.Equal(commandArgument, result);

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
@@ -94,7 +95,7 @@ public class CTextBoxTests
page0.ProcessRequest(fakeWebContext0); page0.ProcessRequest(fakeWebContext0);
FakeWebContext fakeWebContext1 = new(requestMethod: "POST"); FakeWebContext fakeWebContext1 = new(requestMethod: "POST");
fakeWebContext1.RequestForm.Add(cTextBox0.TxtContent.ClientID, changedValue); fakeWebContext1.RequestForm.SafeSet(cTextBox0.TxtContent.ClientID, changedValue);
Page page1 = new(); Page page1 = new();
CTextBox cTextBox1 = new() { Text = text }; CTextBox cTextBox1 = new() { Text = text };
page1.Controls.Add(cTextBox1); page1.Controls.Add(cTextBox1);
@@ -188,7 +189,7 @@ public class CTextBoxTests
}; };
page.Controls.Add(button); page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked"); fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
int? resultHeight = cTextBox.GetClientsideHeight(); int? resultHeight = cTextBox.GetClientsideHeight();
@@ -222,7 +223,7 @@ public class CTextBoxTests
button.Click += (_, _) => { cTextBox.SetClientsideHeight(null); }; button.Click += (_, _) => { cTextBox.SetClientsideHeight(null); };
page.Controls.Add(button); page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked"); fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
int? resultHeight = cTextBox.GetClientsideHeight(); int? resultHeight = cTextBox.GetClientsideHeight();
@@ -259,7 +260,7 @@ public class CTextBoxTests
}; };
page.Controls.Add(button); page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked"); fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
int? resultHeight = cTextBox.GetClientsideHeight(); int? resultHeight = cTextBox.GetClientsideHeight();
@@ -286,7 +287,7 @@ public class CTextBoxTests
}; };
page.Controls.Add(button); page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked"); fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
int? resultHeight = cTextBox.GetClientsideHeight(); int? resultHeight = cTextBox.GetClientsideHeight();

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
@@ -51,7 +52,7 @@ public class HiddenFieldTests
HiddenField hiddenField = new() { Value = value }; HiddenField hiddenField = new() { Value = value };
page.Controls.Add(hiddenField); page.Controls.Add(hiddenField);
fakeWebContext.RequestForm.Add(hiddenField.ClientID, changedValue); fakeWebContext.RequestForm.SafeSet(hiddenField.ClientID, changedValue);
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
Assert.Equal(changedValue, hiddenField.Value); Assert.Equal(changedValue, hiddenField.Value);

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
@@ -27,7 +28,7 @@ public class HtmlFormTests
public void MustRenderCorrectly__WithQueryParameters() public void MustRenderCorrectly__WithQueryParameters()
{ {
FakeWebContext fakeWebContext = new(); FakeWebContext fakeWebContext = new();
fakeWebContext.RequestQuery.Add("test", "value"); fakeWebContext.RequestQuery.SafeSet("test", "value");
Page page = new(); Page page = new();
HtmlForm htmlForm = new(); HtmlForm htmlForm = new();
page.Controls.Add(htmlForm); page.Controls.Add(htmlForm);

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
@@ -138,7 +139,7 @@ public class TextBoxTests
TextBox textBox = new() { Text = text }; TextBox textBox = new() { Text = text };
page.Controls.Add(textBox); page.Controls.Add(textBox);
fakeWebContext.RequestForm.Add(textBox.ClientID, changedValue); fakeWebContext.RequestForm.SafeSet(textBox.ClientID, changedValue);
page.ProcessRequest(fakeWebContext); page.ProcessRequest(fakeWebContext);
Assert.Equal(changedValue, textBox.Text); Assert.Equal(changedValue, textBox.Text);

View File

@@ -22,7 +22,7 @@ public class FakeWebContext : IWebContext
public Dictionary<string, string?> RequestHeader { get; } = new(); public Dictionary<string, string?> RequestHeader { get; } = new();
public Dictionary<string, string> RequestCookies { get; } = new(); public Dictionary<string, string?> RequestCookies { get; } = new();
public Dictionary<string, string?> RequestQuery { get; } = new(); public Dictionary<string, string?> RequestQuery { get; } = new();
@@ -32,7 +32,7 @@ public class FakeWebContext : IWebContext
public long? RequestContentLength { get; } public long? RequestContentLength { get; }
public byte[]? RequestReadBin() public byte[] RequestReadBin()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@@ -1,3 +1,4 @@
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes; using VAR.WebFormsCore.Tests.Fakes;
using Xunit; using Xunit;
@@ -31,7 +32,7 @@ public class FrmEchoTests
public void ProcessRequest__OneQueryParameterGet__FormData() public void ProcessRequest__OneQueryParameterGet__FormData()
{ {
FakeWebContext fakeWebContext = new(); FakeWebContext fakeWebContext = new();
fakeWebContext.RequestQuery.Add("Test", "Value"); fakeWebContext.RequestQuery.SafeSet("Test", "Value");
FrmEcho frmEcho = new(); FrmEcho frmEcho = new();
frmEcho.ProcessRequest(fakeWebContext); frmEcho.ProcessRequest(fakeWebContext);
@@ -53,7 +54,7 @@ public class FrmEchoTests
public void ProcessRequest__OneFormParameterPost__FormData() public void ProcessRequest__OneFormParameterPost__FormData()
{ {
FakeWebContext fakeWebContext = new(requestMethod: "POST"); FakeWebContext fakeWebContext = new(requestMethod: "POST");
fakeWebContext.RequestForm.Add("Test", "Value"); fakeWebContext.RequestForm.SafeSet("Test", "Value");
FrmEcho frmEcho = new(); FrmEcho frmEcho = new();
frmEcho.ProcessRequest(fakeWebContext); frmEcho.ProcessRequest(fakeWebContext);

View File

@@ -55,7 +55,7 @@ public class PageCommonTests
(GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetLoginHandler(loginHandler); (GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetLoginHandler(loginHandler);
(GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetAuthenticated(true); (GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetAuthenticated(true);
FakeWebContext fakeWebContext = new(requestMethod: "POST"); FakeWebContext fakeWebContext = new(requestMethod: "POST");
fakeWebContext.RequestForm.Add("ctl00_ctl02_btnLogout", "Logout"); fakeWebContext.RequestForm.SafeSet("ctl00_ctl02_btnLogout", "Logout");
TestEmptyForm testEmptyForm = new(mustBeAuthenticated: true); TestEmptyForm testEmptyForm = new(mustBeAuthenticated: true);
testEmptyForm.ProcessRequest(fakeWebContext); testEmptyForm.ProcessRequest(fakeWebContext);

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Text; using System.Text;
using VAR.Json; using VAR.Json;
@@ -11,23 +12,17 @@ public static class ExtensionMethods
{ {
if (context.RequestMethod == "POST") if (context.RequestMethod == "POST")
{ {
foreach (string key in context.RequestForm.Keys) if (context.RequestForm.ContainsKey(parameter))
{ {
if (string.IsNullOrEmpty(key) == false && key == parameter) return context.RequestForm.SafeGet(parameter, null) ?? string.Empty;
{
return context.RequestForm[key] ?? string.Empty;
}
} }
} }
foreach (string key in context.RequestQuery.Keys) if (context.RequestQuery.ContainsKey(parameter))
{ {
if (string.IsNullOrEmpty(key) == false && key == parameter) return context.RequestQuery.SafeGet(parameter, null) ?? string.Empty;
{
return context.RequestQuery[key] ?? string.Empty;
}
} }
return string.Empty; return string.Empty;
} }
@@ -42,4 +37,23 @@ public static class ExtensionMethods
} }
#endregion IWebContext #endregion IWebContext
#region Dictionary
public static TValue? SafeGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
return dictionary.TryGetValue(key, out TValue? value) ? value : defaultValue;
}
public static void SafeSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
dictionary[key] = value;
}
public static void SafeRemove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
dictionary.Remove(key);
}
#endregion Dictionary
} }

View File

@@ -7,8 +7,9 @@ public interface IWebContext
{ {
string RequestPath { get; } string RequestPath { get; }
string RequestMethod { get; } string RequestMethod { get; }
Dictionary<string, string?> RequestHeader { get; } Dictionary<string, string?> RequestHeader { get; }
Dictionary<string, string> RequestCookies { get; } Dictionary<string, string?> RequestCookies { get; }
Dictionary<string, string?> RequestQuery { get; } Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> RequestForm { get; } Dictionary<string, string?> RequestForm { get; }