1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
* Copyright (C) 2010-2015 Jeremy Lainé
* Contact: https://github.com/jlaine/qdjango
*
* This file is part of the QDjango Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QtTest>
#include <QUrl>
#include "QDjangoHttpController.h"
#include "QDjangoHttpRequest.h"
#include "QDjangoHttpResponse.h"
#include "QDjangoHttpServer.h"
#include "QDjangoUrlResolver.h"
/** Test QDjangoHttpServer class.
*/
class tst_QDjangoHttpServer : public QObject
{
Q_OBJECT
private slots:
void cleanupTestCase();
void initTestCase();
void testCloseConnection();
void testGet_data();
void testGet();
void testPost_data();
void testPost();
QDjangoHttpResponse* _q_index(const QDjangoHttpRequest &request);
QDjangoHttpResponse* _q_error(const QDjangoHttpRequest &request);
private:
QDjangoHttpServer *httpServer;
};
void tst_QDjangoHttpServer::cleanupTestCase()
{
httpServer->close();
delete httpServer;
}
void tst_QDjangoHttpServer::initTestCase()
{
httpServer = new QDjangoHttpServer;
httpServer->urls()->set(QRegExp(QLatin1String("^$")), this, "_q_index");
httpServer->urls()->set(QRegExp(QLatin1String("^internal-server-error$")), this, "_q_error");
QCOMPARE(httpServer->serverAddress(), QHostAddress(QHostAddress::Null));
QCOMPARE(httpServer->serverPort(), quint16(0));
QCOMPARE(httpServer->listen(QHostAddress::LocalHost, 8123), true);
QCOMPARE(httpServer->serverAddress(), QHostAddress(QHostAddress::LocalHost));
QCOMPARE(httpServer->serverPort(), quint16(8123));
}
void tst_QDjangoHttpServer::testCloseConnection()
{
QNetworkAccessManager network;
QNetworkRequest req(QUrl("http://127.0.0.1:8123/"));
req.setRawHeader("Connection", "close");
QNetworkReply *reply = network.get(req);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QVERIFY(reply);
QCOMPARE(reply->error(), QNetworkReply::NoError);
//QCOMPARE(reply->readAll(), body);
delete reply;
}
void tst_QDjangoHttpServer::testGet_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<int>("err");
QTest::addColumn<QByteArray>("body");
const QString errorTemplate = QLatin1String(
"<html>"
"<head><title>Error</title></head>"
"<body><p>%1</p></body>"
"</html>");
#if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
int internalServerError = int(QNetworkReply::InternalServerError);
#else
int internalServerError = int(QNetworkReply::UnknownContentError);
#endif
QTest::newRow("root") << "/" << int(QNetworkReply::NoError) << QByteArray("method=GET|path=/");
QTest::newRow("query-string") << "/?message=bar" << int(QNetworkReply::NoError) << QByteArray("method=GET|path=/|get=bar");
QTest::newRow("not-found") << "/not-found" << int(QNetworkReply::ContentNotFoundError) << errorTemplate.arg(QLatin1String("The document you requested was not found.")).toUtf8();
QTest::newRow("internal-server-error") << "/internal-server-error" << internalServerError << errorTemplate.arg(QLatin1String("An internal server error was encountered.")).toUtf8();
}
void tst_QDjangoHttpServer::testGet()
{
QFETCH(QString, path);
QFETCH(int, err);
QFETCH(QByteArray, body);
QNetworkAccessManager network;
QNetworkReply *reply = network.get(QNetworkRequest(QUrl(QLatin1String("http://127.0.0.1:8123") + path)));
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QVERIFY(reply);
QCOMPARE(int(reply->error()), err);
QCOMPARE(reply->readAll(), body);
delete reply;
}
void tst_QDjangoHttpServer::testPost_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<QByteArray>("data");
QTest::addColumn<int>("err");
QTest::addColumn<QByteArray>("body");
QTest::newRow("empty") << "/" << QByteArray() << int(QNetworkReply::NoError) << QByteArray("method=POST|path=/");
QTest::newRow("simple") << "/" << QByteArray("message=bar") << int(QNetworkReply::NoError) << QByteArray("method=POST|path=/|post=bar");
QTest::newRow("multi") << "/" << QByteArray("bob=wiz&message=bar&zoo=wow") << int(QNetworkReply::NoError) << QByteArray("method=POST|path=/|post=bar");
}
void tst_QDjangoHttpServer::testPost()
{
QFETCH(QString, path);
QFETCH(QByteArray, data);
QFETCH(int, err);
QFETCH(QByteArray, body);
QNetworkAccessManager network;
QNetworkRequest req(QUrl(QLatin1String("http://127.0.0.1:8123") + path));
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
QNetworkReply *reply = network.post(req, data);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QVERIFY(reply);
QCOMPARE(int(reply->error()), err);
QCOMPARE(reply->readAll(), body);
delete reply;
}
QDjangoHttpResponse *tst_QDjangoHttpServer::_q_index(const QDjangoHttpRequest &request)
{
QDjangoHttpResponse *response = new QDjangoHttpResponse;
response->setHeader(QLatin1String("Content-Type"), QLatin1String("text/plain"));
QString output = QLatin1String("method=") + request.method();
output += QLatin1String("|path=") + request.path();
const QString getValue = request.get(QLatin1String("message"));
if (!getValue.isEmpty())
output += QLatin1String("|get=") + getValue;
const QString postValue = request.post(QLatin1String("message"));
if (!postValue.isEmpty())
output += QLatin1String("|post=") + postValue;
response->setBody(output.toUtf8());
return response;
}
QDjangoHttpResponse *tst_QDjangoHttpServer::_q_error(const QDjangoHttpRequest &request)
{
Q_UNUSED(request);
return QDjangoHttpController::serveInternalServerError(request);
}
QTEST_MAIN(tst_QDjangoHttpServer)
#include "tst_qdjangohttpserver.moc"
|