Exemplo n.º 1
0
        public void GetHttpClient__returns_http_client_with_required_headers(GitSearchType searchType)
        {
            //Arrange

            //Act
            var httpClient = subject.GetHttpClient(searchType);

            //Assert
            httpClient.BaseAddress.AbsoluteUri.Should().Be(StaticVariables.searchBaseAddress);
            Assert.Contains("application/json", httpClient.DefaultRequestHeaders.GetValues("accept"));
            if (searchType == GitSearchType.commits)
            {
                Assert.Contains("application/vnd.github.cloak-preview", httpClient.DefaultRequestHeaders.GetValues("accept"));
            }
            else if (searchType == GitSearchType.topics)
            {
                Assert.Contains("application/vnd.github.mercy-preview+json", httpClient.DefaultRequestHeaders.GetValues("accept"));
            }
            else
            {
                Assert.Contains("application/vnd.github.v3+json", httpClient.DefaultRequestHeaders.GetValues("accept"));
            }
            Assert.Contains(StaticVariables.userAgent, httpClient.DefaultRequestHeaders.GetValues("User-Agent"));
            Assert.Contains(StaticVariables.githubAuthorisation, httpClient.DefaultRequestHeaders.GetValues("Authorization"));
        }
Exemplo n.º 2
0
        public void GetSearchResponse__returns_repository_search_results_from_github_repository_search_api()
        {
            //Arrange
            var mockHttpClient = Substitute.For <HttpClient>();

            mockHttpClient.BaseAddress = new Uri(StaticVariables.searchBaseAddress);
            mockSubject.GetHttpClient(GitSearchType.repositories).Returns(mockHttpClient);

            var uriWithQueryStrings = $"{GitSearchType.repositories}?q={HttpUtility.UrlEncode(searchTerm)}&per_page={perPage}&page={pageNumber}";

            mockSubject.GetUriWithQueryStrings(GitSearchType.repositories, searchTerm, sort, order, perPage, pageNumber).Returns(uriWithQueryStrings);

            //Act
            var result = mockSubject.GetSearchResponse <GitBaseResponse <GitRepositorySearchResponse> >(GitSearchType.repositories, searchTerm, sort, order, perPage, pageNumber);

            //Assert
            mockSubject.Received(1).GetHttpClient(GitSearchType.repositories);
            mockSubject.Received(1).GetUriWithQueryStrings(GitSearchType.repositories, searchTerm, sort, order, perPage, pageNumber);
            mockSubject.GetHttpClient(GitSearchType.repositories).Received(1).GetAsync(mockSubject.GetUriWithQueryStrings(GitSearchType.repositories, searchTerm, sort, order, perPage, pageNumber));
        }