Skip to content

Commit

Permalink
Add double? overload to QueryStringBuilderExtensions; Update Http…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
Cryptoc1 committed Jul 19, 2024
1 parent 8cefe11 commit dd182af
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Http/ObjectPoolProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static ObjectPool<QueryStringBuilder> CreateQueryStringBuilderPool( this
public sealed class PooledQueryStringBuilderPolicy : PooledObjectPolicy<QueryStringBuilder>
{
/// <summary> The initial capacity of pooled instances. </summary>
public int InitialCapacity { get; set; } = 256;
public int InitialCapacity { get; set; } = sizeof( char ) * 128;

/// <summary> The maximum capacity of pooled instances. </summary>
public int MaximumRetainedCapacity { get; set; } = 4096;
public int MaximumRetainedCapacity { get; set; } = sizeof( char ) * 2048;

/// <inheritdoc/>
public override QueryStringBuilder Create( ) => new( InitialCapacity );
Expand Down
20 changes: 20 additions & 0 deletions src/Http/QueryStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class QueryStringBuilder
/// <summary> Get or set the suggested size of the instance. </summary>
public int Capacity { get => builder.Capacity; set => builder.Capacity = value; }

internal int Length { get => builder.Length; set => builder.Length = value; }

/// <summary> Create an empty query string. </summary>
public QueryStringBuilder( )
{
Expand Down Expand Up @@ -140,6 +142,24 @@ public static QueryStringBuilder Append( this QueryStringBuilder builder, string
return builder;
}

/// <summary> Appends a parameter with the given <paramref name="name"/> and <paramref name="value"/>. </summary>
/// <param name="builder"> The builder to be mutated </param>
/// <param name="name"> The name of the parameter. </param>
/// <param name="value"> The value of the parameter. </param>
/// <returns> The mutated query string. </returns>
public static QueryStringBuilder Append( this QueryStringBuilder builder, string name, double? value )
{
ArgumentNullException.ThrowIfNull( builder );
ArgumentException.ThrowIfNullOrWhiteSpace( name );

if( value.HasValue )
{
return builder.Append( name, value.Value.ToString( CultureInfo.InvariantCulture ) );
}

return builder;
}

/// <summary> Appends a parameter with the given <paramref name="name"/> and <paramref name="value"/>. </summary>
/// <param name="builder"> The builder to be mutated </param>
/// <param name="name"> The name of the parameter. </param>
Expand Down
13 changes: 13 additions & 0 deletions test/Http/PooledQueryStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ public void Policy_DoesNot_Return_BuildersExceedingCapacity( )
Assert.False( policy.Return( builder ) );
}

[Fact( DisplayName = "Policy: returns builder to pool" )]
public void Policy_Returns_BuilderToPool( )
{
var policy = new PooledQueryStringBuilderPolicy();
var builder = policy.Create()
.Append( "test", "123456789" );

Assert.True( policy.Return( builder ) );

// NOTE: policy should clear the build
Assert.Equal( 1, builder.Length );
}

[Fact( DisplayName = "Policy: uses initial capacity" )]
public void Policy_Uses_InitialCapacity( )
{
Expand Down
21 changes: 21 additions & 0 deletions test/Http/QueryStringBuilderExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Globalization;

namespace ESCd.Extensions.Http.Tests;

public sealed class QueryStringBuilerExtensionTests
Expand Down Expand Up @@ -58,6 +60,25 @@ public void Append_DateTimeOffset_DoesNot_AppendNull( )
Assert.Empty( builder.ToString() );
}

[Fact( DisplayName = "Append (double): appends double string" )]
public void Append_Double_AppendsDoubleString( )
{
var builder = new QueryStringBuilder()
.Append( "test", double.MaxValue );

Assert.Equal( $"?test={Uri.EscapeDataString( double.MaxValue.ToString( CultureInfo.InvariantCulture ) )}", builder.ToString() );
}

[Fact( DisplayName = "Append (double?): does not append null" )]
public void Append_Double_DoesNot_AppendNull( )
{
var builder = new QueryStringBuilder()
.Append( "test", default( double? ) );

Assert.Empty( builder.ToString() );
}


[Fact( DisplayName = "Append (int): appends int string" )]
public void Append_Int_AppendsIntString( )
{
Expand Down
7 changes: 7 additions & 0 deletions test/Http/QueryStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public void Builder_Can_BeImplicityCastToString( )
Assert.Empty( value );
}

[Fact( DisplayName = "Builder: uses given Uri's query string" )]
public void Builder_Uses_UriQueryString( )
{
var query = new QueryStringBuilder( new Uri( "https://example.net?test=true" ) );
Assert.Equal( "?test=true", query.ToString() );
}

[Fact( DisplayName = "Clear: resets builder to empty" )]
public void Clear_ResetsBuilderToEmpty( )
{
Expand Down

0 comments on commit dd182af

Please sign in to comment.