2
0

unit-uri.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <catch2/catch.hpp>
  2. #include <iostream>
  3. #include <gul/uri.h>
  4. using URI = gul::uri;
  5. SCENARIO("regex test 1")
  6. {
  7. // userinfo host port
  8. // ┌──┴───┐ ┌──────┴──────┐ ┌┴┐
  9. std::string str = "https://[email protected]:123/forum/questions/?tag=networking&order=newest#top";
  10. //└─┬─┘ └───────────┬──────────────┘└───────┬───────┘ └───────────┬─────────────┘ └┬┘
  11. //scheme authority path query fragment
  12. URI uri(str);
  13. REQUIRE( uri.scheme == "https");
  14. REQUIRE( uri.user == "john.doe");
  15. REQUIRE( uri.host == "www.example.com");
  16. REQUIRE( uri.password == "");
  17. REQUIRE( uri.port == "123");
  18. REQUIRE( uri.path == "/forum/questions/");
  19. REQUIRE( uri.query == "tag=networking&order=newest");
  20. REQUIRE( uri.fragment == "top");
  21. REQUIRE( uri.toString() == str);
  22. }
  23. SCENARIO("regex test 2")
  24. {
  25. std::string str = "ldap://[2001:db8::7]/c=GB?objectClass?one";
  26. // └┬─┘ └─────┬─────┘└─┬─┘ └──────┬──────┘
  27. // scheme authority path query
  28. URI uri(str);
  29. REQUIRE( uri.scheme == "ldap");
  30. REQUIRE( uri.user == "");
  31. REQUIRE( uri.host == "[2001:db8::7]");
  32. REQUIRE( uri.password == "");
  33. REQUIRE( uri.port == "");
  34. REQUIRE( uri.path == "/c=GB");
  35. REQUIRE( uri.query == "objectClass?one");
  36. REQUIRE( uri.fragment == "");
  37. REQUIRE( uri.toString() == str);
  38. }
  39. SCENARIO("regex test 3")
  40. {
  41. std::string str = "urn:oasis:names:specification:docbook:dtd:xml:4.1.2";
  42. // └┬┘ └──────────────────────┬──────────────────────┘
  43. // scheme path
  44. URI uri(str);
  45. REQUIRE( uri.scheme == "urn");
  46. REQUIRE( uri.user == "");
  47. REQUIRE( uri.host == "");
  48. REQUIRE( uri.password == "");
  49. REQUIRE( uri.port == "");
  50. REQUIRE( uri.path == "oasis:names:specification:docbook:dtd:xml:4.1.2");
  51. REQUIRE( uri.query == "");
  52. REQUIRE( uri.fragment == "");
  53. REQUIRE( uri.toString() == str);
  54. }
  55. SCENARIO("regex test 4")
  56. {
  57. std::string str = "data:application/octet-stream;base64,AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA";
  58. // └┬┘ └──────────────────────┬───────────────────────────────────────┘
  59. // scheme path
  60. URI uri(str);
  61. REQUIRE( uri.scheme == "data");
  62. REQUIRE( uri.user == "");
  63. REQUIRE( uri.host == "");
  64. REQUIRE( uri.password == "");
  65. REQUIRE( uri.port == "");
  66. REQUIRE( uri.path == "application/octet-stream;base64,AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA");
  67. REQUIRE( uri.query == "");
  68. REQUIRE( uri.fragment == "");
  69. REQUIRE( uri.toString() == str);
  70. }
  71. SCENARIO("regex test 5")
  72. {
  73. std::string str = "file:/test.txt";
  74. // └┬┘ └──────────────────────┬───────────────────────────────────────┘
  75. // scheme path
  76. URI uri(str);
  77. REQUIRE( uri.scheme == "file");
  78. REQUIRE( uri.user == "");
  79. REQUIRE( uri.host == "");
  80. REQUIRE( uri.password == "");
  81. REQUIRE( uri.port == "");
  82. REQUIRE( uri.path == "/test.txt");
  83. REQUIRE( uri.query == "");
  84. REQUIRE( uri.fragment == "");
  85. REQUIRE( uri.toString() == str);
  86. }
  87. SCENARIO("regex test 6: ssh absolute path")
  88. {
  89. std::string str = "ssh://username:password@localhost/home/user";
  90. // └┬┘ └──────────────────────┬───────────────────────────────────────┘
  91. // scheme path
  92. URI uri(str);
  93. REQUIRE( uri.scheme == "ssh");
  94. REQUIRE( uri.user == "username");
  95. REQUIRE( uri.password == "password");
  96. REQUIRE( uri.host == "localhost");
  97. REQUIRE( uri.port == "");
  98. REQUIRE( uri.path == "/home/user");
  99. REQUIRE( uri.query == "");
  100. REQUIRE( uri.fragment == "");
  101. REQUIRE( uri.toString() == str);
  102. }
  103. SCENARIO("regex test 7: testing paths and authority")
  104. {
  105. {
  106. std::string str = "file:///this.txt";
  107. URI uri(str);
  108. REQUIRE( uri.scheme == "file");
  109. REQUIRE( uri.getAuthority() == "");
  110. REQUIRE( uri.user == "");
  111. REQUIRE( uri.password == "");
  112. REQUIRE( uri.host == "");
  113. REQUIRE( uri.port == "");
  114. REQUIRE( uri.path == "/this.txt");
  115. REQUIRE( uri.query == "");
  116. REQUIRE( uri.fragment == "");
  117. }
  118. // This is an invalid
  119. {
  120. std::string str = "file://this.txt";
  121. URI uri(str);
  122. REQUIRE( uri.scheme == "file");
  123. REQUIRE( uri.getAuthority() == "this.txt");
  124. REQUIRE( uri.user == "");
  125. REQUIRE( uri.password == "");
  126. REQUIRE( uri.host == "this.txt");
  127. REQUIRE( uri.port == "");
  128. REQUIRE( uri.path == "");
  129. REQUIRE( uri.query == "");
  130. REQUIRE( uri.fragment == "");
  131. }
  132. }
  133. SCENARIO("Data URI")
  134. {
  135. //data:<mediatype>[;base64],<data>
  136. std::string str = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";
  137. URI uri(str);
  138. REQUIRE( uri.scheme == "data");
  139. REQUIRE( uri.path == "text/plain;base64,SGVsbG8sIFdvcmxkIQ==");
  140. THEN("We can get string views to parts of the path")
  141. {
  142. REQUIRE( uri.getMediaEncoding() == "text/plain;base64");
  143. REQUIRE( uri.getMediaType() == "base64");
  144. REQUIRE( uri.getMediaData() == "SGVsbG8sIFdvcmxkIQ==");
  145. }
  146. }