PageRenderTime 56ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Reactive/ObservableRepositoryContentsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 779 lines | 623 code | 156 blank | 0 comment | 54 complexity | ea090f19e6ab16b5945a4cc6980acde7 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NSubstitute;
  7. using Octokit.Internal;
  8. using Octokit.Reactive;
  9. using Xunit;
  10. namespace Octokit.Tests.Reactive
  11. {
  12. public class ObservableRepositoryContentsClientTests
  13. {
  14. public class TheCtor
  15. {
  16. [Fact]
  17. public void EnsuresNonNullArguments()
  18. {
  19. Assert.Throws<ArgumentNullException>(
  20. () => new ObservableRepositoryContentsClient(null));
  21. }
  22. }
  23. public class TheGetReadmeMethod
  24. {
  25. [Fact]
  26. public async Task ReturnsReadme()
  27. {
  28. string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world"));
  29. var readmeInfo = new ReadmeResponse(
  30. encodedContent,
  31. "README.md",
  32. "https://github.example.com/readme",
  33. "https://github.example.com/readme.md",
  34. "base64");
  35. var gitHubClient = Substitute.For<IGitHubClient>();
  36. var apiConnection = Substitute.For<IApiConnection>();
  37. apiConnection.GetHtml(new Uri(readmeInfo.Url)).Returns(Task.FromResult("<html>README</html>"));
  38. var readmeFake = new Readme(readmeInfo, apiConnection);
  39. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  40. gitHubClient.Repository.Content.GetReadme("fake", "repo").Returns(Task.FromResult(readmeFake));
  41. IApiResponse<string> apiResponse = new ApiResponse<string>(new Response(), "<html>README</html>");
  42. gitHubClient.Connection.GetHtml(Args.Uri, null)
  43. .Returns(Task.FromResult(apiResponse));
  44. var readme = await contentsClient.GetReadme("fake", "repo");
  45. Assert.Equal("README.md", readme.Name);
  46. gitHubClient.Repository.Content.Received(1).GetReadme("fake", "repo");
  47. gitHubClient.Connection.DidNotReceive().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"),
  48. Args.EmptyDictionary);
  49. var htmlReadme = await readme.GetHtmlContent();
  50. Assert.Equal("<html>README</html>", htmlReadme);
  51. apiConnection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null);
  52. }
  53. [Fact]
  54. public async Task ReturnsReadmeWithRepositoryId()
  55. {
  56. string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world"));
  57. var readmeInfo = new ReadmeResponse(
  58. encodedContent,
  59. "README.md",
  60. "https://github.example.com/readme",
  61. "https://github.example.com/readme.md",
  62. "base64");
  63. var gitHubClient = Substitute.For<IGitHubClient>();
  64. var apiConnection = Substitute.For<IApiConnection>();
  65. apiConnection.GetHtml(new Uri(readmeInfo.Url)).Returns(Task.FromResult("<html>README</html>"));
  66. var readmeFake = new Readme(readmeInfo, apiConnection);
  67. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  68. gitHubClient.Repository.Content.GetReadme(1).Returns(Task.FromResult(readmeFake));
  69. IApiResponse<string> apiResponse = new ApiResponse<string>(new Response(), "<html>README</html>");
  70. gitHubClient.Connection.GetHtml(Args.Uri, null)
  71. .Returns(Task.FromResult(apiResponse));
  72. var readme = await contentsClient.GetReadme(1);
  73. Assert.Equal("README.md", readme.Name);
  74. gitHubClient.Repository.Content.Received(1).GetReadme(1);
  75. gitHubClient.Connection.DidNotReceive().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"),
  76. Args.EmptyDictionary);
  77. var htmlReadme = await readme.GetHtmlContent();
  78. Assert.Equal("<html>README</html>", htmlReadme);
  79. apiConnection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null);
  80. }
  81. [Fact]
  82. public void EnsuresNonNullArguments()
  83. {
  84. var gitHubClient = Substitute.For<IGitHubClient>();
  85. var client = new ObservableRepositoryContentsClient(gitHubClient);
  86. Assert.Throws<ArgumentNullException>(() => client.GetReadme(null, "name"));
  87. Assert.Throws<ArgumentNullException>(() => client.GetReadme("owner", null));
  88. Assert.Throws<ArgumentException>(() => client.GetReadme("", "name"));
  89. Assert.Throws<ArgumentException>(() => client.GetReadme("owner", ""));
  90. }
  91. }
  92. public class TheGetReadmeHtmlMethod
  93. {
  94. [Fact]
  95. public async Task ReturnsReadmeHtml()
  96. {
  97. var connection = Substitute.For<IConnection>();
  98. var gitHubClient = new GitHubClient(connection);
  99. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  100. IApiResponse<string> apiResponse = new ApiResponse<string>(new Response(), "<html>README</html>");
  101. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(apiResponse));
  102. var readme = await contentsClient.GetReadmeHtml("fake", "repo");
  103. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/readme"), null);
  104. Assert.Equal("<html>README</html>", readme);
  105. }
  106. [Fact]
  107. public async Task ReturnsReadmeHtmlWithRepositoryId()
  108. {
  109. var connection = Substitute.For<IConnection>();
  110. var gitHubClient = new GitHubClient(connection);
  111. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  112. IApiResponse<string> apiResponse = new ApiResponse<string>(new Response(), "<html>README</html>");
  113. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult(apiResponse));
  114. var readme = await contentsClient.GetReadmeHtml(1);
  115. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "repositories/1/readme"), null);
  116. Assert.Equal("<html>README</html>", readme);
  117. }
  118. [Fact]
  119. public void EnsuresNonNullArguments()
  120. {
  121. var gitHubClient = Substitute.For<IGitHubClient>();
  122. var client = new ObservableRepositoryContentsClient(gitHubClient);
  123. Assert.Throws<ArgumentNullException>(() => client.GetReadmeHtml(null, "name"));
  124. Assert.Throws<ArgumentNullException>(() => client.GetReadmeHtml("owner", null));
  125. Assert.Throws<ArgumentException>(() => client.GetReadmeHtml("", "name"));
  126. Assert.Throws<ArgumentException>(() => client.GetReadmeHtml("owner", ""));
  127. }
  128. }
  129. public class TheGetContentsMethod
  130. {
  131. [Fact]
  132. public async Task ReturnsContents()
  133. {
  134. var result = new List<RepositoryContent> { new RepositoryContent() };
  135. var connection = Substitute.For<IConnection>();
  136. var gitHubClient = new GitHubClient(connection);
  137. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  138. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  139. (
  140. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  141. result
  142. );
  143. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  144. .Returns(Task.FromResult(response));
  145. var contents = await contentsClient.GetAllContents("fake", "repo").ToList();
  146. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/"), null, null);
  147. Assert.Equal(1, contents.Count);
  148. }
  149. [Fact]
  150. public async Task ReturnsContentsWithRepositoryId()
  151. {
  152. var result = new List<RepositoryContent> { new RepositoryContent() };
  153. var connection = Substitute.For<IConnection>();
  154. var gitHubClient = new GitHubClient(connection);
  155. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  156. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  157. (
  158. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  159. result
  160. );
  161. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  162. .Returns(Task.FromResult(response));
  163. var contents = await contentsClient.GetAllContents(1).ToList();
  164. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/"), null, null);
  165. Assert.Equal(1, contents.Count);
  166. }
  167. [Fact]
  168. public async Task ReturnsAllContents()
  169. {
  170. var result = new List<RepositoryContent> { new RepositoryContent() };
  171. var connection = Substitute.For<IConnection>();
  172. var gitHubClient = new GitHubClient(connection);
  173. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  174. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  175. (
  176. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  177. result
  178. );
  179. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  180. .Returns(Task.FromResult(response));
  181. var contents = await contentsClient.GetAllContents("fake", "repo").ToList();
  182. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/"), null, null);
  183. Assert.Equal(1, contents.Count);
  184. }
  185. [Fact]
  186. public async Task ReturnsAllContentsWithRepositoryId()
  187. {
  188. var result = new List<RepositoryContent> { new RepositoryContent() };
  189. var connection = Substitute.For<IConnection>();
  190. var gitHubClient = new GitHubClient(connection);
  191. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  192. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  193. (
  194. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  195. result
  196. );
  197. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  198. .Returns(Task.FromResult(response));
  199. var contents = await contentsClient.GetAllContents(1).ToList();
  200. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/"), null, null);
  201. Assert.Equal(1, contents.Count);
  202. }
  203. [Fact]
  204. public void EnsuresNonNullArguments()
  205. {
  206. var gitHubClient = Substitute.For<IGitHubClient>();
  207. var client = new ObservableRepositoryContentsClient(gitHubClient);
  208. Assert.Throws<ArgumentNullException>(() => client.GetAllContents(null, "name"));
  209. Assert.Throws<ArgumentNullException>(() => client.GetAllContents("owner", null));
  210. Assert.Throws<ArgumentNullException>(() => client.GetAllContents(null, "name", "path"));
  211. Assert.Throws<ArgumentNullException>(() => client.GetAllContents("owner", null, "path"));
  212. Assert.Throws<ArgumentNullException>(() => client.GetAllContents("owner", "name", null));
  213. Assert.Throws<ArgumentNullException>(() => client.GetAllContents(1, null));
  214. Assert.Throws<ArgumentException>(() => client.GetAllContents("", "name"));
  215. Assert.Throws<ArgumentException>(() => client.GetAllContents("owner", ""));
  216. Assert.Throws<ArgumentException>(() => client.GetAllContents("", "name", "path"));
  217. Assert.Throws<ArgumentException>(() => client.GetAllContents("owner", "", "path"));
  218. Assert.Throws<ArgumentException>(() => client.GetAllContents("owner", "name", ""));
  219. Assert.Throws<ArgumentException>(() => client.GetAllContents(1, ""));
  220. }
  221. }
  222. public class TheGetContentsByRefMethod
  223. {
  224. [Fact]
  225. public async Task ReturnsContentsByRef()
  226. {
  227. var result = new List<RepositoryContent> { new RepositoryContent() };
  228. var connection = Substitute.For<IConnection>();
  229. var gitHubClient = new GitHubClient(connection);
  230. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  231. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  232. (
  233. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  234. result
  235. );
  236. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  237. .Returns(Task.FromResult(response));
  238. var contents = await contentsClient.GetAllContentsByRef("fake", "repo", "master", "readme.md").ToList();
  239. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/readme.md?ref=master"), null, null);
  240. Assert.Equal(1, contents.Count);
  241. }
  242. [Fact]
  243. public async Task ReturnsContentsByRefWithRepositoryId()
  244. {
  245. var result = new List<RepositoryContent> { new RepositoryContent() };
  246. var connection = Substitute.For<IConnection>();
  247. var gitHubClient = new GitHubClient(connection);
  248. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  249. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  250. (
  251. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  252. result
  253. );
  254. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  255. .Returns(Task.FromResult(response));
  256. var contents = await contentsClient.GetAllContentsByRef(1, "master", "readme.md").ToList();
  257. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/readme.md?ref=master"), null, null);
  258. Assert.Equal(1, contents.Count);
  259. }
  260. [Fact]
  261. public async Task ReturnsAllContentsByRef()
  262. {
  263. var result = new List<RepositoryContent> { new RepositoryContent() };
  264. var connection = Substitute.For<IConnection>();
  265. var gitHubClient = new GitHubClient(connection);
  266. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  267. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  268. (
  269. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  270. result
  271. );
  272. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  273. .Returns(Task.FromResult(response));
  274. var contents = await contentsClient.GetAllContentsByRef("fake", "repo", "master").ToList();
  275. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/?ref=master"), null, null);
  276. Assert.Equal(1, contents.Count);
  277. }
  278. [Fact]
  279. public async Task ReturnsAllContentsByRefWithRepositoryId()
  280. {
  281. var result = new List<RepositoryContent> { new RepositoryContent() };
  282. var connection = Substitute.For<IConnection>();
  283. var gitHubClient = new GitHubClient(connection);
  284. var contentsClient = new ObservableRepositoryContentsClient(gitHubClient);
  285. IApiResponse<List<RepositoryContent>> response = new ApiResponse<List<RepositoryContent>>
  286. (
  287. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit()) },
  288. result
  289. );
  290. connection.Get<List<RepositoryContent>>(Args.Uri, null, null)
  291. .Returns(Task.FromResult(response));
  292. var contents = await contentsClient.GetAllContentsByRef(1, "master").ToList();
  293. connection.Received().Get<List<RepositoryContent>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/?ref=master"), null, null);
  294. Assert.Equal(1, contents.Count);
  295. }
  296. [Fact]
  297. public void EnsuresNonNullArguments()
  298. {
  299. var gitHubClient = Substitute.For<IGitHubClient>();
  300. var client = new ObservableRepositoryContentsClient(gitHubClient);
  301. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef(null, "name", "ref"));
  302. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef("owner", null, "ref"));
  303. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", null));
  304. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef(null, "name", "path", "reference"));
  305. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef("owner", null, "path", "reference"));
  306. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", null, "reference"));
  307. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", "path", null));
  308. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef(1, null, "reference"));
  309. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef(1, "path", null));
  310. Assert.Throws<ArgumentNullException>(() => client.GetAllContentsByRef(1, null));
  311. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("", "name", "ref"));
  312. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("owner", "", "ref"));
  313. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", ""));
  314. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("", "name", "path", "reference"));
  315. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("owner", "", "path", "reference"));
  316. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", "", "reference"));
  317. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", "path", ""));
  318. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef(1, "", "reference"));
  319. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef(1, "path", ""));
  320. Assert.Throws<ArgumentException>(() => client.GetAllContentsByRef(1, ""));
  321. }
  322. }
  323. public class TheCreateFileMethod
  324. {
  325. [Fact]
  326. public void RequestsCorrectUrl()
  327. {
  328. var connection = Substitute.For<IConnection>();
  329. var gitHubClient = new GitHubClient(connection);
  330. var client = new ObservableRepositoryContentsClient(gitHubClient);
  331. string expectedUri = "repos/org/repo/contents/path/to/file";
  332. client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  333. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  334. }
  335. [Fact]
  336. public void RequestsCorrectUrlWithRepositoryId()
  337. {
  338. var connection = Substitute.For<IConnection>();
  339. var gitHubClient = new GitHubClient(connection);
  340. var client = new ObservableRepositoryContentsClient(gitHubClient);
  341. string expectedUri = "repositories/1/contents/path/to/file";
  342. client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  343. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  344. }
  345. [Fact]
  346. public void PassesRequestObject()
  347. {
  348. var connection = Substitute.For<IConnection>();
  349. var gitHubClient = new GitHubClient(connection);
  350. var client = new ObservableRepositoryContentsClient(gitHubClient);
  351. client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  352. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(
  353. Arg.Any<Uri>(),
  354. Arg.Is<CreateFileRequest>(a =>
  355. a.Message == "message"
  356. && a.Content == "myfilecontents"
  357. && a.Branch == "mybranch"));
  358. }
  359. [Fact]
  360. public void PassesRequestObjectWithRepositoryId()
  361. {
  362. var connection = Substitute.For<IConnection>();
  363. var gitHubClient = new GitHubClient(connection);
  364. var client = new ObservableRepositoryContentsClient(gitHubClient);
  365. client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  366. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(
  367. Arg.Any<Uri>(),
  368. Arg.Is<CreateFileRequest>(a =>
  369. a.Message == "message"
  370. && a.Content == "myfilecontents"
  371. && a.Branch == "mybranch"));
  372. }
  373. [Fact]
  374. public void EnsuresNonNullArguments()
  375. {
  376. var gitHubClient = Substitute.For<IGitHubClient>();
  377. var client = new ObservableRepositoryContentsClient(gitHubClient);
  378. Assert.Throws<ArgumentNullException>(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  379. Assert.Throws<ArgumentNullException>(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  380. Assert.Throws<ArgumentNullException>(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
  381. Assert.Throws<ArgumentNullException>(() => client.CreateFile("org", "repo", "path/to/file", null));
  382. Assert.Throws<ArgumentNullException>(() => client.CreateFile(1, null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
  383. Assert.Throws<ArgumentNullException>(() => client.CreateFile(1, "path/to/file", null));
  384. Assert.Throws<ArgumentException>(() => client.CreateFile("", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  385. Assert.Throws<ArgumentException>(() => client.CreateFile("org", "", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  386. Assert.Throws<ArgumentException>(() => client.CreateFile("org", "repo", "", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  387. Assert.Throws<ArgumentException>(() => client.CreateFile(1, "", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  388. }
  389. }
  390. public class TheDeleteFileMethod
  391. {
  392. [Fact]
  393. public void RequestsCorrectUrl()
  394. {
  395. var connection = Substitute.For<IConnection>();
  396. var gitHubClient = new GitHubClient(connection);
  397. var client = new ObservableRepositoryContentsClient(gitHubClient);
  398. string expectedUri = "repos/org/repo/contents/path/to/file";
  399. client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  400. gitHubClient.Connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  401. }
  402. [Fact]
  403. public void RequestsCorrectUrlWithRepositoryId()
  404. {
  405. var connection = Substitute.For<IConnection>();
  406. var gitHubClient = new GitHubClient(connection);
  407. var client = new ObservableRepositoryContentsClient(gitHubClient);
  408. string expectedUri = "repositories/1/contents/path/to/file";
  409. client.DeleteFile(1, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  410. gitHubClient.Connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  411. }
  412. [Fact]
  413. public void PassesRequestObject()
  414. {
  415. var connection = Substitute.For<IConnection>();
  416. var gitHubClient = new GitHubClient(connection);
  417. var client = new ObservableRepositoryContentsClient(gitHubClient);
  418. client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  419. gitHubClient.Connection.Received().Delete(
  420. Arg.Any<Uri>(),
  421. Arg.Is<DeleteFileRequest>(a =>
  422. a.Message == "message"
  423. && a.Sha == "1234abc"
  424. && a.Branch == "mybranch"));
  425. }
  426. [Fact]
  427. public void PassesRequestObjectWithRepositoryId()
  428. {
  429. var connection = Substitute.For<IConnection>();
  430. var gitHubClient = new GitHubClient(connection);
  431. var client = new ObservableRepositoryContentsClient(gitHubClient);
  432. client.DeleteFile(1, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  433. gitHubClient.Connection.Received().Delete(
  434. Arg.Any<Uri>(),
  435. Arg.Is<DeleteFileRequest>(a =>
  436. a.Message == "message"
  437. && a.Sha == "1234abc"
  438. && a.Branch == "mybranch"));
  439. }
  440. [Fact]
  441. public void EnsuresNonNullArguments()
  442. {
  443. var connection = Substitute.For<IConnection>();
  444. var gitHubClient = new GitHubClient(connection);
  445. var client = new ObservableRepositoryContentsClient(gitHubClient);
  446. Assert.Throws<ArgumentNullException>(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  447. Assert.Throws<ArgumentNullException>(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  448. Assert.Throws<ArgumentNullException>(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch")));
  449. Assert.Throws<ArgumentNullException>(() => client.DeleteFile("org", "repo", "path/to/file", null));
  450. Assert.Throws<ArgumentNullException>(() => client.DeleteFile(1, null, new DeleteFileRequest("message", "1234abc", "mybranch")));
  451. Assert.Throws<ArgumentNullException>(() => client.DeleteFile(1, "path/to/file", null));
  452. Assert.Throws<ArgumentException>(() => client.DeleteFile("", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  453. Assert.Throws<ArgumentException>(() => client.DeleteFile("org", "", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  454. Assert.Throws<ArgumentException>(() => client.DeleteFile("org", "repo", "", new DeleteFileRequest("message", "1234abc", "mybranch")));
  455. Assert.Throws<ArgumentException>(() => client.DeleteFile(1, "", new DeleteFileRequest("message", "1234abc", "mybranch")));
  456. }
  457. }
  458. public class TheUpdateFileMethod
  459. {
  460. [Fact]
  461. public void RequestsCorrectUrl()
  462. {
  463. var connection = Substitute.For<IConnection>();
  464. var gitHubClient = new GitHubClient(connection);
  465. var client = new ObservableRepositoryContentsClient(gitHubClient);
  466. string expectedUri = "repos/org/repo/contents/path/to/file";
  467. client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  468. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  469. }
  470. [Fact]
  471. public void RequestsCorrectUrlWithRepositoryId()
  472. {
  473. var connection = Substitute.For<IConnection>();
  474. var gitHubClient = new GitHubClient(connection);
  475. var client = new ObservableRepositoryContentsClient(gitHubClient);
  476. string expectedUri = "repositories/1/contents/path/to/file";
  477. client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  478. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  479. }
  480. [Fact]
  481. public void PassesRequestObject()
  482. {
  483. var connection = Substitute.For<IConnection>();
  484. var gitHubClient = new GitHubClient(connection);
  485. var client = new ObservableRepositoryContentsClient(gitHubClient);
  486. client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  487. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(
  488. Arg.Any<Uri>(),
  489. Arg.Is<UpdateFileRequest>(a =>
  490. a.Message == "message"
  491. && a.Content == "myfilecontents"
  492. && a.Sha == "1234abc"
  493. && a.Branch == "mybranch"));
  494. }
  495. [Fact]
  496. public void PassesRequestObjectWithRepositoriesId()
  497. {
  498. var connection = Substitute.For<IConnection>();
  499. var gitHubClient = new GitHubClient(connection);
  500. var client = new ObservableRepositoryContentsClient(gitHubClient);
  501. client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  502. gitHubClient.Connection.Received().Put<RepositoryContentChangeSet>(
  503. Arg.Any<Uri>(),
  504. Arg.Is<UpdateFileRequest>(a =>
  505. a.Message == "message"
  506. && a.Content == "myfilecontents"
  507. && a.Sha == "1234abc"
  508. && a.Branch == "mybranch"));
  509. }
  510. [Fact]
  511. public void EnsuresNonNullArguments()
  512. {
  513. var gitHubClient = Substitute.For<IGitHubClient>();
  514. var client = new ObservableRepositoryContentsClient(gitHubClient);
  515. Assert.Throws<ArgumentNullException>(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  516. Assert.Throws<ArgumentNullException>(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  517. Assert.Throws<ArgumentNullException>(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  518. Assert.Throws<ArgumentNullException>(() => client.UpdateFile("org", "repo", "path/to/file", null));
  519. Assert.Throws<ArgumentNullException>(() => client.UpdateFile(1, null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  520. Assert.Throws<ArgumentNullException>(() => client.UpdateFile(1, "path/to/file", null));
  521. Assert.Throws<ArgumentException>(() => client.UpdateFile("", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  522. Assert.Throws<ArgumentException>(() => client.UpdateFile("org", "", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  523. Assert.Throws<ArgumentException>(() => client.UpdateFile("org", "repo", "", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  524. Assert.Throws<ArgumentException>(() => client.UpdateFile(1, "", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  525. }
  526. }
  527. public class TheGetArchiveMethod
  528. {
  529. [Fact]
  530. public void RequestsCorrectUrl1()
  531. {
  532. var gitHubClient = Substitute.For<IGitHubClient>();
  533. var client = new ObservableRepositoryContentsClient(gitHubClient);
  534. client.GetArchive("org", "repo");
  535. gitHubClient.Received().Repository.Content.GetArchive("org", "repo");
  536. }
  537. [Fact]
  538. public void RequestsCorrectUrl1WithRepositoryId()
  539. {
  540. var gitHubClient = Substitute.For<IGitHubClient>();
  541. var client = new ObservableRepositoryContentsClient(gitHubClient);
  542. client.GetArchive(1);
  543. gitHubClient.Received().Repository.Content.GetArchive(1);
  544. }
  545. [Fact]
  546. public void RequestsCorrectUrl2()
  547. {
  548. var gitHubClient = Substitute.For<IGitHubClient>();
  549. var client = new ObservableRepositoryContentsClient(gitHubClient);
  550. client.GetArchive("org", "repo", ArchiveFormat.Zipball);
  551. gitHubClient.Received().Repository.Content.GetArchive("org", "repo", ArchiveFormat.Zipball);
  552. }
  553. [Fact]
  554. public void RequestsCorrectUrl2WithRepositoryId()
  555. {
  556. var gitHubClient = Substitute.For<IGitHubClient>();
  557. var client = new ObservableRepositoryContentsClient(gitHubClient);
  558. client.GetArchive(1, ArchiveFormat.Zipball);
  559. gitHubClient.Received().Repository.Content.GetArchive(1, ArchiveFormat.Zipball);
  560. }
  561. [Fact]
  562. public void RequestsCorrectUrl3()
  563. {
  564. var gitHubClient = Substitute.For<IGitHubClient>();
  565. var client = new ObservableRepositoryContentsClient(gitHubClient);
  566. client.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref");
  567. gitHubClient.Received().Repository.Content.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref");
  568. }
  569. [Fact]
  570. public void RequestsCorrectUrl3WithRepositoryId()
  571. {
  572. var gitHubClient = Substitute.For<IGitHubClient>();
  573. var client = new ObservableRepositoryContentsClient(gitHubClient);
  574. client.GetArchive(1, ArchiveFormat.Zipball, "ref");
  575. gitHubClient.Received().Repository.Content.GetArchive(1, ArchiveFormat.Zipball, "ref");
  576. }
  577. [Fact]
  578. public void RequestsCorrectUrl4()
  579. {
  580. var gitHubClient = Substitute.For<IGitHubClient>();
  581. var client = new ObservableRepositoryContentsClient(gitHubClient);
  582. client.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  583. gitHubClient.Received().Repository.Content.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  584. }
  585. [Fact]
  586. public void RequestsCorrectUrl4WithRepositoryId()
  587. {
  588. var gitHubClient = Substitute.For<IGitHubClient>();
  589. var client = new ObservableRepositoryContentsClient(gitHubClient);
  590. client.GetArchive(1, ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  591. gitHubClient.Received().Repository.Content.GetArchive(1, ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  592. }
  593. [Fact]
  594. public void EnsuresNonNullArguments()
  595. {
  596. var gitHubClient = Substitute.For<IGitHubClient>();
  597. var client = new ObservableRepositoryContentsClient(gitHubClient);
  598. Assert.Throws<ArgumentNullException>(() => client.GetArchive(null, "repo"));
  599. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", null));
  600. Assert.Throws<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball));
  601. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball));
  602. Assert.Throws<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball, "ref"));
  603. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball, "ref"));
  604. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, null));
  605. Assert.Throws<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  606. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  607. Assert.Throws<ArgumentNullException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, null, TimeSpan.MaxValue));
  608. Assert.Throws<ArgumentNullException>(() => client.GetArchive(1, ArchiveFormat.Tarball, null));
  609. Assert.Throws<ArgumentNullException>(() => client.GetArchive(1, ArchiveFormat.Tarball, null, TimeSpan.MaxValue));
  610. Assert.Throws<ArgumentException>(() => client.GetArchive("", "repo"));
  611. Assert.Throws<ArgumentException>(() => client.GetArchive("org", ""));
  612. Assert.Throws<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball));
  613. Assert.Throws<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball));
  614. Assert.Throws<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball, "ref"));
  615. Assert.Throws<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball, "ref"));
  616. Assert.Throws<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  617. Assert.Throws<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  618. Assert.Throws<ArgumentException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, "ref", TimeSpan.Zero));
  619. Assert.Throws<ArgumentException>(() => client.GetArchive(1, ArchiveFormat.Tarball, "ref", TimeSpan.Zero));
  620. }
  621. }
  622. }
  623. }