private static AzureDevOpsRestClient GetFakeClient(object returnObject)
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(returnObject), Encoding.UTF8, "application/json")
            });

            var fakeHttpClient = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("https://fakebaseAddress.com/")
            };

            return(new AzureDevOpsRestClient(fakeHttpClient, Substitute.For <INuKeeperLogger>(), "PAT"));
        }
Esempio n. 2
0
        public void ThrowsWithBadStatusCode()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent("", Encoding.UTF8, "application/json")
            });
            var fakeHttpClient = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("https://fakebaseAddress.com/")
            };
            var restClient = new AzureDevOpsRestClient(fakeHttpClient, Substitute.For <INuKeeperLogger>(), "PAT");
            var exception  = Assert.ThrowsAsync <NuKeeperException>(async() => await restClient.GetGitRepositories("Project"));

            Assert.IsTrue(exception.Message.Contains("Error", StringComparison.InvariantCultureIgnoreCase));
        }
        public void ThrowsWithBadJson()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject("<body>Login Page</body>"), Encoding.UTF8, "application/json")
            });
            var fakeHttpClient = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("https://fakebaseAddress.com/")
            };
            var httpClientFactory = Substitute.For <IHttpClientFactory>();

            httpClientFactory.CreateClient().Returns(fakeHttpClient);
            var restClient = new AzureDevOpsRestClient(httpClientFactory, Substitute.For <INuKeeperLogger>(), "PAT", fakeHttpClient.BaseAddress);
            var exception  = Assert.ThrowsAsync <NuKeeperException>(async() => await restClient.GetGitRepositories("Project"));
        }
        public void ThrowsWithUnauthorized()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Unauthorized,
                Content    = new StringContent("", Encoding.UTF8, "application/json")
            });
            var fakeHttpClient = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("https://fakebaseAddress.com/")
            };
            var httpClientFactory = Substitute.For <IHttpClientFactory>();

            httpClientFactory.CreateClient().Returns(fakeHttpClient);
            var restClient = new AzureDevOpsRestClient(httpClientFactory, Substitute.For <INuKeeperLogger>(), "PAT", fakeHttpClient.BaseAddress);
            var exception  = Assert.ThrowsAsync <NuKeeperException>(async() => await restClient.GetGitRepositories("Project"));

            Assert.IsTrue(exception.Message.Contains("Unauthorised", StringComparison.InvariantCultureIgnoreCase));
        }