PageRenderTime 95ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/mordor/tests/uri.cpp

http://github.com/mozy/mordor
C++ | 510 lines | 439 code | 53 blank | 18 comment | 3 complexity | 7f787a5a8f7f53212c30416363585571 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2009 - Mozy, Inc.
  2. #include "mordor/uri.h"
  3. #include "mordor/streams/buffer.h"
  4. #include "mordor/test/test.h"
  5. using namespace Mordor;
  6. using namespace Mordor::Test;
  7. static void serializeAndParse(const char *uri, const char *expected = NULL)
  8. {
  9. if (!expected) expected = uri;
  10. MORDOR_TEST_ASSERT_EQUAL(URI(uri).toString(), expected);
  11. }
  12. MORDOR_UNITTEST(URI, serializationAndParsing)
  13. {
  14. MORDOR_TEST_ASSERT_EQUAL(URI::Path("/a/b/c/./../../g").toString(),
  15. "/a/b/c/./../../g");
  16. serializeAndParse("example://a/b/c/%7Bfoo%7D");
  17. serializeAndParse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d", "eXAMPLE://a/./b/../b/c/%7Bfoo%7D");
  18. serializeAndParse("http://a/b/c/d;p?q");
  19. serializeAndParse("g:h");
  20. serializeAndParse("g");
  21. serializeAndParse("http://a/b/c/g");
  22. serializeAndParse("./g");
  23. serializeAndParse("g/");
  24. serializeAndParse("http://a/b/c/g/");
  25. serializeAndParse("/g");
  26. serializeAndParse("http://a/g");
  27. serializeAndParse("//g");
  28. serializeAndParse("http://g");
  29. serializeAndParse("?y");
  30. serializeAndParse("http://a/b/c/d;p?y");
  31. serializeAndParse("g?y");
  32. serializeAndParse("http://a/b/c/g?y");
  33. serializeAndParse("#s");
  34. serializeAndParse("http://a/b/c/d;p?q#s");
  35. serializeAndParse("g#s");
  36. serializeAndParse("http://a/b/c/g#s");
  37. serializeAndParse("g?y#s");
  38. serializeAndParse("http://a/b/c/g?y#s");
  39. serializeAndParse(";x");
  40. serializeAndParse("http://a/b/c/;x");
  41. serializeAndParse("g;x");
  42. serializeAndParse("http://a/b/c/g;x");
  43. serializeAndParse("g;x?y#s");
  44. serializeAndParse("http://a/b/c/g;x?y#s");
  45. serializeAndParse("");
  46. serializeAndParse("http://a/b/c/d;p?q");
  47. serializeAndParse(".");
  48. serializeAndParse("http://a/b/c/");
  49. serializeAndParse("./");
  50. serializeAndParse("..");
  51. serializeAndParse("http://a/b/");
  52. serializeAndParse("../");
  53. serializeAndParse("../g");
  54. serializeAndParse("http://a/b/g");
  55. serializeAndParse("../..");
  56. serializeAndParse("http://a/");
  57. serializeAndParse("../../");
  58. serializeAndParse("../../g");
  59. serializeAndParse("http://a/g");
  60. serializeAndParse("../../../g");
  61. serializeAndParse("../../../../g");
  62. serializeAndParse("/./g");
  63. serializeAndParse("/../g");
  64. serializeAndParse("g.");
  65. serializeAndParse("http://a/b/c/g.");
  66. serializeAndParse(".g");
  67. serializeAndParse("http://a/b/c/.g");
  68. serializeAndParse("g..");
  69. serializeAndParse("http://a/b/c/g..");
  70. serializeAndParse("..g");
  71. serializeAndParse("http://a/b/c/..g");
  72. serializeAndParse("./../g");
  73. serializeAndParse("./g/.");
  74. serializeAndParse("http://a/b/c/g/");
  75. serializeAndParse("g/./h");
  76. serializeAndParse("http://a/b/c/g/h");
  77. serializeAndParse("g/../h");
  78. serializeAndParse("http://a/b/c/h");
  79. serializeAndParse("g;x=1/./y");
  80. serializeAndParse("http://a/b/c/g;x=1/y");
  81. serializeAndParse("g;x=1/../y");
  82. serializeAndParse("http://a/b/c/y");
  83. serializeAndParse("g?y/./x");
  84. serializeAndParse("http://a/b/c/g?y/./x");
  85. serializeAndParse("g?y/../x");
  86. serializeAndParse("http://a/b/c/g?y/../x");
  87. serializeAndParse("g#s/./x");
  88. serializeAndParse("http://a/b/c/g#s/./x");
  89. serializeAndParse("g#s/../x");
  90. serializeAndParse("http://a/b/c/g#s/../x");
  91. serializeAndParse("http:g");
  92. serializeAndParse("http:/hi");
  93. serializeAndParse("http:////hi");
  94. }
  95. MORDOR_UNITTEST(URI, pathNormalization)
  96. {
  97. URI::Path p("/a/b/c/./../../g");
  98. p.removeDotComponents();
  99. MORDOR_TEST_ASSERT_EQUAL(p, URI::Path("/a/g"));
  100. }
  101. MORDOR_UNITTEST(URI, normalization)
  102. {
  103. URI lhs("example://a/b/c/%7Bfoo%7D");
  104. URI rhs("eXAMPLE://a/./b/../b/%63/%7bfoo%7d");
  105. lhs.normalize();
  106. rhs.normalize();
  107. MORDOR_TEST_ASSERT(lhs.isDefined());
  108. MORDOR_TEST_ASSERT(rhs.isDefined());
  109. MORDOR_TEST_ASSERT(lhs.schemeDefined());
  110. MORDOR_TEST_ASSERT(rhs.schemeDefined());
  111. MORDOR_TEST_ASSERT_EQUAL(lhs.scheme(), rhs.scheme());
  112. MORDOR_TEST_ASSERT(!lhs.authority.portDefined());
  113. MORDOR_TEST_ASSERT(!rhs.authority.portDefined());
  114. MORDOR_TEST_ASSERT(lhs.authority.hostDefined());
  115. MORDOR_TEST_ASSERT(rhs.authority.hostDefined());
  116. MORDOR_TEST_ASSERT_EQUAL(lhs.authority.host(), rhs.authority.host());
  117. MORDOR_TEST_ASSERT(!lhs.authority.userinfoDefined());
  118. MORDOR_TEST_ASSERT(!rhs.authority.userinfoDefined());
  119. MORDOR_TEST_ASSERT_EQUAL(lhs.authority, rhs.authority);
  120. MORDOR_TEST_ASSERT_EQUAL(lhs.path.segments, rhs.path.segments);
  121. MORDOR_TEST_ASSERT_EQUAL(lhs.path, rhs.path);
  122. MORDOR_TEST_ASSERT(!lhs.queryDefined());
  123. MORDOR_TEST_ASSERT(!rhs.queryDefined());
  124. MORDOR_TEST_ASSERT(!lhs.fragmentDefined());
  125. MORDOR_TEST_ASSERT(!rhs.fragmentDefined());
  126. MORDOR_TEST_ASSERT_EQUAL(lhs, rhs);
  127. }
  128. MORDOR_UNITTEST(URI, transform)
  129. {
  130. URI base("http://a/b/c/d;p?q");
  131. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g:h")), URI("g:h"));
  132. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g")), URI("http://a/b/c/g"));
  133. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("./g")), URI("http://a/b/c/g"));
  134. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g/")), URI("http://a/b/c/g/"));
  135. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("/g")), URI("http://a/g"));
  136. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("//g")), URI("http://g"));
  137. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("?y")), URI("http://a/b/c/d;p?y"));
  138. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g?y")), URI("http://a/b/c/g?y"));
  139. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("#s")), URI("http://a/b/c/d;p?q#s"));
  140. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g#s")), URI("http://a/b/c/g#s"));
  141. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g?y#s")), URI("http://a/b/c/g?y#s"));
  142. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI(";x")), URI("http://a/b/c/;x"));
  143. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g;x")), URI("http://a/b/c/g;x"));
  144. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g;x?y#s")), URI("http://a/b/c/g;x?y#s"));
  145. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("")), URI("http://a/b/c/d;p?q"));
  146. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI(".")), URI("http://a/b/c/"));
  147. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("./")), URI("http://a/b/c/"));
  148. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("..")), URI("http://a/b/"));
  149. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../")), URI("http://a/b/"));
  150. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../g")), URI("http://a/b/g"));
  151. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../..")), URI("http://a/"));
  152. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../../")), URI("http://a/"));
  153. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../../g")), URI("http://a/g"));
  154. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../../../g")), URI("http://a/g"));
  155. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("../../../../g")), URI("http://a/g"));
  156. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("/./g")), URI("http://a/g"));
  157. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("/../g")), URI("http://a/g"));
  158. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g.")), URI("http://a/b/c/g."));
  159. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI(".g")), URI("http://a/b/c/.g"));
  160. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g..")), URI("http://a/b/c/g.."));
  161. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("..g")), URI("http://a/b/c/..g"));
  162. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("./../g")), URI("http://a/b/g"));
  163. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("./g/.")), URI("http://a/b/c/g/"));
  164. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g/./h")), URI("http://a/b/c/g/h"));
  165. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g/../h")), URI("http://a/b/c/h"));
  166. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g;x=1/./y")), URI("http://a/b/c/g;x=1/y"));
  167. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g;x=1/../y")), URI("http://a/b/c/y"));
  168. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g?y/./x")), URI("http://a/b/c/g?y/./x"));
  169. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g?y/../x")), URI("http://a/b/c/g?y/../x"));
  170. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g#s/./x")), URI("http://a/b/c/g#s/./x"));
  171. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("g#s/../x")), URI("http://a/b/c/g#s/../x"));
  172. MORDOR_TEST_ASSERT_EQUAL(URI::transform(base, URI("http:g")), URI("http:g"));
  173. MORDOR_TEST_ASSERT_EQUAL(URI::transform("http:hi", "bob"), "http:bob");
  174. MORDOR_TEST_ASSERT_EQUAL(URI::transform("http://authority", "bob"), "http://authority/bob");
  175. }
  176. MORDOR_UNITTEST(URI, serializeCompleteOnBlockBoundary)
  177. {
  178. Buffer b("http://abc/");
  179. b.copyIn("more");
  180. URI uri(b);
  181. MORDOR_TEST_ASSERT_EQUAL(URI(b), "http://abc/more");
  182. }
  183. MORDOR_UNITTEST(URI, bigBase64URI)
  184. {
  185. serializeAndParse("/partialObjects/"
  186. "49ZtbkNPlEEi8T+sQLb5mh9zm1DcyaaRoyHUOC9sEfaKIgLh+eKZNUrqR+j3Iybhx321iz"
  187. "y3J+Mw7gZmIlVcZrP0qHNDxuEQHMUHLqxhXoXcN18+x4XedNLqc8KhnJtHLXndcKMJu5Cg"
  188. "xp2BI9NXDDDuBmYiVVxms/Soc0PG4RAcxQcurGFehSY0Wf0fG5eWquA0b0hozVjE4xxyAF"
  189. "TkSU39Hl3XcsUUMO4GZiJVXGaz9KhzQ8bhEBzFBy6sYV6F9718Fox0OiJ3PqBvo2gr352W"
  190. "vZBqmEeUV1n0CkcClc0w7gZmIlVcZrP0qHNDxuEQHMUHLqxhXoWapmDUfha0WO9SjTUn4F"
  191. "Jeht8Gjdy6mYpDqvUbB+3OoDDuBmYiVVxms/Soc0PG4RAcxQcurGFehcefjKkVeAR2HShU"
  192. "2UpBh5g/89ZP9czSJ8qKSKCPGyHWMO4GZiJVXGaz9KhzQ8bhEBzFBy6sYV6FAig0fJADqV"
  193. "eInu5RU/pgEXJlZ1MBce/F+rv7MI3g5jgw7gZmIlVcZrP0qHNDxuEQHMUHLqxhXoW4GIxe"
  194. "C1lnhkTtrAv3jhk17r3ZwL8Fq7CvpUHeAQl/JTDuBmYiVVxms/Soc0PG4RAcxQcurGFehc"
  195. "s4fMw9uBwTihHQAPFbcyDTjZtTMGlaovGaP6xe1H1TMO4GZiJVXGaz9KhzQ8bhEBzFBy6s"
  196. "YV6FFAhiH0dzP8E0IRZP+oxeL2JkfxiO5v8r7eWnYtMY8d4w7gZmIlVcZrP0qHNDxuEQHM"
  197. "UHLqxhXoUgoQ1pQreM2tYMR9QaJ7CsSOSJs+Qi5KIzV50DBUYLDjDuBmYiVVxms/Soc0PG"
  198. "4RAcxQcurGFehdeUg8nHldHqihIknc3OP/QRtBawAyEFY4p0RKlRxnA0MO4GZiJVXGaz9K"
  199. "hzQ8bhEBzFBy6sYV6FbRY5v48No3N72yRSA9JiYPhS/YTYcUFz");
  200. }
  201. MORDOR_UNITTEST(URI, queryString)
  202. {
  203. URI uri("http://a/b?a&b");
  204. MORDOR_TEST_ASSERT(uri.queryDefined());
  205. MORDOR_TEST_ASSERT_EQUAL(uri.query(), "a&b");
  206. uri = "http://a/b?a%20b";
  207. MORDOR_TEST_ASSERT(uri.queryDefined());
  208. MORDOR_TEST_ASSERT_EQUAL(uri.query(), "a b");
  209. URI::QueryString qs = uri.queryString();
  210. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  211. MORDOR_TEST_ASSERT_EQUAL(qs.begin()->first, "a b");
  212. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a+b");
  213. qs = "a%2Bb";
  214. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  215. MORDOR_TEST_ASSERT_EQUAL(qs.begin()->first, "a+b");
  216. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a%2Bb");
  217. qs = "a&b;c";
  218. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 3u);
  219. URI::QueryString::iterator it = qs.begin();
  220. MORDOR_TEST_ASSERT_EQUAL(it->first, "a");
  221. ++it;
  222. MORDOR_TEST_ASSERT_EQUAL(it->first, "b");
  223. ++it;
  224. MORDOR_TEST_ASSERT_EQUAL(it->first, "c");
  225. qs = "a+b";
  226. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  227. MORDOR_TEST_ASSERT_EQUAL(qs.begin()->first, "a b");
  228. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a+b");
  229. qs = "";
  230. MORDOR_TEST_ASSERT(qs.empty());
  231. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "");
  232. qs = "&&;&&;;&";
  233. MORDOR_TEST_ASSERT(qs.empty());
  234. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "");
  235. qs = "a=b&c";
  236. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 2u);
  237. it = qs.begin();
  238. MORDOR_TEST_ASSERT_EQUAL(it->first, "a");
  239. MORDOR_TEST_ASSERT_EQUAL(it->second, "b");
  240. ++it;
  241. MORDOR_TEST_ASSERT_EQUAL(it->first, "c");
  242. qs = "a=b=c=d&e=f=g=h";
  243. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 2u);
  244. it = qs.begin();
  245. MORDOR_TEST_ASSERT_EQUAL(it->first, "a");
  246. MORDOR_TEST_ASSERT_EQUAL(it->second, "b=c=d");
  247. ++it;
  248. MORDOR_TEST_ASSERT_EQUAL(it->first, "e");
  249. MORDOR_TEST_ASSERT_EQUAL(it->second, "f=g=h");
  250. qs = "a=";
  251. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  252. it = qs.begin();
  253. MORDOR_TEST_ASSERT_EQUAL(it->first, "a");
  254. MORDOR_TEST_ASSERT(it->second.empty());
  255. qs = "a=&=b&=";
  256. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 2u);
  257. it = qs.begin();
  258. MORDOR_TEST_ASSERT(it->first.empty());
  259. MORDOR_TEST_ASSERT_EQUAL(it->second, "b");
  260. ++it;
  261. MORDOR_TEST_ASSERT_EQUAL(it->first, "a");
  262. MORDOR_TEST_ASSERT(it->second.empty());
  263. qs.clear();
  264. qs.insert(std::make_pair("ampersand", "and&and"));
  265. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "ampersand=and%26and");
  266. uri = URI();
  267. uri.query(qs);
  268. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "?ampersand=and%26and");
  269. }
  270. MORDOR_UNITTEST(URI, encoding)
  271. {
  272. URI uri;
  273. uri.path.segments.push_back(std::string());
  274. uri.path.segments.push_back("WiX Tutorial \xe2\x80\x94 Introduction to the Windows Installer XML Toolset.URL");
  275. MORDOR_TEST_ASSERT_EQUAL(uri.toString(),
  276. "/WiX%20Tutorial%20%E2%80%94%20Introduction%20to%20the%20Windows%20Installer%20XML%20Toolset.URL");
  277. uri.path.segments[1] = "\xe5\xa4\x9a\xe8\xa8\x80\xe8\xaa\x9e\xe5\xaf\xbe\xe5\xbf\x9c\xe3\x82"
  278. "\xb5\xe3\x83\xbc\xe3\x83\x81\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3"
  279. "\x83\xb3\xe3\x81\xae\xe6\x97\xa5\xe6\x9c\xac\xe7\x89\x88\xe3\x80\x82"
  280. "\xe3\x82\xa6\xe3\x82\xa7\xe3\x83\x96\xe3\x80\x81\xe3\x82\xa4\xe3\x83"
  281. "\xa1\xe3\x83\xbc\xe3\x82\xb8\xe3\x81\x8a\xe3\x82\x88\xe3\x81\xb3\xe3"
  282. "\x83\x8b\xe3\x83\xa5\xe3\x83\xbc\xe3\x82\xb9\xe6\xa4\x9c\xe7\xb4\xa2.txt";
  283. MORDOR_TEST_ASSERT_EQUAL(uri.toString(),
  284. "/%E5%A4%9A%E8%A8%80%E8%AA%9E%E5%AF%BE%E5%BF%9C%E3%82"
  285. "%B5%E3%83%BC%E3%83%81%E3%82%A8%E3%83%B3%E3%82%B8%E3"
  286. "%83%B3%E3%81%AE%E6%97%A5%E6%9C%AC%E7%89%88%E3%80%82"
  287. "%E3%82%A6%E3%82%A7%E3%83%96%E3%80%81%E3%82%A4%E3%83"
  288. "%A1%E3%83%BC%E3%82%B8%E3%81%8A%E3%82%88%E3%81%B3%E3"
  289. "%83%8B%E3%83%A5%E3%83%BC%E3%82%B9%E6%A4%9C%E7%B4%A2.txt");
  290. }
  291. MORDOR_UNITTEST(URI, emptyFirstComponent)
  292. {
  293. serializeAndParse("/");
  294. serializeAndParse("http://localhost/");
  295. serializeAndParse("http://localhost//");
  296. serializeAndParse("http://localhost///");
  297. URI::Path path;
  298. path = "/";
  299. MORDOR_TEST_ASSERT_EQUAL(path.segments.size(), 2u);
  300. MORDOR_TEST_ASSERT(path.isAbsolute());
  301. MORDOR_TEST_ASSERT(path.segments.front().empty());
  302. MORDOR_TEST_ASSERT(path.segments.back().empty());
  303. path = "";
  304. MORDOR_TEST_ASSERT(path.isRelative());
  305. MORDOR_TEST_ASSERT(path.segments.empty());
  306. path = "//";
  307. MORDOR_TEST_ASSERT(path.isAbsolute());
  308. MORDOR_TEST_ASSERT_EQUAL(path.segments.size(), 3u);
  309. path = "a//";
  310. MORDOR_TEST_ASSERT(path.isRelative());
  311. MORDOR_TEST_ASSERT_EQUAL(path.segments.size(), 3u);
  312. URI uri = "http://localhost/";
  313. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/");
  314. uri.path.append("hi");
  315. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi");
  316. uri.path.segments.push_back("");
  317. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi/");
  318. uri.path.segments.push_back("bye");
  319. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi//bye");
  320. uri.path.segments.push_back("");
  321. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi//bye/");
  322. uri.path.append("adios");
  323. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi//bye/adios");
  324. uri = "http://localhost/";
  325. uri.path.append("hi");
  326. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http://localhost/hi");
  327. // scheme == http, authority is not defined, path = "//hi";
  328. // serialization has to add an extra // so it's not ambiguous with
  329. // authority
  330. uri = URI();
  331. uri.scheme("http");
  332. uri.path.segments.push_back(std::string());
  333. uri.path.segments.push_back(std::string());
  334. uri.path.segments.push_back("hi");
  335. MORDOR_TEST_ASSERT_EQUAL(uri.toString(), "http:////hi");
  336. }
  337. MORDOR_UNITTEST(URI, append)
  338. {
  339. URI uri = "http://localhost";
  340. uri.path.append("hi");
  341. MORDOR_TEST_ASSERT_EQUAL(uri, "http://localhost/hi");
  342. uri.path.append("bye");
  343. MORDOR_TEST_ASSERT_EQUAL(uri, "http://localhost/hi/bye");
  344. uri.path.append(std::string());
  345. MORDOR_TEST_ASSERT_EQUAL(uri, "http://localhost/hi/bye/");
  346. uri.path.append(std::string());
  347. MORDOR_TEST_ASSERT_EQUAL(uri, "http://localhost/hi/bye/");
  348. uri = "http:";
  349. uri.path.append("hi");
  350. MORDOR_TEST_ASSERT_EQUAL(uri, "http:hi");
  351. uri.path.append("bye");
  352. MORDOR_TEST_ASSERT_EQUAL(uri, "http:hi/bye");
  353. URI::Path path;
  354. path.append("hi");
  355. MORDOR_TEST_ASSERT_EQUAL(path, "hi");
  356. path.append("bye");
  357. MORDOR_TEST_ASSERT_EQUAL(path, "hi/bye");
  358. // The following four tests ensure that path's hidden pointer back to the
  359. // URI owning it is copied/assigned correctly through URI and Path's
  360. // copy constructors and assignement operators
  361. // Force operator=(const URI &uri)
  362. URI uri2 = "http://localhost";
  363. uri = uri2;
  364. uri2.authority.hostDefined(false);
  365. uri.path.append("hi");
  366. MORDOR_TEST_ASSERT_EQUAL(uri, "http://localhost/hi");
  367. // Force URI::URI(const URI &uri)
  368. uri2.authority.host("localhost");
  369. URI uri3(uri2);
  370. uri2.authority.hostDefined(false);
  371. uri3.path.append("hi");
  372. MORDOR_TEST_ASSERT_EQUAL(uri3, "http://localhost/hi");
  373. // Force operator=(const Path &path)
  374. path = uri3.path;
  375. path.segments.clear();
  376. path.append("hi");
  377. MORDOR_TEST_ASSERT_EQUAL(path, "hi");
  378. // Force Path::Path(const Path &path)
  379. URI::Path path2(uri3.path);
  380. path2.segments.clear();
  381. path2.append("hi");
  382. MORDOR_TEST_ASSERT_EQUAL(path2, "hi");
  383. }
  384. MORDOR_UNITTEST(URI, makeAbsolute)
  385. {
  386. URI::Path path;
  387. path.makeAbsolute();
  388. MORDOR_TEST_ASSERT_EQUAL(path, "/");
  389. path.makeAbsolute();
  390. MORDOR_TEST_ASSERT_EQUAL(path, "/");
  391. path.makeRelative();
  392. MORDOR_TEST_ASSERT(path.segments.empty());
  393. path.makeRelative();
  394. MORDOR_TEST_ASSERT(path.segments.empty());
  395. path.append("hi");
  396. MORDOR_TEST_ASSERT_EQUAL(path, "hi");
  397. path.makeAbsolute();
  398. MORDOR_TEST_ASSERT_EQUAL(path, "/hi");
  399. path.makeAbsolute();
  400. MORDOR_TEST_ASSERT_EQUAL(path, "/hi");
  401. path.makeRelative();
  402. MORDOR_TEST_ASSERT_EQUAL(path, "hi");
  403. path.makeRelative();
  404. MORDOR_TEST_ASSERT_EQUAL(path, "hi");
  405. }
  406. MORDOR_UNITTEST(URI, queryStringConvenience)
  407. {
  408. URI::QueryString qs;
  409. qs["a"] = "1";
  410. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  411. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=1");
  412. qs.insert(std::make_pair("a", "2"));
  413. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 2u);
  414. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=1&a=2");
  415. // Returns only the first item (removing others)
  416. qs["a"] = "3";
  417. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 1u);
  418. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3");
  419. qs.insert(std::make_pair("a", "4"));
  420. // Implicitly creates the item, even though we don't assign to it
  421. MORDOR_TEST_ASSERT(qs["b"].empty());
  422. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 3u);
  423. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3&a=4&b");
  424. // Make sure we're using equal_range correctly (insert between existing elements)
  425. qs["a2"] = "5";
  426. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 4u);
  427. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3&a=4&a2=5&b");
  428. // Create a const reference, so we call the const version
  429. const URI::QueryString &qs2 = qs;
  430. // Does not create a new item
  431. MORDOR_TEST_ASSERT(qs2["c"].empty());
  432. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 4u);
  433. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3&a=4&a2=5&b");
  434. // Returns the first item, but keeps the rest
  435. MORDOR_TEST_ASSERT_EQUAL(qs2["a"], "3");
  436. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 4u);
  437. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3&a=4&a2=5&b");
  438. // Does not create a new item (in between)
  439. MORDOR_TEST_ASSERT(qs2["a3"].empty());
  440. MORDOR_TEST_ASSERT_EQUAL(qs.size(), 4u);
  441. MORDOR_TEST_ASSERT_EQUAL(qs.toString(), "a=3&a=4&a2=5&b");
  442. }
  443. MORDOR_UNITTEST(URI, decodeOK)
  444. {
  445. std::string str("https%3A%2F%2Fredmine.mozycorp.com%2Fissues%2F118200");
  446. MORDOR_TEST_ASSERT_EQUAL(URI::decode(str), "https://redmine.mozycorp.com/issues/118200");
  447. }
  448. MORDOR_UNITTEST(URI, decodeInvalidURL)
  449. {
  450. std::vector<std::string> invalidStr;
  451. invalidStr.push_back("%4"); // invalid length, should have 2 chars after %
  452. invalidStr.push_back("%3xpath"); // invalid first char after %
  453. invalidStr.push_back("%x3path"); // invalid second char after %
  454. for (std::vector<std::string>::iterator it = invalidStr.begin(); it != invalidStr.end(); ++it) {
  455. MORDOR_TEST_ASSERT_EXCEPTION(URI::decode(*it), std::invalid_argument);
  456. }
  457. }