Use QStringLiteral where appropriate
[contrib/qtwebsockets.git] / tests / manual / websockets / tst_websockets.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
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->write(message), qint64(message.size()));
138
139     QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
140     QCOMPARE(spy.count(), 1);
141     QCOMPARE(spy.at(0).count(), 1);
142     QCOMPARE(spy.takeFirst().at(0).toString(), message);
143 }
144
145 void tst_WebSocketsTest::testBinaryMessage()
146 {
147     QSignalSpy spy(m_pWebSocket, SIGNAL(binaryMessageReceived(QByteArray)));
148
149     QByteArray data("Hello world!");
150
151     QCOMPARE(m_pWebSocket->write(data), qint64(data.size()));
152
153     QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
154     QCOMPARE(spy.count(), 1);
155     QCOMPARE(spy.at(0).count(), 1);
156     QCOMPARE(spy.takeFirst().at(0).toByteArray(), data);
157 }
158
159 void tst_WebSocketsTest::testLocalAddress()
160 {
161     QCOMPARE(m_pWebSocket->localAddress().toString(), QStringLiteral("127.0.0.1"));
162     quint16 localPort = m_pWebSocket->localPort();
163     QVERIFY2(localPort > 0, "Local port is invalid.");
164 }
165
166 void tst_WebSocketsTest::testPeerAddress()
167 {
168     QHostInfo hostInfo = QHostInfo::fromName(m_url.host());
169     QList<QHostAddress> addresses = hostInfo.addresses();
170     QVERIFY(addresses.length() > 0);
171     QHostAddress peer = m_pWebSocket->peerAddress();
172     bool found = false;
173     Q_FOREACH(QHostAddress a, addresses)
174     {
175         if (a == peer)
176         {
177             found = true;
178             break;
179         }
180     }
181
182     if (!found)
183     {
184         QFAIL("PeerAddress is not found as a result of a reverse lookup");
185     }
186     QCOMPARE(m_pWebSocket->peerName(), m_url.host());
187     QCOMPARE(m_pWebSocket->peerPort(), (quint16)m_url.port(80));
188 }
189
190 void tst_WebSocketsTest::testProxy()
191 {
192     QNetworkProxy oldProxy = m_pWebSocket->proxy();
193     QNetworkProxy proxy(QNetworkProxy::HttpProxy, QStringLiteral("proxy.network.com"), 80);
194     m_pWebSocket->setProxy(proxy);
195     QCOMPARE(proxy, m_pWebSocket->proxy());
196     m_pWebSocket->setProxy(oldProxy);
197     QCOMPARE(oldProxy, m_pWebSocket->proxy());
198 }
199
200 void tst_WebSocketsTest::testInvalidWithUnopenedSocket()
201 {
202     QWebSocket qws;
203     QCOMPARE(qws.isValid(), false);
204 }
205
206 QTEST_MAIN(tst_WebSocketsTest)
207
208 #include "tst_websockets.moc"
209