File: HttpClientTest.C

package info (click to toggle)
witty 3.3.3%2Bdfsg-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,228 kB
  • ctags: 26,694
  • sloc: cpp: 147,809; ansic: 77,999; xml: 16,331; sh: 1,303; makefile: 198; java: 86; sql: 14
file content (163 lines) | stat: -rw-r--r-- 3,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#ifdef WT_THREADED

#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition.hpp>

#include <Wt/WApplication>
#include <Wt/WIOService>
#include <Wt/Http/Client>
#include <Wt/Test/WTestEnvironment>

using namespace Wt;
using namespace Wt::Http;

namespace {

  class TestFixture : public WApplication
  {
  public:
    TestFixture(const WEnvironment& env)
      : WApplication(env),
	done_(false)
    { }

    void waitDone()
    {
      boost::mutex::scoped_lock guard(doneMutex_);

      while (!done_)
	doneCondition_.wait(guard);
    }

    void reset() 
    {
      done_ = false;
    }

    void onDone(boost::system::error_code err, const Message& m)
    {
      assert (WApplication::instance() == this);

      boost::mutex::scoped_lock guard(doneMutex_);

      err_ = err;
      message_ = m;

      if (err_)
	std::cerr << "Http client error: " << err_.message() << std::endl;
      else {
	std::cerr << "Http client result status: " << m.status() << std::endl;
	for (unsigned i = 0; i < m.headers().size(); ++i) {
	  const Message::Header& h = m.headers()[i];
	  std::cerr << " " << h.name() << ": " << h.value() << std::endl;
	}
	std::cerr << " Body: -----" << std::endl;
	std::cerr << m.body();
	std::cerr << "-----" << std::endl;
      }

      done_ = true;
      doneCondition_.notify_one();
    }

  private:
    bool done_;
    boost::condition doneCondition_;
    boost::mutex doneMutex_;

    boost::system::error_code err_;
    Message message_;
  };
}

BOOST_AUTO_TEST_CASE( http_client_test1 )
{
  Wt::Test::WTestEnvironment environment;
  TestFixture app(environment);

  Client *c = new Client(&app);
  c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));

  std::string ok = "www.google.com/";

  if (c->get("https://" + ok)) {
    environment.endRequest();
    app.waitDone();
    environment.startRequest();
  }
}

BOOST_AUTO_TEST_CASE( http_client_test2 )
{
  Wt::Test::WTestEnvironment environment;
  TestFixture app(environment);

  Client *c = new Client(&app);
  c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));

  std::string verifyFail = "pause.perl.org/";

  /* Does no longer fail! Too bad */
  if (c->get("https://" + verifyFail)) {
    environment.endRequest();
    app.waitDone();
    environment.startRequest();
  }
}

BOOST_AUTO_TEST_CASE( http_client_test3 )
{
  Wt::Test::WTestEnvironment environment;
  TestFixture app(environment);

  Client *c = new Client(&app);
  c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));

  std::string asioFail = "www.google.be/";

  /* Asio no longer fails. Good! */
  if (c->get("https://" + asioFail)) {
    environment.endRequest();
    app.waitDone();
    environment.startRequest();
  }
}

BOOST_AUTO_TEST_CASE( http_client_test4 )
{
  Wt::Test::WTestEnvironment environment;
  TestFixture app(environment);

  environment.server()->ioService().start();
  
  Client *c = new Client(&app);
  c->done().connect(boost::bind(&TestFixture::onDone, &app, _1, _2));

  std::string ok = "www.google.com/";

  if (c->get("https://" + ok)) {
    environment.endRequest();
    app.waitDone();
    environment.startRequest();
  }

  environment.server()->ioService().stop();
  environment.server()->ioService().start();
  
  app.reset();

  if (c->get("https://" + ok)) {
    environment.endRequest();
    app.waitDone();
    environment.startRequest();
  }
}

#endif // WT_THREADED