File tree Expand file tree Collapse file tree 5 files changed +91
-4
lines changed Expand file tree Collapse file tree 5 files changed +91
-4
lines changed Original file line number Diff line number Diff line change 1+ name : Build and Test Pull Requests
2+
3+ on :
4+ pull_request :
5+ branches : [ "main" ]
6+
7+ jobs :
8+ build :
9+ name : Build and Test
10+
11+ runs-on : ubuntu-latest
12+
13+ strategy :
14+ matrix :
15+ os : [ubuntu-latest, windows-latest, macos-latest]
16+
17+ steps :
18+ - uses : actions/checkout@v3
19+ - name : Setup .NET (5.0.x)
20+ uses : actions/setup-dotnet@v3
21+ with :
22+ dotnet-version : 5.0.x
23+ - name : Display Available .NET SDKs
24+ run : dotnet --list-sdks
25+ - name : Restore dependencies
26+ run : dotnet restore ./src
27+ - name : Build
28+ run : dotnet build ./src --no-restore
29+ - name : Test
30+ run : dotnet test ./src --no-build --verbosity normal
Original file line number Diff line number Diff line change 1+ using Shouldly ;
2+
3+ namespace FluentHttpClient . Tests ;
4+
5+ public class QueryParamsTests
6+ {
7+ [ Fact ]
8+ public void DoesNotRemoveEmptyValues ( )
9+ {
10+ FluentHttpClient . RemoveEmptyQueryParameters = false ;
11+
12+ var expected = "?name=bob&age=&color=blue" ;
13+
14+ var qp = new QueryParams
15+ {
16+ { "name" , "bob" } ,
17+ { "age" , "" } ,
18+ { "color" , "blue" }
19+ } ;
20+
21+ var actual = qp . ToString ( ) ;
22+ actual . ShouldBe ( expected ) ;
23+ }
24+
25+ [ Fact ]
26+ public void RemovesEmptyValues ( )
27+ {
28+ FluentHttpClient . RemoveEmptyQueryParameters = true ;
29+
30+ var expected = "?name=bob&color=blue" ;
31+
32+ var qp = new QueryParams
33+ {
34+ { "name" , "bob" } ,
35+ { "age" , "" } ,
36+ { "color" , "blue" }
37+ } ;
38+
39+ var actual = qp . ToString ( ) ;
40+ actual . ShouldBe ( expected ) ;
41+ }
42+ }
Original file line number Diff line number Diff line change @@ -10,4 +10,9 @@ public static class FluentHttpClient
1010 PropertyNameCaseInsensitive = true ,
1111 DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
1212 } ;
13+
14+ /// <summary>
15+ /// When true, query parameters that are null or empty will not be included when converted to a string.
16+ /// </summary>
17+ public static bool RemoveEmptyQueryParameters { get ; set ; } = false ;
1318}
Original file line number Diff line number Diff line change 1414 <PackageTags >fluent httpclient rest http api web client</PackageTags >
1515 <AssemblyVersion >$(Version)</AssemblyVersion >
1616 <FileVersion >$(Version)</FileVersion >
17- <Version >3.0.2 </Version >
17+ <Version >3.0.3 </Version >
1818 <GeneratePackageOnBuild >true</GeneratePackageOnBuild >
1919 <PackageReadmeFile >README.md</PackageReadmeFile >
2020 <PackageLicenseFile >LICENSE</PackageLicenseFile >
Original file line number Diff line number Diff line change @@ -9,8 +9,18 @@ public class QueryParams : NameValueCollection
99{
1010 public override string ToString ( )
1111 {
12- return HasKeys ( )
13- ? string . Empty
14- : "?" + string . Join ( "&" , ( from key in AllKeys let value = Get ( key ) select Uri . EscapeDataString ( key ) + "=" + Uri . EscapeDataString ( value ) ) . ToArray ( ) ) ;
12+ if ( ! HasKeys ( ) ) return string . Empty ;
13+
14+ var result = (
15+ from key in AllKeys
16+ let value = Get ( key )
17+ where (
18+ ! FluentHttpClient . RemoveEmptyQueryParameters
19+ || ! string . IsNullOrWhiteSpace ( value )
20+ )
21+ select $ "{ Uri . EscapeDataString ( key ) } ={ Uri . EscapeDataString ( value ) } "
22+ ) . ToArray ( ) ;
23+
24+ return $ "?{ string . Join ( "&" , result ) } ";
1525 }
1626}
You can’t perform that action at this time.
0 commit comments