Files
octokit.net/Octokit.Tests/Helpers/UriExtensionsTests.cs
Ryan Gribble 5e89232521 Release v0.25 - She'll be Comin' Round the Mountain (#1656)
* Run `build -Target FormatCode` to fixup whitespace etc

* Fix delete release asset integration test

* Fix repository fork test

* Fix pagination test for PR Review Request

* First cut of release notes

* update release notes

* Update release notes

* include links to contributors

* Add breaking changes/advisories section

* Tidy up formatting

* Tidy up wording
2017-08-23 21:27:15 +10:00

150 lines
5.3 KiB
C#

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", "foo val"},
{"bar", "barval"}
});
Assert.Equal(new Uri("https://example.com?foo=foo%20val&bar=barval"), uriWithParameters);
}
[Fact]
public void AppendsParametersAsQueryStringWithRelativeUri()
{
var uri = new Uri("issues", UriKind.Relative);
var uriWithParameters = uri.ApplyParameters(new Dictionary<string, string>
{
{"foo", "fooval"},
{"bar", "barval"}
});
Assert.Equal(new Uri("issues?foo=fooval&bar=barval", UriKind.Relative), uriWithParameters);
}
[Fact]
public void ThrowsExceptionWhenNullValueProvided()
{
var uri = new Uri("https://example.com");
var parameters = new Dictionary<string, string>
{
{"foo", null }
};
Assert.Throws<ArgumentNullException>(() => uri.ApplyParameters(parameters));
}
[Fact]
public void ThrowsExceptionWhenNullValueProvidedWithRelativeUri()
{
var uri = new Uri("api/example", UriKind.Relative);
var parameters = new Dictionary<string, string>
{
{"foo", null }
};
Assert.Throws<ArgumentNullException>(() => uri.ApplyParameters(parameters));
}
[Fact]
public void DoesNotChangeUrlWhenParametersEmpty()
{
var uri = new Uri("https://example.com");
var uriWithEmptyParameters = uri.ApplyParameters(new Dictionary<string, string>());
var uriWithNullParameters = uri.ApplyParameters(null);
Assert.Equal(uri, uriWithEmptyParameters);
Assert.Equal(uri, uriWithNullParameters);
}
[Fact]
public void DoesNotChangeUrlWhenParametersEmptyWithRelativeUri()
{
var uri = new Uri("api/example", UriKind.Relative);
var uriWithEmptyParameters = uri.ApplyParameters(new Dictionary<string, string>());
var uriWithNullParameters = uri.ApplyParameters(null);
Assert.Equal(uri, uriWithEmptyParameters);
Assert.Equal(uri, uriWithNullParameters);
}
[Fact]
public void CombinesExistingParametersWithNewParameters()
{
var uri = new Uri("https://api.github.com/repositories/1/milestones?state=closed&sort=due_date&direction=asc&page=2");
var parameters = new Dictionary<string, string> { { "state", "open" }, { "sort", "other" }, { "per_page", "5" } };
var actual = uri.ApplyParameters(parameters);
Assert.Equal(
new Uri("https://api.github.com/repositories/1/milestones?state=open&sort=other&per_page=5&direction=asc&page=2"),
actual);
}
[Fact]
public void CombinesExistingParametersWithNewParametersToRelativeUri()
{
var uri = new Uri("repositories/1/milestones?state=closed&sort=due_date&direction=asc&page=2", UriKind.Relative);
var parameters = new Dictionary<string, string> { { "state", "open" }, { "sort", "other" }, { "per_page", "5" } };
var actual = uri.ApplyParameters(parameters);
Assert.Equal(
new Uri("repositories/1/milestones?state=open&sort=other&per_page=5&direction=asc&page=2", UriKind.Relative),
actual);
}
[Fact]
public void DoesNotChangePassedInDictionary()
{
var uri = new Uri("https://api.github.com/repositories/1/milestones?state=closed&sort=due_date&direction=asc&page=2");
var parameters = new Dictionary<string, string> { { "state", "open" }, { "sort", "other" } };
uri.ApplyParameters(parameters);
Assert.Equal(2, parameters.Count);
}
[Fact]
public void DoesNotChangePassedInDictionaryForRelativeUri()
{
var uri = new Uri("/repositories/1/milestones?state=closed&sort=due_date&direction=asc&page=2", UriKind.Relative);
var parameters = new Dictionary<string, string> { { "state", "open" }, { "sort", "other" } };
uri.ApplyParameters(parameters);
Assert.Equal(2, parameters.Count);
}
[Fact]
public void EnsuresArgumentNotNull()
{
Uri uri = null;
Assert.Throws<ArgumentNullException>(() => uri.ApplyParameters(new Dictionary<string, string>()));
}
}
}
}