https_ut.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <library/cpp/http/simple/http_client.h>
  2. #include <library/cpp/http/server/response.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <library/cpp/testing/unittest/tests_data.h>
  5. #include <util/system/shellcommand.h>
  6. Y_UNIT_TEST_SUITE(Https) {
  7. using TShellCommandPtr = std::unique_ptr<TShellCommand>;
  8. static TShellCommandPtr start(ui16 port) {
  9. const TString data = ArcadiaSourceRoot() + "/library/cpp/http/simple/ut/https_server";
  10. const TString command =
  11. TStringBuilder()
  12. << BuildRoot() << "/library/cpp/http/simple/ut/https_server/https_server"
  13. << " --port " << port
  14. << " --keyfile " << data << "/http_server.key"
  15. << " --certfile " << data << "/http_server.crt";
  16. auto res = std::make_unique<TShellCommand>(
  17. command,
  18. TShellCommandOptions()
  19. .SetAsync(true)
  20. .SetLatency(50)
  21. .SetErrorStream(&Cerr));
  22. res->Run();
  23. i32 tries = 100000;
  24. while (tries-- > 0) {
  25. try {
  26. TKeepAliveHttpClient client("https://localhost", port);
  27. client.DisableVerificationForHttps();
  28. client.DoGet("/ping");
  29. break;
  30. } catch (const std::exception& e) {
  31. Cout << "== failed to connect to new server: " << e.what() << Endl;
  32. Sleep(TDuration::MilliSeconds(1));
  33. }
  34. }
  35. return res;
  36. }
  37. static void get(TKeepAliveHttpClient & client) {
  38. TStringStream out;
  39. ui32 code = 0;
  40. UNIT_ASSERT_NO_EXCEPTION(code = client.DoGet("/ping", &out));
  41. UNIT_ASSERT_VALUES_EQUAL_C(code, 200, out.Str());
  42. UNIT_ASSERT_VALUES_EQUAL(out.Str(), "pong.my");
  43. }
  44. Y_UNIT_TEST(keepAlive) {
  45. TPortManager pm;
  46. ui16 port = pm.GetPort(443);
  47. TShellCommandPtr httpsServer = start(port);
  48. TKeepAliveHttpClient client("https://localhost",
  49. port,
  50. TDuration::Seconds(40),
  51. TDuration::Seconds(40));
  52. client.DisableVerificationForHttps();
  53. get(client);
  54. get(client);
  55. httpsServer->Terminate().Wait();
  56. httpsServer = start(port);
  57. get(client);
  58. }
  59. static void get(TSimpleHttpClient & client) {
  60. TStringStream out;
  61. UNIT_ASSERT_NO_EXCEPTION_C(client.DoGet("/ping", &out), out.Str());
  62. UNIT_ASSERT_VALUES_EQUAL(out.Str(), "pong.my");
  63. }
  64. Y_UNIT_TEST(simple) {
  65. TPortManager pm;
  66. ui16 port = pm.GetPort(443);
  67. TShellCommandPtr httpsServer = start(port);
  68. TSimpleHttpClient client("https://localhost",
  69. port,
  70. TDuration::Seconds(40),
  71. TDuration::Seconds(40));
  72. get(client);
  73. get(client);
  74. }
  75. }