HttpTester.cpp

Go to the documentation of this file.
00001 #include "Qt.h"
00002 #include "ClientSession.h"
00003 #include "ResponseHeader.h"
00004 #include "Imports.cpp"
00005 
00006 // Further imports required for testing:
00007 #include "Networks/Transports/NodeAddress.h"
00008 #include "Networks/Transports/TcpConnection.h"
00009 
00010 using Networks::Transports::NodeAddress;
00011 using Networks::Transports::TcpConnection;
00012 
00013 using namespace Http;
00014 
00015 class HttpTester : public QObject
00016 {
00017     Q_OBJECT
00018 
00019 public:
00020             HttpTester (QCoreApplication *);
00021             ~HttpTester();
00022 
00023 private slots:
00024     void    connectionEstablishedSlot();
00025     void    responseHeaderReadSlot();
00026     void    responseBodyReadSlot();
00027     void    readErrorSlot();
00028 
00029 private:
00030     QCoreApplication    *app;
00031     TcpConnection       *connection;
00032     ClientSession       *session;
00033     QBuffer             buffer;
00034 };
00035 
00036 HttpTester::HttpTester (QCoreApplication *app_)
00037 {
00038     app         = app_;
00039     connection  = new TcpConnection;
00040     session     = 0;
00041 
00042     connect (connection, SIGNAL (connectionEstablished()),
00043              this, SLOT (connectionEstablishedSlot()));
00044 
00045     NodeAddress nodeAddress ("localhost", 80);
00046     qDebug() << "Establishing TCP connection to " << nodeAddress.toString();
00047     connection->connectToNode (nodeAddress);
00048 }
00049 
00050 HttpTester::~HttpTester()
00051 {
00052     if (session)
00053         delete session;
00054     delete connection;
00055 }
00056 
00057 void HttpTester::connectionEstablishedSlot()
00058 {
00059     qDebug() << "Connection established!";
00060 
00061     QUrl        uri ("http://localhost/test.txt");
00062     Header      header;
00063     session     = new ClientSession (connection);
00064 
00065     connect (session, SIGNAL (responseHeaderRead()),
00066              this, SLOT (responseHeaderReadSlot()));
00067     connect (session, SIGNAL (responseBodyRead()),
00068              this, SLOT (responseBodyReadSlot()));
00069 
00070     header.setFieldValue ("X-My-Field", "my-value");
00071     session->get (uri, header);
00072 }
00073 
00074 void HttpTester::responseHeaderReadSlot()
00075 {
00076     const ResponseHeader *header = session->responseHeader();
00077     qDebug() << "Responst header: " << header->toString();
00078 
00079     buffer.open (QBuffer::ReadWrite);
00080     session->readResponseBody (&buffer);
00081 }
00082 
00083 void HttpTester::responseBodyReadSlot()
00084 {
00085     qDebug() << "response body read: ";
00086     qDebug() << buffer.data();
00087 }
00088 
00089 void HttpTester::readErrorSlot()
00090 {
00091 }
00092 
00093 int main(int argc, char *argv[])
00094 {
00095     QCoreApplication app(argc, argv);
00096     qRegisterMetaType<qint64>("qint64");
00097     HttpTester httpTester (&app);
00098     return app.exec();
00099 }
00100 
00101 #include "HttpTester.moc"