UrlTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #include "UrlTest.hpp"
  25. #include "oatpp/network/Url.hpp"
  26. #include "oatpp-test/Checker.hpp"
  27. namespace oatpp { namespace test { namespace network {
  28. void UrlTest::onRun() {
  29. typedef oatpp::network::Url Url;
  30. {
  31. const char* urlText = "http://root@127.0.0.1:8000/path/to/resource/?q1=1&q2=2";
  32. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  33. auto url = Url::Parser::parseUrl(urlText);
  34. OATPP_ASSERT(url.scheme && url.scheme == "http");
  35. OATPP_ASSERT(url.authority.userInfo && url.authority.userInfo == "root");
  36. OATPP_ASSERT(url.authority.host && url.authority.host == "127.0.0.1");
  37. OATPP_ASSERT(url.authority.port == 8000);
  38. OATPP_ASSERT(url.path && url.path == "/path/to/resource/");
  39. OATPP_ASSERT(url.queryParams.getSize() == 2);
  40. OATPP_ASSERT(url.queryParams.get("q1") == "1");
  41. OATPP_ASSERT(url.queryParams.get("q2") == "2");
  42. }
  43. {
  44. const char* urlText = "ftp://root@oatpp.io:8000/path/to/resource?q1=1&q2=2";
  45. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  46. auto url = Url::Parser::parseUrl(urlText);
  47. OATPP_ASSERT(url.scheme && url.scheme == "ftp");
  48. OATPP_ASSERT(url.authority.userInfo && url.authority.userInfo == "root");
  49. OATPP_ASSERT(url.authority.host && url.authority.host == "oatpp.io");
  50. OATPP_ASSERT(url.authority.port == 8000);
  51. OATPP_ASSERT(url.path && url.path == "/path/to/resource");
  52. OATPP_ASSERT(url.queryParams.getSize() == 2);
  53. OATPP_ASSERT(url.queryParams.get("q1") == "1");
  54. OATPP_ASSERT(url.queryParams.get("q2") == "2");
  55. }
  56. {
  57. const char* urlText = "https://oatpp.io/?q1=1&q2=2";
  58. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  59. auto url = Url::Parser::parseUrl(urlText);
  60. OATPP_ASSERT(url.scheme && url.scheme == "https");
  61. OATPP_ASSERT(url.authority.userInfo == nullptr);
  62. OATPP_ASSERT(url.authority.host && url.authority.host == "oatpp.io");
  63. OATPP_ASSERT(url.authority.port == -1);
  64. OATPP_ASSERT(url.path && url.path == "/");
  65. OATPP_ASSERT(url.queryParams.getSize() == 2);
  66. OATPP_ASSERT(url.queryParams.get("q1") == "1");
  67. OATPP_ASSERT(url.queryParams.get("q2") == "2");
  68. }
  69. {
  70. const char* urlText = "https://oatpp.io/";
  71. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  72. auto url = Url::Parser::parseUrl(urlText);
  73. OATPP_ASSERT(url.scheme && url.scheme == "https");
  74. OATPP_ASSERT(url.authority.userInfo == nullptr);
  75. OATPP_ASSERT(url.authority.host && url.authority.host == "oatpp.io");
  76. OATPP_ASSERT(url.authority.port == -1);
  77. OATPP_ASSERT(url.path && url.path == "/");
  78. OATPP_ASSERT(url.queryParams.getSize() == 0);
  79. }
  80. {
  81. const char* urlText = "https://oatpp.io";
  82. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  83. auto url = Url::Parser::parseUrl(urlText);
  84. OATPP_ASSERT(url.scheme && url.scheme == "https");
  85. OATPP_ASSERT(url.authority.userInfo == nullptr);
  86. OATPP_ASSERT(url.authority.host && url.authority.host == "oatpp.io");
  87. OATPP_ASSERT(url.authority.port == -1);
  88. OATPP_ASSERT(url.path == nullptr);
  89. OATPP_ASSERT(url.queryParams.getSize() == 0);
  90. }
  91. {
  92. const char* urlText = "oatpp.io";
  93. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  94. auto url = Url::Parser::parseUrl(urlText);
  95. OATPP_ASSERT(url.scheme == nullptr);
  96. OATPP_ASSERT(url.authority.userInfo == nullptr);
  97. OATPP_ASSERT(url.authority.host && url.authority.host == "oatpp.io");
  98. OATPP_ASSERT(url.authority.port == -1);
  99. OATPP_ASSERT(url.path == nullptr);
  100. OATPP_ASSERT(url.queryParams.getSize() == 0);
  101. }
  102. {
  103. const char* urlText = "?key1=value1&key2=value2&key3=value3";
  104. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  105. auto params = Url::Parser::parseQueryParams(urlText);
  106. OATPP_ASSERT(params.getSize() == 3);
  107. OATPP_ASSERT(params.get("key1") == "value1");
  108. OATPP_ASSERT(params.get("key2") == "value2");
  109. OATPP_ASSERT(params.get("key2") == "value2");
  110. }
  111. {
  112. const char *urlText = "?key1=value1&key2&key3=value3";
  113. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  114. auto params = Url::Parser::parseQueryParams(urlText);
  115. OATPP_ASSERT(params.getSize() == 3);
  116. OATPP_ASSERT(params.get("key1") == "value1");
  117. OATPP_ASSERT(params.get("key2") == "");
  118. OATPP_ASSERT(params.get("key3") == "value3");
  119. }
  120. {
  121. const char *urlText = "?key1=value1&key2&key3";
  122. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  123. auto params = Url::Parser::parseQueryParams(urlText);
  124. OATPP_ASSERT(params.getSize() == 3);
  125. OATPP_ASSERT(params.get("key1") == "value1");
  126. OATPP_ASSERT(params.get("key2") == "");
  127. OATPP_ASSERT(params.get("key3") == "");
  128. }
  129. {
  130. const char *urlText = "label?key1=value1&key2=value2&key3=value3";
  131. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  132. auto params = Url::Parser::parseQueryParams(urlText);
  133. OATPP_ASSERT(params.getSize() == 3);
  134. OATPP_ASSERT(params.get("key1") == "value1");
  135. OATPP_ASSERT(params.get("key2") == "value2");
  136. OATPP_ASSERT(params.get("key2") == "value2");
  137. }
  138. {
  139. const char* urlText = "label?key1=value1&key2&key3=value3";
  140. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  141. auto params = Url::Parser::parseQueryParams(urlText);
  142. OATPP_ASSERT(params.getSize() == 3);
  143. OATPP_ASSERT(params.get("key1") == "value1");
  144. OATPP_ASSERT(params.get("key2") == "");
  145. OATPP_ASSERT(params.get("key3") == "value3");
  146. }
  147. {
  148. const char* urlText = "label?key1=value1&key2&key3";
  149. OATPP_LOGV(TAG, "urlText='%s'", urlText);
  150. auto params = Url::Parser::parseQueryParams(urlText);
  151. OATPP_ASSERT(params.getSize() == 3);
  152. OATPP_ASSERT(params.get("key1") == "value1");
  153. OATPP_ASSERT(params.get("key2") == "");
  154. OATPP_ASSERT(params.get("key3") == "");
  155. }
  156. }
  157. }}}