Merge pull request #10 from Kuraisu/range-support

This commit is contained in:
Nathan Osman
2016-10-10 13:21:14 -07:00
15 changed files with 1078 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ set(TESTS
TestQFilesystemHandler
TestQHttpHandler
TestQHttpParser
TestQHttpRange
TestQHttpServer
TestQHttpSocket
TestQIByteArray

View File

@@ -43,6 +43,9 @@ private Q_SLOTS:
void initTestCase();
void testRangeRequests_data();
void testRangeRequests();
void testRequests_data();
void testRequests();
@@ -111,6 +114,85 @@ void TestQFilesystemHandler::testRequests()
}
}
void TestQFilesystemHandler::testRangeRequests_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<QString>("range");
QTest::addColumn<int>("statusCode");
QTest::addColumn<QString>("contentRange");
QTest::addColumn<QByteArray>("data");
QTest::newRow("full file")
<< "inside" << ""
<< static_cast<int>(QHttpSocket::OK)
<< ""
<< Data;
QTest::newRow("range 0-2")
<< "inside" << "0-2"
<< static_cast<int>(QHttpSocket::PartialContent)
<< "bytes 0-2/4"
<< Data.mid(0, 3);
QTest::newRow("range 1-2")
<< "inside" << "1-2"
<< static_cast<int>(QHttpSocket::PartialContent)
<< "bytes 1-2/4"
<< Data.mid(1, 2);
QTest::newRow("skip first 1 byte")
<< "inside" << "1-"
<< static_cast<int>(QHttpSocket::PartialContent)
<< "bytes 1-3/4"
<< Data.mid(1);
QTest::newRow("last 2 bytes")
<< "inside" << "-2"
<< static_cast<int>(QHttpSocket::PartialContent)
<< "bytes 2-3/4"
<< Data.mid(2);
QTest::newRow("bad range request")
<< "inside" << "abcd"
<< static_cast<int>(QHttpSocket::OK)
<< ""
<< Data;
}
void TestQFilesystemHandler::testRangeRequests()
{
QFETCH(QString, path);
QFETCH(QString, range);
QFETCH(int, statusCode);
QFETCH(QString, contentRange);
QFETCH(QByteArray, data);
QFilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root"));
QSocketPair pair;
QTRY_VERIFY(pair.isConnected());
QSimpleHttpClient client(pair.client());
QHttpSocket socket(pair.server(), &pair);
if (!range.isEmpty()) {
QHttpSocket::QHttpHeaderMap inHeaders;
inHeaders.insert("Range", QByteArray("bytes=") + range.toUtf8());
client.sendHeaders("GET", path.toUtf8(), inHeaders);
QTRY_VERIFY(socket.isHeadersParsed());
}
handler.route(&socket, path);
QTRY_COMPARE(client.statusCode(), statusCode);
if (!data.isNull()) {
QTRY_COMPARE(client.data(), data);
QCOMPARE(client.headers().value("Content-Length").toInt(), data.length());
QCOMPARE(client.headers().value("Content-Range"), contentRange.toLatin1());
}
}
bool TestQFilesystemHandler::createFile(const QString &path)
{
QFile file(QDir(dir.path()).absoluteFilePath(path));

291
tests/TestQHttpRange.cpp Normal file
View File

@@ -0,0 +1,291 @@
/*
* Copyright (c) 2016 Aleksei Ermakov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QObject>
#include <QString>
#include <QTest>
#include <QHttpEngine/QHttpRange>
class TestQHttpRange : public QObject
{
Q_OBJECT
public:
TestQHttpRange();
private Q_SLOTS:
void testDefaultConstructor();
void testAssignmentOperator();
void testFromToLength_data();
void testFromToLength();
void testIsValid_data();
void testIsValid();
void testParseFromString_data();
void testParseFromString();
void testContentRange_data();
void testContentRange();
};
TestQHttpRange::TestQHttpRange()
{
}
void TestQHttpRange::testDefaultConstructor()
{
QHttpRange range;
QCOMPARE(range.isValid(), false);
}
void TestQHttpRange::testAssignmentOperator()
{
QHttpRange range;
QHttpRange otherRange(100, 200, -1);
range = otherRange;
QCOMPARE(range.isValid(), true);
QCOMPARE(range.from(), 100);
QCOMPARE(range.to(), 200);
}
void TestQHttpRange::testFromToLength_data()
{
QTest::addColumn<int>("inFrom");
QTest::addColumn<int>("inTo");
QTest::addColumn<int>("inDataSize");
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("length");
QTest::newRow("Last 500 bytes")
<< -500 << -1 << -1
<< -500 << -1 << 500;
QTest::newRow("Last 500 bytes with 800 dataSize")
<< -500 << -1 << 800
<< 300 << 799 << 500;
QTest::newRow("Skip first 10 bytes")
<< 10 << -1 << -1
<< 10 << -1 << -1;
QTest::newRow("Skip first 10 bytes with 100 dataSize")
<< 10 << -1 << 100
<< 10 << 99 << 90;
}
void TestQHttpRange::testFromToLength()
{
QFETCH(int, inFrom);
QFETCH(int, inTo);
QFETCH(int, inDataSize);
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, length);
QHttpRange range(inFrom, inTo, inDataSize);
QCOMPARE(range.from(), from);
QCOMPARE(range.to(), to);
QCOMPARE(range.length(), length);
}
void TestQHttpRange::testIsValid_data()
{
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("dataSize");
QTest::addColumn<bool>("valid");
QTest::newRow("Normal range")
<< 0 << 100 << -1 << true;
QTest::newRow("Normal range with 'dataSize'")
<< 0 << 99 << 100 << true;
QTest::newRow("Last N bytes")
<< -500 << -1 << -1 << true;
QTest::newRow("Last N bytes with 'dataSize'")
<< -500 << -1 << 500 << true;
QTest::newRow("Skip first N bytes")
<< 10 << -1 << -1 << true;
QTest::newRow("Skip first N bytes with 'dataSize'")
<< 10 << -1 << 500 << true;
QTest::newRow("OutOfBounds 'to' > 'from'")
<< 100 << 50 << -1 << false;
QTest::newRow("OutOfBounds 'from' > 'dataSize'")
<< 100 << 200 << 150 << false;
QTest::newRow("Last N bytes where N > 'dataSize'")
<< -500 << -1 << 499 << false;
QTest::newRow("Skip first N bytes where N > 'dataSize'")
<< 500 << -1 << 499 << false;
}
void TestQHttpRange::testIsValid()
{
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, dataSize);
QFETCH(bool, valid);
QHttpRange range(from, to, dataSize);
QCOMPARE(range.isValid(), valid);
}
void TestQHttpRange::testParseFromString_data()
{
QTest::addColumn<QString>("data");
QTest::addColumn<int>("dataSize");
QTest::addColumn<bool>("valid");
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("length");
QTest::newRow("Normal range")
<< "0-99" << -1
<< true << 0 << 99 << 100;
QTest::newRow("Normal range with 'dataSize'")
<< "0-99" << 100
<< true << 0 << 99 << 100;
QTest::newRow("Last N bytes")
<< "-256" << -1
<< true << -256 << -1 << 256;
QTest::newRow("Last N bytes with 'dataSize'")
<< "-256" << 256
<< true << 0 << 255 << 256;
QTest::newRow("Skip first N bytes")
<< "100-" << -1
<< true << 100 << -1 << -1;
QTest::newRow("Skip first N bytes with 'dataSize'")
<< "100-" << 200
<< true << 100 << 199 << 100;
QTest::newRow("OutOfBounds 'to' > 'from'")
<< "100-50" << -1
<< false;
QTest::newRow("OutOfBounds 'from' > 'dataSize'")
<< "0-200" << 100
<< false;
QTest::newRow("Last N bytes where N > 'dataSize'")
<< "-500" << 200
<< false;
QTest::newRow("Skip first N bytes where N > 'dataSize'")
<< "100-" << 100
<< false;
QTest::newRow("Bad input: '-'")
<< "-" << -1
<< false;
QTest::newRow("Bad input: 'abc-def'")
<< "abc-def" << -1
<< false;
QTest::newRow("Bad input: 'abcdef'")
<< "abcdef" << -1
<< false;
}
void TestQHttpRange::testParseFromString()
{
QFETCH(QString, data);
QFETCH(int, dataSize);
QFETCH(bool, valid);
QHttpRange range(data, dataSize);
QCOMPARE(range.isValid(), valid);
if(valid) {
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, length);
QCOMPARE(range.from(), from);
QCOMPARE(range.to(), to);
QCOMPARE(range.length(), length);
}
}
void TestQHttpRange::testContentRange_data()
{
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("dataSize");
QTest::addColumn<QString>("contentRange");
QTest::newRow("Normal range with 'dataSize'")
<< 0 << 100 << 1000
<< "0-100/1000";
QTest::newRow("Normal range without 'dataSize'")
<< 0 << 100 << -1
<< "0-100/*";
QTest::newRow("Invalid range with 'dataSize'")
<< 100 << 10 << 1200
<< "*/1200";
QTest::newRow("Invalid range without 'dataSize'")
<< 100 << 10 << -1
<< "";
}
void TestQHttpRange::testContentRange()
{
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, dataSize);
QFETCH(QString, contentRange);
QHttpRange range(from, to, dataSize);
QCOMPARE(range.contentRange(), contentRange);
}
QTEST_MAIN(TestQHttpRange)
#include "TestQHttpRange.moc"

