HttpClientTest.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WConfig.h>
  7. #ifdef WT_THREADED
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/thread.hpp>
  10. #include <boost/thread/condition.hpp>
  11. #include <Wt/WApplication>
  12. #include <Wt/WIOService>
  13. #include <Wt/Http/Client>
  14. #include <Wt/Test/WTestEnvironment>
  15. using namespace Wt;
  16. using namespace Wt::Http;
  17. namespace {
  18. class TestFixture : public WApplication
  19. {
  20. public:
  21. TestFixture(const WEnvironment& env)
  22. : WApplication(env),
  23. done_(false)
  24. { }
  25. void waitDone()
  26. {
  27. boost::mutex::scoped_lock guard(doneMutex_);
  28. while (!done_)
  29. doneCondition_.wait(guard);
  30. }
  31. void reset()
  32. {
  33. done_ = false;
  34. }
  35. void onDone(boost::system::error_code err, const Message& m)
  36. {
  37. assert (WApplication::instance() == this);
  38. boost::mutex::scoped_lock guard(doneMutex_);
  39. err_ = err;
  40. message_ = m;
  41. if (err_)
  42. std::cerr << "Http client error: " << err_.message() << std::endl;
  43. else {
  44. std::cerr << "Http client result status: " << m.status() << std::endl;
  45. for (unsigned i = 0; i < m.headers().size(); ++i) {
  46. const Message::Header& h = m.headers()[i];
  47. std::cerr << " " << h.name() << ": " << h.value() << std::endl;
  48. }
  49. std::cerr << " Body: -----" << std::endl;
  50. std::cerr << m.body();
  51. std::cerr << "-----" << std::endl;
  52. }
  53. done_ = true;
  54. doneCondition_.notify_one();
  55. }
  56. private:
  57. bool done_;
  58. boost::condition doneCondition_;
  59. boost::mutex doneMutex_;
  60. boost::system::error_code err_;
  61. Message message_;
  62. };
  63. }
  64. BOOST_AUTO_TEST_CASE( http_client_test1 )
  65. {
  66. Wt::Test::WTestEnvironment environment;
  67. TestFixture app(environment);
  68. Client *c = new Client(&app);
  69. c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));
  70. std::string ok = "www.google.com/";
  71. if (c->get("https://" + ok)) {
  72. environment.endRequest();
  73. app.waitDone();
  74. environment.startRequest();
  75. }
  76. }
  77. BOOST_AUTO_TEST_CASE( http_client_test2 )
  78. {
  79. Wt::Test::WTestEnvironment environment;
  80. TestFixture app(environment);
  81. Client *c = new Client(&app);
  82. c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));
  83. std::string verifyFail = "pause.perl.org/";
  84. /* Does no longer fail! Too bad */
  85. if (c->get("https://" + verifyFail)) {
  86. environment.endRequest();
  87. app.waitDone();
  88. environment.startRequest();
  89. }
  90. }
  91. BOOST_AUTO_TEST_CASE( http_client_test3 )
  92. {
  93. Wt::Test::WTestEnvironment environment;
  94. TestFixture app(environment);
  95. Client *c = new Client(&app);
  96. c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));
  97. std::string asioFail = "www.google.be/";
  98. /* Asio no longer fails. Good! */
  99. if (c->get("https://" + asioFail)) {
  100. environment.endRequest();
  101. app.waitDone();
  102. environment.startRequest();
  103. }
  104. }
  105. BOOST_AUTO_TEST_CASE( http_client_test4 )
  106. {
  107. Wt::Test::WTestEnvironment environment;
  108. TestFixture app(environment);
  109. environment.server()->ioService().start();
  110. Client *c = new Client(&app);
  111. c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));
  112. std::string ok = "www.google.com/";
  113. if (c->get("https://" + ok)) {
  114. environment.endRequest();
  115. app.waitDone();
  116. environment.startRequest();
  117. }
  118. environment.server()->ioService().stop();
  119. environment.server()->ioService().start();
  120. app.reset();
  121. if (c->get("https://" + ok)) {
  122. environment.endRequest();
  123. app.waitDone();
  124. environment.startRequest();
  125. }
  126. }
  127. #endif // WT_THREADED