UriTemplateTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // UriTemplate.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Collections.Specialized;
  32. using NUnit.Framework;
  33. namespace MonoTests.System
  34. {
  35. [TestFixture]
  36. public class UriTemplateTest
  37. {
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void ConstructorNull ()
  41. {
  42. new UriTemplate (null);
  43. }
  44. [Test]
  45. public void ConstructorEmpty ()
  46. {
  47. // it does not raise an error at this state.
  48. new UriTemplate (String.Empty);
  49. }
  50. [Test]
  51. public void ConstructorNullDictionary ()
  52. {
  53. new UriTemplate (String.Empty, null);
  54. }
  55. [Test]
  56. public void IgnoreTrailingSlashDefault ()
  57. {
  58. Assert.IsFalse (new UriTemplate (String.Empty).IgnoreTrailingSlash);
  59. }
  60. [Test]
  61. [ExpectedException (typeof (FormatException))]
  62. public void ConstructorBrokenTemplate ()
  63. {
  64. // it used to be allowed but now it isn't in 3.5 SP1.
  65. new UriTemplate ("{");
  66. }
  67. [Test]
  68. [ExpectedException (typeof (FormatException))]
  69. public void ConstructorBrokenTemplate2 ()
  70. {
  71. new UriTemplate ("http://localhost:8080/{foo}/{");
  72. }
  73. [Test]
  74. [ExpectedException (typeof (FormatException))]
  75. public void ConstructorBrokenTemplate3 ()
  76. {
  77. new UriTemplate ("http://localhost:8080/{foo}/*/baz");
  78. }
  79. [Test]
  80. public void ToString ()
  81. {
  82. Assert.AreEqual ("urn:foo", new UriTemplate ("urn:foo").ToString (), "#1");
  83. // It used to be allowed but now it isn't in 3.5 SP1.
  84. //Assert.AreEqual ("{", new UriTemplate ("{").ToString (), "#2");
  85. }
  86. [Test]
  87. public void Variables ()
  88. {
  89. var t = new UriTemplate ("urn:foo");
  90. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#1a");
  91. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#1b");
  92. t = new UriTemplate ("http://localhost:8080/");
  93. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#2a");
  94. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#2b");
  95. t = new UriTemplate ("http://localhost:8080/foo/");
  96. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#3a");
  97. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#3b");
  98. t = new UriTemplate ("http://localhost:8080/{foo}");
  99. Assert.AreEqual (1, t.PathSegmentVariableNames.Count, "#4a");
  100. Assert.AreEqual ("FOO", t.PathSegmentVariableNames [0], "#4b");
  101. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#4c");
  102. // This became invalid in 3.5 SP1
  103. //t = new UriTemplate ("http://localhost:8080/{foo}/{");
  104. //Assert.AreEqual (1, t.PathSegmentVariableNames.Count, "#5a");
  105. //Assert.AreEqual ("FOO", t.PathSegmentVariableNames [0], "#5b");
  106. //Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#5c");
  107. t = new UriTemplate ("http://localhost:8080/hoge?test={foo}&test2={bar}");
  108. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#6a");
  109. Assert.AreEqual (2, t.QueryValueVariableNames.Count, "#6b");
  110. Assert.AreEqual ("FOO", t.QueryValueVariableNames [0], "#6c");
  111. Assert.AreEqual ("BAR", t.QueryValueVariableNames [1], "#6d");
  112. }
  113. [Test]
  114. [ExpectedException (typeof (ArgumentException))]
  115. public void VariablesInSameSegment ()
  116. {
  117. new UriTemplate ("http://localhost:8080/{foo}{bar}");
  118. }
  119. [Test]
  120. [Category ("NotDotNet")] //.NET 3.5 SP1 incorrectly matches the port part
  121. public void VariablesInNonPathQuery ()
  122. {
  123. var t = new UriTemplate ("http://localhost:{foo}/");
  124. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#8a");
  125. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#8b");
  126. }
  127. [Test]
  128. [ExpectedException (typeof (InvalidOperationException))]
  129. public void DuplicateNameInTemplate ()
  130. {
  131. // one name to two places to match
  132. new UriTemplate ("http://localhost:8080/hoge?test={foo}&test2={foo}");
  133. }
  134. [Test]
  135. [ExpectedException (typeof (InvalidOperationException))]
  136. public void DuplicateNameInTemplate2 ()
  137. {
  138. // one name to two places to match
  139. new UriTemplate ("http://localhost:8080/hoge/{foo}?test={foo}");
  140. }
  141. [Test]
  142. [ExpectedException (typeof (ArgumentNullException))]
  143. public void BindByNameNullBaseAddress ()
  144. {
  145. var t = new UriTemplate ("http://localhost:8080/");
  146. t.BindByName (null, new NameValueCollection ());
  147. }
  148. [Test]
  149. [ExpectedException (typeof (ArgumentException))]
  150. public void BindByNameRelativeBaseAddress ()
  151. {
  152. var t = new UriTemplate ("http://localhost:8080/");
  153. t.BindByName (new Uri ("", UriKind.Relative), new NameValueCollection ());
  154. }
  155. [Test]
  156. [Category ("NotWorking")] // not worthy
  157. public void BindByNameFileUriBaseAddress ()
  158. {
  159. var t = new UriTemplate ("http://localhost:8080/");
  160. var u = t.BindByName (new Uri ("file:///"), new NameValueCollection ());
  161. Assert.AreEqual ("file:///http://localhost:8080/", u.ToString ());
  162. }
  163. [Test] // it is allowed.
  164. public void BindByNameFileExtraNames ()
  165. {
  166. var t = new UriTemplate ("http://localhost:8080/");
  167. var n = new NameValueCollection ();
  168. n.Add ("name", "value");
  169. t.BindByName (new Uri ("http://localhost/"), n);
  170. }
  171. [Test]
  172. [ExpectedException (typeof (ArgumentException))]
  173. public void BindByNameFileMissingName ()
  174. {
  175. var t = new UriTemplate ("/{foo}/");
  176. t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ());
  177. }
  178. [Test]
  179. [ExpectedException (typeof (ArgumentException))]
  180. public void BindInSameSegment ()
  181. {
  182. new UriTemplate ("/hoo/{foo}{bar}");
  183. }
  184. [Test]
  185. public void BindByName ()
  186. {
  187. var t = new UriTemplate ("/{foo}/{bar}/");
  188. var n = new NameValueCollection ();
  189. n.Add ("Bar", "value1"); // case insensitive
  190. n.Add ("FOO", "value2"); // case insensitive
  191. var u = t.BindByName (new Uri ("http://localhost/"), n);
  192. Assert.AreEqual ("http://localhost/value2/value1/", u.ToString ());
  193. }
  194. [Test]
  195. public void BindByNameManySlashes ()
  196. {
  197. var t = new UriTemplate ("////{foo}/{bar}/");
  198. var n = new NameValueCollection ();
  199. n.Add ("Bar", "value1"); // case insensitive
  200. n.Add ("FOO", "value2"); // case insensitive
  201. var u = t.BindByName (new Uri ("http://localhost/"), n);
  202. Assert.AreEqual ("http://localhost////value2/value1/", u.ToString ());
  203. }
  204. [Test]
  205. public void BindByNameManySlashes2 ()
  206. {
  207. var t = new UriTemplate ("////{foo}/{bar}/");
  208. var n = new NameValueCollection ();
  209. n.Add ("Bar", "value1"); // case insensitive
  210. n.Add ("FOO", "value2"); // case insensitive
  211. var u = t.BindByName (new Uri ("http://localhost//"), n);
  212. Assert.AreEqual ("http://localhost/////value2/value1/", u.ToString ());
  213. }
  214. [Test]
  215. public void BindByNameWithDefaults ()
  216. {
  217. var d = new Dictionary<string,string> ();
  218. d.Add ("Bar", "value1"); // case insensitive
  219. d.Add ("FOO", "value2"); // case insensitive
  220. var t = new UriTemplate ("/{foo}/{bar}/", d);
  221. var u = t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ());
  222. Assert.AreEqual ("http://localhost/value2/value1/", u.ToString ());
  223. }
  224. [Test]
  225. [ExpectedException (typeof (ArgumentException))]
  226. public void BindByNameWithDefaults2 ()
  227. {
  228. var d = new Dictionary<string,string> ();
  229. d.Add ("Bar", "value1"); // case insensitive
  230. d.Add ("FOO", "value2"); // case insensitive
  231. var t = new UriTemplate ("/{foo}/{bar}/{baz}", d);
  232. t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ()); // missing baz
  233. }
  234. [Test]
  235. [ExpectedException (typeof (ArgumentNullException))]
  236. public void BindByPositionNullBaseAddress ()
  237. {
  238. var t = new UriTemplate ("http://localhost:8080/");
  239. t.BindByPosition (null);
  240. }
  241. [Test]
  242. [ExpectedException (typeof (ArgumentException))]
  243. public void BindByPositionRelativeBaseAddress ()
  244. {
  245. var t = new UriTemplate ("http://localhost:8080/");
  246. t.BindByPosition (new Uri ("", UriKind.Relative));
  247. }
  248. [Test]
  249. [Category ("NotWorking")] // not worthy
  250. public void BindByPositionFileUriBaseAddress ()
  251. {
  252. var t = new UriTemplate ("http://localhost:8080/");
  253. Assert.AreEqual (new Uri ("file:///http://localhost:8080/"), t.BindByPosition (new Uri ("file:///")));
  254. }
  255. [Test] // it is NOT allowed (unlike BindByName)
  256. [ExpectedException (typeof (FormatException))]
  257. public void BindByPositionFileExtraValues ()
  258. {
  259. var t = new UriTemplate ("http://localhost:8080/");
  260. t.BindByPosition (new Uri ("http://localhost/"), "value");
  261. }
  262. [Test]
  263. [ExpectedException (typeof (FormatException))]
  264. public void BindByPositionFileMissingValues ()
  265. {
  266. var t = new UriTemplate ("/{foo}/");
  267. t.BindByPosition (new Uri ("http://localhost/"));
  268. }
  269. [Test]
  270. public void BindByPosition ()
  271. {
  272. var t = new UriTemplate ("/{foo}/{bar}/");
  273. var u = t.BindByPosition (new Uri ("http://localhost/"), "value1", "value2");
  274. Assert.AreEqual ("http://localhost/value1/value2/", u.ToString ());
  275. }
  276. [Test]
  277. [ExpectedException (typeof (FormatException))] // it does not allow default values
  278. public void BindByPositionWithDefaults ()
  279. {
  280. var d = new Dictionary<string,string> ();
  281. d ["baz"] = "value3";
  282. var t = new UriTemplate ("/{foo}/{bar}/{baz}", d);
  283. t.BindByPosition (new Uri ("http://localhost/"), "value1", "value2");
  284. }
  285. [Test]
  286. public void MatchNoTemplateItem ()
  287. {
  288. var t = new UriTemplate ("/hooray");
  289. var n = new NameValueCollection ();
  290. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray")), "#1");
  291. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  292. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  293. }
  294. [Test]
  295. public void MatchWrongTemplate ()
  296. {
  297. var t = new UriTemplate ("/hoo{foo}");
  298. var n = new NameValueCollection ();
  299. var m = t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray"));
  300. Assert.AreEqual ("ray", m.BoundVariables ["foo"], "#1");
  301. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  302. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  303. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo/ray")), "#4");
  304. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo")), "#5");
  305. // this matches (as if there were no template).
  306. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo{foo}")), "#6");
  307. }
  308. [Test]
  309. public void Match ()
  310. {
  311. var t = new UriTemplate ("/{foo}/{bar}");
  312. var n = new NameValueCollection ();
  313. Uri baseUri = new Uri ("http://localhost/");
  314. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/hooray")), "#1");
  315. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/extra")), "#2");
  316. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/1/2/")), "#3");
  317. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/foooo/baaar"));
  318. Assert.IsNotNull (m, "#4");
  319. Assert.AreEqual ("foooo", m.BoundVariables ["foo"], "#5");
  320. Assert.AreEqual ("baaar", m.BoundVariables ["bar"], "#6");
  321. }
  322. [Test]
  323. public void Match2 ()
  324. {
  325. var t = new UriTemplate ("/{foo}/{bar}?p1={baz}");
  326. var n = new NameValueCollection ();
  327. Uri baseUri = new Uri ("http://localhost/");
  328. Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/X/Y")), "#1");
  329. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/X/Y?p2=v&p1=vv"));
  330. Assert.IsNotNull (m, "#2");
  331. // QueryParameters must contain non-template query parameters.
  332. Assert.AreEqual (2, m.QueryParameters.Count, "#3");
  333. Assert.AreEqual ("v", m.QueryParameters ["p2"], "#4");
  334. Assert.AreEqual ("vv", m.QueryParameters ["p1"], "#5");
  335. }
  336. [Test]
  337. public void MatchWildcard ()
  338. {
  339. var t = new UriTemplate ("/hoge/*?p1={foo}");
  340. var m = t.Match (new Uri ("http://localhost"), new Uri ("http://localhost/hoge/ppp/qqq?p1=v1"));
  341. Assert.IsNotNull (m, "#0");
  342. Assert.IsNotNull (m.QueryParameters, "#1.0");
  343. Assert.AreEqual ("v1", m.QueryParameters ["p1"], "#1");
  344. Assert.IsNotNull (m.WildcardPathSegments, "#2.0");
  345. Assert.AreEqual (2, m.WildcardPathSegments.Count, "#2");
  346. Assert.AreEqual ("ppp", m.WildcardPathSegments [0], "#3");
  347. Assert.AreEqual ("qqq", m.WildcardPathSegments [1], "#4");
  348. }
  349. [Test]
  350. public void IgnoreTrailingSlash ()
  351. {
  352. var t = new UriTemplate ("/{foo}/{bar}", true);
  353. var n = new NameValueCollection ();
  354. Uri baseUri = new Uri ("http://localhost/");
  355. Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#1");
  356. t = new UriTemplate ("/{foo}/{bar}", false);
  357. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#2");
  358. }
  359. [Test]
  360. public void SimpleWebGet () {
  361. UriTemplate t = new UriTemplate ("GetBlog");
  362. Assert.IsNotNull(t.Match(new Uri("http://localhost:8000/BlogService"),
  363. new Uri("http://localhost:8000/BlogService/GetBlog")), "Matches simple WebGet method");
  364. Assert.IsNull(t.Match (new Uri ("http://localhost:8000/BlogService"),
  365. new Uri ("http://localhost:8000/BlogService/GetData")), "Doesn't match wrong WebGet method");
  366. }
  367. [Test]
  368. [ExpectedException (typeof (ArgumentException))]
  369. public void DictContainsNullValue ()
  370. {
  371. var t = new UriTemplate ("/id-{foo}/{bar}");
  372. var dic = new Dictionary<string,string> ();
  373. dic ["foo"] = null;
  374. dic ["bar"] = "bbb";
  375. t.BindByName (new Uri ("http://localhost:8080"), dic);
  376. }
  377. [Test]
  378. public void DictContainsCaseInsensitiveKey ()
  379. {
  380. var t = new UriTemplate ("/id-{foo}/{bar}");
  381. var dic = new Dictionary<string,string> ();
  382. dic ["foo"] = "aaa";
  383. dic ["Bar"] = "bbb";
  384. var uri = t.BindByName (new Uri ("http://localhost:8080"), dic);
  385. Assert.AreEqual ("http://localhost:8080/id-aaa/bbb", uri.ToString ());
  386. }
  387. }
  388. }