File indexing completed on 2025-03-09 04:23:15
0001 #ifndef TEST_TESTWEBDAVCLIENT 0002 #define TEST_TESTWEBDAVCLIENT 0003 0004 #include <QCoreApplication> 0005 #include <QList> 0006 #include <QObject> 0007 #include <QTest> 0008 0009 #include "../WebDAVClient.hpp" 0010 #include "../dto/WebDAVItem.hpp" 0011 #include "../utils/Environment.hpp" 0012 #include "../utils/WebDAVReply.hpp" 0013 0014 class TestWebDAVClient : public QObject 0015 { 0016 Q_OBJECT 0017 0018 private: 0019 QCoreApplication *app; 0020 WebDAVClient *client; 0021 0022 void listDirOutputHandler(WebDAVReply *reply) 0023 { 0024 connect(reply, &WebDAVReply::listDirResponse, [=](QNetworkReply *listDirReply, QList<WebDAVItem> items) { 0025 qDebug() << "URL :" << listDirReply->url(); 0026 qDebug() << "Received List of" << items.length() << "items"; 0027 qDebug() << endl << "---------------------------------------"; 0028 0029 for (WebDAVItem item : items) { 0030 qDebug().noquote() << endl << item.toString(); 0031 qDebug() << endl << "---------------------------------------"; 0032 } 0033 0034 QCoreApplication::exit(0); 0035 }); 0036 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0037 qDebug() << err; 0038 QCoreApplication::exit(1); 0039 }); 0040 } 0041 0042 private slots: 0043 void initTestCase() 0044 { 0045 int argc = 1; 0046 char *argv[] = {"test"}; 0047 0048 this->app = new QCoreApplication(argc, argv); 0049 this->client = new WebDAVClient(Environment::get("LIBWEBDAV_TEST_HOST"), Environment::get("LIBWEBDAV_TEST_USER"), Environment::get("LIBWEBDAV_TEST_PASSWORD")); 0050 } 0051 0052 void testListDir() 0053 { 0054 this->listDirOutputHandler(this->client->listDir(Environment::get("LIBWEBDAV_TEST_PATH"))); 0055 this->app->exec(); 0056 } 0057 0058 void testListDirWithDepth() 0059 { 0060 this->listDirOutputHandler(this->client->listDir(Environment::get("LIBWEBDAV_TEST_PATH"), ListDepthEnum::One)); 0061 0062 this->app->exec(); 0063 } 0064 0065 void testDownload() 0066 { 0067 QString url = "/remote.php/webdav/Nextcloud%20Manual.pdf"; 0068 0069 WebDAVReply *reply = this->client->downloadFrom(url); 0070 0071 connect(reply, &WebDAVReply::downloadResponse, [=](QNetworkReply *reply) { 0072 if (!reply->error()) { 0073 qDebug() << "\nDownload Success" 0074 << "\nURL :" << reply->url() << "\nSize :" << reply->size(); 0075 } else { 0076 qDebug() << "ERROR(DOWNLOAD)" << reply->error(); 0077 } 0078 QCoreApplication::exit(0); 0079 }); 0080 connect(reply, &WebDAVReply::downloadProgressResponse, [=](qint64 bytesReceived, qint64 bytesTotal) { 0081 int percent = ((float)bytesReceived / bytesTotal) * 100; 0082 0083 qDebug() << "\nReceived : " << bytesReceived << "\nTotal : " << bytesTotal << "\nPercent : " << percent; 0084 }); 0085 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0086 qDebug() << "ERROR" << err; 0087 QCoreApplication::exit(1); 0088 }); 0089 0090 this->app->exec(); 0091 } 0092 0093 void testUpload() 0094 { 0095 QString url = Environment::get("LIBWEBDAV_TEST_PATH"); 0096 QFile file("/home/anupam/libwebdav/lib/WebDAVClient.cpp"); 0097 file.open(QIODevice::ReadOnly); 0098 0099 WebDAVReply *reply = this->client->uploadTo(url, "tttt.cpp", &file); 0100 0101 connect(reply, &WebDAVReply::uploadFinished, [=](QNetworkReply *reply) { 0102 if (!reply->error()) { 0103 qDebug() << "\nUpload Success" 0104 << "\nURL :" << reply->url() << "\nSize :" << reply->size(); 0105 } else { 0106 qDebug() << "ERROR(UPLOAD)" << reply->error(); 0107 } 0108 QCoreApplication::exit(0); 0109 }); 0110 0111 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0112 qDebug() << "ERROR" << err; 0113 QCoreApplication::exit(1); 0114 }); 0115 0116 this->app->exec(); 0117 } 0118 0119 void testCreateDir() 0120 { 0121 QString dirName = QDate::currentDate().toString(Qt::DateFormat::ISODate); 0122 WebDAVReply *reply = this->client->createDir(Environment::get("LIBWEBDAV_TEST_PATH"), dirName); 0123 0124 connect(reply, &WebDAVReply::createDirFinished, [=](QNetworkReply *reply) { 0125 if (!reply->error()) { 0126 qDebug() << "\nDir Created" 0127 << "\nURL :" << reply->url(); 0128 } else { 0129 qDebug() << "ERROR(CREATE DIR)" << reply->error(); 0130 } 0131 QCoreApplication::exit(0); 0132 }); 0133 0134 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0135 qDebug() << "ERROR" << err; 0136 QCoreApplication::exit(1); 0137 }); 0138 0139 this->app->exec(); 0140 } 0141 0142 void testCopyDir() 0143 { 0144 QString dirName = QDate::currentDate().toString(Qt::DateFormat::ISODate); 0145 WebDAVReply *reply = this->client->copy(Environment::get("LIBWEBDAV_TEST_PATH") + "/tttt.cpp", Environment::get("LIBWEBDAV_TEST_PATH") + "/" + dirName + "/tttt-copy.cpp"); 0146 0147 connect(reply, &WebDAVReply::copyFinished, [=](QNetworkReply *reply) { 0148 if (!reply->error()) { 0149 qDebug() << "\nItem Copied" 0150 << "\nURL :" << reply->url(); 0151 } else { 0152 qDebug() << "ERROR(COPY)" << reply->error(); 0153 } 0154 QCoreApplication::exit(0); 0155 }); 0156 0157 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0158 qDebug() << "ERROR" << err; 0159 QCoreApplication::exit(1); 0160 }); 0161 0162 this->app->exec(); 0163 } 0164 0165 void testMoveDir() 0166 { 0167 QString dirName = QDate::currentDate().toString(Qt::DateFormat::ISODate); 0168 WebDAVReply *reply = this->client->move(Environment::get("LIBWEBDAV_TEST_PATH") + "/tttt.cpp", Environment::get("LIBWEBDAV_TEST_PATH") + "/" + dirName + "/tttt.cpp", true); 0169 0170 connect(reply, &WebDAVReply::moveFinished, [=](QNetworkReply *reply) { 0171 if (!reply->error()) { 0172 qDebug() << "\nItem Moved" 0173 << "\nURL :" << reply->url(); 0174 } else { 0175 qDebug() << "ERROR(MOVE DIR)" << reply->error(); 0176 } 0177 QCoreApplication::exit(0); 0178 }); 0179 0180 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0181 qDebug() << "ERROR" << err; 0182 QCoreApplication::exit(1); 0183 }); 0184 0185 this->app->exec(); 0186 } 0187 0188 void testRemove() 0189 { 0190 QString dirName = QDate::currentDate().toString(Qt::DateFormat::ISODate); 0191 0192 WebDAVReply *reply = this->client->remove(Environment::get("LIBWEBDAV_TEST_PATH") + "/" + dirName + "/tttt.cpp"); 0193 0194 connect(reply, &WebDAVReply::removeFinished, [=](QNetworkReply *reply) { 0195 if (!reply->error()) { 0196 qDebug() << "\nItem Removed" 0197 << "\nURL :" << reply->url(); 0198 } else { 0199 qDebug() << "ERROR(REMOVE File)" << reply->error(); 0200 } 0201 QCoreApplication::exit(0); 0202 }); 0203 0204 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0205 qDebug() << "ERROR" << err; 0206 QCoreApplication::exit(1); 0207 }); 0208 0209 this->app->exec(); 0210 } 0211 0212 void testRemoveDir() 0213 { 0214 QString dirName = QDate::currentDate().toString(Qt::DateFormat::ISODate); 0215 WebDAVReply *reply = this->client->remove(Environment::get("LIBWEBDAV_TEST_PATH") + "/" + dirName); 0216 0217 connect(reply, &WebDAVReply::removeFinished, [=](QNetworkReply *reply) { 0218 if (!reply->error()) { 0219 qDebug() << "\nItem Removed" 0220 << "\nURL :" << reply->url(); 0221 } else { 0222 qDebug() << "ERROR(REMOVE Dir)" << reply->error(); 0223 } 0224 QCoreApplication::exit(0); 0225 }); 0226 0227 connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) { 0228 qDebug() << "ERROR" << err; 0229 QCoreApplication::exit(1); 0230 }); 0231 0232 this->app->exec(); 0233 } 0234 0235 void cleanupTestCase() 0236 { 0237 delete this->app; 0238 } 0239 }; 0240 0241 QTEST_MAIN(TestWebDAVClient) 0242 #include "TestWebDAVClient.moc" 0243 0244 #endif