headers_ut.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <util/generic/set.h>
  2. #include <util/generic/string.h>
  3. #include <util/generic/strbuf.h>
  4. #include <utility>
  5. #include <library/cpp/http/io/headers.h>
  6. #include <library/cpp/testing/unittest/registar.h>
  7. namespace {
  8. class THeadersExistence {
  9. public:
  10. THeadersExistence() = default;
  11. THeadersExistence(const THttpHeaders& headers) {
  12. for (THttpHeaders::TConstIterator it = headers.Begin();
  13. it != headers.End();
  14. ++it) {
  15. Add(it->Name(), it->Value());
  16. }
  17. }
  18. public:
  19. void Add(TStringBuf name, TStringBuf value) {
  20. Impl.emplace(TString(name), TString(value));
  21. }
  22. bool operator==(const THeadersExistence& rhs) const {
  23. return Impl == rhs.Impl;
  24. }
  25. private:
  26. typedef TMultiSet<std::pair<TString, TString>> TImpl;
  27. TImpl Impl;
  28. };
  29. }
  30. bool operator==(const THeadersExistence& lhs, const THttpHeaders& rhs) {
  31. return lhs == THeadersExistence(rhs);
  32. }
  33. bool operator==(const THttpHeaders& lhs, const THeadersExistence& rhs) {
  34. return THeadersExistence(lhs) == rhs;
  35. }
  36. class THttpHeadersTest: public TTestBase {
  37. UNIT_TEST_SUITE(THttpHeadersTest);
  38. UNIT_TEST(TestAddOperation1Arg);
  39. UNIT_TEST(TestAddOperation2Args);
  40. UNIT_TEST(TestAddOrReplaceOperation1Arg);
  41. UNIT_TEST(TestAddOrReplaceOperation2Args);
  42. UNIT_TEST(TestAddHeaderTemplateness);
  43. UNIT_TEST(TestFindHeader);
  44. UNIT_TEST_SUITE_END();
  45. private:
  46. typedef void (*TAddHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value);
  47. typedef void (*TAddOrReplaceHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value);
  48. public:
  49. void TestAddOperation1Arg();
  50. void TestAddOperation2Args();
  51. void TestAddOrReplaceOperation1Arg();
  52. void TestAddOrReplaceOperation2Args();
  53. void TestAddHeaderTemplateness();
  54. void TestFindHeader();
  55. private:
  56. static void AddHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  57. headers.AddHeader(THttpInputHeader(TString(name), TString(value)));
  58. }
  59. static void AddHeaderImpl2Args(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  60. headers.AddHeader(TString(name), TString(value));
  61. }
  62. static void AddOrReplaceHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  63. headers.AddOrReplaceHeader(THttpInputHeader(TString(name), TString(value)));
  64. }
  65. static void AddOrReplaceHeaderImpl2Args(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  66. headers.AddOrReplaceHeader(TString(name), TString(value));
  67. }
  68. void DoTestAddOperation(TAddHeaderFunction);
  69. void DoTestAddOrReplaceOperation(TAddHeaderFunction, TAddOrReplaceHeaderFunction);
  70. };
  71. UNIT_TEST_SUITE_REGISTRATION(THttpHeadersTest);
  72. void THttpHeadersTest::TestAddOperation1Arg() {
  73. DoTestAddOperation(AddHeaderImpl1Arg);
  74. }
  75. void THttpHeadersTest::TestAddOperation2Args() {
  76. DoTestAddOperation(AddHeaderImpl2Args);
  77. }
  78. void THttpHeadersTest::TestAddOrReplaceOperation1Arg() {
  79. DoTestAddOrReplaceOperation(AddHeaderImpl1Arg, AddOrReplaceHeaderImpl1Arg);
  80. }
  81. void THttpHeadersTest::TestAddOrReplaceOperation2Args() {
  82. DoTestAddOrReplaceOperation(AddHeaderImpl2Args, AddOrReplaceHeaderImpl2Args);
  83. }
  84. void THttpHeadersTest::DoTestAddOperation(TAddHeaderFunction addHeader) {
  85. THttpHeaders h1;
  86. addHeader(h1, "h1", "v1");
  87. addHeader(h1, "h2", "v1");
  88. addHeader(h1, "h3", "v1");
  89. addHeader(h1, "h3", "v2");
  90. addHeader(h1, "h3", "v2");
  91. THeadersExistence h2;
  92. h2.Add("h1", "v1");
  93. h2.Add("h2", "v1");
  94. h2.Add("h3", "v1");
  95. h2.Add("h3", "v2");
  96. h2.Add("h3", "v2");
  97. UNIT_ASSERT(h2 == h1);
  98. }
  99. // Sorry, but AddOrReplaceHeader replaces only first occurence
  100. void THttpHeadersTest::DoTestAddOrReplaceOperation(TAddHeaderFunction addHeader, TAddOrReplaceHeaderFunction addOrReplaceHeader) {
  101. THttpHeaders h1;
  102. addHeader(h1, "h1", "v1");
  103. addOrReplaceHeader(h1, "h2", "v1");
  104. addOrReplaceHeader(h1, "h2", "v2");
  105. addOrReplaceHeader(h1, "h2", "v3");
  106. addHeader(h1, "h2", "v4");
  107. addHeader(h1, "h3", "v1");
  108. addHeader(h1, "h3", "v2");
  109. addOrReplaceHeader(h1, "h3", "v3");
  110. THeadersExistence h2;
  111. h2.Add("h1", "v1");
  112. h2.Add("h2", "v3");
  113. h2.Add("h2", "v4");
  114. h2.Add("h3", "v2");
  115. h2.Add("h3", "v3");
  116. UNIT_ASSERT(h2 == h1);
  117. }
  118. void THttpHeadersTest::TestAddHeaderTemplateness() {
  119. THttpHeaders h1;
  120. h1.AddHeader("h1", "v1");
  121. h1.AddHeader("h2", TString("v2"));
  122. h1.AddHeader("h3", TStringBuf("v3"));
  123. h1.AddHeader("h4", TStringBuf("v4"));
  124. THeadersExistence h2;
  125. h2.Add("h1", "v1");
  126. h2.Add("h2", "v2");
  127. h2.Add("h3", "v3");
  128. h2.Add("h4", "v4");
  129. UNIT_ASSERT(h1 == h2);
  130. }
  131. void THttpHeadersTest::TestFindHeader() {
  132. THttpHeaders sut;
  133. sut.AddHeader("NaMe", "Value");
  134. UNIT_ASSERT(sut.FindHeader("name"));
  135. UNIT_ASSERT(sut.FindHeader("name")->Value() == "Value");
  136. }