UriTemplateTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.ObjectModel;
  30. using System.Collections.Specialized;
  31. using NUnit.Framework;
  32. namespace MonoTests.System
  33. {
  34. [TestFixture]
  35. public class UriTemplateTest
  36. {
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void ConstructorNull ()
  40. {
  41. new UriTemplate (null);
  42. }
  43. [Test]
  44. public void ConstructorEmpty ()
  45. {
  46. // it does not raise an error at this state.
  47. new UriTemplate (String.Empty);
  48. }
  49. [Test]
  50. public void ConstructorBrokenTemplate ()
  51. {
  52. // it does not raise an error at this state.
  53. new UriTemplate ("{");
  54. }
  55. [Test]
  56. public void ToString ()
  57. {
  58. Assert.AreEqual ("urn:foo", new UriTemplate ("urn:foo").ToString (), "#1");
  59. Assert.AreEqual ("{", new UriTemplate ("{").ToString (), "#2");
  60. }
  61. [Test]
  62. public void Variables ()
  63. {
  64. var t = new UriTemplate ("urn:foo");
  65. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#1a");
  66. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#1b");
  67. t = new UriTemplate ("http://localhost:8080/");
  68. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#2a");
  69. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#2b");
  70. t = new UriTemplate ("http://localhost:8080/foo/");
  71. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#3a");
  72. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#3b");
  73. t = new UriTemplate ("http://localhost:8080/{foo}");
  74. Assert.AreEqual (1, t.PathSegmentVariableNames.Count, "#4a");
  75. Assert.AreEqual ("FOO", t.PathSegmentVariableNames [0], "#4b");
  76. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#4c");
  77. t = new UriTemplate ("http://localhost:8080/{foo}/{");
  78. Assert.AreEqual (1, t.PathSegmentVariableNames.Count, "#5a");
  79. Assert.AreEqual ("FOO", t.PathSegmentVariableNames [0], "#5b");
  80. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#5c");
  81. t = new UriTemplate ("http://localhost:8080/hoge?test={foo}&test2={bar}");
  82. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#6a");
  83. Assert.AreEqual (2, t.QueryValueVariableNames.Count, "#6b");
  84. Assert.AreEqual ("FOO", t.QueryValueVariableNames [0], "#6c");
  85. Assert.AreEqual ("BAR", t.QueryValueVariableNames [1], "#6d");
  86. }
  87. [Test]
  88. [Category ("NotWorking")]
  89. public void VariablesInSameSegment ()
  90. {
  91. var t = new UriTemplate ("http://localhost:8080/{foo}{bar}");
  92. Assert.AreEqual (1, t.PathSegmentVariableNames.Count, "#7a");
  93. // wow.
  94. Assert.AreEqual ("FOO}{BAR", t.PathSegmentVariableNames [0], "#7b");
  95. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#7c");
  96. }
  97. [Test]
  98. public void VariablesInNonPathQuery ()
  99. {
  100. var t = new UriTemplate ("http://localhost:{foo}/");
  101. Assert.AreEqual (0, t.PathSegmentVariableNames.Count, "#8a");
  102. Assert.AreEqual (0, t.QueryValueVariableNames.Count, "#8b");
  103. }
  104. [Test]
  105. [ExpectedException (typeof (InvalidOperationException))]
  106. public void DuplicateNameInTemplate ()
  107. {
  108. // one name to two places to match
  109. new UriTemplate ("http://localhost:8080/hoge?test={foo}&test2={foo}");
  110. }
  111. [Test]
  112. [ExpectedException (typeof (InvalidOperationException))]
  113. public void DuplicateNameInTemplate2 ()
  114. {
  115. // one name to two places to match
  116. new UriTemplate ("http://localhost:8080/hoge/{foo}?test={foo}");
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentNullException))]
  120. public void BindByNameNullBaseAddress ()
  121. {
  122. var t = new UriTemplate ("http://localhost:8080/");
  123. t.BindByName (null, new NameValueCollection ());
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentException))]
  127. public void BindByNameRelativeBaseAddress ()
  128. {
  129. var t = new UriTemplate ("http://localhost:8080/");
  130. t.BindByName (new Uri ("", UriKind.Relative), new NameValueCollection ());
  131. }
  132. [Test]
  133. [ExpectedException (typeof (ArgumentException))]
  134. public void BindByNameFileUriBaseAddress ()
  135. {
  136. var t = new UriTemplate ("http://localhost:8080/");
  137. t.BindByName (new Uri ("file:///"), new NameValueCollection ());
  138. }
  139. [Test] // it is allowed.
  140. public void BindByNameFileExtraNames ()
  141. {
  142. var t = new UriTemplate ("http://localhost:8080/");
  143. var n = new NameValueCollection ();
  144. n.Add ("name", "value");
  145. t.BindByName (new Uri ("http://localhost/"), n);
  146. }
  147. [Test]
  148. [ExpectedException (typeof (FormatException))]
  149. public void BindByNameFileMissingName ()
  150. {
  151. var t = new UriTemplate ("/{foo}/");
  152. t.BindByName (new Uri ("http://localhost/"), new NameValueCollection ());
  153. }
  154. [Test]
  155. public void BindInSameSegment ()
  156. {
  157. new UriTemplate ("/hoo/{foo}{bar}");
  158. }
  159. [Test]
  160. public void BindByName ()
  161. {
  162. var t = new UriTemplate ("/{foo}/{bar}/");
  163. var n = new NameValueCollection ();
  164. n.Add ("Bar", "value1"); // case insensitive
  165. n.Add ("FOO", "value2"); // case insensitive
  166. var u = t.BindByName (new Uri ("http://localhost/"), n);
  167. Assert.AreEqual ("http://localhost/value2/value1/", u.ToString ());
  168. }
  169. [Test]
  170. [ExpectedException (typeof (ArgumentNullException))]
  171. public void BindByPositionNullBaseAddress ()
  172. {
  173. var t = new UriTemplate ("http://localhost:8080/");
  174. t.BindByPosition (null);
  175. }
  176. [Test]
  177. [ExpectedException (typeof (ArgumentException))]
  178. public void BindByPositionRelativeBaseAddress ()
  179. {
  180. var t = new UriTemplate ("http://localhost:8080/");
  181. t.BindByPosition (new Uri ("", UriKind.Relative));
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentException))]
  185. public void BindByPositionFileUriBaseAddress ()
  186. {
  187. var t = new UriTemplate ("http://localhost:8080/");
  188. t.BindByPosition (new Uri ("file:///"));
  189. }
  190. [Test] // it is NOT allowed (unlike BindByName)
  191. [ExpectedException (typeof (FormatException))]
  192. public void BindByPositionFileExtraValues ()
  193. {
  194. var t = new UriTemplate ("http://localhost:8080/");
  195. t.BindByPosition (new Uri ("http://localhost/"), "value");
  196. }
  197. [Test]
  198. [ExpectedException (typeof (FormatException))]
  199. public void BindByPositionFileMissingValues ()
  200. {
  201. var t = new UriTemplate ("/{foo}/");
  202. t.BindByPosition (new Uri ("http://localhost/"));
  203. }
  204. [Test]
  205. public void BindByPosition ()
  206. {
  207. var t = new UriTemplate ("/{foo}/{bar}/");
  208. var u = t.BindByPosition (new Uri ("http://localhost/"), "value1", "value2");
  209. Assert.AreEqual ("http://localhost/value1/value2/", u.ToString ());
  210. }
  211. [Test]
  212. public void MatchNoTemplateItem ()
  213. {
  214. var t = new UriTemplate ("/hooray");
  215. var n = new NameValueCollection ();
  216. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray")), "#1");
  217. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  218. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  219. }
  220. [Test]
  221. [Category("NotWorking")]
  222. public void MatchWrongTemplate ()
  223. {
  224. var t = new UriTemplate ("/hoo{foo}");
  225. var n = new NameValueCollection ();
  226. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray")), "#1");
  227. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/foobar")), "#2");
  228. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hooray/foobar")), "#3");
  229. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo/ray")), "#4");
  230. Assert.IsNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo")), "#5");
  231. // this matches (as if there were no template).
  232. Assert.IsNotNull (t.Match (new Uri ("http://localhost/"), new Uri ("http://localhost/hoo{foo}")), "#6");
  233. }
  234. [Test]
  235. public void Match ()
  236. {
  237. var t = new UriTemplate ("/{foo}/{bar}");
  238. var n = new NameValueCollection ();
  239. Uri baseUri = new Uri ("http://localhost/");
  240. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/hooray")), "#1");
  241. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/extra")), "#2");
  242. Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/1/2/")), "#3");
  243. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/foooo/baaar"));
  244. Assert.IsNotNull (m, "#4");
  245. Assert.AreEqual ("foooo", m.BoundVariables ["foo"], "#5");
  246. Assert.AreEqual ("baaar", m.BoundVariables ["bar"], "#6");
  247. }
  248. [Test]
  249. public void Match2 ()
  250. {
  251. var t = new UriTemplate ("/{foo}/{bar}?p1={baz}");
  252. var n = new NameValueCollection ();
  253. Uri baseUri = new Uri ("http://localhost/");
  254. Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/X/Y")), "#1");
  255. UriTemplateMatch m = t.Match (baseUri, new Uri ("http://localhost/X/Y?p2=v&p1=vv"));
  256. Assert.IsNotNull (m, "#2");
  257. // QueryParameters must contain non-template query parameters.
  258. Assert.AreEqual (2, m.QueryParameters.Count, "#3");
  259. Assert.AreEqual ("v", m.QueryParameters ["p2"], "#4");
  260. Assert.AreEqual ("vv", m.QueryParameters ["p1"], "#5");
  261. }
  262. [Test]
  263. public void SimpleWebGet () {
  264. UriTemplate t = new UriTemplate ("GetBlog");
  265. Assert.IsNotNull(t.Match(new Uri("http://localhost:8000/BlogService"),
  266. new Uri("http://localhost:8000/BlogService/GetBlog")), "Matches simple WebGet method");
  267. Assert.IsNull(t.Match (new Uri ("http://localhost:8000/BlogService"),
  268. new Uri ("http://localhost:8000/BlogService/GetData")), "Doesn't match wrong WebGet method");
  269. }
  270. }
  271. }