UriTemplateTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 BindByNameWithDefaults ()
  196. {
  197. var d = new Dictionary<string,string> ();
  198. d.Add ("Bar", "value1"); // case insensitive
  199. d.Add ("FOO", "value2"); // case insensitive
  200. var t = new UriTemplate ("/{foo}/{bar}/", d);
  201. var u = t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ());
  202. Assert.AreEqual ("http://localhost/value2/value1/", u.ToString ());
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentException))]
  206. public void BindByNameWithDefaults2 ()
  207. {
  208. var d = new Dictionary<string,string> ();
  209. d.Add ("Bar", "value1"); // case insensitive
  210. d.Add ("FOO", "value2"); // case insensitive
  211. var t = new UriTemplate ("/{foo}/{bar}/{baz}", d);
  212. t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ()); // missing baz
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentNullException))]
  216. public void BindByPositionNullBaseAddress ()
  217. {
  218. var t = new UriTemplate ("http://localhost:8080/");
  219. t.BindByPosition (null);
  220. }
  221. [Test]
  222. [ExpectedException (typeof (ArgumentException))]
  223. public void BindByPositionRelativeBaseAddress ()
  224. {
  225. var t = new UriTemplate ("http://localhost:8080/");
  226. t.BindByPosition (new Uri ("", UriKind.Relative));
  227. }
  228. [Test]
  229. [Category ("NotWorking")] // not worthy
  230. public void BindByPositionFileUriBaseAddress ()
  231. {
  232. var t = new UriTemplate ("http://localhost:8080/");
  233. Assert.AreEqual (new Uri ("file:///http://localhost:8080/"), t.BindByPosition (new Uri ("file:///")));
  234. }
  235. [Test] // it is NOT allowed (unlike BindByName)
  236. [ExpectedException (typeof (FormatException))]
  237. public void BindByPositionFileExtraValues ()
  238. {
  239. var t = new UriTemplate ("http://localhost:8080/");
  240. t.BindByPosition (new Uri ("http://localhost/"), "value");
  241. }
  242. [Test]
  243. [ExpectedException (typeof (FormatException))]
  244. public void BindByPositionFileMissingValues ()
  245. {
  246. var t = new UriTemplate ("/{foo}/");
  247. t.BindByPosition (new Uri ("http://localhost/"));
  248. }
  249. [Test]
  250. public void BindByPosition ()
  251. {
  252. var t = new UriTemplate ("/{foo}/{bar}/");
  253. var u = t.BindByPosition (new Uri ("http://localhost/"), "value1", "value2");
  254. Assert.AreEqual ("http://localhost/value1/value2/", u.ToString ());
  255. }
  256. [Test]
  257. [ExpectedException (typeof (FormatException))] // it does not allow default values
  258. public void BindByPositionWithDefaults ()
  259. {
  260. var d = new Dictionary<string,string> ();
  261. d ["baz"] = "value3";
  262. var t = new UriTemplate ("/{foo}/{bar}/{baz}", d);
  263. t.BindByPosition (new Uri ("http://localhost/"), "value1", "value2");
  264. }
  265. [Test]
  266. public void MatchNoTemplateItem ()
  267. {
  268. var t = new UriTemplate ("/hooray");
  269. var n = new NameValueCollection ();
  270. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray")), "#1");
  271. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  272. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  273. }
  274. [Test]
  275. public void MatchWrongTemplate ()
  276. {
  277. var t = new UriTemplate ("/hoo{foo}");
  278. var n = new NameValueCollection ();
  279. var m = t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray"));
  280. Assert.AreEqual ("ray", m.BoundVariables ["foo"], "#1");
  281. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  282. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  283. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo/ray")), "#4");
  284. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo")), "#5");
  285. // this matches (as if there were no template).
  286. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo{foo}")), "#6");
  287. }
  288. [Test]
  289. public void Match ()
  290. {
  291. var t = new UriTemplate ("/{foo}/{bar}");
  292. var n = new NameValueCollection ();
  293. Uri baseUri = new Uri ("http://localhost/");
  294. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/hooray")), "#1");
  295. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/extra")), "#2");
  296. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/1/2/")), "#3");
  297. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/foooo/baaar"));
  298. Assert.IsNotNull (m, "#4");
  299. Assert.AreEqual ("foooo", m.BoundVariables ["foo"], "#5");
  300. Assert.AreEqual ("baaar", m.BoundVariables ["bar"], "#6");
  301. }
  302. [Test]
  303. public void Match2 ()
  304. {
  305. var t = new UriTemplate ("/{foo}/{bar}?p1={baz}");
  306. var n = new NameValueCollection ();
  307. Uri baseUri = new Uri ("http://localhost/");
  308. Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/X/Y")), "#1");
  309. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/X/Y?p2=v&p1=vv"));
  310. Assert.IsNotNull (m, "#2");
  311. // QueryParameters must contain non-template query parameters.
  312. Assert.AreEqual (2, m.QueryParameters.Count, "#3");
  313. Assert.AreEqual ("v", m.QueryParameters ["p2"], "#4");
  314. Assert.AreEqual ("vv", m.QueryParameters ["p1"], "#5");
  315. }
  316. [Test]
  317. public void MatchWildcard ()
  318. {
  319. var t = new UriTemplate ("/hoge/*?p1={foo}");
  320. var m = t.Match (new Uri ("http://localhost"), new Uri ("http://localhost/hoge/ppp/qqq?p1=v1"));
  321. Assert.IsNotNull (m, "#0");
  322. Assert.IsNotNull (m.QueryParameters, "#1.0");
  323. Assert.AreEqual ("v1", m.QueryParameters ["p1"], "#1");
  324. Assert.IsNotNull (m.WildcardPathSegments, "#2.0");
  325. Assert.AreEqual (2, m.WildcardPathSegments.Count, "#2");
  326. Assert.AreEqual ("ppp", m.WildcardPathSegments [0], "#3");
  327. Assert.AreEqual ("qqq", m.WildcardPathSegments [1], "#4");
  328. }
  329. [Test]
  330. public void IgnoreTrailingSlash ()
  331. {
  332. var t = new UriTemplate ("/{foo}/{bar}", true);
  333. var n = new NameValueCollection ();
  334. Uri baseUri = new Uri ("http://localhost/");
  335. Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#1");
  336. t = new UriTemplate ("/{foo}/{bar}", false);
  337. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#2");
  338. }
  339. [Test]
  340. public void SimpleWebGet () {
  341. UriTemplate t = new UriTemplate ("GetBlog");
  342. Assert.IsNotNull(t.Match(new Uri("http://localhost:8000/BlogService"),
  343. new Uri("http://localhost:8000/BlogService/GetBlog")), "Matches simple WebGet method");
  344. Assert.IsNull(t.Match (new Uri ("http://localhost:8000/BlogService"),
  345. new Uri ("http://localhost:8000/BlogService/GetData")), "Doesn't match wrong WebGet method");
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ArgumentException))]
  349. public void DictContainsNullValue ()
  350. {
  351. var t = new UriTemplate ("/id-{foo}/{bar}");
  352. var dic = new Dictionary<string,string> ();
  353. dic ["foo"] = null;
  354. dic ["bar"] = "bbb";
  355. t.BindByName (new Uri ("http://localhost:8080"), dic);
  356. }
  357. [Test]
  358. public void DictContainsCaseInsensitiveKey ()
  359. {
  360. var t = new UriTemplate ("/id-{foo}/{bar}");
  361. var dic = new Dictionary<string,string> ();
  362. dic ["foo"] = "aaa";
  363. dic ["Bar"] = "bbb";
  364. var uri = t.BindByName (new Uri ("http://localhost:8080"), dic);
  365. Assert.AreEqual ("http://localhost:8080/id-aaa/bbb", uri.ToString ());
  366. }
  367. }
  368. }