1
0

uri_test.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hdfspp/uri.h"
  19. #include <gmock/gmock.h>
  20. using ::testing::_;
  21. using namespace hdfs;
  22. URI expect_uri_throw(const char *uri) {
  23. bool threw = false;
  24. std::string what_msg;
  25. URI val;
  26. try {
  27. val = URI::parse_from_string(uri);
  28. } catch (const uri_parse_error& e) {
  29. threw = true;
  30. what_msg = e.what();
  31. } catch (...) {
  32. threw = true;
  33. }
  34. EXPECT_TRUE(threw);
  35. EXPECT_EQ(what_msg, uri);
  36. return val;
  37. }
  38. URI expect_uri_nothrow(const char *uri) {
  39. bool threw = false;
  40. std::string what_msg;
  41. URI val;
  42. try {
  43. val = URI::parse_from_string(uri);
  44. } catch (const uri_parse_error& e) {
  45. threw = true;
  46. what_msg = e.what();
  47. } catch (...) {
  48. threw = true;
  49. }
  50. EXPECT_FALSE(threw);
  51. EXPECT_EQ(what_msg, "");
  52. return val;
  53. }
  54. TEST(UriTest, TestDegenerateInputs) {
  55. /* Empty input */
  56. expect_uri_nothrow("");
  57. /* Invalid encoding */
  58. expect_uri_throw("%%");
  59. /* Invalid port */
  60. expect_uri_throw("hdfs://nn:foo/");
  61. /* Negative port */
  62. expect_uri_throw("hdfs://nn:-100/");
  63. /* Empty paths */
  64. expect_uri_nothrow("hdfs://////");
  65. }
  66. TEST(UriTest, TestNominalInputs) {
  67. /* Simple input */
  68. {
  69. URI uri = expect_uri_nothrow("hdfs:///foo");
  70. EXPECT_EQ("hdfs", uri.get_scheme());
  71. EXPECT_EQ("", uri.get_host());
  72. EXPECT_FALSE(uri.has_port());
  73. EXPECT_EQ(0, uri.get_port_or_default(0));
  74. EXPECT_EQ("/foo", uri.get_path());
  75. EXPECT_EQ("", uri.get_fragment());
  76. EXPECT_EQ("", uri.get_query());
  77. }
  78. /* With authority */
  79. {
  80. URI uri = expect_uri_nothrow("hdfs://host:100/foo");
  81. EXPECT_EQ("hdfs", uri.get_scheme());
  82. EXPECT_EQ("host", uri.get_host());
  83. EXPECT_TRUE(uri.has_port());
  84. EXPECT_EQ(100, uri.get_port());
  85. EXPECT_EQ(100, uri.get_port_or_default(0));
  86. EXPECT_EQ("/foo", uri.get_path());
  87. EXPECT_EQ("", uri.get_fragment());
  88. EXPECT_EQ("", uri.get_query());
  89. }
  90. /* No scheme */
  91. {
  92. URI uri = expect_uri_nothrow("/foo");
  93. EXPECT_EQ("", uri.get_scheme());
  94. EXPECT_EQ("", uri.get_host());
  95. EXPECT_FALSE(uri.has_port());
  96. EXPECT_EQ(0, uri.get_port_or_default(0));
  97. EXPECT_EQ("/foo", uri.get_path());
  98. EXPECT_EQ("", uri.get_fragment());
  99. EXPECT_EQ("", uri.get_query());
  100. }
  101. /* All fields */
  102. {
  103. URI uri = expect_uri_nothrow("hdfs://nn:8020/path/to/data?a=b&c=d#fragment");
  104. EXPECT_EQ("hdfs", uri.get_scheme());
  105. EXPECT_EQ("nn", uri.get_host());
  106. EXPECT_TRUE(uri.has_port());
  107. EXPECT_EQ(8020, uri.get_port());
  108. EXPECT_EQ(8020, uri.get_port_or_default(0));
  109. EXPECT_EQ("/path/to/data", uri.get_path());
  110. EXPECT_EQ("a=b&c=d", uri.get_query());
  111. EXPECT_EQ(3, uri.get_path_elements().size());
  112. EXPECT_EQ("path", uri.get_path_elements()[0]);
  113. EXPECT_EQ("to", uri.get_path_elements()[1]);
  114. EXPECT_EQ("data", uri.get_path_elements()[2]);
  115. EXPECT_EQ(2, uri.get_query_elements().size());
  116. EXPECT_EQ("a", uri.get_query_elements()[0].key);
  117. EXPECT_EQ("b", uri.get_query_elements()[0].value);
  118. EXPECT_EQ("c", uri.get_query_elements()[1].key);
  119. EXPECT_EQ("d", uri.get_query_elements()[1].value);
  120. EXPECT_EQ("fragment", uri.get_fragment());
  121. }
  122. }
  123. TEST(UriTest, TestEncodedInputs) {
  124. // Note that scheme and port cannot be uri-encoded
  125. /* Encoded input */
  126. {
  127. URI uri = expect_uri_nothrow("S://%5E:1/+%5E%20?%5E=%5E#%5E");
  128. EXPECT_EQ("S", uri.get_scheme());
  129. EXPECT_EQ("^", uri.get_host());
  130. EXPECT_EQ(1, uri.get_port_or_default(0));
  131. EXPECT_EQ("/ ^ ", uri.get_path());
  132. EXPECT_EQ("^", uri.get_fragment());
  133. EXPECT_EQ("^=^", uri.get_query());
  134. }
  135. /* Lowercase */
  136. {
  137. URI uri = expect_uri_nothrow("S://%5e:1/+%5e%20?%5e=%5e#%5e");
  138. EXPECT_EQ("S", uri.get_scheme());
  139. EXPECT_EQ("^", uri.get_host());
  140. EXPECT_EQ(1, uri.get_port_or_default(0));
  141. EXPECT_EQ("/ ^ ", uri.get_path());
  142. EXPECT_EQ("^", uri.get_fragment());
  143. EXPECT_EQ("^=^", uri.get_query());
  144. }
  145. }
  146. TEST(UriTest, TestDecodedInputsAndOutputs) {
  147. /* All fields non-encoded and shouldn't be interpreted */
  148. {
  149. URI uri = expect_uri_nothrow("S://%25/%25+?%25=%25#%25");
  150. EXPECT_EQ("S", uri.get_scheme());
  151. EXPECT_EQ("%", uri.get_host());
  152. EXPECT_EQ(0, uri.get_port_or_default(0));
  153. EXPECT_EQ("/% ", uri.get_path());
  154. EXPECT_EQ("%", uri.get_fragment());
  155. EXPECT_EQ("%=%", uri.get_query());
  156. }
  157. /* All fields encode fields on their way out */
  158. {
  159. URI uri = expect_uri_nothrow("S://%25/%25+?%25=%25#%25");
  160. EXPECT_EQ("S", uri.get_scheme(true));
  161. EXPECT_EQ("%25", uri.get_host(true));
  162. EXPECT_EQ(0, uri.get_port_or_default(0));
  163. EXPECT_EQ("/%25+", uri.get_path(true));
  164. EXPECT_EQ("%25", uri.get_fragment(true));
  165. EXPECT_EQ("%25=%25", uri.get_query(true));
  166. }
  167. }
  168. TEST(UriTest, TestSetters) {
  169. /* Non-encoded inputs */
  170. {
  171. URI uri;
  172. uri.set_scheme("S");
  173. uri.set_host("%");
  174. uri.set_port(100);
  175. uri.set_path("%/%/%");
  176. uri.set_fragment("%");
  177. uri.set_query("%25=%25"); //set_query must always be encoded
  178. EXPECT_EQ("S://%25:100/%25/%25/%25?%25=%25#%25", uri.str());
  179. }
  180. /* Incremental adders, non-encoded */
  181. {
  182. URI uri;
  183. uri.set_scheme("S");
  184. uri.set_host("%");
  185. uri.set_port(100);
  186. uri.set_fragment("%");
  187. EXPECT_EQ("S://%25:100#%25", uri.str());
  188. uri.add_path("%");
  189. uri.add_query("%", "%");
  190. EXPECT_EQ("S://%25:100/%25?%25=%25#%25", uri.str());
  191. uri.add_path("%");
  192. uri.add_query("%", "%");
  193. EXPECT_EQ("S://%25:100/%25/%25?%25=%25&%25=%25#%25", uri.str());
  194. }
  195. /* Encoded inputs */
  196. {
  197. URI uri;
  198. uri.set_scheme("S", true);
  199. uri.set_host("%25", true);
  200. uri.set_port(100);
  201. uri.set_path("%25/%25/%25", true);
  202. uri.set_fragment("%25", true);
  203. uri.set_query("%25=%25"); //set_query must always be encoded
  204. EXPECT_EQ("S://%25:100/%25/%25/%25?%25=%25#%25", uri.str());
  205. }
  206. /* Incremental adders, encoded */
  207. {
  208. URI uri;
  209. uri.set_scheme("S", true);
  210. uri.set_host("%25", true);
  211. uri.set_port(100);
  212. uri.set_fragment("%25", true);
  213. EXPECT_EQ("S://%25:100#%25", uri.str());
  214. uri.add_path("%25", true);
  215. uri.add_query("%25", "%25", true);
  216. EXPECT_EQ("S://%25:100/%25?%25=%25#%25", uri.str());
  217. uri.add_path("%25", true);
  218. uri.add_query("%25", "%25", true);
  219. EXPECT_EQ("S://%25:100/%25/%25?%25=%25&%25=%25#%25", uri.str());
  220. }
  221. }
  222. TEST(UriTest, QueryManip) {
  223. // Not encoded, just basic adding and removing query parts
  224. {
  225. URI uri = URI::parse_from_string("hdfs://nn:8020/path?thedude=lebowski&donny=outofhiselement");
  226. EXPECT_TRUE(uri.has_port());
  227. EXPECT_EQ(uri.get_query(), "thedude=lebowski&donny=outofhiselement");
  228. std::vector<URI::Query> queries = uri.get_query_elements();
  229. EXPECT_EQ(queries.size(), 2);
  230. EXPECT_EQ(queries[0].key, "thedude");
  231. EXPECT_EQ(queries[0].value, "lebowski");
  232. EXPECT_EQ(queries[1].key, "donny");
  233. EXPECT_EQ(queries[1].value, "outofhiselement");
  234. uri.remove_query("donny"); // that's a bummer, man
  235. EXPECT_EQ(uri.get_query(), "thedude=lebowski");
  236. queries = uri.get_query_elements();
  237. EXPECT_EQ(queries.size(), 1);
  238. EXPECT_EQ(queries[0].key, "thedude");
  239. EXPECT_EQ(queries[0].value, "lebowski");
  240. uri.add_query("HeyPeter", "CheckItOut");
  241. EXPECT_EQ(uri.get_query(), "thedude=lebowski&HeyPeter=CheckItOut");
  242. queries = uri.get_query_elements();
  243. EXPECT_EQ(queries.size(), 2);
  244. }
  245. }
  246. int main(int argc, char *argv[]) {
  247. /*
  248. * The following line must be executed to initialize Google Mock
  249. * (and Google Test) before running the tests.
  250. */
  251. ::testing::InitGoogleMock(&argc, argv);
  252. return RUN_ALL_TESTS();
  253. }