bool complete;
bool failed;
string bodyData;
- string bodyContentType;
-
+
TestRequest(const std::string& url, const std::string method = "GET") :
HTTP::Request(url, method),
complete(false)
{
- bodyContentType = "text/plain";
+
}
- std::map<string, string> sendHeaders;
std::map<string, string> headers;
protected:
- string_list requestHeaders() const
- {
- string_list r;
- std::map<string, string>::const_iterator it;
- for (it = sendHeaders.begin(); it != sendHeaders.end(); ++it) {
- r.push_back(it->first);
- }
- return r;
- }
-
- string header(const string& name) const
- {
- std::map<string, string>::const_iterator it = sendHeaders.find(name);
- if (it == sendHeaders.end()) {
- return string();
- }
-
- return it->second;
- }
-
- virtual void responseHeadersComplete()
- {
- }
- virtual void responseComplete()
+ virtual void onDone()
{
complete = true;
}
- virtual void failure()
+ virtual void onFail()
{
failed = true;
}
bodyData += string(s, n);
}
- virtual std::string requestBodyType() const
- {
- return bodyContentType;
- }
-
virtual void responseHeader(const string& header, const string& value)
{
headers[header] = value;
class TestServer : public NetChannel
{
+ simgear::NetChannelPoller _poller;
public:
TestServer()
{
open();
bind(NULL, 2000); // localhost, any port
listen(5);
+
+ _poller.addChannel(this);
}
virtual ~TestServer()
//cout << "did accept from " << addr.getHost() << ":" << addr.getPort() << endl;
TestServerChannel* chan = new TestServerChannel();
chan->setHandle(handle);
+
+ _poller.addChannel(chan);
+ }
+
+ void poll()
+ {
+ _poller.poll();
}
};
+TestServer testServer;
+
void waitForComplete(HTTP::Client* cl, TestRequest* tr)
{
SGTimeStamp start(SGTimeStamp::now());
while (start.elapsedMSec() < 1000) {
cl->update();
+ testServer.poll();
+
if (tr->complete) {
return;
}
SGTimeStamp start(SGTimeStamp::now());
while (start.elapsedMSec() < 1000) {
cl->update();
+ testServer.poll();
+
if (tr->failed) {
return;
}
int main(int argc, char* argv[])
{
- TestServer s;
HTTP::Client cl;
-
+ // force all requests to use the same connection for this test
+ cl.setMaxConnections(1);
+
// test URL parsing
TestRequest* tr1 = new TestRequest("http://localhost.woo.zar:2000/test1?foo=bar");
COMPARE(tr1->scheme(), "http");
{
TestRequest* tr = new TestRequest("http://localhost:2000/test_headers");
HTTP::Request_ptr own(tr);
- tr->sendHeaders["X-Foo"] = "Bar";
- tr->sendHeaders["X-AnotherHeader"] = "A longer value";
+ tr->requestHeader("X-Foo") = "Bar";
+ tr->requestHeader("X-AnotherHeader") = "A longer value";
cl.makeRequest(tr);
waitForComplete(&cl, tr);
cout << "testing HTTP 1.1 pipelineing" << endl;
{
+
cl.setProxy("", 80);
TestRequest* tr = new TestRequest("http://localhost:2000/test1");
HTTP::Request_ptr own(tr);
{
cout << "POST" << endl;
TestRequest* tr = new TestRequest("http://localhost:2000/test_post?foo=abc&bar=1234&username=johndoe", "POST");
- tr->bodyContentType = "application/x-www-form-urlencoded";
+ tr->setBodyData("", "application/x-www-form-urlencoded");
HTTP::Request_ptr own(tr);
cl.makeRequest(tr);