headers_ut.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(TestConstructorFromArrayRef);
  39. UNIT_TEST(TestAddOperation1Arg);
  40. UNIT_TEST(TestAddOperation2Args);
  41. UNIT_TEST(TestAddOrReplaceOperation1Arg);
  42. UNIT_TEST(TestAddOrReplaceOperation2Args);
  43. UNIT_TEST(TestAddHeaderTemplateness);
  44. UNIT_TEST(TestFindHeader);
  45. UNIT_TEST_SUITE_END();
  46. private:
  47. typedef void (*TAddHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value);
  48. typedef void (*TAddOrReplaceHeaderFunction)(THttpHeaders&, TStringBuf name, TStringBuf value);
  49. public:
  50. void TestConstructorFromArrayRef();
  51. void TestAddOperation1Arg();
  52. void TestAddOperation2Args();
  53. void TestAddOrReplaceOperation1Arg();
  54. void TestAddOrReplaceOperation2Args();
  55. void TestAddHeaderTemplateness();
  56. void TestFindHeader();
  57. private:
  58. static void AddHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  59. headers.AddHeader(THttpInputHeader(TString(name), TString(value)));
  60. }
  61. static void AddHeaderImpl2Args(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  62. headers.AddHeader(TString(name), TString(value));
  63. }
  64. static void AddOrReplaceHeaderImpl1Arg(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  65. headers.AddOrReplaceHeader(THttpInputHeader(TString(name), TString(value)));
  66. }
  67. static void AddOrReplaceHeaderImpl2Args(THttpHeaders& headers, TStringBuf name, TStringBuf value) {
  68. headers.AddOrReplaceHeader(TString(name), TString(value));
  69. }
  70. void DoTestAddOperation(TAddHeaderFunction);
  71. void DoTestAddOrReplaceOperation(TAddHeaderFunction, TAddOrReplaceHeaderFunction);
  72. };
  73. UNIT_TEST_SUITE_REGISTRATION(THttpHeadersTest);
  74. void THttpHeadersTest::TestConstructorFromArrayRef() {
  75. THeadersExistence expected;
  76. expected.Add("h1", "v1");
  77. expected.Add("h2", "v2");
  78. // Construct from vector
  79. TVector<THttpInputHeader> headerVec{
  80. {"h1", "v1"},
  81. {"h2", "v2"}
  82. };
  83. THttpHeaders h1(headerVec);
  84. UNIT_ASSERT(expected == h1);
  85. // Construct from initializer list
  86. THttpHeaders h2({
  87. {"h1", "v1"},
  88. {"h2", "v2"}
  89. });
  90. UNIT_ASSERT(expected == h2);
  91. }
  92. void THttpHeadersTest::TestAddOperation1Arg() {
  93. DoTestAddOperation(AddHeaderImpl1Arg);
  94. }
  95. void THttpHeadersTest::TestAddOperation2Args() {
  96. DoTestAddOperation(AddHeaderImpl2Args);
  97. }
  98. void THttpHeadersTest::TestAddOrReplaceOperation1Arg() {
  99. DoTestAddOrReplaceOperation(AddHeaderImpl1Arg, AddOrReplaceHeaderImpl1Arg);
  100. }
  101. void THttpHeadersTest::TestAddOrReplaceOperation2Args() {
  102. DoTestAddOrReplaceOperation(AddHeaderImpl2Args, AddOrReplaceHeaderImpl2Args);
  103. }
  104. void THttpHeadersTest::DoTestAddOperation(TAddHeaderFunction addHeader) {
  105. THttpHeaders h1;
  106. addHeader(h1, "h1", "v1");
  107. addHeader(h1, "h2", "v1");
  108. addHeader(h1, "h3", "v1");
  109. addHeader(h1, "h3", "v2");
  110. addHeader(h1, "h3", "v2");
  111. THeadersExistence h2;
  112. h2.Add("h1", "v1");
  113. h2.Add("h2", "v1");
  114. h2.Add("h3", "v1");
  115. h2.Add("h3", "v2");
  116. h2.Add("h3", "v2");
  117. UNIT_ASSERT(h2 == h1);
  118. }
  119. // Sorry, but AddOrReplaceHeader replaces only first occurence
  120. void THttpHeadersTest::DoTestAddOrReplaceOperation(TAddHeaderFunction addHeader, TAddOrReplaceHeaderFunction addOrReplaceHeader) {
  121. THttpHeaders h1;
  122. addHeader(h1, "h1", "v1");
  123. addOrReplaceHeader(h1, "h2", "v1");
  124. addOrReplaceHeader(h1, "h2", "v2");
  125. addOrReplaceHeader(h1, "h2", "v3");
  126. addHeader(h1, "h2", "v4");
  127. addHeader(h1, "h3", "v1");
  128. addHeader(h1, "h3", "v2");
  129. addOrReplaceHeader(h1, "h3", "v3");
  130. THeadersExistence h2;
  131. h2.Add("h1", "v1");
  132. h2.Add("h2", "v3");
  133. h2.Add("h2", "v4");
  134. h2.Add("h3", "v2");
  135. h2.Add("h3", "v3");
  136. UNIT_ASSERT(h2 == h1);
  137. }
  138. void THttpHeadersTest::TestAddHeaderTemplateness() {
  139. THttpHeaders h1;
  140. h1.AddHeader("h1", "v1");
  141. h1.AddHeader("h2", TString("v2"));
  142. h1.AddHeader("h3", TStringBuf("v3"));
  143. h1.AddHeader("h4", TStringBuf("v4"));
  144. THeadersExistence h2;
  145. h2.Add("h1", "v1");
  146. h2.Add("h2", "v2");
  147. h2.Add("h3", "v3");
  148. h2.Add("h4", "v4");
  149. UNIT_ASSERT(h1 == h2);
  150. }
  151. void THttpHeadersTest::TestFindHeader() {
  152. THttpHeaders sut;
  153. sut.AddHeader("NaMe", "Value");
  154. UNIT_ASSERT(sut.FindHeader("name"));
  155. UNIT_ASSERT(sut.FindHeader("name")->Value() == "Value");
  156. }