| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | #include <QtTest/QtTest> |
| 29 | #include <QtTest/qtestcase.h> |
| 30 | #include <QtCore/QDebug> |
| 31 | #include <QtCore/QByteArray> |
| 32 | #include <QtCore/QRegularExpression> |
| 33 | #include <QtCore/QtEndian> |
| 34 | |
| 35 | #include "private/qwebsockethandshakerequest_p.h" |
| 36 | #include "private/qwebsockethandshakeresponse_p.h" |
| 37 | #include "private/qwebsocketprotocol_p.h" |
| 38 | #include "QtWebSockets/qwebsocketprotocol.h" |
| 39 | |
| 40 | QT_USE_NAMESPACE |
| 41 | |
| 42 | Q_DECLARE_METATYPE(QWebSocketProtocol::CloseCode) |
| 43 | Q_DECLARE_METATYPE(QWebSocketProtocol::OpCode) |
| 44 | |
| 45 | class tst_HandshakeResponse : public QObject |
| 46 | { |
| 47 | Q_OBJECT |
| 48 | |
| 49 | public: |
| 50 | tst_HandshakeResponse(); |
| 51 | |
| 52 | private Q_SLOTS: |
| 53 | void initTestCase(); |
| 54 | void cleanupTestCase(); |
| 55 | void init(); |
| 56 | void cleanup(); |
| 57 | |
| 58 | void tst_date_response(); |
| 59 | }; |
| 60 | |
| 61 | tst_HandshakeResponse::tst_HandshakeResponse() |
| 62 | {} |
| 63 | |
| 64 | void tst_HandshakeResponse::initTestCase() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void tst_HandshakeResponse::cleanupTestCase() |
| 69 | {} |
| 70 | |
| 71 | void tst_HandshakeResponse::init() |
| 72 | { |
| 73 | qRegisterMetaType<QWebSocketProtocol::OpCode>(typeName: "QWebSocketProtocol::OpCode" ); |
| 74 | qRegisterMetaType<QWebSocketProtocol::CloseCode>(typeName: "QWebSocketProtocol::CloseCode" ); |
| 75 | } |
| 76 | |
| 77 | void tst_HandshakeResponse::cleanup() |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void tst_HandshakeResponse::tst_date_response() |
| 82 | { |
| 83 | QWebSocketHandshakeRequest request(80, false); |
| 84 | QString buffer; |
| 85 | QTextStream input(&buffer); |
| 86 | input << QStringLiteral("GET / HTTP/1.1\r\nHost: example.com\r\nSec-WebSocket-Version: 13\r\n" ) + |
| 87 | QStringLiteral("Sec-WebSocket-Key: AVDFBDDFF\r\n" ) + |
| 88 | QStringLiteral("Upgrade: websocket\r\n" ) + |
| 89 | QStringLiteral("Connection: Upgrade\r\n\r\n" ); |
| 90 | request.readHandshake(textStream&: input, maxHeaderLineLength: 8 * 1024, maxHeaders: 100); |
| 91 | |
| 92 | QWebSocketHandshakeResponse response(request, "example.com" , true, |
| 93 | QList<QWebSocketProtocol::Version>() << QWebSocketProtocol::Version13, |
| 94 | QList<QString>(), |
| 95 | QList<QString>()); |
| 96 | QString data; |
| 97 | QTextStream output(&data); |
| 98 | output << response; |
| 99 | |
| 100 | QStringList list = data.split(sep: "\r\n" ); |
| 101 | int index = list.indexOf(rx: QRegularExpression("Date:.*" )); |
| 102 | QVERIFY(index > -1); |
| 103 | QVERIFY(QLocale::c().toDateTime(list[index], "'Date:' ddd, dd MMM yyyy hh:mm:ss 'GMT'" ).isValid()); |
| 104 | } |
| 105 | |
| 106 | QTEST_MAIN(tst_HandshakeResponse) |
| 107 | |
| 108 | #include "tst_handshakeresponse.moc" |
| 109 | |