cgiparam_ut.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "cgiparam.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TCgiParametersTest) {
  4. Y_UNIT_TEST(TestScan1) {
  5. TCgiParameters C;
  6. C.Scan("aaa=b%62b&ccc=ddd&ag0=");
  7. UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true);
  8. UNIT_ASSERT_EQUAL(C.NumOfValues("ag0") == 1, true);
  9. UNIT_ASSERT(C.Has("ccc", "ddd"));
  10. UNIT_ASSERT(C.Has("ag0", ""));
  11. UNIT_ASSERT(!C.Has("a", "bbb"));
  12. UNIT_ASSERT(!C.Has("aaa", "bb"));
  13. UNIT_ASSERT(C.Has("ccc"));
  14. UNIT_ASSERT(!C.Has("zzzzzz"));
  15. }
  16. Y_UNIT_TEST(TestQuick) {
  17. TQuickCgiParam C("aaa=b%62b&ccc=ddd&ag0=");
  18. UNIT_ASSERT_EQUAL(C.Get("aaa") == "bbb", true);
  19. UNIT_ASSERT(C.Has("ccc", "ddd"));
  20. UNIT_ASSERT(C.Has("ag0", ""));
  21. UNIT_ASSERT(!C.Has("a", "bbb"));
  22. UNIT_ASSERT(!C.Has("aaa", "bb"));
  23. UNIT_ASSERT(C.Has("ccc"));
  24. UNIT_ASSERT(!C.Has("zzzzzz"));
  25. TQuickCgiParam D = std::move(C);
  26. UNIT_ASSERT(D.Has("aaa"));
  27. TQuickCgiParam E("");
  28. UNIT_ASSERT(!E.Has("aaa"));
  29. C = std::move(E);
  30. UNIT_ASSERT(!C.Has("aaa"));
  31. }
  32. Y_UNIT_TEST(TestScan2) {
  33. const TString parsee("=000&aaa=bbb&ag0=&ccc=ddd");
  34. TCgiParameters c;
  35. c.Scan(parsee);
  36. UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee);
  37. }
  38. Y_UNIT_TEST(TestScan3) {
  39. const TString parsee("aaa=bbb&ag0=&ccc=ddd");
  40. TCgiParameters c;
  41. c.Scan(parsee);
  42. c.InsertUnescaped("d", "x=x");
  43. UNIT_ASSERT_VALUES_EQUAL(c.Print(), parsee + "&d=x\%3Dx");
  44. }
  45. Y_UNIT_TEST(TestScanAddAll1) {
  46. TCgiParameters c;
  47. c.ScanAddAll("qw");
  48. UNIT_ASSERT_VALUES_EQUAL(c.size(), 1u);
  49. UNIT_ASSERT(c.Get("qw").empty());
  50. }
  51. Y_UNIT_TEST(TestScanAddAll2) {
  52. TCgiParameters c;
  53. c.ScanAddAll("qw&");
  54. UNIT_ASSERT_VALUES_EQUAL(c.size(), 1u);
  55. UNIT_ASSERT(c.Get("qw").empty());
  56. }
  57. Y_UNIT_TEST(TestScanAddAll3) {
  58. TCgiParameters c;
  59. c.ScanAddAll("qw=1&x");
  60. UNIT_ASSERT_VALUES_EQUAL(c.size(), 2u);
  61. UNIT_ASSERT_VALUES_EQUAL(c.Get("qw"), "1");
  62. UNIT_ASSERT(c.Get("x").empty());
  63. }
  64. Y_UNIT_TEST(TestScanAddAll4) {
  65. TCgiParameters c;
  66. c.ScanAddAll("ccc=1&aaa=1&ccc=3&bbb&ccc=2");
  67. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&ccc=1&ccc=3&ccc=2");
  68. }
  69. Y_UNIT_TEST(TestScanAddAllUnescaped1) {
  70. TCgiParameters c;
  71. c.ScanAddAllUnescaped("ccc=1&aaa=1&ccc=3&bbb&ccc=2");
  72. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&ccc=1&ccc=3&ccc=2");
  73. }
  74. Y_UNIT_TEST(TestScanAddAllUnescaped2) {
  75. TCgiParameters c;
  76. c.ScanAddAllUnescaped("text=something&null");
  77. UNIT_ASSERT_VALUES_EQUAL(c.size(), 2u);
  78. UNIT_ASSERT_VALUES_EQUAL(c.Get("text"), "something");
  79. UNIT_ASSERT(c.Get("null").empty());
  80. }
  81. Y_UNIT_TEST(TestScanAddAllUnescaped3) {
  82. TCgiParameters c;
  83. c.ScanAddAllUnescaped("text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%2C");
  84. UNIT_ASSERT_VALUES_EQUAL(c.Get("text"), "%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%2C");
  85. }
  86. Y_UNIT_TEST(TestEraseAll) {
  87. TCgiParameters c;
  88. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3");
  89. c.EraseAll("par");
  90. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=");
  91. }
  92. Y_UNIT_TEST(TestErase) {
  93. TCgiParameters c;
  94. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3&par=1");
  95. c.Erase("par", 1);
  96. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&par=1&par=3&par=1");
  97. c.Erase("par", "1");
  98. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&par=3");
  99. }
  100. Y_UNIT_TEST(TestReplaceUnescaped1) {
  101. TCgiParameters c;
  102. c.ScanAddAll("many_keys=1&aaa=1&many_keys=2&bbb&many_keys=3");
  103. c.ReplaceUnescaped("many_keys", "new_value");
  104. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&many_keys=new_value");
  105. }
  106. Y_UNIT_TEST(TestReplaceUnescaped2) {
  107. TCgiParameters c;
  108. c.ScanAddAll("par=1&only_one=1&par=2&bbb&par=3");
  109. c.ReplaceUnescaped("only_one", "new_value");
  110. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "bbb=&only_one=new_value&par=1&par=2&par=3");
  111. }
  112. Y_UNIT_TEST(TestReplaceUnescaped3) {
  113. TCgiParameters c;
  114. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3");
  115. c.ReplaceUnescaped("no_such_key", "new_value");
  116. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&no_such_key=new_value&par=1&par=2&par=3");
  117. }
  118. Y_UNIT_TEST(TestReplaceUnescapedRange1) {
  119. TCgiParameters c;
  120. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3");
  121. c.ReplaceUnescaped("par", {"x", "y", "z"}); // 3 old values, 3 new values
  122. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&par=x&par=y&par=z");
  123. }
  124. Y_UNIT_TEST(TestReplaceUnescapedRange2) {
  125. TCgiParameters c;
  126. c.ScanAddAll("par=1&aaa=1&par=2&bbb");
  127. c.ReplaceUnescaped("par", {"x", "y", "z"}); // 2 old values, 3 new values
  128. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&par=x&par=y&par=z");
  129. }
  130. Y_UNIT_TEST(TestReplaceUnescapedRange3) {
  131. TCgiParameters c;
  132. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3");
  133. c.ReplaceUnescaped("par", {"x", "y"}); // 3 old values, 2 new values
  134. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=1&bbb=&par=x&par=y");
  135. }
  136. Y_UNIT_TEST(TestNumOfValues) {
  137. TCgiParameters c;
  138. c.ScanAddAll("par=1&aaa=1&par=2&bbb&par=3");
  139. UNIT_ASSERT_VALUES_EQUAL(c.NumOfValues("par"), 3u);
  140. }
  141. Y_UNIT_TEST(TestUnscape) {
  142. TCgiParameters c("f=1&t=%84R%84%7C%84%80%84%7E&reqenc=SHIFT_JIS&p=0");
  143. UNIT_ASSERT_VALUES_EQUAL(c.Get("t"), "\x84R\x84\x7C\x84\x80\x84\x7E");
  144. }
  145. Y_UNIT_TEST(TestEmpty) {
  146. UNIT_ASSERT(TCgiParameters().Print().empty());
  147. }
  148. Y_UNIT_TEST(TestJoinUnescaped) {
  149. TCgiParameters c;
  150. c.Scan("foo=1&foo=2");
  151. c.JoinUnescaped("foo", ';', "0");
  152. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "foo=1;2;0");
  153. }
  154. Y_UNIT_TEST(TestContInit) {
  155. TCgiParameters c = {std::make_pair("a", "a1"), std::make_pair("b", "b1"), std::make_pair("a", "a2")};
  156. UNIT_ASSERT_VALUES_EQUAL(c.NumOfValues("a"), 2u);
  157. UNIT_ASSERT_VALUES_EQUAL(c.NumOfValues("b"), 1u);
  158. UNIT_ASSERT_VALUES_EQUAL(c.Get("b"), "b1");
  159. UNIT_ASSERT_VALUES_EQUAL(c.Get("a", 0), "a1");
  160. UNIT_ASSERT_VALUES_EQUAL(c.Get("a", 1), "a2");
  161. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "a=a1&a=a2&b=b1");
  162. }
  163. Y_UNIT_TEST(TestPrintAsQuote) {
  164. TCgiParameters c = {
  165. std::make_pair("aaa", "value/with/slashes"),
  166. std::make_pair("b/b/b", "value_without_slashes"),
  167. std::make_pair("ccc", "value")};
  168. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "aaa=value/with/slashes&b/b/b=value_without_slashes&ccc=value");
  169. UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), "aaa=value%2Fwith%2Fslashes&b%2Fb%2Fb=value_without_slashes&ccc=value");
  170. }
  171. Y_UNIT_TEST(TestPrintAsQuoteEmpty) {
  172. TCgiParameters c = {};
  173. UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), "");
  174. }
  175. Y_UNIT_TEST(TestPrintAsQuoteEmptyKeyOrValue) {
  176. TCgiParameters c = {
  177. std::make_pair("", "value/of/empty"),
  178. std::make_pair("key/for/empty", "")};
  179. UNIT_ASSERT_VALUES_EQUAL(c.Print(), "=value/of/empty&key/for/empty=");
  180. UNIT_ASSERT_VALUES_EQUAL(c.QuotedPrint(""), "=value%2Fof%2Fempty&key%2Ffor%2Fempty=");
  181. }
  182. }