Exemplo n.º 1
0
        public async Task Search_Failed()
        {
            // Arrange
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.BadRequest
            };

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponseMessage)
            .Verifiable();

            // use real http client with mocked handler here
            var httpClient = new HttpClient(handlerMock.Object)
            {
                BaseAddress = new Uri("http://altinn3.no/repos/api/v1")
            };

            GiteaAPIWrapper giteaApi = GetServiceForTest("testUser", httpClient);

            SearchResults result = await giteaApi.SearchRepo(GetSearchOptions());

            Assert.Null(result);
        }
Exemplo n.º 2
0
        public async Task Search_Successfull_Repo_Returned_Multiple_Pages()
        {
            // Arrange
            SearchResults searchResult = new SearchResults();

            searchResult.Ok   = true;
            searchResult.Data = GetRepositories();

            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonSerializer.Serialize(searchResult), Encoding.UTF8, "application/json"),
            };

            httpResponseMessage.Headers.Add("X-Total-Count", $"{searchResult.Data.Count}");
            string link = "<https://dev.altinn.studio/repos/api/v1/repos/search?limit=10&order=desc&page=2&sort=created&uid=658>; rel=\"next\"," +
                          "<https://dev.altinn.studio/repos/api/v1/repos/search?limit=10&order=desc&page=27&sort=created&uid=658>; rel=\"last\"";

            httpResponseMessage.Headers.Add("Link", link);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponseMessage)
            .Verifiable();

            // use real http client with mocked handler here
            var httpClient = new HttpClient(handlerMock.Object)
            {
                BaseAddress = new Uri("http://altinn3.no/repos/api/v1")
            };

            GiteaAPIWrapper giteaApi = GetServiceForTest("testUser", httpClient);

            SearchOptions searchOptions = GetSearchOptions();

            searchOptions.Limit = 10;
            SearchResults result = await giteaApi.SearchRepo(searchOptions);

            Assert.Equal(searchResult.Data.Count, result.TotalCount);
            Assert.Equal(27, result.TotalPages);
        }