This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-sonarcloud-scanning
- Loading branch information
Showing
17 changed files
with
571 additions
and
362 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,8 @@ | |
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Extensions\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
57 changes: 57 additions & 0 deletions
57
Data.TRAMS.Tests/Extensions/DictionaryQuerystringExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Generic; | ||
using Data.TRAMS.ExtensionMethods; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Data.TRAMS.Tests.Extensions | ||
{ | ||
public class DictionaryQuerystringExtensionsTests | ||
{ | ||
private static readonly IDictionary<string, string> Parameters = new Dictionary<string, string> | ||
{ | ||
{ "first", "v1" }, | ||
{ "second", "v2" } | ||
}; | ||
|
||
[Fact] | ||
public void Should_convert_string_pairs_to_a_querystring() | ||
{ | ||
Parameters.ToQueryString().Should().Be("?first=v1&second=v2"); | ||
} | ||
|
||
[Fact] | ||
public void Should_optionally_prefix_with_a_question_mark() | ||
{ | ||
Parameters.ToQueryString(prefix: false).StartsWith('?').Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void Should_produce_an_empty_string_if_no_parameters_are_provided() | ||
{ | ||
new Dictionary<string, string>().ToQueryString().Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void Should_cope_with_null_values() | ||
{ | ||
var withNullValue = new Dictionary<string, string> | ||
{ | ||
{ "key", null } | ||
}.ToQueryString(); | ||
|
||
withNullValue.Should().Be("?key="); | ||
} | ||
|
||
[Fact] | ||
public void Should_optionally_skip_parameters_with_null_values() | ||
{ | ||
var skippedEmpty = new Dictionary<string, string>(Parameters) | ||
{ | ||
{ "key", null } | ||
}.ToQueryString(keepEmpty: false); | ||
|
||
skippedEmpty.Should().NotContain("key="); | ||
skippedEmpty.Should().Be("?first=v1&second=v2"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Data.TRAMS/ExtensionMethods/DictionaryQuerystringExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace Data.TRAMS.ExtensionMethods | ||
{ | ||
public static class DictionaryQuerystringExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a <see cref="IDictionary{string, string}" /> to an encoded querystring. | ||
/// </summary> | ||
/// <param name="parameters"> | ||
/// An <see cref="IDictionary{string, string}" /> containing parameter names (of | ||
/// <see cref="string" />) and values (of <see cref="string" />) | ||
/// </param> | ||
/// <param name="prefix">A <see cref="bool" /> defining whether the querystring should have a '?' prefix (default: true)</param> | ||
/// <param name="keepEmpty"> | ||
/// A <see cref="bool" /> defining whether keys with null/empty values should be kept (default: | ||
/// true) | ||
/// </param> | ||
/// <returns>A string representing the parameters combined, UrlEncoded and (optionally) prefixed ready to be used in a URI</returns> | ||
public static string ToQueryString(this IDictionary<string, string> parameters, bool prefix = true, | ||
bool keepEmpty = true) | ||
{ | ||
IList<string> parameterPairs = parameters | ||
.Where(x => keepEmpty || string.IsNullOrWhiteSpace(x.Value) is false) | ||
.Select(x => $"{Encode(x.Key)}={Encode(x.Value)}") | ||
.ToList(); | ||
|
||
return parameterPairs.Count > 0 | ||
? $"{(prefix ? "?" : string.Empty)}{string.Join("&", parameterPairs)}" | ||
: string.Empty; | ||
|
||
string Encode(string x) | ||
{ | ||
return string.IsNullOrWhiteSpace(x) ? string.Empty : UrlEncoder.Default.Encode(x); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.