mirror of
https://github.com/dotnetcore/Util.git
synced 2026-10-29 11:36:32 +08:00
更新参数生成器
This commit is contained in:
@@ -362,6 +362,7 @@ https://github.com/dotnetcore/util/
|
||||
- 二维码操作 - 基于QRCoder [已发布]
|
||||
- 短信操作 -基于LuoSiMao [已发布]
|
||||
- 加密操作 [已发布]
|
||||
- Url参数生成器 [已发布]
|
||||
- 分层架构基类及组件
|
||||
- 实体基类 [已发布]
|
||||
- 聚合根基类 [已发布]
|
||||
@@ -426,3 +427,4 @@ https://github.com/dotnetcore/util/
|
||||
- 2017年10月21日,更新了Ui组件基类及TagHelper基类,图标组件(Util.Ui.Angular/Material/Icons)及单元测试。
|
||||
- 2017年10月25日,更新了二维码操作(Util.Tools.QrCode,基于QRCoder)和短信操作(Util.Tools.Sms,封装了LuoSiMao接口)。
|
||||
- 2017年10月29日,更新了加密操作(Util.Helpers.Encrypt,集成了MD5,DES,AES,RSA加密操作)及单元测试。
|
||||
- 2017年10月30日,更新了Url参数生成器(Util.Parameters)及单元测试。
|
||||
|
||||
@@ -74,4 +74,8 @@ Util是一个.net core平台下的应用框架,旨在提升小型团队的开
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Signatures\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Util.Parameters.Formats {
|
||||
/// <summary>
|
||||
/// 参数格式化器
|
||||
/// </summary>
|
||||
public interface IParameterFormat {
|
||||
/// <summary>
|
||||
/// 格式化
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
string Format( string key, string value );
|
||||
/// <summary>
|
||||
/// 连接参数
|
||||
/// </summary>
|
||||
/// <param name="left">左操作数</param>
|
||||
/// <param name="right">右操作数</param>
|
||||
string Join( string left, string right );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace Util.Parameters.Formats {
|
||||
/// <summary>
|
||||
/// 参数格式化器
|
||||
/// </summary>
|
||||
public abstract class ParameterFormatBase : IParameterFormat {
|
||||
/// <summary>
|
||||
/// 格式化
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public string Format( string key, string value ) {
|
||||
return $"{key}{FormatSeparator}{value}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化分割符
|
||||
/// </summary>
|
||||
protected abstract string FormatSeparator { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 连接参数
|
||||
/// </summary>
|
||||
/// <param name="left">左操作数</param>
|
||||
/// <param name="right">右操作数</param>
|
||||
public string Join( string left, string right ) {
|
||||
if ( string.IsNullOrWhiteSpace( left ) )
|
||||
return right;
|
||||
if ( string.IsNullOrWhiteSpace( right ) )
|
||||
return left;
|
||||
return $"{left}{JoinSeparator}{right}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接符
|
||||
/// </summary>
|
||||
protected abstract string JoinSeparator { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Util.Parameters.Formats {
|
||||
/// <summary>
|
||||
/// Url参数格式化器
|
||||
/// </summary>
|
||||
internal class UrlParameterFormat : ParameterFormatBase {
|
||||
/// <summary>
|
||||
/// Url参数格式化器实例
|
||||
/// </summary>
|
||||
public static readonly IParameterFormat Instance = new UrlParameterFormat();
|
||||
|
||||
/// <summary>
|
||||
/// 格式化分割符
|
||||
/// </summary>
|
||||
protected override string FormatSeparator => "=";
|
||||
|
||||
/// <summary>
|
||||
/// 连接符
|
||||
/// </summary>
|
||||
protected override string JoinSeparator => "&";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Util.Parameters.Formats;
|
||||
|
||||
namespace Util.Parameters {
|
||||
/// <summary>
|
||||
/// 参数生成器
|
||||
/// </summary>
|
||||
public class ParameterBuilder {
|
||||
/// <summary>
|
||||
/// 参数集合
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> _params;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化参数生成器
|
||||
/// </summary>
|
||||
public ParameterBuilder() {
|
||||
_params = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数,如果参数已存在则替换
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public ParameterBuilder Add( string key, object value ) {
|
||||
if( string.IsNullOrWhiteSpace( key ) )
|
||||
return this;
|
||||
string strValue = GetValue( value ).Trim();
|
||||
if( string.IsNullOrWhiteSpace( strValue ) )
|
||||
return this;
|
||||
key = key.Trim();
|
||||
if( _params.ContainsKey( key ) )
|
||||
_params[key] = strValue;
|
||||
else
|
||||
_params.Add( key, strValue );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
private string GetValue( object value ) {
|
||||
if( value is string )
|
||||
return value.ToString();
|
||||
if( value is DateTime dateTime )
|
||||
return dateTime.ToString( "yyyy-MM-dd HH:mm:ss" );
|
||||
if( value is bool boolValue )
|
||||
return boolValue.ToString().ToLower();
|
||||
return value.SafeString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典
|
||||
/// </summary>
|
||||
public IDictionary<string, string> GetDictionary() {
|
||||
return _params;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取键值对集合
|
||||
/// </summary>
|
||||
public IEnumerable<KeyValuePair<string, string>> GetKeyValuePairs() {
|
||||
return _params;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空
|
||||
/// </summary>
|
||||
public void Clear() {
|
||||
_params.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除参数
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
public bool Remove( string key ) {
|
||||
return _params.Remove( key );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取结果
|
||||
/// </summary>
|
||||
/// <param name="format">参数格式化器</param>
|
||||
/// <param name="isSort">是否按参数名排序</param>
|
||||
public string Result( IParameterFormat format, bool isSort = false ) {
|
||||
if( format == null )
|
||||
throw new ArgumentNullException( nameof( format ) );
|
||||
var result = string.Empty;
|
||||
foreach( var param in GetDictionary( isSort ) )
|
||||
result = format.Join( result, format.Format( param.Key, param.Value ) );
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典
|
||||
/// </summary>
|
||||
private IDictionary<string, string> GetDictionary( bool isSort ) {
|
||||
if( isSort == false )
|
||||
return _params;
|
||||
return new SortedDictionary<string, string>( _params );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
using Util.Parameters.Formats;
|
||||
|
||||
namespace Util.Parameters {
|
||||
/// <summary>
|
||||
/// Url参数生成器
|
||||
/// </summary>
|
||||
public class UrlParameterBuilder {
|
||||
/// <summary>
|
||||
/// 参数生成器
|
||||
/// </summary>
|
||||
private readonly ParameterBuilder _builder;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化Url参数生成器
|
||||
/// </summary>
|
||||
public UrlParameterBuilder() {
|
||||
_builder = new ParameterBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public UrlParameterBuilder Add( string key, object value ) {
|
||||
_builder.Add( key, value );
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典
|
||||
/// </summary>
|
||||
public IDictionary<string, string> GetDictionary() {
|
||||
return _builder.GetDictionary();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取键值对集合
|
||||
/// </summary>
|
||||
public IEnumerable<KeyValuePair<string, string>> GetKeyValuePairs() {
|
||||
return _builder.GetKeyValuePairs();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取结果,格式:参数名=参数值&参数名=参数值
|
||||
/// </summary>
|
||||
/// <param name="isSort">是否按参数名排序</param>
|
||||
public string Result( bool isSort = false ) {
|
||||
return _builder.Result( UrlParameterFormat.Instance, isSort );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接Url
|
||||
/// </summary>
|
||||
/// <param name="url">地址</param>
|
||||
public string JoinUrl( string url ) {
|
||||
return $"{GetUrl( url )}{Result()}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Url
|
||||
/// </summary>
|
||||
private string GetUrl( string url ) {
|
||||
if( !url.Contains( "?" ) )
|
||||
return $"{url}?";
|
||||
if( url.EndsWith( "?" ) )
|
||||
return url;
|
||||
if( url.EndsWith( "&" ) )
|
||||
return url;
|
||||
return $"{url}&";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空
|
||||
/// </summary>
|
||||
public void Clear() {
|
||||
_builder.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除参数
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
public bool Remove( string key ) {
|
||||
return _builder.Remove( key );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,90 +3,6 @@ using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Util.Webs.Clients {
|
||||
/// <summary>
|
||||
/// Http请求
|
||||
/// </summary>
|
||||
public interface IRequest<out TRequest> where TRequest : IRequest<TRequest> {
|
||||
/// <summary>
|
||||
/// 设置内容类型
|
||||
/// </summary>
|
||||
/// <param name="contentType">内容类型</param>
|
||||
TRequest ContentType( HttpContentType contentType );
|
||||
/// <summary>
|
||||
/// 设置内容类型
|
||||
/// </summary>
|
||||
/// <param name="contentType">内容类型</param>
|
||||
TRequest ContentType( string contentType );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="expiresDate">有效时间,单位:天</param>
|
||||
TRequest Cookie( string name, string value, double expiresDate );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="expiresDate">到期时间</param>
|
||||
TRequest Cookie( string name, string value, DateTime expiresDate );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="path">源服务器URL子集</param>
|
||||
/// <param name="domain">所属域</param>
|
||||
/// <param name="expiresDate">到期时间</param>
|
||||
TRequest Cookie( string name, string value, string path = "/", string domain = null, DateTime? expiresDate = null );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="cookie">cookie</param>
|
||||
TRequest Cookie( Cookie cookie );
|
||||
/// <summary>
|
||||
/// 超时时间
|
||||
/// </summary>
|
||||
/// <param name="timeout">超时时间,单位:秒</param>
|
||||
TRequest Timeout( int timeout );
|
||||
/// <summary>
|
||||
/// 请求头
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
TRequest Header<T>( string key, T value );
|
||||
/// <summary>
|
||||
/// 添加参数
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
TRequest Data<T>( string key, T value );
|
||||
/// <summary>
|
||||
/// 添加Json参数
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
TRequest JsonData<T>( T value );
|
||||
/// <summary>
|
||||
/// 请求失败回调函数
|
||||
/// </summary>
|
||||
/// <param name="action">执行失败的回调函数,参数为响应结果</param>
|
||||
TRequest OnFail( Action<string> action );
|
||||
/// <summary>
|
||||
/// 请求失败回调函数
|
||||
/// </summary>
|
||||
/// <param name="action">执行失败的回调函数,第一个参数为响应结果,第二个参数为状态码</param>
|
||||
TRequest OnFail( Action<string, HttpStatusCode> action );
|
||||
/// <summary>
|
||||
/// 获取结果
|
||||
/// </summary>
|
||||
string Result();
|
||||
/// <summary>
|
||||
/// 获取结果
|
||||
/// </summary>
|
||||
Task<string> ResultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Http请求
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Util.Webs.Clients {
|
||||
/// <summary>
|
||||
/// Http请求
|
||||
/// </summary>
|
||||
public interface IRequest<out TRequest> where TRequest : IRequest<TRequest> {
|
||||
/// <summary>
|
||||
/// 设置内容类型
|
||||
/// </summary>
|
||||
/// <param name="contentType">内容类型</param>
|
||||
TRequest ContentType( HttpContentType contentType );
|
||||
/// <summary>
|
||||
/// 设置内容类型
|
||||
/// </summary>
|
||||
/// <param name="contentType">内容类型</param>
|
||||
TRequest ContentType( string contentType );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="expiresDate">有效时间,单位:天</param>
|
||||
TRequest Cookie( string name, string value, double expiresDate );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="expiresDate">到期时间</param>
|
||||
TRequest Cookie( string name, string value, DateTime expiresDate );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="name">名称</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="path">源服务器URL子集</param>
|
||||
/// <param name="domain">所属域</param>
|
||||
/// <param name="expiresDate">到期时间</param>
|
||||
TRequest Cookie( string name, string value, string path = "/", string domain = null, DateTime? expiresDate = null );
|
||||
/// <summary>
|
||||
/// 设置Cookie
|
||||
/// </summary>
|
||||
/// <param name="cookie">cookie</param>
|
||||
TRequest Cookie( Cookie cookie );
|
||||
/// <summary>
|
||||
/// 超时时间
|
||||
/// </summary>
|
||||
/// <param name="timeout">超时时间,单位:秒</param>
|
||||
TRequest Timeout( int timeout );
|
||||
/// <summary>
|
||||
/// 请求头
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
TRequest Header<T>( string key, T value );
|
||||
/// <summary>
|
||||
/// 添加参数
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
TRequest Data<T>( string key, T value );
|
||||
/// <summary>
|
||||
/// 添加Json参数
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
TRequest JsonData<T>( T value );
|
||||
/// <summary>
|
||||
/// 请求失败回调函数
|
||||
/// </summary>
|
||||
/// <param name="action">执行失败的回调函数,参数为响应结果</param>
|
||||
TRequest OnFail( Action<string> action );
|
||||
/// <summary>
|
||||
/// 请求失败回调函数
|
||||
/// </summary>
|
||||
/// <param name="action">执行失败的回调函数,第一个参数为响应结果,第二个参数为状态码</param>
|
||||
TRequest OnFail( Action<string, HttpStatusCode> action );
|
||||
/// <summary>
|
||||
/// 获取结果
|
||||
/// </summary>
|
||||
string Result();
|
||||
/// <summary>
|
||||
/// 获取结果
|
||||
/// </summary>
|
||||
Task<string> ResultAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using Util.Parameters;
|
||||
using Util.Tests.Samples;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Parameters {
|
||||
/// <summary>
|
||||
/// 参数生成器测试
|
||||
/// </summary>
|
||||
public class ParameterBuilderTest {
|
||||
/// <summary>
|
||||
/// 参数生成器
|
||||
/// </summary>
|
||||
private readonly ParameterBuilder _dictionary;
|
||||
|
||||
/// <summary>
|
||||
/// 测试初始化
|
||||
/// </summary>
|
||||
public ParameterBuilderTest() {
|
||||
_dictionary = new ParameterBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证key为空
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData( null )]
|
||||
[InlineData( "" )]
|
||||
[InlineData( " " )]
|
||||
public void TestAdd_KeyIsEmpty( string key ) {
|
||||
Assert.Empty( _dictionary.Add( key, "b" ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证value为空
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData( null )]
|
||||
[InlineData( "" )]
|
||||
[InlineData( " " )]
|
||||
public void TestAdd_ValueIsEmpty( string value ) {
|
||||
Assert.Empty( _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去除键两端空格
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_TrimKey() {
|
||||
Assert.Equal( "a:1", _dictionary.Add( " a ", "1" ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去除值两端空格
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_TrimValue() {
|
||||
Assert.Equal( "a:1", _dictionary.Add( "a", " 1 " ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加字符串参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_String() {
|
||||
Assert.Equal( "a:1", _dictionary.Add( "a", "1" ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加2个字符串参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_String_2() {
|
||||
Assert.Equal( "a:1|b:2", _dictionary.Add( "a", " 1" ).Add( "b ", "2 " ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加日期参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_DateTime() {
|
||||
DateTime value = new DateTime( 2000, 10, 10, 10, 10, 10 );
|
||||
Assert.Equal( "a:2000-10-10 10:10:10", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加日期参数 - 可空
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_DateTime_Nullable() {
|
||||
DateTime? value = new DateTime( 2000, 10, 10, 10, 10, 10 );
|
||||
Assert.Equal( "a:2000-10-10 10:10:10", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加日期参数 - 可空 - 传入null
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_DateTime_Nullable_Null() {
|
||||
DateTime? value = null;
|
||||
Assert.Empty( _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加整型参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Int() {
|
||||
int value = 1;
|
||||
Assert.Equal( "a:1", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加整型参数 - 可空
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Int_Nullable() {
|
||||
int? value = 1;
|
||||
Assert.Equal( "a:1", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加整型参数 - 可空 - 传入null
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Int_Nullable_Null() {
|
||||
int? value = null;
|
||||
Assert.Empty( _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加布尔参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Bool() {
|
||||
bool value = true;
|
||||
Assert.Equal( "a:true", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加布尔参数 - 可空
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Bool_Nullable() {
|
||||
bool? value = false;
|
||||
Assert.Equal( "a:false", _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加布尔参数 - 可空 - 传入null
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Bool_Nullable_Null() {
|
||||
bool? value = null;
|
||||
Assert.Empty( _dictionary.Add( "a", value ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 已添加参数则覆盖
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_Exist() {
|
||||
Assert.Equal( "a:2", _dictionary.Add( "a", "1" ).Add( "a", "2" ).Result( new ParameterFormatterSample() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Util.Parameters;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Tests.Parameters {
|
||||
/// <summary>
|
||||
/// Url参数生成器测试
|
||||
/// </summary>
|
||||
public class UrlParameterBuilderTest {
|
||||
/// <summary>
|
||||
/// Url参数生成器
|
||||
/// </summary>
|
||||
private readonly UrlParameterBuilder _builder;
|
||||
|
||||
/// <summary>
|
||||
/// 测试初始化
|
||||
/// </summary>
|
||||
public UrlParameterBuilderTest() {
|
||||
_builder = new UrlParameterBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试默认值
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestDefault() {
|
||||
Assert.Empty( _builder.Result() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_1() {
|
||||
Assert.Equal( "a=b", _builder.Add( "a", "b" ).Result() );
|
||||
Assert.Equal( "a=b&c=d", _builder.Add( "c", "d" ).Result() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数 - 测试空格
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestAdd_2() {
|
||||
Assert.Equal( "b=2&a=1", _builder.Add( " b ", " 2 " ).Add( " a ", " 1 " ).Result() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数 - 测试排序
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestResult_Sort() {
|
||||
Assert.Equal( "a=1&b=2", _builder.Add( "b", "2" ).Add( "a", "1" ).Result( true ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接URL
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData( "http://test.com", "http://test.com?a=1&b=2" )]
|
||||
[InlineData( "http://test.com?", "http://test.com?a=1&b=2" )]
|
||||
[InlineData( "http://test.com?c=3", "http://test.com?c=3&a=1&b=2" )]
|
||||
[InlineData( "http://test.com?c=3&", "http://test.com?c=3&a=1&b=2" )]
|
||||
public void TestJoinUrl( string url, string result ) {
|
||||
Assert.Equal( result, _builder.Add( "a", "1" ).Add( "b", "2" ).JoinUrl( url ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Util.Parameters.Formats;
|
||||
|
||||
namespace Util.Tests.Samples {
|
||||
/// <summary>
|
||||
/// 参数格式化器样例
|
||||
/// </summary>
|
||||
public class ParameterFormatterSample : ParameterFormatBase {
|
||||
/// <summary>
|
||||
/// 格式化分割符
|
||||
/// </summary>
|
||||
protected override string FormatSeparator => ":";
|
||||
|
||||
/// <summary>
|
||||
/// 连接符
|
||||
/// </summary>
|
||||
protected override string JoinSeparator => "|";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user