Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Octokit.Tests/Helpers/UriExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,64 @@ public void EnsuresArgumentNotNull()
Uri uri = null;
Assert.Throws<ArgumentNullException>(() => uri.ApplyParameters(new Dictionary<string, string>()));
}

[Fact]
public void AppendsStartPageIfNone()
{
var uri = new Uri("https://api.github.com/repositories/1/labels");

var parameters = new Dictionary<string, string>();
Pagination.Setup(parameters, new ApiOptions { StartPage = 3, PageSize = 7 });

var actual = uri.ApplyParameters(parameters);

Assert.Equal(
new Uri("https://api.github.com/repositories/1/labels?per_page=7&page=3"),
actual);
}

[Fact]
public void AppendsStartPageIfNoneForRelativeUri()
{
var uri = new Uri("/repositories/1/labels", UriKind.Relative);

var parameters = new Dictionary<string, string>();
Pagination.Setup(parameters, new ApiOptions { StartPage = 3, PageSize = 7 });

var actual = uri.ApplyParameters(parameters);

Assert.Equal(
new Uri("/repositories/1/labels?per_page=7&page=3", UriKind.Relative),
actual);
}

[Fact]
public void DoesNotChangePageNumberInNextPageUriWithStartPage()
{
const string nextPageUriString = "https://api.github.com/repositories/1/labels?per_page=5&page=2";
var nextPageUri = new Uri(nextPageUriString);

var parameters = new Dictionary<string, string>();
Pagination.Setup(parameters, new ApiOptions { StartPage = 1, PageSize = 5 });

var actual = nextPageUri.ApplyParameters(parameters);

Assert.Equal(new Uri(nextPageUriString), actual);
}

[Fact]
public void DoesNotChangePageNumberInNextPageUriWithStartPageForRelativeUri()
{
const string nextPageUriString = "/repositories/1/labels?per_page=5&page=2";
var nextPageUri = new Uri(nextPageUriString, UriKind.Relative);

var parameters = new Dictionary<string, string>();
Pagination.Setup(parameters, new ApiOptions { StartPage = 1, PageSize = 5 });

var actual = nextPageUri.ApplyParameters(parameters);

Assert.Equal(new Uri(nextPageUriString, UriKind.Relative), actual);
}
}
}
}
9 changes: 8 additions & 1 deletion Octokit/Helpers/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public static Uri ApplyParameters(this Uri uri, IDictionary<string, string> para

foreach (var existing in existingParameters)
{
if (!p.ContainsKey(existing.Key))
if (existing.Key == "page")
{
// See https://github.com/octokit/octokit.net/issues/1955
// See https://github.com/octokit/octokit.net/issues/2602
// Desired behavior: don't replace page number in nextPageUri
p[existing.Key] = existing.Value;
}
else if (!p.ContainsKey(existing.Key))
{
p.Add(existing);
}
Expand Down