summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--rbutil/rbutilqt/test/test-httpget.cpp55
1 files changed, 31 insertions, 24 deletions
diff --git a/rbutil/rbutilqt/test/test-httpget.cpp b/rbutil/rbutilqt/test/test-httpget.cpp
index a062203e70..c6f5abf9fc 100644
--- a/rbutil/rbutilqt/test/test-httpget.cpp
+++ b/rbutil/rbutilqt/test/test-httpget.cpp
@@ -32,11 +32,13 @@
{
Q_OBJECT
public:
- HttpDaemon(quint16 port, QObject* parent = 0) : QTcpServer(parent)
+ HttpDaemon(quint16 port = 0, QObject* parent = 0) : QTcpServer(parent)
{
listen(QHostAddress::Any, port);
}
+ quint16 port(void) { return this->serverPort(); }
+
#if QT_VERSION < 0x050000
void incomingConnection(int socket)
#else
@@ -133,7 +135,10 @@ class TestHttpGet : public QObject
void cleanup(void);
public slots:
- void waitTimeout(void) { m_waitTimeoutOccured = true; }
+ void waitTimeout(void)
+ {
+ m_waitTimeoutOccured = true;
+ }
QDir temporaryFolder(void)
{
// Qt unfortunately doesn't support creating temporary folders so
@@ -160,19 +165,21 @@ class TestHttpGet : public QObject
}
private:
HttpDaemon *m_daemon;
+ QByteArray m_port;
bool m_waitTimeoutOccured;
QString m_now;
QDir m_cachedir;
- HttpGet *m_getter;
- QSignalSpy *m_doneSpy;
+ HttpGet *m_getter = NULL;
+ QSignalSpy *m_doneSpy = NULL;
};
void TestHttpGet::init(void)
{
m_now = QDateTime::currentDateTime().toString("ddd, d MMM yyyy hh:mm:ss");
- m_daemon = new HttpDaemon(8080, this);
+ m_daemon = new HttpDaemon(0, this); // use port 0 to auto-pick
m_daemon->reset();
+ m_port = QString("%1").arg(m_daemon->port()).toLatin1();
m_cachedir = temporaryFolder();
m_getter = new HttpGet(this);
m_doneSpy = new QSignalSpy(m_getter, SIGNAL(done(bool)));
@@ -183,10 +190,10 @@ void TestHttpGet::cleanup(void)
{
rmTree(m_cachedir.absolutePath());
if(m_getter) {
- m_getter->abort(); delete m_getter;
+ m_getter->abort(); delete m_getter; m_getter = NULL;
}
- if(m_daemon) delete m_daemon;
- if(m_doneSpy) delete m_doneSpy;
+ if(m_daemon) { delete m_daemon; m_daemon = NULL; }
+ if(m_doneSpy) { delete m_doneSpy; m_doneSpy = NULL; }
}
void TestHttpGet::testFileUrlRequest(void)
@@ -197,7 +204,7 @@ void TestHttpGet::testFileUrlRequest(void)
QTemporaryFile datafile;
datafile.open();
datafile.write(teststring.toLatin1());
- m_getter->getFile("file://" + datafile.fileName());
+ m_getter->getFile(QUrl("file://" + datafile.fileName()));
datafile.close();
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -230,7 +237,7 @@ void TestHttpGet::testUncachedRepeatedRequest(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -240,7 +247,7 @@ void TestHttpGet::testUncachedRepeatedRequest(void)
QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);
// request second time
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
QCOMPARE(m_doneSpy->count(), 2);
@@ -259,7 +266,7 @@ void TestHttpGet::testCachedRequest(void)
QList<QByteArray> responses;
responses << QByteArray(
"HTTP/1.1 302 Found\r\n"
- "Location: http://localhost:8080/test2.txt\r\n"
+ "Location: http://localhost:" + m_port + "/test2.txt\r\n"
"Date: " + m_now.toLatin1() + "\r\n"
"Last-Modified: " + m_now.toLatin1() + "\r\n"
"\r\n");
@@ -279,7 +286,7 @@ void TestHttpGet::testCachedRequest(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
m_getter->setCache(m_cachedir);
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -293,7 +300,7 @@ void TestHttpGet::testCachedRequest(void)
QCOMPARE(m_getter->httpResponse(), 200);
// request real file, this time the response should come from cache.
- m_getter->getFile(QUrl("http://localhost:8080/test2.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test2.txt"));
while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
QCOMPARE(m_doneSpy->count(), 2); // 2 requests, 2 times done()
@@ -329,7 +336,7 @@ void TestHttpGet::testUserAgent(void)
m_getter->setGlobalUserAgent(TEST_USER_AGENT);
m_getter->setCache(m_cachedir);
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -353,7 +360,7 @@ void TestHttpGet::testUncachedMovedRequest(void)
QList<QByteArray> responses;
responses << QByteArray(
"HTTP/1.1 302 Found\r\n"
- "Location: http://localhost:8080/test2.txt\r\n"
+ "Location: http://localhost:" + m_port + "/test2.txt\r\n"
"Date: " + m_now.toLatin1() + "\r\n"
"Last-Modified: " + m_now.toLatin1() + "\r\n"
"\r\n");
@@ -367,7 +374,7 @@ void TestHttpGet::testUncachedMovedRequest(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.php?var=1&b=foo"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -382,7 +389,7 @@ void TestHttpGet::testResponseCode(void)
{
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -407,7 +414,7 @@ void TestHttpGet::testContentToBuffer(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -433,7 +440,7 @@ void TestHttpGet::testContentToFile(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
m_getter->setFile(&tf);
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -450,7 +457,7 @@ void TestHttpGet::testContentToFile(void)
void TestHttpGet::testNoServer(void)
{
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8081/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:53/test1.txt"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
@@ -485,7 +492,7 @@ void TestHttpGet::testServerTimestamp(void)
int count = m_doneSpy->count();
for(int i = 0; i < responses.size(); ++i) {
- m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
while(m_doneSpy->count() == count && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();
count = m_doneSpy->count();
@@ -498,7 +505,7 @@ void TestHttpGet::testMovedQuery(void)
QList<QByteArray> responses;
responses << QByteArray(
"HTTP/1.1 302 Found\r\n"
- "Location: http://localhost:8080/test2.php\r\n"
+ "Location: http://localhost:" + m_port + "/test2.php\r\n"
"Date: " + m_now.toLatin1() + "\r\n"
"Last-Modified: " + m_now.toLatin1() + "\r\n"
"\r\n");
@@ -512,7 +519,7 @@ void TestHttpGet::testMovedQuery(void)
QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
- m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo"));
+ m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.php?var=1&b=foo"));
while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
QCoreApplication::processEvents();