Use const & for your foreach variables
[contrib/qtwebsockets.git] / tests / manual / websockets / tst_websockets.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/qtestcase.h>
43 #include <QSignalSpy>
44 #include <QHostInfo>
45 #include <QDebug>
46 #include "QtWebSockets/QWebSocket"
47
48 class tst_WebSocketsTest : public QObject
49 {
50     Q_OBJECT
51
52 public:
53     tst_WebSocketsTest();
54
55 private Q_SLOTS:
56     void initTestCase();
57     void cleanupTestCase();
58     void init();
59     void cleanup();
60
61     /**
62       * @brief Test isValid() with an unoped socket
63       */
64     void testInvalidWithUnopenedSocket();
65
66     /**
67      * @brief testTextMessage Tests sending and receiving a text message
68      */
69     void testTextMessage();
70
71     void testBinaryMessage();
72
73     /**
74      * @brief Tests the method localAddress and localPort
75      */
76     void testLocalAddress();
77
78     /**
79      * @brief Test the methods peerAddress, peerName and peerPort
80      */
81     void testPeerAddress();
82
83     /**
84      * @brief Test the methods proxy() and setProxy() and check if it can be correctly set
85      */
86     void testProxy();
87
88     /**
89      * @brief Runs the autobahn tests against our implementation
90      */
91     //void autobahnTest();
92
93 private:
94     QWebSocket *m_pWebSocket;
95     QUrl m_url;
96 };
97
98 tst_WebSocketsTest::tst_WebSocketsTest() :
99     m_pWebSocket(0),
100     m_url(QStringLiteral("ws://localhost:9000"))
101 {
102 }
103
104 void tst_WebSocketsTest::initTestCase()
105 {
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());
110 }
111
112 void tst_WebSocketsTest::cleanupTestCase()
113 {
114     if (m_pWebSocket)
115     {
116         m_pWebSocket->close();
117         //QVERIFY2(m_pWebSocket->waitForDisconnected(1000), "Disconnection failed.");
118         delete m_pWebSocket;
119         m_pWebSocket = 0;
120     }
121 }
122
123 void tst_WebSocketsTest::init()
124 {
125 }
126
127 void tst_WebSocketsTest::cleanup()
128 {
129 }
130
131 void tst_WebSocketsTest::testTextMessage()
132 {
133     const QString message = QStringLiteral("Hello world!");
134
135     QSignalSpy spy(m_pWebSocket, SIGNAL(textMessageReceived(QString)));
136
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);
142 }
143
144 void tst_WebSocketsTest::testBinaryMessage()
145 {
146     QSignalSpy spy(m_pWebSocket, SIGNAL(binaryMessageReceived(QByteArray)));
147
148     QByteArray data("Hello world!");
149
150     QCOMPARE(m_pWebSocket->sendBinaryMessage(data), qint64(data.size()));
151
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);
156 }
157
158 void tst_WebSocketsTest::testLocalAddress()
159 {
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.");
163 }
164
165 void tst_WebSocketsTest::testPeerAddress()
166 {
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();
171     bool found = false;
172     Q_FOREACH (const QHostAddress &a, addresses)
173     {
174         if (a == peer)
175         {
176             found = true;
177             break;
178         }
179     }
180
181     if (!found)
182     {
183         QFAIL("PeerAddress is not found as a result of a reverse lookup");
184     }
185     QCOMPARE(m_pWebSocket->peerName(), m_url.host());
186     QCOMPARE(m_pWebSocket->peerPort(), (quint16)m_url.port(80));
187 }
188
189 void tst_WebSocketsTest::testProxy()
190 {
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());
197 }
198
199 void tst_WebSocketsTest::testInvalidWithUnopenedSocket()
200 {
201     QWebSocket qws;
202     QCOMPARE(qws.isValid(), false);
203 }
204
205 QTEST_MAIN(tst_WebSocketsTest)
206
207 #include "tst_websockets.moc"
208