httpreqdata_ut.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "httpreqdata.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TRequestServerDataTest) {
  4. Y_UNIT_TEST(Headers) {
  5. TServerRequestData sd;
  6. sd.AddHeader("x-xx", "y-yy");
  7. sd.AddHeader("x-Xx", "y-yy");
  8. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1);
  9. sd.AddHeader("x-XxX", "y-yyy");
  10. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2);
  11. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("X-XX"), TStringBuf("y-yy"));
  12. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("X-XXX"), TStringBuf("y-yyy"));
  13. }
  14. Y_UNIT_TEST(ComplexHeaders) {
  15. TServerRequestData sd;
  16. sd.SetHost("zzz", 1);
  17. sd.AddHeader("x-Xx", "y-yy");
  18. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1);
  19. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("X-XX"), TStringBuf("y-yy"));
  20. sd.AddHeader("x-Xz", "y-yy");
  21. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2);
  22. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("X-Xz"), TStringBuf("y-yy"));
  23. UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "zzz");
  24. UNIT_ASSERT_VALUES_EQUAL(sd.ServerPort(), "1");
  25. sd.AddHeader("Host", "1234");
  26. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 3);
  27. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("Host"), TStringBuf("1234"));
  28. UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "1234");
  29. sd.AddHeader("Host", "12345:678");
  30. UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 3);
  31. UNIT_ASSERT_VALUES_EQUAL(sd.HeaderInOrEmpty("Host"), TStringBuf("12345:678"));
  32. UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "12345");
  33. UNIT_ASSERT_VALUES_EQUAL(sd.ServerPort(), "678");
  34. }
  35. Y_UNIT_TEST(ParseInvalid) {
  36. TServerRequestData rd;
  37. //Not crashes on null
  38. UNIT_ASSERT(!rd.Parse(" \0/yandsearch?&gta=fake&haha=da HTTP 1.1 OK"));
  39. rd.Scan();
  40. UNIT_ASSERT_VALUES_EQUAL(rd.Query(), "");
  41. UNIT_ASSERT_VALUES_EQUAL(rd.CgiParam.size(), 0);
  42. rd.Clear();
  43. }
  44. Y_UNIT_TEST(ParseScan) {
  45. TServerRequestData rd;
  46. // Parse parses url without host
  47. UNIT_ASSERT(!rd.Parse(" http://yandex.ru/yandsearch?&gta=fake&haha=da HTTP 1.1 OK"));
  48. // This should work
  49. UNIT_ASSERT(rd.Parse(" /yandsearch?&gta=fake&haha=da HTTP 1.1 OK"));
  50. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), "&gta=fake&haha=da");
  51. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), rd.OrigQuery());
  52. rd.Scan();
  53. UNIT_ASSERT(rd.CgiParam.Has("gta", "fake"));
  54. UNIT_ASSERT(rd.CgiParam.Has("haha", "da"));
  55. UNIT_ASSERT(!rd.CgiParam.Has("no-param"));
  56. rd.Clear();
  57. }
  58. Y_UNIT_TEST(Ctor) {
  59. const TString qs("gta=fake&haha=da");
  60. TServerRequestData rd(qs.c_str());
  61. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), qs);
  62. UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQuery(), qs);
  63. UNIT_ASSERT(rd.CgiParam.Has("gta"));
  64. UNIT_ASSERT(rd.CgiParam.Has("haha"));
  65. UNIT_ASSERT(!rd.CgiParam.Has("no-param"));
  66. }
  67. Y_UNIT_TEST(HashCut) {
  68. const TString qs("&gta=fake&haha=da");
  69. const TString header = " /yandsearch?" + qs + "#&uberParam=yes&q=? HTTP 1.1 OK";
  70. TServerRequestData rd;
  71. rd.Parse(header.c_str());
  72. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), qs);
  73. UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQuery(), qs);
  74. rd.Scan();
  75. UNIT_ASSERT(rd.CgiParam.Has("gta"));
  76. UNIT_ASSERT(rd.CgiParam.Has("haha"));
  77. UNIT_ASSERT(!rd.CgiParam.Has("uberParam"));
  78. }
  79. Y_UNIT_TEST(MisplacedHashCut) {
  80. TServerRequestData rd;
  81. rd.Parse(" /y#ndsearch?&gta=fake&haha=da&uberParam=yes&q=? HTTP 1.1 OK");
  82. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), "");
  83. UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQuery(), "");
  84. rd.Scan();
  85. UNIT_ASSERT(rd.CgiParam.empty());
  86. }
  87. Y_UNIT_TEST(CornerCase) {
  88. TServerRequestData rd;
  89. rd.Parse(" /yandsearch?#");
  90. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), "");
  91. UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQuery(), "");
  92. rd.Scan();
  93. UNIT_ASSERT(rd.CgiParam.empty());
  94. }
  95. Y_UNIT_TEST(AppendQueryString) {
  96. const TString qs("gta=fake&haha=da");
  97. TServerRequestData rd(qs.c_str());
  98. UNIT_ASSERT(rd.CgiParam.Has("gta", "fake"));
  99. UNIT_ASSERT(rd.CgiParam.Has("haha", "da"));
  100. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), qs);
  101. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), rd.OrigQuery());
  102. constexpr TStringBuf appendix = "gta=true&gta=new";
  103. rd.AppendQueryString(appendix);
  104. UNIT_ASSERT_STRINGS_EQUAL(rd.Query(), qs + '&' + appendix);
  105. UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQuery(), qs);
  106. rd.Scan();
  107. UNIT_ASSERT(rd.CgiParam.Has("gta", "true"));
  108. UNIT_ASSERT(rd.CgiParam.Has("gta", "new"));
  109. }
  110. Y_UNIT_TEST(SetRemoteAddrSimple) {
  111. static const TString TEST = "abacaba.search.yandex.net";
  112. TServerRequestData rd;
  113. rd.SetRemoteAddr(TEST);
  114. UNIT_ASSERT_STRINGS_EQUAL(TEST, rd.RemoteAddr());
  115. }
  116. Y_UNIT_TEST(SetRemoteAddrRandom) {
  117. for (size_t size = 0; size < 2 * INET6_ADDRSTRLEN; ++size) {
  118. const TString test = NUnitTest::RandomString(size, size);
  119. TServerRequestData rd;
  120. rd.SetRemoteAddr(test);
  121. UNIT_ASSERT_STRINGS_EQUAL(test.substr(0, INET6_ADDRSTRLEN - 1), rd.RemoteAddr());
  122. }
  123. }
  124. } // TRequestServerDataTest