http_ut.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include <library/cpp/http/simple/http_client.h>
  2. #include <library/cpp/http/server/response.h>
  3. #include <library/cpp/testing/mock_server/server.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <library/cpp/testing/unittest/tests_data.h>
  6. #include <util/system/event.h>
  7. #include <util/system/thread.h>
  8. #include <thread>
  9. Y_UNIT_TEST_SUITE(SimpleHttp) {
  10. static THttpServerOptions createOptions(ui16 port, bool keepAlive) {
  11. THttpServerOptions o;
  12. o.AddBindAddress("localhost", port);
  13. o.SetThreads(1);
  14. o.SetMaxConnections(1);
  15. o.SetMaxQueueSize(1);
  16. o.EnableKeepAlive(keepAlive);
  17. return o;
  18. }
  19. class TPong: public TRequestReplier {
  20. TDuration Sleep_;
  21. ui16 Port_;
  22. public:
  23. TPong(TDuration sleep = TDuration(), ui16 port = 80)
  24. : Sleep_(sleep)
  25. , Port_(port)
  26. {
  27. }
  28. bool DoReply(const TReplyParams& params) override {
  29. TStringBuf path = TParsedHttpFull(params.Input.FirstLine()).Path;
  30. params.Input.ReadAll();
  31. if (path == "/redirect") {
  32. params.Output << "HTTP/1.1 307 Internal Redirect\r\n"
  33. "Location: http://localhost:"
  34. << Port_
  35. << "/redirect2?some_param=qwe\r\n"
  36. "Non-Authoritative-Reason: HSTS\r\n\r\n"
  37. "must be missing";
  38. return true;
  39. }
  40. if (path == "/redirect2") {
  41. UNIT_ASSERT_VALUES_EQUAL("some_param=qwe", TParsedHttpFull(params.Input.FirstLine()).Cgi);
  42. params.Output << "HTTP/1.1 307 Internal Redirect\r\n"
  43. "Location: http://localhost:"
  44. << Port_
  45. << "/ping\r\n"
  46. "Non-Authoritative-Reason: HSTS\r\n\r\n"
  47. "must be missing too";
  48. return true;
  49. }
  50. if (path != "/ping") {
  51. UNIT_ASSERT_C(false, "path is incorrect: '" << path << "'");
  52. }
  53. Sleep(Sleep_);
  54. THttpResponse resp(HTTP_OK);
  55. resp.SetContent("pong");
  56. resp.OutTo(params.Output);
  57. return true;
  58. }
  59. };
  60. class TCodedPong: public TRequestReplier {
  61. HttpCodes Code_;
  62. public:
  63. TCodedPong(HttpCodes code)
  64. : Code_(code)
  65. {
  66. }
  67. bool DoReply(const TReplyParams& params) override {
  68. if (TParsedHttpFull(params.Input.FirstLine()).Path != "/ping") {
  69. UNIT_ASSERT(false);
  70. }
  71. THttpResponse resp(Code_);
  72. resp.SetContent("pong");
  73. resp.OutTo(params.Output);
  74. return true;
  75. }
  76. };
  77. class T500: public TRequestReplier {
  78. ui16 Port_;
  79. public:
  80. T500(ui16 port)
  81. : Port_(port)
  82. {
  83. }
  84. bool DoReply(const TReplyParams& params) override {
  85. TStringBuf path = TParsedHttpFull(params.Input.FirstLine()).Path;
  86. if (path == "/bad_redirect") {
  87. params.Output << "HTTP/1.1 500 Internal Redirect\r\n"
  88. "Location: http://localhost:1/qwerty\r\n"
  89. "Non-Authoritative-Reason: HSTS\r\n\r\n";
  90. return true;
  91. }
  92. if (path == "/redirect_to_500") {
  93. params.Output << "HTTP/1.1 307 Internal Redirect\r\n"
  94. "Location: http://localhost:"
  95. << Port_
  96. << "/500\r\n"
  97. "Non-Authoritative-Reason: HSTS\r\n\r\n";
  98. return true;
  99. }
  100. THttpResponse resp(HTTP_INTERNAL_SERVER_ERROR);
  101. resp.SetContent("bang");
  102. resp.OutTo(params.Output);
  103. return true;
  104. }
  105. };
  106. Y_UNIT_TEST(simpleSuccessful) {
  107. TPortManager pm;
  108. ui16 port = pm.GetPort(80);
  109. NMock::TMockServer server(createOptions(port, false), []() { return new TPong; });
  110. TSimpleHttpClient cl("localhost", port);
  111. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  112. {
  113. TStringStream s;
  114. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", &s));
  115. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  116. Sleep(TDuration::MilliSeconds(500));
  117. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  118. }
  119. {
  120. TStringStream s;
  121. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", &s));
  122. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  123. Sleep(TDuration::MilliSeconds(500));
  124. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  125. }
  126. {
  127. TStringStream s;
  128. UNIT_ASSERT_NO_EXCEPTION(cl.DoPost("/ping", "", &s));
  129. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  130. Sleep(TDuration::MilliSeconds(500));
  131. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  132. }
  133. {
  134. TStringStream s;
  135. UNIT_ASSERT_NO_EXCEPTION(cl.DoPost("/ping", "", &s));
  136. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  137. Sleep(TDuration::MilliSeconds(500));
  138. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  139. }
  140. }
  141. Y_UNIT_TEST(simpleMessages) {
  142. TPortManager pm;
  143. ui16 port = pm.GetPort(80);
  144. NMock::TMockServer server(createOptions(port, false), []() { return new TPong; });
  145. TSimpleHttpClient cl("localhost", port);
  146. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  147. {
  148. TStringStream s;
  149. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", &s));
  150. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  151. Sleep(TDuration::MilliSeconds(500));
  152. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  153. }
  154. {
  155. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", nullptr));
  156. Sleep(TDuration::MilliSeconds(500));
  157. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  158. }
  159. server.SetGenerator([]() { return new TCodedPong(HTTP_CONTINUE); });
  160. {
  161. TStringStream s;
  162. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoPost("/ping", "", &s),
  163. THttpRequestException,
  164. "Got 100 at localhost/ping\n"
  165. "Full http response:\n");
  166. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  167. Sleep(TDuration::MilliSeconds(500));
  168. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  169. }
  170. {
  171. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoPost("/ping", "", nullptr),
  172. THttpRequestException,
  173. "Got 100 at localhost/ping\n"
  174. "Full http response:\n"
  175. "pong");
  176. Sleep(TDuration::MilliSeconds(500));
  177. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  178. }
  179. }
  180. Y_UNIT_TEST(simpleTimeout) {
  181. TPortManager pm;
  182. ui16 port = pm.GetPort(80);
  183. NMock::TMockServer server(createOptions(port, true), []() { return new TPong(TDuration::MilliSeconds(300)); });
  184. TSimpleHttpClient cl("localhost", port, TDuration::MilliSeconds(50), TDuration::MilliSeconds(50));
  185. TStringStream s;
  186. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/ping", &s),
  187. TSystemError,
  188. "Resource temporarily unavailable");
  189. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoPost("/ping", "", &s),
  190. TSystemError,
  191. "Resource temporarily unavailable");
  192. }
  193. Y_UNIT_TEST(simpleError) {
  194. TPortManager pm;
  195. ui16 port = pm.GetPort(80);
  196. NMock::TMockServer server(createOptions(port, true), []() { return new TPong; });
  197. TSimpleHttpClient cl("localhost", port);
  198. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  199. {
  200. TStringStream s;
  201. server.SetGenerator([]() { return new TCodedPong(HTTP_CONTINUE); });
  202. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/ping", &s),
  203. THttpRequestException,
  204. "Got 100 at localhost/ping\n"
  205. "Full http response:");
  206. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  207. Sleep(TDuration::MilliSeconds(500));
  208. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  209. }
  210. {
  211. TStringStream s;
  212. server.SetGenerator([]() { return new TCodedPong(HTTP_OK); });
  213. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", &s));
  214. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  215. Sleep(TDuration::MilliSeconds(500));
  216. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  217. server.SetGenerator([]() { return new TCodedPong(HTTP_PARTIAL_CONTENT); });
  218. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping", &s));
  219. Sleep(TDuration::MilliSeconds(500));
  220. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  221. }
  222. {
  223. TStringStream s;
  224. server.SetGenerator([]() { return new TCodedPong(HTTP_MULTIPLE_CHOICES); });
  225. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/ping", &s),
  226. THttpRequestException,
  227. "Got 300 at localhost/ping\n"
  228. "Full http response:");
  229. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  230. Sleep(TDuration::MilliSeconds(500));
  231. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  232. }
  233. }
  234. Y_UNIT_TEST(redirectable) {
  235. TPortManager pm;
  236. ui16 port = pm.GetPort(80);
  237. NMock::TMockServer server(createOptions(port, true), [port]() { return new TPong(TDuration(), port); });
  238. TRedirectableHttpClient cl("localhost", port);
  239. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  240. {
  241. TStringStream s;
  242. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/redirect", &s));
  243. UNIT_ASSERT_VALUES_EQUAL("pong", s.Str());
  244. Sleep(TDuration::MilliSeconds(500));
  245. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  246. }
  247. server.SetGenerator([port]() { return new T500(port); });
  248. TStringStream s;
  249. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/bad_redirect", &s),
  250. THttpRequestException,
  251. "can not connect to ");
  252. Sleep(TDuration::MilliSeconds(500));
  253. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  254. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/redirect_to_500", &s),
  255. THttpRequestException,
  256. "Got 500 at http://localhost/500\n"
  257. "Full http response:\n");
  258. UNIT_ASSERT_VALUES_EQUAL("bang", s.Str());
  259. Sleep(TDuration::MilliSeconds(500));
  260. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  261. }
  262. Y_UNIT_TEST(keepaliveSuccessful) {
  263. auto test = [](bool keepalive, i64 clientCount) {
  264. TPortManager pm;
  265. ui16 port = pm.GetPort(80);
  266. NMock::TMockServer server(createOptions(port, keepalive), []() { return new TPong; });
  267. TKeepAliveHttpClient cl("localhost", port);
  268. UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount());
  269. {
  270. TStringStream s;
  271. int code = -1;
  272. UNIT_ASSERT_NO_EXCEPTION_C(code = cl.DoGet("/ping", &s), keepalive);
  273. UNIT_ASSERT_VALUES_EQUAL_C(200, code, keepalive);
  274. UNIT_ASSERT_VALUES_EQUAL_C("pong", s.Str(), keepalive);
  275. Sleep(TDuration::MilliSeconds(500));
  276. UNIT_ASSERT_VALUES_EQUAL(clientCount, server.GetClientCount());
  277. }
  278. {
  279. TStringStream s;
  280. int code = -1;
  281. UNIT_ASSERT_NO_EXCEPTION_C(code = cl.DoGet("/ping", &s), keepalive);
  282. UNIT_ASSERT_VALUES_EQUAL_C(200, code, keepalive);
  283. UNIT_ASSERT_VALUES_EQUAL_C("pong", s.Str(), keepalive);
  284. Sleep(TDuration::MilliSeconds(500));
  285. UNIT_ASSERT_VALUES_EQUAL(clientCount, server.GetClientCount());
  286. }
  287. {
  288. TStringStream s;
  289. int code = -1;
  290. UNIT_ASSERT_NO_EXCEPTION_C(code = cl.DoPost("/ping", "", &s), keepalive);
  291. UNIT_ASSERT_VALUES_EQUAL_C(200, code, keepalive);
  292. UNIT_ASSERT_VALUES_EQUAL_C("pong", s.Str(), keepalive);
  293. Sleep(TDuration::MilliSeconds(500));
  294. UNIT_ASSERT_VALUES_EQUAL(clientCount, server.GetClientCount());
  295. }
  296. {
  297. TStringStream s;
  298. int code = -1;
  299. UNIT_ASSERT_NO_EXCEPTION_C(code = cl.DoPost("/ping", "", &s), keepalive);
  300. UNIT_ASSERT_VALUES_EQUAL_C(200, code, keepalive);
  301. UNIT_ASSERT_VALUES_EQUAL_C("pong", s.Str(), keepalive);
  302. Sleep(TDuration::MilliSeconds(500));
  303. UNIT_ASSERT_VALUES_EQUAL(clientCount, server.GetClientCount());
  304. }
  305. };
  306. test(true, 1);
  307. test(false, 0);
  308. }
  309. Y_UNIT_TEST(keepaliveTimeout) {
  310. TPortManager pm;
  311. ui16 port = pm.GetPort(80);
  312. NMock::TMockServer server(createOptions(port, true), []() { return new TPong(TDuration::MilliSeconds(300)); });
  313. TKeepAliveHttpClient cl("localhost", port, TDuration::MilliSeconds(50), TDuration::MilliSeconds(50));
  314. TStringStream s;
  315. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoGet("/ping", &s),
  316. TSystemError,
  317. "Resource temporarily unavailable");
  318. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoPost("/ping", "", &s),
  319. TSystemError,
  320. "Resource temporarily unavailable");
  321. }
  322. Y_UNIT_TEST(keepaliveHeaders) {
  323. TPortManager pm;
  324. ui16 port = pm.GetPort(80);
  325. NMock::TMockServer server(createOptions(port, true), []() { return new TPong; });
  326. TKeepAliveHttpClient cl("localhost", port);
  327. TStringStream s;
  328. THttpHeaders h;
  329. UNIT_ASSERT_VALUES_EQUAL(200, cl.DoGet("/ping", &s, {}, &h));
  330. TStringStream hs;
  331. h.OutTo(&hs);
  332. UNIT_ASSERT_VALUES_EQUAL("Content-Length: 4\r\nConnection: Keep-Alive\r\n", hs.Str());
  333. }
  334. Y_UNIT_TEST(keepaliveRaw) {
  335. TPortManager pm;
  336. ui16 port = pm.GetPort(80);
  337. NMock::TMockServer server(createOptions(port, true), []() { return new TPong; });
  338. TKeepAliveHttpClient cl("localhost", port);
  339. TStringStream s;
  340. THttpHeaders h;
  341. TString raw = "POST /ping HTTP/1.1\r\n"
  342. "Connection: Keep-Alive\r\n"
  343. "Accept-Encoding: gzip, deflate\r\n"
  344. "Content-Length: 9\r\n"
  345. "Content-Type: application/x-www-form-urlencoded\r\n"
  346. "User-Agent: Python-urllib/2.6\r\n"
  347. "\r\n"
  348. "some body";
  349. UNIT_ASSERT_VALUES_EQUAL(200, cl.DoRequestRaw(raw, &s, &h));
  350. TStringStream hs;
  351. h.OutTo(&hs);
  352. UNIT_ASSERT_VALUES_EQUAL("Content-Length: 4\r\nConnection: Keep-Alive\r\n", hs.Str());
  353. raw = "GET /ping HT TP/1.1\r\n";
  354. UNIT_ASSERT_EXCEPTION_CONTAINS(cl.DoRequestRaw(raw, &s, &h), TSystemError, "can not read from socket input stream");
  355. }
  356. Y_UNIT_TEST(keepaliveWithClosedByPeer) {
  357. TPortManager pm;
  358. ui16 port = pm.GetPort(80);
  359. NMock::TMockServer::TGenerator gen = []() { return new TPong; };
  360. THolder<NMock::TMockServer> server = MakeHolder<NMock::TMockServer>(createOptions(port, true), gen);
  361. TKeepAliveHttpClient cl("localhost", port);
  362. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping"));
  363. server.Reset();
  364. server = MakeHolder<NMock::TMockServer>(createOptions(port, true), gen);
  365. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping"));
  366. TKeepAliveHttpClient cl2("localhost", port);
  367. UNIT_ASSERT_NO_EXCEPTION(cl2.DoGet("/ping"));
  368. Sleep(TDuration::MilliSeconds(500));
  369. UNIT_ASSERT_NO_EXCEPTION(cl.DoGet("/ping"));
  370. }
  371. }