public void RequestsCorrectUrl()
        {
            var gitHubClient = Substitute.For<IGitHubClient>();
            var client = new ObservableIssuesClient(gitHubClient);

            client.GetAllForRepository("fake", "repo");

            gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
                Arg.Any<IDictionary<string, string>>(),
                "application/vnd.github.squirrel-girl-preview");
        }
        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, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
                .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse));
            gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
                .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse));
            var client = new ObservableIssuesClient(gitHubClient);

            var results = await client.GetAllForRepository("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);
        }
        public void EnsuresNonNullArguments()
        {
            var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());

            var options = new ApiOptions();
            var request = new RepositoryIssueRequest();

            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (RepositoryIssueRequest)null));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request, options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request, options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", request, null));

            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (ApiOptions)null));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (RepositoryIssueRequest)null));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, null, options));
            Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, request, null));

            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", options));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", options));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", request));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", request));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", request, options));
            Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", request, options));
        }
        public void SendsAppropriateParametersWithRepositoryIdWithApiOptions()
        {
            var gitHubClient = Substitute.For<IGitHubClient>();
            var client = new ObservableIssuesClient(gitHubClient);

            var options = new ApiOptions
            {
                PageCount = 1,
                StartPage = 1,
                PageSize = 1
            };

            client.GetAllForRepository(1, new RepositoryIssueRequest
            {
                SortDirection = SortDirection.Ascending
            }, options);

            gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
                Arg.Is<IDictionary<string, string>>(d => d.Count == 6
                && d["filter"] == "assigned"
                && d["state"] == "open"
                && d["sort"] == "created"
                && d["direction"] == "asc"
                && d["page"] == "1"
                && d["per_page"] == "1"),
                null);
        }
        public void SendsAppropriateParameters()
        {
            var gitHubClient = Substitute.For<IGitHubClient>();
            var client = new ObservableIssuesClient(gitHubClient);

            client.GetAllForRepository("fake", "repo", new RepositoryIssueRequest
            {
                SortDirection = SortDirection.Ascending
            });

            gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
                Arg.Is<IDictionary<string, string>>(d => d.Count == 4
                && d["filter"] == "assigned"
                && d["state"] == "open"
                && d["sort"] == "created"
                && d["direction"] == "asc"),
                "application/vnd.github.squirrel-girl-preview");
        }
        public void RequestsCorrectUrlWithApiOptions()
        {
            var gitHubClient = Substitute.For<IGitHubClient>();
            var client = new ObservableIssuesClient(gitHubClient);

            var options = new ApiOptions
            {
                PageCount = 1,
                StartPage = 1,
                PageSize = 1
            };

            client.GetAllForRepository("fake", "repo", options);

            gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"), 
                Arg.Is<IDictionary<string, string>>(d => d.Count == 6 
                && d["filter"] == "assigned" 
                && d["state"] == "open"
                && d["sort"] == "created"
                && d["direction"] == "desc"
                && d["page"] == "1"
                && d["per_page"] == "1"),
                "application/vnd.github.squirrel-girl-preview");
        }
        public void RequestsCorrectUrlWithRepositoryId()
        {
            var gitHubClient = Substitute.For<IGitHubClient>();
            var client = new ObservableIssuesClient(gitHubClient);

            client.GetAllForRepository(1);

            gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
                Arg.Any<IDictionary<string, string>>(), null);
        }
        public async Task EnsuresArgumentsNotNull()
        {
            var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());

            var options = new ApiOptions();
            var request = new RepositoryIssueRequest();

            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name").ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", options).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request, options).ToTask());

            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name").ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", options).ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", request).ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", request, options).ToTask());

            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, options).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request, options).ToTask());

            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "").ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", options).ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", request).ToTask());
            await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", request, options).ToTask());

            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (RepositoryIssueRequest)null).ToTask());

            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, options).ToTask());
            await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", request, null).ToTask());
        }