mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-06 03:55:55 +00:00
Add method to supply query params to URI
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace Octokit.Tests.Helpers
|
||||
{
|
||||
public class UriExtensionsTests
|
||||
{
|
||||
public class TheApplyParametersMethod
|
||||
{
|
||||
[Fact]
|
||||
public void AppendsParametersAsQueryString()
|
||||
{
|
||||
var uri = new Uri("https://example.com");
|
||||
|
||||
var uriWithParameters = uri.ApplyParameters(new Dictionary<string, string>
|
||||
{
|
||||
{"foo", "fooval"},
|
||||
{"bar", "barval"}
|
||||
});
|
||||
|
||||
Assert.Equal(new Uri("https://example.com?foo=fooval&bar=barval"), uriWithParameters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverwritesExistingParameters()
|
||||
{
|
||||
var uri = new Uri("https://example.com?crap=crapola");
|
||||
|
||||
var uriWithParameters = uri.ApplyParameters(new Dictionary<string, string>
|
||||
{
|
||||
{"foo", "fooval"},
|
||||
{"bar", "barval"}
|
||||
});
|
||||
|
||||
Assert.Equal(new Uri("https://example.com?foo=fooval&bar=barval"), uriWithParameters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DoesNotChangeUrlWhenParametersIsNullOrEmpty()
|
||||
{
|
||||
var uri = new Uri("https://example.com");
|
||||
|
||||
var uriWithNullParameters = uri.ApplyParameters(null);
|
||||
var uriWithEmptyParameters = uri.ApplyParameters(new Dictionary<string, string>());
|
||||
|
||||
Assert.Same(uri, uriWithNullParameters);
|
||||
Assert.Equal(uri, uriWithEmptyParameters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnsuresUriNotNull()
|
||||
{
|
||||
Uri uri = null;
|
||||
Assert.Throws<ArgumentNullException>(() => uri.ApplyParameters(new Dictionary<string, string>()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Authentication\CredentialsTests.cs" />
|
||||
<Compile Include="Clients\SshKeysClientTests.cs" />
|
||||
<Compile Include="Helpers\UriExtensionsTests.cs" />
|
||||
<Compile Include="Http\ApiConnectionTests.cs" />
|
||||
<Compile Include="Clients\AuthorizationsClientTests.cs" />
|
||||
<Compile Include="Clients\AutoCompleteClientTests.cs" />
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
public static class UriExtensions
|
||||
{
|
||||
public static Uri ApplyParameters(this Uri uri, IDictionary<string, string> parameters)
|
||||
{
|
||||
Ensure.ArgumentNotNull(uri, "uri");
|
||||
|
||||
if (parameters == null) return uri;
|
||||
|
||||
var uriBuilder = new UriBuilder(uri)
|
||||
{
|
||||
Query = String.Join("&", parameters.Select(kvp => kvp.Key + "=" + kvp.Value))
|
||||
};
|
||||
return uriBuilder.Uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
<Compile Include="Authentication\Authenticator.cs" />
|
||||
<Compile Include="Clients\SshKeysClient.cs" />
|
||||
<Compile Include="Helpers\CollectionExtensions.cs" />
|
||||
<Compile Include="Helpers\UriExtensions.cs" />
|
||||
<Compile Include="Http\ApiConnection.cs" />
|
||||
<Compile Include="Http\IApiConnection.cs" />
|
||||
<Compile Include="Http\IHttpClient.cs" />
|
||||
|
||||
@@ -116,6 +116,7 @@
|
||||
<Compile Include="Clients\SshKeysClient.cs" />
|
||||
<Compile Include="Clients\UsersClient.cs" />
|
||||
<Compile Include="Helpers\CollectionExtensions.cs" />
|
||||
<Compile Include="Helpers\UriExtensions.cs" />
|
||||
<Compile Include="Http\ApiConnection.cs" />
|
||||
<Compile Include="Http\ApiResponse.cs" />
|
||||
<Compile Include="Http\Credentials.cs" />
|
||||
|
||||
Reference in New Issue
Block a user