diff --git a/Octokit.Tests/Helpers/StringExtensionsTests.cs b/Octokit.Tests/Helpers/StringExtensionsTests.cs index f342199ef9..e4687019db 100644 --- a/Octokit.Tests/Helpers/StringExtensionsTests.cs +++ b/Octokit.Tests/Helpers/StringExtensionsTests.cs @@ -89,5 +89,30 @@ public void EscapeDoubleQuotesEscapesAllDoubleQuotes() Assert.Equal("\\\"test milestone\\\"", input.EscapeDoubleQuotes()); } } + + public class EncodeSharpMethod + { + [Fact] + public void EncodeSharpReturnsNullForNullInput() + { + Assert.Equal(string.Empty, (null as string).EncodeSharp()); + } + + [Fact] + public void EncodeSharpReturnsInputWithoutDoubleQuotes() + { + string input = "some test input without double quotes in it"; + + Assert.Equal(input, input.EncodeSharp()); + } + + [Fact] + public void EncodeAllSharp() + { + string input = "#some test input with # in it#"; + + Assert.Equal("%23some test input with %23 in it%23", input.EncodeSharp()); + } + } } } diff --git a/Octokit/Helpers/StringExtensions.cs b/Octokit/Helpers/StringExtensions.cs index 42ed83361f..e3d288acaf 100644 --- a/Octokit/Helpers/StringExtensions.cs +++ b/Octokit/Helpers/StringExtensions.cs @@ -23,8 +23,9 @@ public static bool IsNotBlank(this string value) public static Uri FormatUri(this string pattern, params object[] args) { Ensure.ArgumentNotNullOrEmptyString(pattern, nameof(pattern)); + var uriString = string.Format(CultureInfo.InvariantCulture, pattern, args).EncodeSharp(); - return new Uri(string.Format(CultureInfo.InvariantCulture, pattern, args), UriKind.Relative); + return new Uri(uriString, UriKind.Relative); } public static string UriEncode(this string input) @@ -106,6 +107,11 @@ internal static string EscapeDoubleQuotes(this string value) return value; } + internal static string EncodeSharp(this string value) + { + return !string.IsNullOrEmpty(value) ? value?.Replace("#", "%23") : string.Empty; + } + static IEnumerable SplitUpperCase(this string source) { Ensure.ArgumentNotNullOrEmptyString(source, nameof(source));