Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/autotests/connectiontest.cpp
- This file was added.
1 | #include <QDebug> | ||||
---|---|---|---|---|---|
2 | #include <QMutex> | ||||
3 | #include <QTcpServer> | ||||
4 | #include <QTest> | ||||
5 | #include <QTimer> | ||||
6 | #include <QThread> | ||||
7 | #include <QWaitCondition> | ||||
8 | | ||||
9 | #include "../connection.h" | ||||
10 | | ||||
11 | namespace Bugzilla | ||||
12 | { | ||||
13 | | ||||
14 | class ConnectionTest : public QObject | ||||
15 | { | ||||
16 | Q_OBJECT | ||||
17 | private Q_SLOTS: | ||||
18 | | ||||
19 | void initTestCase() | ||||
20 | { | ||||
21 | } | ||||
22 | | ||||
23 | void testGet() | ||||
24 | { | ||||
25 | // qhttpserver is still in qt-labs. as a simple solution do some dumb | ||||
26 | // http socketing. | ||||
27 | QTcpServer t; | ||||
28 | QCOMPARE(t.listen(QHostAddress::LocalHost, 0), true); | ||||
29 | connect(&t, &QTcpServer::newConnection, | ||||
30 | this, [&t]() { | ||||
31 | QTcpSocket *socket = t.nextPendingConnection(); | ||||
32 | socket->waitForReadyRead(); | ||||
33 | QString httpBlob = socket->readAll(); | ||||
34 | qDebug() << httpBlob; | ||||
35 | // The query is important to see if this actually gets properly | ||||
36 | // passed along! | ||||
37 | if (httpBlob.startsWith("GET /hi?informal=yes")) { | ||||
38 | QFile file(QFINDTESTDATA("data/hi.http")); | ||||
39 | Q_ASSERT(file.open(QFile::ReadOnly | QFile::Text)); | ||||
40 | socket->write(file.readAll()); | ||||
41 | socket->waitForBytesWritten(); | ||||
42 | socket->close(); | ||||
43 | return; | ||||
44 | } | ||||
45 | qDebug() << httpBlob; | ||||
46 | Q_ASSERT_X(false, "server", "Unexpected request"); | ||||
47 | }); | ||||
48 | | ||||
49 | QUrl root("http://localhost"); | ||||
50 | root.setPort(t.serverPort()); | ||||
51 | HTTPConnection c(root); | ||||
52 | QUrlQuery query; | ||||
53 | query.addQueryItem("informal", "yes"); | ||||
54 | auto job = c.get("/hi", query); | ||||
55 | job->exec(); | ||||
56 | QCOMPARE(job->data(), "Hello!\n"); | ||||
57 | } | ||||
58 | | ||||
59 | void testGetJsonError() | ||||
60 | { | ||||
61 | // qhttpserver is still in qt-labs. as a simple solution do some dumb | ||||
62 | // http socketing. | ||||
63 | QTcpServer t; | ||||
64 | QCOMPARE(t.listen(QHostAddress::LocalHost, 0), true); | ||||
65 | connect(&t, &QTcpServer::newConnection, | ||||
66 | this, [&t]() { | ||||
67 | QTcpSocket *socket = t.nextPendingConnection(); | ||||
68 | socket->waitForReadyRead(); | ||||
69 | QString httpBlob = socket->readAll(); | ||||
70 | qDebug() << httpBlob; | ||||
71 | QFile file(QFINDTESTDATA("data/error.http")); | ||||
72 | QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); | ||||
73 | socket->write(file.readAll()); | ||||
74 | socket->waitForBytesWritten(); | ||||
75 | socket->close(); | ||||
76 | return; | ||||
77 | }); | ||||
78 | | ||||
79 | QUrl root("http://localhost"); | ||||
80 | root.setPort(t.serverPort()); | ||||
81 | HTTPConnection c(root); | ||||
82 | auto job = c.get("/hi"); | ||||
83 | job->exec(); | ||||
84 | QCOMPARE(job->error(), 207); // error from json blob | ||||
85 | } | ||||
86 | | ||||
87 | void testPut() | ||||
88 | { | ||||
89 | // qhttpserver is still in qt-labs. as a simple solution do some dumb | ||||
90 | // http socketing. | ||||
91 | QThread thread; | ||||
92 | QTcpServer server; | ||||
93 | server.moveToThread(&thread); | ||||
94 | | ||||
95 | int segments = 0; // lambda member essentially | ||||
96 | | ||||
97 | connect(&server, &QTcpServer::newConnection, | ||||
98 | &server, [&server, &segments]() { | ||||
99 | QCOMPARE(server.thread(), QThread::currentThread()); | ||||
100 | QTcpSocket *socket = server.nextPendingConnection(); | ||||
101 | connect(socket, &QTcpSocket::readyRead, | ||||
102 | [&server, &segments, socket] { | ||||
103 | ++segments; | ||||
104 | | ||||
105 | qDebug() << "segment" << segments; | ||||
106 | auto data = socket->readAll(); | ||||
107 | qDebug() << data; | ||||
108 | if (segments == 1) { | ||||
109 | Q_ASSERT(data.startsWith("PUT /put HTTP/")); | ||||
110 | } else if (segments == 2) { | ||||
111 | Q_ASSERT(data.startsWith("Content-Length: 12")); | ||||
112 | } else if (segments == 3) { | ||||
113 | Q_ASSERT(data == "hello there!"); | ||||
114 | | ||||
115 | // send reply. this is a bit awkward, but in lieu | ||||
116 | // of a proper server we'll just assume the request | ||||
117 | // was "complete" as our marker expectations are met. | ||||
118 | // this is however highly dependent on KIO's behavior. | ||||
119 | // Sooooo this test may turn flaky in the future. | ||||
120 | | ||||
121 | QFile file(QFINDTESTDATA("data/put.http")); | ||||
122 | QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); | ||||
123 | QByteArray ret = file.readAll(); | ||||
124 | ret.replace("\n", "\r\n"); | ||||
125 | qDebug() << ret; | ||||
126 | socket->write(ret); | ||||
127 | socket->waitForBytesWritten(); | ||||
128 | | ||||
129 | socket->disconnect(); | ||||
130 | socket->close(); | ||||
131 | } | ||||
132 | }); | ||||
133 | }); | ||||
134 | | ||||
135 | thread.start(); | ||||
136 | | ||||
137 | QMutex portMutex; | ||||
138 | QWaitCondition portCondition; | ||||
139 | quint16 port; | ||||
140 | portMutex.lock(); | ||||
141 | QTimer::singleShot(0, &server, [&server, &portMutex, &portCondition, &port]() { | ||||
142 | Q_ASSERT(server.listen(QHostAddress::LocalHost, 0)); | ||||
143 | QMutexLocker locker(&portMutex); | ||||
144 | port = server.serverPort(); | ||||
145 | portCondition.wakeAll(); | ||||
146 | }); | ||||
147 | portCondition.wait(&portMutex); | ||||
148 | portMutex.unlock(); | ||||
149 | | ||||
150 | QUrl root("http://localhost"); | ||||
151 | root.setPort(server.serverPort()); | ||||
152 | HTTPConnection c(root); | ||||
153 | APIJob *job = c.put("/put", "hello there!"); | ||||
154 | job->exec(); | ||||
155 | | ||||
156 | thread.quit(); | ||||
157 | thread.wait(); | ||||
158 | | ||||
159 | QCOMPARE(job->error(), KJob::NoError); | ||||
160 | } | ||||
161 | }; | ||||
162 | | ||||
163 | } // namespace Bugzilla | ||||
164 | | ||||
165 | QTEST_MAIN(Bugzilla::ConnectionTest) | ||||
166 | | ||||
167 | #include "connectiontest.moc" |