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:
@@ -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
|
||||
{
|
||||
if (_requestCookies == null)
|
||||
{
|
||||
_requestCookies = _context.Request.Cookies
|
||||
.ToDictionary(p => p.Key, p => p.Value);
|
||||
.ToDictionary(p => p.Key, p => (string?)p.Value);
|
||||
}
|
||||
|
||||
return _requestCookies;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ExtensionMethodsTests
|
||||
FakeWebContext fakeWebContext = new();
|
||||
string key = "Key";
|
||||
string value = "Value";
|
||||
fakeWebContext.RequestQuery.Add(key, value);
|
||||
fakeWebContext.RequestQuery.SafeSet(key, value);
|
||||
|
||||
string result = fakeWebContext.GetRequestParameter(key);
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ExtensionMethodsTests
|
||||
FakeWebContext fakeWebContext = new(requestMethod: "POST");
|
||||
string key = "Key";
|
||||
string value = "Value";
|
||||
fakeWebContext.RequestForm.Add(key, value);
|
||||
fakeWebContext.RequestForm.SafeSet(key, value);
|
||||
|
||||
string result = fakeWebContext.GetRequestParameter(key);
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ExtensionMethodsTests
|
||||
string keyInvalid = "KeyInvalid";
|
||||
string key = "Key";
|
||||
string value = "Value";
|
||||
fakeWebContext.RequestQuery.Add(keyInvalid, value);
|
||||
fakeWebContext.RequestQuery.SafeSet(keyInvalid, value);
|
||||
|
||||
string result = fakeWebContext.GetRequestParameter(key);
|
||||
|
||||
@@ -82,7 +82,7 @@ public class ExtensionMethodsTests
|
||||
string keyInvalid = "KeyInvalid";
|
||||
string key = "Key";
|
||||
string value = "Value";
|
||||
fakeWebContext.RequestForm.Add(keyInvalid, value);
|
||||
fakeWebContext.RequestForm.SafeSet(keyInvalid, value);
|
||||
|
||||
string result = fakeWebContext.GetRequestParameter(key);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Controls;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
@@ -56,7 +57,7 @@ public class ButtonTests
|
||||
button.Click += (o, _) => { result = (o as Button)?.CommandArgument; };
|
||||
page.Controls.Add(button);
|
||||
|
||||
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
|
||||
fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
|
||||
Assert.Equal(commandArgument, result);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Controls;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
@@ -94,7 +95,7 @@ public class CTextBoxTests
|
||||
page0.ProcessRequest(fakeWebContext0);
|
||||
|
||||
FakeWebContext fakeWebContext1 = new(requestMethod: "POST");
|
||||
fakeWebContext1.RequestForm.Add(cTextBox0.TxtContent.ClientID, changedValue);
|
||||
fakeWebContext1.RequestForm.SafeSet(cTextBox0.TxtContent.ClientID, changedValue);
|
||||
Page page1 = new();
|
||||
CTextBox cTextBox1 = new() { Text = text };
|
||||
page1.Controls.Add(cTextBox1);
|
||||
@@ -188,7 +189,7 @@ public class CTextBoxTests
|
||||
};
|
||||
page.Controls.Add(button);
|
||||
|
||||
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
|
||||
fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
int? resultHeight = cTextBox.GetClientsideHeight();
|
||||
|
||||
@@ -222,7 +223,7 @@ public class CTextBoxTests
|
||||
button.Click += (_, _) => { cTextBox.SetClientsideHeight(null); };
|
||||
page.Controls.Add(button);
|
||||
|
||||
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
|
||||
fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
int? resultHeight = cTextBox.GetClientsideHeight();
|
||||
|
||||
@@ -259,7 +260,7 @@ public class CTextBoxTests
|
||||
};
|
||||
page.Controls.Add(button);
|
||||
|
||||
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
|
||||
fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
int? resultHeight = cTextBox.GetClientsideHeight();
|
||||
|
||||
@@ -286,7 +287,7 @@ public class CTextBoxTests
|
||||
};
|
||||
page.Controls.Add(button);
|
||||
|
||||
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
|
||||
fakeWebContext.RequestForm.SafeSet(button.ClientID, "Clicked");
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
int? resultHeight = cTextBox.GetClientsideHeight();
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Controls;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
@@ -51,7 +52,7 @@ public class HiddenFieldTests
|
||||
HiddenField hiddenField = new() { Value = value };
|
||||
page.Controls.Add(hiddenField);
|
||||
|
||||
fakeWebContext.RequestForm.Add(hiddenField.ClientID, changedValue);
|
||||
fakeWebContext.RequestForm.SafeSet(hiddenField.ClientID, changedValue);
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
|
||||
Assert.Equal(changedValue, hiddenField.Value);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Controls;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
@@ -27,7 +28,7 @@ public class HtmlFormTests
|
||||
public void MustRenderCorrectly__WithQueryParameters()
|
||||
{
|
||||
FakeWebContext fakeWebContext = new();
|
||||
fakeWebContext.RequestQuery.Add("test", "value");
|
||||
fakeWebContext.RequestQuery.SafeSet("test", "value");
|
||||
Page page = new();
|
||||
HtmlForm htmlForm = new();
|
||||
page.Controls.Add(htmlForm);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Controls;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
@@ -138,7 +139,7 @@ public class TextBoxTests
|
||||
TextBox textBox = new() { Text = text };
|
||||
page.Controls.Add(textBox);
|
||||
|
||||
fakeWebContext.RequestForm.Add(textBox.ClientID, changedValue);
|
||||
fakeWebContext.RequestForm.SafeSet(textBox.ClientID, changedValue);
|
||||
page.ProcessRequest(fakeWebContext);
|
||||
|
||||
Assert.Equal(changedValue, textBox.Text);
|
||||
|
||||
@@ -22,7 +22,7 @@ public class FakeWebContext : IWebContext
|
||||
|
||||
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();
|
||||
|
||||
@@ -32,7 +32,7 @@ public class FakeWebContext : IWebContext
|
||||
|
||||
public long? RequestContentLength { get; }
|
||||
|
||||
public byte[]? RequestReadBin()
|
||||
public byte[] RequestReadBin()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
using VAR.WebFormsCore.Tests.Fakes;
|
||||
using Xunit;
|
||||
@@ -31,7 +32,7 @@ public class FrmEchoTests
|
||||
public void ProcessRequest__OneQueryParameterGet__FormData()
|
||||
{
|
||||
FakeWebContext fakeWebContext = new();
|
||||
fakeWebContext.RequestQuery.Add("Test", "Value");
|
||||
fakeWebContext.RequestQuery.SafeSet("Test", "Value");
|
||||
FrmEcho frmEcho = new();
|
||||
|
||||
frmEcho.ProcessRequest(fakeWebContext);
|
||||
@@ -53,7 +54,7 @@ public class FrmEchoTests
|
||||
public void ProcessRequest__OneFormParameterPost__FormData()
|
||||
{
|
||||
FakeWebContext fakeWebContext = new(requestMethod: "POST");
|
||||
fakeWebContext.RequestForm.Add("Test", "Value");
|
||||
fakeWebContext.RequestForm.SafeSet("Test", "Value");
|
||||
FrmEcho frmEcho = new();
|
||||
|
||||
frmEcho.ProcessRequest(fakeWebContext);
|
||||
|
||||
@@ -55,7 +55,7 @@ public class PageCommonTests
|
||||
(GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetLoginHandler(loginHandler);
|
||||
(GlobalConfig.Get() as FakeGlobalConfig)?.FakeSetAuthenticated(true);
|
||||
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.ProcessRequest(fakeWebContext);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using VAR.Json;
|
||||
|
||||
@@ -11,21 +12,15 @@ public static class ExtensionMethods
|
||||
{
|
||||
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[key] ?? string.Empty;
|
||||
}
|
||||
return context.RequestForm.SafeGet(parameter, null) ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string key in context.RequestQuery.Keys)
|
||||
if (context.RequestQuery.ContainsKey(parameter))
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) == false && key == parameter)
|
||||
{
|
||||
return context.RequestQuery[key] ?? string.Empty;
|
||||
}
|
||||
return context.RequestQuery.SafeGet(parameter, null) ?? string.Empty;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
@@ -42,4 +37,23 @@ public static class ExtensionMethods
|
||||
}
|
||||
|
||||
#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
|
||||
}
|
||||
@@ -7,8 +7,9 @@ public interface IWebContext
|
||||
{
|
||||
string RequestPath { get; }
|
||||
string RequestMethod { get; }
|
||||
|
||||
Dictionary<string, string?> RequestHeader { get; }
|
||||
Dictionary<string, string> RequestCookies { get; }
|
||||
Dictionary<string, string?> RequestCookies { get; }
|
||||
Dictionary<string, string?> RequestQuery { get; }
|
||||
Dictionary<string, string?> RequestForm { get; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user