url_ut.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "url.h"
  2. #include <util/string/cast.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. Y_UNIT_TEST_SUITE(TUtilUrlTest) {
  5. Y_UNIT_TEST(TestGetHostAndGetHostAndPort) {
  6. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru/bebe"));
  7. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe"));
  8. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru"));
  9. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru"));
  10. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru:8080"));
  11. UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080"));
  12. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru/bebe:8080"));
  13. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe:8080"));
  14. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru:8080/bebe"));
  15. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("https://ya.ru:8080/bebe"));
  16. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetHost("www.ya.ru:8080/bebe"));
  17. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetHost("https://www.ya.ru:8080/bebe"));
  18. UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080/bebe"));
  19. // irl RFC3986 sometimes gets ignored
  20. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetHost("pravda-kmv.ru?page=news&id=6973"));
  21. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetHostAndPort("pravda-kmv.ru?page=news&id=6973"));
  22. // check simple string
  23. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetHost("some_blender_url"));
  24. UNIT_ASSERT_VALUES_EQUAL("", GetHost(""));
  25. }
  26. Y_UNIT_TEST(TestGetSchemeHostAndPortWithoutSplit) {
  27. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("ya.ru/bebe"));
  28. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("http://ya.ru/bebe"));
  29. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHostAndPort("http://ya.ru/bebe", /*trimHttp*/false));
  30. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHostAndPort("http://ya.ru/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  31. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetSchemeHostAndPort("http://www.ya.ru/bebe"));
  32. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru", GetSchemeHostAndPort("http://www.ya.ru/bebe", /*trimHttp*/false));
  33. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru", GetSchemeHostAndPort("http://www.ya.ru/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  34. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHostAndPort("https://ya.ru/bebe"));
  35. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHostAndPort("https://ya.ru/bebe", /*trimHttp*/false));
  36. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHostAndPort("https://ya.ru/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  37. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHostAndPort("ftp://ya.ru/bebe"));
  38. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHostAndPort("ftp://ya.ru/bebe", /*trimHttp*/false));
  39. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHostAndPort("ftp://ya.ru/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  40. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("ya.ru:80/bebe"));
  41. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("http://ya.ru:80/bebe"));
  42. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru:81", GetSchemeHostAndPort("http://ya.ru:81/bebe", /*trimHttp*/false));
  43. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru:81", GetSchemeHostAndPort("http://ya.ru:81/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  44. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetSchemeHostAndPort("http://www.ya.ru:80/bebe"));
  45. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru", GetSchemeHostAndPort("http://www.ya.ru:80/bebe", /*trimHttp*/false));
  46. UNIT_ASSERT_VALUES_EQUAL("https://www.ya.ru", GetSchemeHostAndPort("https://www.ya.ru:443/bebe", /*trimHttp*/false));
  47. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru:80", GetSchemeHostAndPort("http://www.ya.ru:80/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  48. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHostAndPort("https://ya.ru:443/bebe"));
  49. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru:444", GetSchemeHostAndPort("https://ya.ru:444/bebe", /*trimHttp*/false));
  50. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru:444", GetSchemeHostAndPort("https://ya.ru:444/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  51. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru:8080", GetSchemeHostAndPort("ftp://ya.ru:8080/bebe"));
  52. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru:1234", GetSchemeHostAndPort("ftp://ya.ru:1234/bebe", /*trimHttp*/false));
  53. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru:80", GetSchemeHostAndPort("ftp://ya.ru:80/bebe", /*trimHttp*/false));
  54. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru:80", GetSchemeHostAndPort("ftp://ya.ru:80/bebe", /*trimHttp*/false, /*trimDefaultPort*/false));
  55. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("ya.ru:80"));
  56. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("ya.ru"));
  57. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHostAndPort("http://ya.ru:80"));
  58. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHostAndPort("http://ya.ru:80", /*trimHttp*/false));
  59. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru:81", GetSchemeHostAndPort("http://ya.ru:81", /*trimHttp*/false));
  60. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru:81", GetSchemeHostAndPort("http://ya.ru:81", /*trimHttp*/false, /*trimDefaultPort*/false));
  61. // irl RFC3986 sometimes gets ignored
  62. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetSchemeHostAndPort("pravda-kmv.ru?page=news&id=6973"));
  63. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetSchemeHostAndPort("pravda-kmv.ru?page=news&id=6973", /*trimHttp*/false));
  64. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetSchemeHostAndPort("pravda-kmv.ru?page=news&id=6973", /*trimHttp*/false, /*trimDefaultPort*/false));
  65. // check simple string
  66. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetSchemeHostAndPort("some_blender_url"));
  67. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetSchemeHostAndPort("some_blender_url", /*trimHttp*/false));
  68. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetSchemeHostAndPort("some_blender_url", /*trimHttp*/false, /*trimDefaultPort*/false));
  69. UNIT_ASSERT_VALUES_EQUAL("", GetSchemeHostAndPort(""));
  70. UNIT_ASSERT_VALUES_EQUAL("", GetSchemeHostAndPort("", /*trimHttp*/false));
  71. UNIT_ASSERT_VALUES_EQUAL("", GetSchemeHostAndPort("", /*trimHttp*/false, /*trimDefaultPort*/false));
  72. }
  73. Y_UNIT_TEST(TestGetSchemeHost) {
  74. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("ya.ru/bebe"));
  75. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("http://ya.ru/bebe"));
  76. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHost("http://ya.ru/bebe", /*trimHttp*/false));
  77. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetSchemeHost("http://www.ya.ru/bebe"));
  78. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru", GetSchemeHost("http://www.ya.ru/bebe", /*trimHttp*/false));
  79. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHost("https://ya.ru/bebe"));
  80. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHost("https://ya.ru/bebe", /*trimHttp*/false));
  81. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHost("ftp://ya.ru/bebe"));
  82. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHost("ftp://ya.ru/bebe", /*trimHttp*/false));
  83. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("ya.ru:80/bebe"));
  84. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("http://ya.ru:80/bebe"));
  85. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHost("http://ya.ru:81/bebe", /*trimHttp*/false));
  86. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetSchemeHost("http://www.ya.ru:80/bebe"));
  87. UNIT_ASSERT_VALUES_EQUAL("http://www.ya.ru", GetSchemeHost("http://www.ya.ru:80/bebe", /*trimHttp*/false));
  88. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHost("https://ya.ru:443/bebe"));
  89. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru", GetSchemeHost("https://ya.ru:444/bebe", /*trimHttp*/false));
  90. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHost("ftp://ya.ru:8080/bebe"));
  91. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", GetSchemeHost("ftp://ya.ru:1234/bebe", /*trimHttp*/false));
  92. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("ya.ru:80"));
  93. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("ya.ru"));
  94. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetSchemeHost("http://ya.ru:80"));
  95. UNIT_ASSERT_VALUES_EQUAL("http://ya.ru", GetSchemeHost("http://ya.ru:81", /*trimHttp*/false));
  96. // irl RFC3986 sometimes gets ignored
  97. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetSchemeHost("pravda-kmv.ru?page=news&id=6973"));
  98. UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetSchemeHost("pravda-kmv.ru?page=news&id=6973", /*trimHttp*/ false));
  99. // check simple string
  100. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetSchemeHost("some_blender_url"));
  101. UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetSchemeHost("some_blender_url", /*trimHttp*/ false));
  102. UNIT_ASSERT_VALUES_EQUAL("", GetSchemeHost(""));
  103. UNIT_ASSERT_VALUES_EQUAL("", GetSchemeHost("", /*trimHttp*/ false));
  104. }
  105. Y_UNIT_TEST(TestGetPathAndQuery) {
  106. UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org"));
  107. UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/"));
  108. UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org:8080"));
  109. UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("ru.wikipedia.org/index.php?123/"));
  110. UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("http://ru.wikipedia.org:8080"));
  111. UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("https://ru.wikipedia.org/index.php?123/"));
  112. UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/#comment"));
  113. UNIT_ASSERT_VALUES_EQUAL("/?1", GetPathAndQuery("ru.wikipedia.org/?1#comment"));
  114. UNIT_ASSERT_VALUES_EQUAL("/?1#comment", GetPathAndQuery("ru.wikipedia.org/?1#comment", false));
  115. }
  116. Y_UNIT_TEST(TestGetDomain) {
  117. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("www.ya.ru"));
  118. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("ya.ru"));
  119. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("a.b.ya.ru"));
  120. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("ya.ru"));
  121. UNIT_ASSERT_VALUES_EQUAL("ya", GetDomain("ya"));
  122. UNIT_ASSERT_VALUES_EQUAL("", GetDomain(""));
  123. }
  124. Y_UNIT_TEST(TestGetParentDomain) {
  125. UNIT_ASSERT_VALUES_EQUAL("", GetParentDomain("www.ya.ru", 0));
  126. UNIT_ASSERT_VALUES_EQUAL("ru", GetParentDomain("www.ya.ru", 1));
  127. UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetParentDomain("www.ya.ru", 2));
  128. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetParentDomain("www.ya.ru", 3));
  129. UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetParentDomain("www.ya.ru", 4));
  130. UNIT_ASSERT_VALUES_EQUAL("com", GetParentDomain("ya.com", 1));
  131. UNIT_ASSERT_VALUES_EQUAL("ya.com", GetParentDomain("ya.com", 2));
  132. UNIT_ASSERT_VALUES_EQUAL("RU", GetParentDomain("RU", 1));
  133. UNIT_ASSERT_VALUES_EQUAL("RU", GetParentDomain("RU", 2));
  134. UNIT_ASSERT_VALUES_EQUAL("", GetParentDomain("", 0));
  135. UNIT_ASSERT_VALUES_EQUAL("", GetParentDomain("", 1));
  136. }
  137. Y_UNIT_TEST(TestGetZone) {
  138. UNIT_ASSERT_VALUES_EQUAL("ru", GetZone("www.ya.ru"));
  139. UNIT_ASSERT_VALUES_EQUAL("com", GetZone("ya.com"));
  140. UNIT_ASSERT_VALUES_EQUAL("RU", GetZone("RU"));
  141. UNIT_ASSERT_VALUES_EQUAL("FHFBN", GetZone("ya.FHFBN"));
  142. UNIT_ASSERT_VALUES_EQUAL("", GetZone(""));
  143. }
  144. Y_UNIT_TEST(TestAddSchemePrefix) {
  145. UNIT_ASSERT_VALUES_EQUAL("http://yandex.ru", AddSchemePrefix("yandex.ru"));
  146. UNIT_ASSERT_VALUES_EQUAL("http://yandex.ru", AddSchemePrefix("http://yandex.ru"));
  147. UNIT_ASSERT_VALUES_EQUAL("https://yandex.ru", AddSchemePrefix("https://yandex.ru"));
  148. UNIT_ASSERT_VALUES_EQUAL("file://yandex.ru", AddSchemePrefix("file://yandex.ru"));
  149. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", AddSchemePrefix("ya.ru", "ftp"));
  150. }
  151. Y_UNIT_TEST(TestSchemeGet) {
  152. UNIT_ASSERT_VALUES_EQUAL("http://", GetSchemePrefix("http://ya.ru/bebe"));
  153. UNIT_ASSERT_VALUES_EQUAL("", GetSchemePrefix("yaru"));
  154. UNIT_ASSERT_VALUES_EQUAL("yaru://", GetSchemePrefix("yaru://ya.ru://zzz"));
  155. UNIT_ASSERT_VALUES_EQUAL("", GetSchemePrefix("ya.ru://zzz"));
  156. UNIT_ASSERT_VALUES_EQUAL("ftp://", GetSchemePrefix("ftp://ya.ru://zzz"));
  157. UNIT_ASSERT_VALUES_EQUAL("https://", GetSchemePrefix("https://")); // is that right?
  158. }
  159. Y_UNIT_TEST(TestSchemeCut) {
  160. UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutSchemePrefix("http://ya.ru/bebe"));
  161. UNIT_ASSERT_VALUES_EQUAL("yaru", CutSchemePrefix("yaru"));
  162. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("yaru://ya.ru://zzz"));
  163. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ya.ru://zzz"));
  164. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutSchemePrefix("ftp://ya.ru://zzz"));
  165. UNIT_ASSERT_VALUES_EQUAL("", CutSchemePrefix("https://")); // is that right?
  166. UNIT_ASSERT_VALUES_EQUAL("ftp://ya.ru", CutHttpPrefix("ftp://ya.ru"));
  167. UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("http://ya.ru/zzz"));
  168. UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("http://ya.ru/zzz", true));
  169. UNIT_ASSERT_VALUES_EQUAL("ya.ru/zzz", CutHttpPrefix("https://ya.ru/zzz"));
  170. UNIT_ASSERT_VALUES_EQUAL("https://ya.ru/zzz", CutHttpPrefix("https://ya.ru/zzz", true));
  171. UNIT_ASSERT_VALUES_EQUAL("", CutHttpPrefix("https://")); // is that right?
  172. UNIT_ASSERT_VALUES_EQUAL("https://", CutHttpPrefix("https://", true)); // is that right?
  173. }
  174. Y_UNIT_TEST(TestMisc) {
  175. UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("www."));
  176. UNIT_ASSERT_VALUES_EQUAL("", CutWWWPrefix("WwW."));
  177. UNIT_ASSERT_VALUES_EQUAL("www", CutWWWPrefix("www"));
  178. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWPrefix("www.ya.ru"));
  179. UNIT_ASSERT_VALUES_EQUAL("", CutWWWNumberedPrefix("www."));
  180. UNIT_ASSERT_VALUES_EQUAL("www", CutWWWNumberedPrefix("www"));
  181. UNIT_ASSERT_VALUES_EQUAL("www27", CutWWWNumberedPrefix("www27"));
  182. UNIT_ASSERT_VALUES_EQUAL("", CutWWWNumberedPrefix("www27."));
  183. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWNumberedPrefix("www.ya.ru"));
  184. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWNumberedPrefix("www2.ya.ru"));
  185. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWNumberedPrefix("www12.ya.ru"));
  186. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutWWWNumberedPrefix("ww2.ya.ru"));
  187. UNIT_ASSERT_VALUES_EQUAL("w1w2w3.ya.ru", CutWWWNumberedPrefix("w1w2w3.ya.ru"));
  188. UNIT_ASSERT_VALUES_EQUAL("123.ya.ru", CutWWWNumberedPrefix("123.ya.ru"));
  189. UNIT_ASSERT_VALUES_EQUAL("", CutMPrefix("m."));
  190. UNIT_ASSERT_VALUES_EQUAL("", CutMPrefix("M."));
  191. UNIT_ASSERT_VALUES_EQUAL("m", CutMPrefix("m"));
  192. UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutMPrefix("m.ya.ru"));
  193. }
  194. Y_UNIT_TEST(TestSplitUrlToHostAndPath) {
  195. TStringBuf host, path;
  196. SplitUrlToHostAndPath("https://yandex.ru/yandsearch", host, path);
  197. UNIT_ASSERT_STRINGS_EQUAL(host, "https://yandex.ru");
  198. UNIT_ASSERT_STRINGS_EQUAL(path, "/yandsearch");
  199. SplitUrlToHostAndPath("yandex.ru/yandsearch", host, path);
  200. UNIT_ASSERT_STRINGS_EQUAL(host, "yandex.ru");
  201. UNIT_ASSERT_STRINGS_EQUAL(path, "/yandsearch");
  202. SplitUrlToHostAndPath("https://yandex.ru", host, path);
  203. UNIT_ASSERT_STRINGS_EQUAL(host, "https://yandex.ru");
  204. UNIT_ASSERT_STRINGS_EQUAL(path, "");
  205. SplitUrlToHostAndPath("invalid url /", host, path);
  206. UNIT_ASSERT_STRINGS_EQUAL(host, "invalid url ");
  207. UNIT_ASSERT_STRINGS_EQUAL(path, "/");
  208. SplitUrlToHostAndPath("some_blender_url", host, path);
  209. UNIT_ASSERT_STRINGS_EQUAL(host, "some_blender_url");
  210. UNIT_ASSERT_STRINGS_EQUAL(path, "");
  211. }
  212. Y_UNIT_TEST(TestSeparateUrlFromQueryAndFragment) {
  213. TStringBuf sanitizedUrl, query, fragment;
  214. SeparateUrlFromQueryAndFragment("https://yandex.ru/yandsearch", sanitizedUrl, query, fragment);
  215. UNIT_ASSERT_STRINGS_EQUAL(sanitizedUrl, "https://yandex.ru/yandsearch");
  216. UNIT_ASSERT_STRINGS_EQUAL(query, "");
  217. UNIT_ASSERT_STRINGS_EQUAL(fragment, "");
  218. SeparateUrlFromQueryAndFragment("https://yandex.ru/yandsearch?param1=val1&param2=val2", sanitizedUrl, query, fragment);
  219. UNIT_ASSERT_STRINGS_EQUAL(sanitizedUrl, "https://yandex.ru/yandsearch");
  220. UNIT_ASSERT_STRINGS_EQUAL(query, "param1=val1&param2=val2");
  221. UNIT_ASSERT_STRINGS_EQUAL(fragment, "");
  222. SeparateUrlFromQueryAndFragment("https://yandex.ru/yandsearch#fragment", sanitizedUrl, query, fragment);
  223. UNIT_ASSERT_STRINGS_EQUAL(sanitizedUrl, "https://yandex.ru/yandsearch");
  224. UNIT_ASSERT_STRINGS_EQUAL(query, "");
  225. UNIT_ASSERT_STRINGS_EQUAL(fragment, "fragment");
  226. SeparateUrlFromQueryAndFragment("https://yandex.ru/yandsearch?param1=val1&param2=val2#fragment", sanitizedUrl, query, fragment);
  227. UNIT_ASSERT_STRINGS_EQUAL(sanitizedUrl, "https://yandex.ru/yandsearch");
  228. UNIT_ASSERT_STRINGS_EQUAL(query, "param1=val1&param2=val2");
  229. UNIT_ASSERT_STRINGS_EQUAL(fragment, "fragment");
  230. }
  231. Y_UNIT_TEST(TestGetSchemeHostAndPort) {
  232. { // all components are present
  233. TStringBuf scheme("unknown"), host("unknown");
  234. ui16 port = 0;
  235. GetSchemeHostAndPort("https://ya.ru:8080/bebe", scheme, host, port);
  236. UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
  237. UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
  238. UNIT_ASSERT_VALUES_EQUAL(port, 8080);
  239. }
  240. { // scheme is abset
  241. TStringBuf scheme("unknown"), host("unknown");
  242. ui16 port = 0;
  243. GetSchemeHostAndPort("ya.ru:8080/bebe", scheme, host, port);
  244. UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
  245. UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
  246. UNIT_ASSERT_VALUES_EQUAL(port, 8080);
  247. }
  248. { // scheme and port are absent
  249. TStringBuf scheme("unknown"), host("unknown");
  250. ui16 port = 0;
  251. GetSchemeHostAndPort("ya.ru/bebe", scheme, host, port);
  252. UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
  253. UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
  254. UNIT_ASSERT_VALUES_EQUAL(port, 0);
  255. }
  256. { // port is absent, but returned its default value for HTTP
  257. TStringBuf scheme("unknown"), host("unknown");
  258. ui16 port = 0;
  259. GetSchemeHostAndPort("http://ya.ru/bebe", scheme, host, port);
  260. UNIT_ASSERT_VALUES_EQUAL(scheme, "http://");
  261. UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
  262. UNIT_ASSERT_VALUES_EQUAL(port, 80);
  263. }
  264. { // port is absent, but returned its default value for HTTPS
  265. TStringBuf scheme("unknown"), host("unknown");
  266. ui16 port = 0;
  267. GetSchemeHostAndPort("https://ya.ru/bebe", scheme, host, port);
  268. UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
  269. UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
  270. UNIT_ASSERT_VALUES_EQUAL(port, 443);
  271. }
  272. { // ipv6
  273. TStringBuf scheme("unknown"), host("unknown");
  274. ui16 port = 0;
  275. GetSchemeHostAndPort("https://[1080:0:0:0:8:800:200C:417A]:443/bebe", scheme, host, port);
  276. UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
  277. UNIT_ASSERT_VALUES_EQUAL(host, "[1080:0:0:0:8:800:200C:417A]");
  278. UNIT_ASSERT_VALUES_EQUAL(port, 443);
  279. }
  280. { // ipv6
  281. TStringBuf scheme("unknown"), host("unknown");
  282. ui16 port = 0;
  283. GetSchemeHostAndPort("[::1]/bebe", scheme, host, port);
  284. UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
  285. UNIT_ASSERT_VALUES_EQUAL(host, "[::1]");
  286. UNIT_ASSERT_VALUES_EQUAL(port, 0);
  287. }
  288. { // ipv6
  289. TStringBuf scheme("unknown"), host("unknown");
  290. ui16 port = 0;
  291. GetSchemeHostAndPort("unknown:///bebe", scheme, host, port);
  292. UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown://");
  293. UNIT_ASSERT_VALUES_EQUAL(host, "");
  294. UNIT_ASSERT_VALUES_EQUAL(port, 0);
  295. }
  296. // port overflow
  297. auto testCase = []() {
  298. TStringBuf scheme("unknown"), host("unknown");
  299. ui16 port = 0;
  300. GetSchemeHostAndPort("https://ya.ru:65536/bebe", scheme, host, port);
  301. };
  302. UNIT_ASSERT_EXCEPTION(testCase(), yexception);
  303. }
  304. Y_UNIT_TEST(TestCutUrlPrefixes) {
  305. UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutUrlPrefixes("http://ya.ru/bebe"));
  306. UNIT_ASSERT_VALUES_EQUAL("yaru", CutUrlPrefixes("yaru"));
  307. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("yaru://ya.ru://zzz"));
  308. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("ya.ru://zzz"));
  309. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("ftp://ya.ru://zzz"));
  310. UNIT_ASSERT_VALUES_EQUAL("", CutUrlPrefixes("https://"));
  311. UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutUrlPrefixes("https://www.ya.ru/bebe"));
  312. UNIT_ASSERT_VALUES_EQUAL("yaru", CutUrlPrefixes("www.yaru"));
  313. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("yaru://www.ya.ru://zzz"));
  314. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("www.ya.ru://zzz"));
  315. UNIT_ASSERT_VALUES_EQUAL("ya.ru://zzz", CutUrlPrefixes("ftp://www.ya.ru://zzz"));
  316. UNIT_ASSERT_VALUES_EQUAL("", CutUrlPrefixes("http://www."));
  317. }
  318. Y_UNIT_TEST(TestUrlPathStartWithToken) {
  319. UNIT_ASSERT_VALUES_EQUAL(true, DoesUrlPathStartWithToken("http://ya.ru/bebe/zzz", "bebe"));
  320. UNIT_ASSERT_VALUES_EQUAL(true, DoesUrlPathStartWithToken("http://ya.ru/bebe?zzz", "bebe"));
  321. UNIT_ASSERT_VALUES_EQUAL(true, DoesUrlPathStartWithToken("http://ya.ru/bebe/", "bebe"));
  322. UNIT_ASSERT_VALUES_EQUAL(true, DoesUrlPathStartWithToken("http://ya.ru/bebe?", "bebe"));
  323. UNIT_ASSERT_VALUES_EQUAL(true, DoesUrlPathStartWithToken("https://ya.ru/bebe", "bebe"));
  324. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("http://ya.ru/bebezzz", "bebe"));
  325. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("http://ya.ru/bebe.zzz", "bebe"));
  326. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("http://ya.ru/", "bebe"));
  327. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("http://ya.ru", "bebe"));
  328. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("http://bebe", "bebe"));
  329. UNIT_ASSERT_VALUES_EQUAL(false, DoesUrlPathStartWithToken("https://bebe/", "bebe"));
  330. }
  331. }