response_ut.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "response.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/string/cast.h>
  4. Y_UNIT_TEST_SUITE(TestHttpResponse) {
  5. Y_UNIT_TEST(TestCodeOnly) {
  6. UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse()), "HTTP/1.1 200 Ok\r\n\r\n");
  7. UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_NOT_FOUND)), "HTTP/1.1 404 Not found\r\n\r\n");
  8. }
  9. Y_UNIT_TEST(TestRedirect) {
  10. THttpResponse resp = THttpResponse(HTTP_FOUND).AddHeader("Location", "yandex.ru");
  11. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), "HTTP/1.1 302 Moved temporarily\r\n"
  12. "Location: yandex.ru\r\n"
  13. "\r\n");
  14. }
  15. Y_UNIT_TEST(TestAddHeader) {
  16. THttpResponse resp(HTTP_FORBIDDEN);
  17. resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne"));
  18. resp.AddHeader("X-Header-2", 10);
  19. resp.AddHeader("X-Header-3", true);
  20. const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n"
  21. "X-Header-1: ValueOne\r\n"
  22. "X-Header-2: 10\r\n"
  23. "X-Header-3: 1\r\n"
  24. "\r\n";
  25. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED);
  26. }
  27. Y_UNIT_TEST(TestAddMultipleHeaders) {
  28. THttpHeaders headers;
  29. headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne"));
  30. headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo"));
  31. headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree"));
  32. const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n"
  33. "X-Header-1: ValueOne\r\n"
  34. "X-Header-2: ValueTwo\r\n"
  35. "X-Header-3: ValueThree\r\n"
  36. "\r\n";
  37. UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_FORBIDDEN).AddMultipleHeaders(headers)),
  38. EXPECTED);
  39. }
  40. Y_UNIT_TEST(TestGetHeaders) {
  41. THttpResponse resp(HTTP_FORBIDDEN);
  42. THttpHeaders headers;
  43. headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne"));
  44. headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo"));
  45. headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree"));
  46. resp.AddMultipleHeaders(headers);
  47. resp.AddHeader("X-Header-4", "ValueFour");
  48. const THttpHeaders& gotHeaders = resp.GetHeaders();
  49. UNIT_ASSERT_VALUES_EQUAL(gotHeaders.Count(), 4);
  50. UNIT_ASSERT(gotHeaders.HasHeader("X-Header-1"));
  51. UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-1")->Value(), "ValueOne");
  52. UNIT_ASSERT(gotHeaders.HasHeader("X-Header-4"));
  53. UNIT_ASSERT_STRINGS_EQUAL(gotHeaders.FindHeader("X-Header-4")->Value(), "ValueFour");
  54. }
  55. Y_UNIT_TEST(TestSetContent) {
  56. const char* EXPECTED = "HTTP/1.1 200 Ok\r\n"
  57. "Content-Length: 10\r\n"
  58. "\r\n"
  59. "0123456789";
  60. UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("0123456789")),
  61. EXPECTED);
  62. }
  63. Y_UNIT_TEST(TestSetContentWithContentType) {
  64. const char* EXPECTED = "HTTP/1.1 200 Ok\r\n"
  65. "Content-Type: text/xml\r\n"
  66. "Content-Length: 28\r\n"
  67. "\r\n"
  68. "<xml><tag value=\"1\" /></xml>";
  69. THttpResponse resp;
  70. resp.SetContent("<xml><tag value=\"1\" /></xml>").SetContentType("text/xml");
  71. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED);
  72. }
  73. Y_UNIT_TEST(TestCopyConstructor) {
  74. THttpResponse resp(HTTP_FORBIDDEN);
  75. resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne"))
  76. .AddHeader("X-Header-2", "ValueTwo")
  77. .AddHeader(THttpInputHeader("X-Header-3", "ValueThree"))
  78. .SetContent("Some stuff")
  79. .SetContentType("text/plain");
  80. THttpResponse copy = resp;
  81. UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp));
  82. }
  83. Y_UNIT_TEST(TestAssignment) {
  84. THttpResponse resp(HTTP_FORBIDDEN);
  85. resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne"));
  86. resp.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo"));
  87. resp.AddHeader(THttpInputHeader("X-Header-3", "ValueThree"));
  88. resp.SetContent("Some stuff").SetContentType("text/plain");
  89. THttpResponse copy;
  90. copy = resp;
  91. UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp));
  92. }
  93. Y_UNIT_TEST(TestEmptyContent) {
  94. UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("")), "HTTP/1.1 200 Ok\r\n\r\n");
  95. }
  96. Y_UNIT_TEST(TestReturnReference) {
  97. THttpResponse resp;
  98. UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader("Header1", 1));
  99. UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader(THttpInputHeader("Header2", "2")));
  100. THttpHeaders headers;
  101. headers.AddHeader(THttpInputHeader("Header3", "3"));
  102. headers.AddHeader(THttpInputHeader("Header4", "4"));
  103. UNIT_ASSERT_EQUAL(&resp, &resp.AddMultipleHeaders(headers));
  104. UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some stuff"));
  105. UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some other stuff").SetContentType("text/plain"));
  106. }
  107. Y_UNIT_TEST(TestSetContentType) {
  108. const char* EXPECTED = "HTTP/1.1 200 Ok\r\n"
  109. "Content-Type: text/xml\r\n"
  110. "Content-Length: 28\r\n"
  111. "\r\n"
  112. "<xml><tag value=\"1\" /></xml>";
  113. THttpResponse resp;
  114. resp.SetContent("<xml><tag value=\"1\" /></xml>")
  115. .SetContentType("application/json")
  116. .SetContentType("text/xml");
  117. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED);
  118. }
  119. Y_UNIT_TEST(TestAddOrReplaceHeader) {
  120. THttpResponse resp(HTTP_FORBIDDEN);
  121. resp.AddOrReplaceHeader(THttpInputHeader("X-Header-1", "ValueOne"));
  122. resp.AddOrReplaceHeader("X-Header-2", 10);
  123. resp.AddOrReplaceHeader("X-Header-3", true);
  124. const char* EXPECTED_ADDED = "HTTP/1.1 403 Forbidden\r\n"
  125. "X-Header-1: ValueOne\r\n"
  126. "X-Header-2: 10\r\n"
  127. "X-Header-3: 1\r\n"
  128. "\r\n";
  129. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED_ADDED);
  130. resp.AddOrReplaceHeader(THttpInputHeader("X-Header-1", "ValueTwo"));
  131. resp.AddOrReplaceHeader("X-Header-2", 20);
  132. resp.AddOrReplaceHeader("X-Header-3", false);
  133. const char* EXPECTED_REPLACED = "HTTP/1.1 403 Forbidden\r\n"
  134. "X-Header-1: ValueTwo\r\n"
  135. "X-Header-2: 20\r\n"
  136. "X-Header-3: 0\r\n"
  137. "\r\n";
  138. UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED_REPLACED);
  139. }
  140. }