public void ReturnsEveryPageOfIssues() { var firstPageUrl = new Uri("repos/fake/repo/issues", UriKind.Relative); var secondPageUrl = new Uri("https://example.com/page/2"); var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } }; var firstPageResponse = new ApiResponse<List<Issue>> { BodyAsObject = new List<Issue> { new Issue {Number = 1}, new Issue {Number = 2}, new Issue {Number = 3}, }, ApiInfo = CreateApiInfo(firstPageLinks) }; var thirdPageUrl = new Uri("https://example.com/page/3"); var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } }; var secondPageResponse = new ApiResponse<List<Issue>> { BodyAsObject = new List<Issue> { new Issue {Number = 4}, new Issue {Number = 5}, new Issue {Number = 6}, }, ApiInfo = CreateApiInfo(secondPageLinks) }; var lastPageResponse = new ApiResponse<List<Issue>> { BodyAsObject = new List<Issue> { new Issue {Number = 7}, }, ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) }; var gitHubClient = Substitute.For<IGitHubClient>(); gitHubClient.Connection.GetAsync<List<Issue>>(Arg.Is(firstPageUrl), Arg.Is<Dictionary<string, string>>(d => d.Count == 4 && d["direction"] == "desc" && d["state"] == "open" && d["sort"] == "created" && d["filter"] == "assigned"), Arg.Any<string>()) .Returns(Task.Factory.StartNew<IResponse<List<Issue>>>(() => firstPageResponse)); gitHubClient.Connection.GetAsync<List<Issue>>(secondPageUrl, null, null) .Returns(Task.Factory.StartNew<IResponse<List<Issue>>>(() => secondPageResponse)); gitHubClient.Connection.GetAsync<List<Issue>>(thirdPageUrl, null, null) .Returns(Task.Factory.StartNew<IResponse<List<Issue>>>(() => lastPageResponse)); var client = new ObservableIssuesClient(gitHubClient); var results = client.GetForRepository("fake", "repo").ToArray().Wait(); Assert.Equal(7, results.Length); Assert.Equal(firstPageResponse.BodyAsObject[0].Number, results[0].Number); Assert.Equal(secondPageResponse.BodyAsObject[1].Number, results[4].Number); Assert.Equal(lastPageResponse.BodyAsObject[0].Number, results[6].Number); }
public async Task ReturnsEveryPageOfIssues() { var firstPageUrl = new Uri("repos/fake/repo/issues", UriKind.Relative); var secondPageUrl = new Uri("https://example.com/page/2"); var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } }; var firstPageResponse = new ApiResponse<List<Issue>> ( CreateResponseWithApiInfo(firstPageLinks), new List<Issue> { CreateIssue(1), CreateIssue(2), CreateIssue(3) } ); var thirdPageUrl = new Uri("https://example.com/page/3"); var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } }; var secondPageResponse = new ApiResponse<List<Issue>> ( CreateResponseWithApiInfo(secondPageLinks), new List<Issue> { CreateIssue(4), CreateIssue(5), CreateIssue(6) } ); var lastPageResponse = new ApiResponse<List<Issue>> ( new Response(), new List<Issue> { CreateIssue(7) } ); var gitHubClient = Substitute.For<IGitHubClient>(); gitHubClient.Connection.Get<List<Issue>>(Arg.Is(firstPageUrl), Arg.Is<Dictionary<string, string>>(d => d.Count == 4 && d["direction"] == "desc" && d["state"] == "open" && d["sort"] == "created" && d["filter"] == "assigned"), Arg.Any<string>()) .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => firstPageResponse)); gitHubClient.Connection.Get<List<Issue>>(secondPageUrl, null, null) .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse)); gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, null, null) .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse)); var client = new ObservableIssuesClient(gitHubClient); var results = await client.GetForRepository("fake", "repo").ToArray(); Assert.Equal(7, results.Length); Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number); Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number); Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number); }