View File

@@ -31,7 +31,7 @@
#include "common/qsocketpair.h"
const QByteArray SampleData = "1234567890";
const QByteArray SampleData = "1234567890123456789012345678901234567890";
class TestQIODeviceCopier : public QObject
{
@@ -39,6 +39,9 @@ class TestQIODeviceCopier : public QObject
private Q_SLOTS:
void testRange_data();
void testRange();
void testQBuffer();
void testQTcpSocket();
void testStop();
@@ -110,5 +113,47 @@ void TestQIODeviceCopier::testStop()
QTRY_COMPARE(destData, SampleData);
}
void TestQIODeviceCopier::testRange_data()
{
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("bufferSize");
QTest::newRow("range: 1-21, bufSize: 8")
<< 1 << 21 << 8;
QTest::newRow("range: 0-21, bufSize: 7")
<< 0 << 21 << 7;
QTest::newRow("range: 10-, bufSize: 5")
<< 10 << -1 << 5;
}
void TestQIODeviceCopier::testRange()
{
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, bufferSize);
QBuffer src;
src.setData(SampleData);
QByteArray destData;
QBuffer dest(&destData);
QIODeviceCopier copier(&src, &dest);
copier.setBufferSize(bufferSize);
copier.setRange(from, to);
QSignalSpy errorSpy(&copier, SIGNAL(error(QString)));
QSignalSpy finishedSpy(&copier, SIGNAL(finished()));
copier.start();
QTRY_COMPARE(finishedSpy.count(), 1);
QCOMPARE(errorSpy.count(), 0);
QCOMPARE(destData, SampleData.mid(from, to - from + 1));
}
QTEST_MAIN(TestQIODeviceCopier)
#include "TestQIODeviceCopier.moc"