PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/UnitTests/GitHub.Primitives/UriStringTests.cs

https://gitlab.com/github-cloud-corporation/VisualStudio
C# | 306 lines | 283 code | 23 blank | 0 comment | 0 complexity | 7abf4af03017c52aad8950c4ecac0f75 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using GitHub.Primitives;
  4. using Xunit;
  5. public class UriStringTests
  6. {
  7. public class TheConstructor : TestBaseClass
  8. {
  9. [Theory]
  10. [InlineData("http://192.168.1.3/foo/bar.git", "192.168.1.3", "foo", "bar")]
  11. [InlineData("http://haacked@example.com/foo/bar", "example.com", "foo", "bar")]
  12. [InlineData("http://haacked:password@example.com/foo/bar", "example.com", "foo", "bar")]
  13. [InlineData("http://example.com/foo/bar.git", "example.com", "foo", "bar")]
  14. [InlineData("http://example.com/foo/bar", "example.com", "foo", "bar")]
  15. [InlineData("http://example.com:1234/foo/bar.git", "example.com", "foo", "bar")]
  16. [InlineData("https://github.com/github/Windows.git", "github.com", "github", "Windows")]
  17. [InlineData("https://github.com/libgit2/libgit2.github.com", "github.com", "libgit2", "libgit2.github.com")]
  18. [InlineData("https://github.com/github/Windows", "github.com", "github", "Windows")]
  19. [InlineData("git@192.168.1.2:github/Windows.git", "192.168.1.2", "github", "Windows")]
  20. [InlineData("git@github.com:github/Windows.git", "github.com", "github", "Windows")]
  21. [InlineData("git@github.com:github/Windows", "github.com", "github", "Windows")]
  22. [InlineData("git@example.com:org/repo.git", "example.com", "org", "repo")]
  23. [InlineData("ssh://git@github.com:443/benstraub/libgit2", "github.com", "benstraub", "libgit2")]
  24. [InlineData("git://git@github.com:443/benstraub/libgit2", "github.com", "benstraub", "libgit2")]
  25. [InlineData("jane;fingerprint=9e:1a:5e:27:16:4d:2a:13:90:2c:64:41:bd:25:fd:35@foo.com:github/Windows.git",
  26. "foo.com", "github", "Windows")]
  27. [InlineData("https://haacked@bitbucket.org/haacked/test-mytest.git", "bitbucket.org", "haacked", "test-mytest")]
  28. [InlineData("https://git01.codeplex.com/nuget", "git01.codeplex.com", null, "nuget")]
  29. [InlineData("https://example.com/vpath/foo/bar", "example.com", "foo", "bar")]
  30. [InlineData("https://example.com/vpath/foo/bar.git", "example.com", "foo", "bar")]
  31. [InlineData("https://github.com/github/Windows.git?pr=24&branch=pr/23&filepath=relative/to/the/path.md",
  32. "github.com", "github", "Windows")]
  33. public void ParsesWellFormedUrlComponents(string url, string expectedHost, string owner, string repositoryName)
  34. {
  35. var cloneUrl = new UriString(url);
  36. Assert.Equal(cloneUrl.Host, expectedHost);
  37. Assert.Equal(cloneUrl.Owner, owner);
  38. Assert.Equal(cloneUrl.RepositoryName, repositoryName);
  39. Assert.False(cloneUrl.IsFileUri);
  40. }
  41. [Theory]
  42. [InlineData(@"..\bar\foo")]
  43. [InlineData(@"..\..\foo")]
  44. [InlineData(@"../..\foo")]
  45. [InlineData(@"../../foo")]
  46. [InlineData(@"..\..\foo.git")]
  47. [InlineData(@"c:\dev\exp\foo")]
  48. [InlineData(@"c:\dev\bar\..\exp\foo")]
  49. [InlineData("c:/dev/exp/foo")]
  50. [InlineData(@"c:\dev\exp\foo.git")]
  51. [InlineData(@"c:\dev\exp\foo.git")]
  52. [InlineData("c:/dev/exp/foo.git")]
  53. [InlineData("c:/dev/exp/bar/../foo.git")]
  54. [InlineData("file:///C:/dev/exp/foo")]
  55. [InlineData("file://C:/dev/exp/foo")]
  56. [InlineData("file://C:/dev/exp/foo.git")]
  57. public void ParsesLocalFileUris(string path)
  58. {
  59. var cloneUrl = new UriString(path);
  60. Assert.Equal(cloneUrl.Host, "");
  61. Assert.Equal(cloneUrl.Owner, "");
  62. Assert.Equal(cloneUrl.RepositoryName, "foo");
  63. Assert.Equal(cloneUrl.ToString(), path.Replace('\\', '/'));
  64. Assert.True(cloneUrl.IsFileUri);
  65. }
  66. [Theory]
  67. [InlineData("complete garbage", "", "", null)]
  68. [InlineData(@"..\other_folder", "", "", "other_folder")]
  69. [InlineData("http://example.com", "example.com", null, null)]
  70. [InlineData("http://example.com?bar", "example.com", null, null)]
  71. [InlineData("https://example.com?bar", "example.com", null, null)]
  72. [InlineData("ssh://git@example.com/Windows.git", "example.com", null, "Windows")]
  73. [InlineData("blah@bar.com:/", "bar.com", null, null)]
  74. [InlineData("blah@bar.com/", "bar.com", null, null)]
  75. [InlineData("blah@bar.com", "bar.com", null, null)]
  76. [InlineData("blah@bar.com:/Windows.git", "bar.com", null, "Windows")]
  77. [InlineData("blah@baz.com/Windows.git", "baz.com", null, "Windows")]
  78. [InlineData("ssh://git@github.com:github/Windows.git", "github.com", "github", "Windows")]
  79. [InlineData("ssh://git@example.com/Windows.git", "example.com", null, "Windows")]
  80. public void ParsesWeirdUrlsAsWellAsPossible(string url, string expectedHost, string owner, string repositoryName)
  81. {
  82. var cloneUrl = new UriString(url);
  83. Assert.Equal(cloneUrl.Host, expectedHost);
  84. Assert.Equal(cloneUrl.Owner, owner);
  85. Assert.Equal(cloneUrl.RepositoryName, repositoryName);
  86. }
  87. [Theory]
  88. [InlineData(@"http:\\example.com/bar\baz")]
  89. [InlineData(@"http://example.com/bar/baz")]
  90. public void NormalizesSeparator(string uriString)
  91. {
  92. var path = new UriString(uriString);
  93. new UriString("http://example.com/bar/baz").Equals(path);
  94. }
  95. [Fact]
  96. public void AcceptsNullConversion()
  97. {
  98. Assert.NotNull(new UriString(null));
  99. }
  100. }
  101. public class TheNameWithOwnerProperty : TestBaseClass
  102. {
  103. [Theory]
  104. [InlineData("http://192.168.1.3/foo/bar.git", "foo/bar")]
  105. [InlineData("http://192.168.1.3/foo/bar", "foo/bar")]
  106. [InlineData("http://192.168.1.3/foo/bar/baz/qux", "baz/qux")]
  107. [InlineData("https://github.com/github/Windows.git", "github/Windows")]
  108. [InlineData("https://github.com/github/", "github")]
  109. [InlineData("blah@bar.com:/Windows.git", "Windows")]
  110. [InlineData("git@github.com:github/Windows.git", "github/Windows")]
  111. public void DependsOnOwnerAndRepoNameNotBeingNull(string url, string expectedNameWithOwner)
  112. {
  113. var cloneUrl = new UriString(url);
  114. Assert.Equal(cloneUrl.NameWithOwner, expectedNameWithOwner);
  115. }
  116. }
  117. public class TheCombineMethod : TestBaseClass
  118. {
  119. [Theory]
  120. [InlineData("http://example.com", "foo/bar", @"http://example.com/foo/bar")]
  121. [InlineData("http://example.com/", "foo/bar", @"http://example.com/foo/bar")]
  122. [InlineData("http://example.com/", "/foo/bar/", @"http://example.com/foo/bar/")]
  123. [InlineData("http://example.com/foo", "bar/", @"http://example.com/foo/bar/")]
  124. [InlineData("http://example.com", @"foo\bar", @"http://example.com/foo/bar")]
  125. [InlineData("http://example.com/", @"foo\bar", @"http://example.com/foo/bar")]
  126. [InlineData("http://example.com/", @"/foo\bar/", @"http://example.com/foo/bar/")]
  127. [InlineData("http://example.com/foo", @"bar\", @"http://example.com/foo/bar/")]
  128. [InlineData("http://example.com/foo?bar", @"baz", @"http://example.com/foo?bar&baz")]
  129. [InlineData("http://example.com/foo?bar", @"&baz", @"http://example.com/foo?bar&baz")]
  130. [InlineData("http://example.com/foo?", @"bar", @"http://example.com/foo?bar")]
  131. [InlineData("http://example.com/foo?", @"&bar", @"http://example.com/foo?&bar")]
  132. public void ComparesHostInsensitively(string uriString, string path, string expected)
  133. {
  134. Assert.Equal(new UriString(uriString).Combine(path), (UriString)expected);
  135. }
  136. }
  137. public class TheIsValidUriProperty : TestBaseClass
  138. {
  139. [Theory]
  140. [InlineData("http://example.com/", true)]
  141. [InlineData("file:///C:/dev/exp/foo", true)]
  142. [InlineData("garbage", false)]
  143. [InlineData("git@192.168.1.2:github/Windows.git", false)]
  144. public void ReturnWhetherTheUriIsParseableByUri(string uriString, bool expected)
  145. {
  146. Assert.Equal(new UriString(uriString).IsValidUri, expected);
  147. }
  148. }
  149. public class TheToRepositoryUrlMethod : TestBaseClass
  150. {
  151. [Theory]
  152. [InlineData("file:///C:/dev/exp/foo", "file:///C:/dev/exp/foo")]
  153. [InlineData("http://example.com/", "http://example.com/")]
  154. [InlineData("http://haacked@example.com/foo/bar", "http://example.com/foo/bar")]
  155. [InlineData("https://github.com/github/Windows", "https://github.com/github/Windows")]
  156. [InlineData("https://github.com/github/Windows.git", "https://github.com/github/Windows")]
  157. [InlineData("https://haacked@github.com/github/Windows.git", "https://github.com/github/Windows")]
  158. [InlineData("http://example.com:4000/github/Windows", "http://example.com:4000/github/Windows")]
  159. [InlineData("git@192.168.1.2:github/Windows.git", "https://192.168.1.2/github/Windows")]
  160. [InlineData("git@example.com:org/repo.git", "https://example.com/org/repo")]
  161. [InlineData("ssh://git@github.com:443/shana/cef", "https://github.com/shana/cef")]
  162. [InlineData("ssh://git@example.com:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
  163. public void ConvertsToWebUrl(string uriString, string expected)
  164. {
  165. Assert.Equal(new Uri(expected), new UriString(uriString).ToRepositoryUrl());
  166. }
  167. [Theory]
  168. [InlineData("asdf", null)]
  169. [InlineData("", null)]
  170. [InlineData("file:///C:/dev/exp/foo", "file:///C:/dev/exp/foo")]
  171. [InlineData("http://example.com/", "http://example.com/")]
  172. [InlineData("http://haacked@example.com/foo/bar", "http://example.com/foo/bar")]
  173. [InlineData("https://github.com/github/Windows", "https://github.com/github/Windows")]
  174. [InlineData("https://github.com/github/Windows.git", "https://github.com/github/Windows")]
  175. [InlineData("https://haacked@github.com/github/Windows.git", "https://github.com/github/Windows")]
  176. [InlineData("http://example.com:4000/github/Windows", "http://example.com:4000/github/Windows")]
  177. [InlineData("git@192.168.1.2:github/Windows.git", "https://192.168.1.2/github/Windows")]
  178. [InlineData("git@example.com:org/repo.git", "https://example.com/org/repo")]
  179. [InlineData("ssh://git@github.com:443/shana/cef", "https://github.com/shana/cef")]
  180. [InlineData("ssh://git@example.com:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
  181. public void ShouldNeverThrow(string url, string expected)
  182. {
  183. Uri uri;
  184. Uri.TryCreate(expected, UriKind.Absolute, out uri);
  185. Assert.Equal(uri, new UriString(url).ToRepositoryUrl());
  186. }
  187. }
  188. public class TheAdditionOperator : TestBaseClass
  189. {
  190. [Theory]
  191. [InlineData("http://example.com", "foo/bar", @"http://example.com/foo/bar")]
  192. [InlineData("http://example.com/", "foo/bar", @"http://example.com/foo/bar")]
  193. [InlineData("http://example.com/", "/foo/bar/", @"http://example.com/foo/bar/")]
  194. [InlineData("http://example.com/foo", "bar/", @"http://example.com/foo/bar/")]
  195. [InlineData("http://example.com", @"foo\bar", @"http://example.com/foo/bar")]
  196. [InlineData("http://example.com/", @"foo\bar", @"http://example.com/foo/bar")]
  197. [InlineData("http://example.com/", @"/foo\bar/", @"http://example.com/foo/bar/")]
  198. [InlineData("http://example.com/foo", @"bar\", @"http://example.com/foo/bar/")]
  199. [InlineData("http://example.com/foo?bar", @"baz", @"http://example.com/foo?bar&baz")]
  200. [InlineData("http://example.com/foo?bar", @"&baz", @"http://example.com/foo?bar&baz")]
  201. [InlineData("http://example.com/foo?", @"bar", @"http://example.com/foo?bar")]
  202. [InlineData("http://example.com/foo?", @"&bar", @"http://example.com/foo?&bar")]
  203. public void CombinesPaths(string uriString, string addition, string expected)
  204. {
  205. UriString path = uriString;
  206. var newPath = path + addition;
  207. Assert.Equal(newPath, (UriString)expected);
  208. }
  209. }
  210. public class ImplicitConversionToString : TestBaseClass
  211. {
  212. [Fact]
  213. public void ConvertsBackToString()
  214. {
  215. var uri = new UriString("http://github.com/foo/bar/");
  216. string cloneUri = uri;
  217. Assert.Equal(cloneUri, "http://github.com/foo/bar/");
  218. }
  219. [Fact]
  220. public void ConvertsNullToNull()
  221. {
  222. UriString uri = null;
  223. Assert.Null(uri);
  224. string cloneUri = uri;
  225. Assert.Null(cloneUri);
  226. }
  227. }
  228. public class ImplicitConversionFromString : TestBaseClass
  229. {
  230. [Fact]
  231. public void ConvertsToCloneUri()
  232. {
  233. UriString cloneUri = "http://github.com/foo/bar/";
  234. Assert.Equal(cloneUri.Host, "github.com");
  235. }
  236. [Fact]
  237. public void ConvertsNullToNull()
  238. {
  239. UriString cloneUri = (string)null;
  240. Assert.Null(cloneUri);
  241. }
  242. }
  243. public class TheIsHypertextTransferProtocolProperty : TestBaseClass
  244. {
  245. [Theory]
  246. [InlineData("http://example.com", true)]
  247. [InlineData("HTTP://example.com", true)]
  248. [InlineData("https://example.com", true)]
  249. [InlineData("HTTPs://example.com", true)]
  250. [InlineData("ftp://example.com", false)]
  251. [InlineData("c:/example.com", false)]
  252. [InlineData("git@github.com:github/Windows", false)]
  253. public void IsTrueOnlyForHttpAndHttps(string url, bool expected)
  254. {
  255. var uri = new UriString(url);
  256. Assert.Equal(uri.IsHypertextTransferProtocol, expected);
  257. }
  258. }
  259. public class TheEqualsMethod : TestBaseClass
  260. {
  261. [Theory]
  262. [InlineData("https://github.com/foo/bar", "https://github.com/foo/bar", true)]
  263. [InlineData("https://github.com/foo/bar", "https://github.com/foo/BAR", false)]
  264. [InlineData("https://github.com/foo/bar", "https://github.com/foo/bar/", false)]
  265. [InlineData("https://github.com/foo/bar", null, false)]
  266. public void ReturnsTrueForCaseSensitiveEquality(string source, string compare, bool expected)
  267. {
  268. Assert.Equal(expected, source.Equals(compare));
  269. Assert.Equal(expected, EqualityComparer<UriString>.Default.Equals(source, compare));
  270. }
  271. [Fact]
  272. public void MakesUriStringSuitableForDictionaryKey()
  273. {
  274. var dictionary = new Dictionary<UriString, string>
  275. {
  276. { new UriString("https://github.com/foo/bar"), "whatever" }
  277. };
  278. Assert.False(dictionary.ContainsKey("https://github.com/foo/not-bar"));
  279. Assert.True(dictionary.ContainsKey("https://github.com/foo/bar"));
  280. Assert.True(dictionary.ContainsKey(new UriString("https://github.com/foo/bar")));
  281. }
  282. }
  283. }