1 /****************************************************************************
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/qtestcase.h>
46 #include "QtWebSockets/QWebSocket"
48 class tst_WebSocketsTest : public QObject
57 void cleanupTestCase();
62 * @brief Test isValid() with an unoped socket
64 void testInvalidWithUnopenedSocket();
67 * @brief testTextMessage Tests sending and receiving a text message
69 void testTextMessage();
71 void testBinaryMessage();
74 * @brief Tests the method localAddress and localPort
76 void testLocalAddress();
79 * @brief Test the methods peerAddress, peerName and peerPort
81 void testPeerAddress();
84 * @brief Test the methods proxy() and setProxy() and check if it can be correctly set
89 * @brief Runs the autobahn tests against our implementation
91 //void autobahnTest();
94 QWebSocket *m_pWebSocket;
98 tst_WebSocketsTest::tst_WebSocketsTest() :
100 m_url(QStringLiteral("ws://localhost:9000"))
104 void tst_WebSocketsTest::initTestCase()
106 m_pWebSocket = new QWebSocket();
107 m_pWebSocket->open(m_url, true);
108 QTRY_VERIFY_WITH_TIMEOUT(m_pWebSocket->state() == QAbstractSocket::ConnectedState, 1000);
109 QVERIFY(m_pWebSocket->isValid());
112 void tst_WebSocketsTest::cleanupTestCase()
116 m_pWebSocket->close();
117 //QVERIFY2(m_pWebSocket->waitForDisconnected(1000), "Disconnection failed.");
123 void tst_WebSocketsTest::init()
127 void tst_WebSocketsTest::cleanup()
131 void tst_WebSocketsTest::testTextMessage()
133 const QString message = QStringLiteral("Hello world!");
135 QSignalSpy spy(m_pWebSocket, SIGNAL(textMessageReceived(QString)));
137 QCOMPARE(m_pWebSocket->sendTextMessage(message), qint64(message.length()));
138 QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
139 QCOMPARE(spy.count(), 1);
140 QCOMPARE(spy.at(0).count(), 1);
141 QCOMPARE(spy.takeFirst().at(0).toString(), message);
144 void tst_WebSocketsTest::testBinaryMessage()
146 QSignalSpy spy(m_pWebSocket, SIGNAL(binaryMessageReceived(QByteArray)));
148 QByteArray data("Hello world!");
150 QCOMPARE(m_pWebSocket->sendBinaryMessage(data), qint64(data.size()));
152 QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
153 QCOMPARE(spy.count(), 1);
154 QCOMPARE(spy.at(0).count(), 1);
155 QCOMPARE(spy.takeFirst().at(0).toByteArray(), data);
158 void tst_WebSocketsTest::testLocalAddress()
160 QCOMPARE(m_pWebSocket->localAddress().toString(), QStringLiteral("127.0.0.1"));
161 quint16 localPort = m_pWebSocket->localPort();
162 QVERIFY2(localPort > 0, "Local port is invalid.");
165 void tst_WebSocketsTest::testPeerAddress()
167 QHostInfo hostInfo = QHostInfo::fromName(m_url.host());
168 QList<QHostAddress> addresses = hostInfo.addresses();
169 QVERIFY(addresses.length() > 0);
170 QHostAddress peer = m_pWebSocket->peerAddress();
172 Q_FOREACH (const QHostAddress &a, addresses)
183 QFAIL("PeerAddress is not found as a result of a reverse lookup");
185 QCOMPARE(m_pWebSocket->peerName(), m_url.host());
186 QCOMPARE(m_pWebSocket->peerPort(), (quint16)m_url.port(80));
189 void tst_WebSocketsTest::testProxy()
191 QNetworkProxy oldProxy = m_pWebSocket->proxy();
192 QNetworkProxy proxy(QNetworkProxy::HttpProxy, QStringLiteral("proxy.network.com"), 80);
193 m_pWebSocket->setProxy(proxy);
194 QCOMPARE(proxy, m_pWebSocket->proxy());
195 m_pWebSocket->setProxy(oldProxy);
196 QCOMPARE(oldProxy, m_pWebSocket->proxy());
199 void tst_WebSocketsTest::testInvalidWithUnopenedSocket()
202 QCOMPARE(qws.isValid(), false);
205 QTEST_MAIN(tst_WebSocketsTest)
207 #include "tst_websockets.moc"