public async Task GetAsync_TestRequestFailed() { var authenticatorMock = new Mock <IAuthenticator>(); authenticatorMock .SetupGet(p => p.Scheme) .Returns("fakeScheme"); authenticatorMock .Setup(p => p.GetAuthValue()) .ReturnsAsync("fakeAuthValue"); authenticatorMock .Setup(p => p.EnhanceClientHandler(It.IsAny <HttpClientHandler>())) .Returns(new HttpClientHandler()); var messageHandler = FakeResponseHandler.Create(HttpStatusCode.NotFound, endpoint, payload); var client = new AuthClient(authenticatorMock.Object, endpoint, new HttpClientHandler(), messageHandler); try { await client.GetAsync <TestObject>(endpoint); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(RestfulClientException)); } finally { authenticatorMock.Verify(a => a.GetAuthValue(), Times.Once); authenticatorMock.Verify(p => p.ClearAuthValue(), Times.Never); } }
public async Task GetDownload_Test() { var response = await AuthClient.GetAsync("/api/export/download/123-Mock-321"); var content = await response.Content.ReadAsByteArrayAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); PdfApiMock.Verify(e => e.DownloadFileAsync("mockroot/123-Mock-321.pdf", It.IsAny <string>(), It.IsAny <string>())); Assert.Equal("file 123-Mock-321.pdf content", Encoding.UTF8.GetString(content)); Assert.Matches("Issues-.{4}-.{2}-.{2}.pdf", response.Content.Headers.ContentDisposition.FileNameStar); }
public async Task GetRepos_Test() { var response = await AuthClient.GetAsync("/api/repository/repositories"); var content = await HtmlHelpers.GetHtmlDocumentAsync(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); EqualExpected(GitHubClientMock.RepositoriesData.ExpectedResult, content.DocumentElement.TextContent); LoggingMock.Verify(e => e.ReportAccessLog(It.Is <ElasticsearchAccessLogDocument>(d => d.ControllerName == "Repository" && d.ActionName == "GetRepos"))); }
public async Task GetIssuesMultiRepo1_Test() { long repoId = GitHubClientMock.RepositoriesData.Data.FirstOrDefault().Id; var response = await AuthClient.GetAsync($"/api/repository/issues?repository_ids={repoId}"); var content = await HtmlHelpers.GetHtmlDocumentAsync(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); EqualExpected(GitHubClientMock.RepositoryIssuesData[repoId].ExpectedResult, content.DocumentElement.TextContent); LoggingMock.Verify(e => e.ReportAccessLog(It.Is <ElasticsearchAccessLogDocument>(d => d.ControllerName == "Repository" && d.ActionName == "GetIssuesRepos"))); }
public async Task GetIssuesMultiRepo2_Test() { long repo1Id = GitHubClientMock.RepositoriesData.Data.FirstOrDefault().Id; long repo2Id = GitHubClientMock.RepositoriesData.Data.Skip(1).FirstOrDefault().Id; var response = await AuthClient.GetAsync($"/api/repository/issues?repository_ids={repo1Id}&repository_ids={repo2Id}"); var content = await HtmlHelpers.GetHtmlDocumentAsync(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // merge expected results JArray expected = JArray.FromObject(GitHubClientMock.RepositoryIssuesData[repo1Id].ExpectedResult); expected.Merge(JArray.FromObject(GitHubClientMock.RepositoryIssuesData[repo2Id].ExpectedResult), new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union }); EqualExpected(expected.ToObject <dynamic>(), content.DocumentElement.TextContent); LoggingMock.Verify(e => e.ReportAccessLog(It.Is <ElasticsearchAccessLogDocument>(d => d.ControllerName == "Repository" && d.ActionName == "GetIssuesRepos"))); }
public async Task GetAsync_TestAuthorizationFailed() { var authValueException = new Exception("Test auth token error"); var authenticatorMock = new Mock <IAuthenticator>(); authenticatorMock .Setup(p => p.GetAuthValue()) .ThrowsAsync(authValueException); authenticatorMock .Setup(p => p.EnhanceClientHandler(It.IsAny <HttpClientHandler>())) .Returns(new HttpClientHandler()); var messageHandler = FakeResponseHandler.Create(HttpStatusCode.OK, endpoint, payload); var client = new AuthClient(authenticatorMock.Object, endpoint, new HttpClientHandler(), messageHandler); try { await client.GetAsync <TestObject>(endpoint); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(RestfulClientException)); var restfulException = ex as RestfulClientException; Assert.IsTrue(restfulException.Message.Contains("AuthClient could not obtain and/or add the authentication token")); Assert.AreEqual(HttpStatusCode.Unauthorized, restfulException.StatusCode); Assert.AreSame(authValueException, restfulException.InnerException); } finally { authenticatorMock.Verify(a => a.GetAuthValue(), Times.Once); authenticatorMock.Verify(p => p.ClearAuthValue(), Times.Never); } }