public async Task EnsuresNonNullArguments() { var connection = Substitute.For<IApiConnection>(); var client = new RepositoryContentsClient(connection); await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadme(null, "name")); await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadme("owner", null)); await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadme("", "name")); await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadme("owner", "")); }
public async Task ReturnsReadme() { string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world")); var readmeInfo = new ReadmeResponse( encodedContent, "README.md", "https://github.example.com/readme", "https://github.example.com/readme.md", "base64"); var connection = Substitute.For<IApiConnection>(); connection.Get<ReadmeResponse>(Args.Uri, null).Returns(Task.FromResult(readmeInfo)); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>")); var contentsClient = new RepositoryContentsClient(connection); var readme = await contentsClient.GetReadme("fake", "repo"); Assert.Equal("README.md", readme.Name); connection.Received().Get<ReadmeResponse>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/readme"), null); connection.DidNotReceive().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"), null); var htmlReadme = await readme.GetHtmlContent(); Assert.Equal("<html>README</html>", htmlReadme); connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null); }
public async Task ReturnsReadme() { string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world")); var readmeInfo = new ReadmeResponse( encodedContent, "README.md", "https://github.example.com/readme", "https://github.example.com/readme.md", "base64"); var connection = Substitute.For <IApiConnection>(); connection.Get <ReadmeResponse>(Args.Uri, null).Returns(Task.FromResult(readmeInfo)); connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>")); var contentsClient = new RepositoryContentsClient(connection); var readme = await contentsClient.GetReadme("fake", "repo"); Assert.Equal("README.md", readme.Name); connection.Received().Get <ReadmeResponse>(Arg.Is <Uri>(u => u.ToString() == "repos/fake/repo/readme"), null); connection.DidNotReceive().GetHtml(Arg.Is <Uri>(u => u.ToString() == "https://github.example.com/readme"), null); var htmlReadme = await readme.GetHtmlContent(); Assert.Equal("<html>README</html>", htmlReadme); connection.Received().GetHtml(Arg.Is <Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null); }
private async Task <string> GetReadmeMd(long repositoryId, bool afterException = false) { await WaitForApiLimit(); try { return((await _repositoryContentsClient.GetReadme(repositoryId)).Content); } catch (NotFoundException) { return(""); } catch (ApiException e) { Write("--- ApiException: {0} waiting 60 seconds --- ", e.Message); Thread.Sleep(60 * 1000); if (!afterException) { return(await GetReadmeMd(repositoryId, true)); } throw new Exception("Second exception, within GetReadme", e); } }