blob: 3dcd8dd9165dc8120a77f3eb0c83df543d4a395c (
plain)
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
|
// Copyright (C) 2023 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QEventLoop>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QScopedPointer>
#include <memory>
namespace {
const QUrl URL = QUrl("http://localhost:6931/test_batch.html");
class BackgroundThread : public QThread
{
Q_OBJECT
void run() override
{
manager = std::make_unique<QNetworkAccessManager>();
eventLoop = std::make_unique<QEventLoop>();
reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, this, &BackgroundThread::requestFinished);
eventLoop->exec();
}
void requestFinished()
{
eventLoop->quit();
}
public:
QNetworkReply *reply{ nullptr };
private:
std::unique_ptr<QNetworkAccessManager> manager;
std::unique_ptr<QEventLoop> eventLoop;
QNetworkRequest request{ URL };
};
} // namespace
class tst_FetchApi : public QObject
{
Q_OBJECT
public:
tst_FetchApi() = default;
private Q_SLOTS:
void sendRequestOnMainThread();
void sendRequestOnBackgroundThread();
};
void tst_FetchApi::sendRequestOnMainThread()
{
QNetworkAccessManager manager;
QNetworkRequest request(URL);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY(reply != nullptr);
QVERIFY(reply->error() == QNetworkReply::NoError);
}
void tst_FetchApi::sendRequestOnBackgroundThread()
{
QSKIP("Skip this test until we fix fetching from background threads.");
QEventLoop mainEventLoop;
BackgroundThread *backgroundThread = new BackgroundThread();
connect(backgroundThread, &BackgroundThread::finished, &mainEventLoop, &QEventLoop::quit);
connect(backgroundThread, &BackgroundThread::finished, backgroundThread, &QObject::deleteLater);
backgroundThread->start();
mainEventLoop.exec();
QVERIFY(backgroundThread != nullptr);
QVERIFY(backgroundThread->reply != nullptr);
QVERIFY(backgroundThread->reply->error() == QNetworkReply::NoError);
}
QTEST_MAIN(tst_FetchApi);
#include "tst_fetchapi.moc"
|