Skip to content

Commit

Permalink
Find proper mime type for QByteArray data
Browse files Browse the repository at this point in the history
QByteArray can be used for any data that QMimeDatabase understands

Change-Id: I17d8f0060065c0a93fc4a8cf6450bdd11aed49d0
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Mårten Nordheim <[email protected]>
Reviewed-by: Mikhail Svetkin <[email protected]>
Reviewed-by: Jesus Fernandez <[email protected]>
  • Loading branch information
Tasuku Suzuki committed Apr 26, 2019
1 parent 5147076 commit 4f64330
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/httpserver/qhttpserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qmimedatabase.h>

QT_BEGIN_NAMESPACE

Expand All @@ -54,17 +55,17 @@ QHttpServerResponse::QHttpServerResponse(const QHttpServerResponse::StatusCode s
}

QHttpServerResponse::QHttpServerResponse(const char *data)
: QHttpServerResponse(mimeTextHtml, QByteArray(data))
: QHttpServerResponse(QByteArray(data))
{
}

QHttpServerResponse::QHttpServerResponse(const QString &data)
: QHttpServerResponse(mimeTextHtml, data.toUtf8())
: QHttpServerResponse(data.toUtf8())
{
}

QHttpServerResponse::QHttpServerResponse(const QByteArray &data)
: QHttpServerResponse(mimeTextHtml, data)
: QHttpServerResponse(QMimeDatabase().mimeTypeForData(data).name().toLocal8Bit(), data)
{
}

Expand Down
3 changes: 2 additions & 1 deletion tests/auto/auto.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ SUBDIRS = \
qabstracthttpserver \
qhttpserver \
qhttpserverresponder \
qhttpserverrouter
qhttpserverrouter \
qhttpserverresponse
4 changes: 2 additions & 2 deletions tests/auto/qhttpserver/tst_qhttpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void tst_QHttpServer::routePost_data()
QTest::addRow("post-body")
<< "/post-body"
<< 200
<< "text/html"
<< "text/plain"
<< "some post data"
<< "some post data";

Expand All @@ -463,7 +463,7 @@ void tst_QHttpServer::routePost_data()
QTest::addRow("post-body - huge body, chunk test")
<< "/post-body"
<< 200
<< "text/html"
<< "text/plain"
<< body
<< body;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/qhttpserverresponder/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<html></html>
<html></html>
Empty file.
Binary file added tests/auto/qhttpserverresponse/data/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/auto/qhttpserverresponse/data/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions tests/auto/qhttpserverresponse/data/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/auto/qhttpserverresponse/data/text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html></html>
1 change: 1 addition & 0 deletions tests/auto/qhttpserverresponse/data/text.plain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
7 changes: 7 additions & 0 deletions tests/auto/qhttpserverresponse/qhttpserverresponse.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CONFIG += testcase
TARGET = tst_qhttpserverresponse
SOURCES += tst_qhttpserverresponse.cpp

QT = httpserver testlib

TESTDATA += data/
93 changes: 93 additions & 0 deletions tests/auto/qhttpserverresponse/tst_qhttpserverresponse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/****************************************************************************
**
** Copyright (C) 2019 Tasuku Suzuki <[email protected]>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtHttpServer module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtHttpServer/qhttpserverresponse.h>

#include <QtCore/qfile.h>
#include <QtTest/qtest.h>

QT_BEGIN_NAMESPACE

class tst_QHttpServerResponse : public QObject
{
Q_OBJECT

private slots:
void mimeTypeDetection_data();
void mimeTypeDetection();
};

void tst_QHttpServerResponse::mimeTypeDetection_data()
{
QTest::addColumn<QString>("content");
QTest::addColumn<QByteArray>("mimeType");

QTest::addRow("application/x-zerosize")
<< QFINDTESTDATA("data/empty")
<< QByteArrayLiteral("application/x-zerosize");

QTest::addRow("text/plain")
<< QFINDTESTDATA("data/text.plain")
<< QByteArrayLiteral("text/plain");

QTest::addRow("text/html")
<< QFINDTESTDATA("data/text.html")
<< QByteArrayLiteral("text/html");

QTest::addRow("image/png")
<< QFINDTESTDATA("data/image.png")
<< QByteArrayLiteral("image/png");

QTest::addRow("image/jpeg")
<< QFINDTESTDATA("data/image.jpeg")
<< QByteArrayLiteral("image/jpeg");

QTest::addRow("image/svg+xml")
<< QFINDTESTDATA("data/image.svg")
<< QByteArrayLiteral("image/svg+xml");
}

void tst_QHttpServerResponse::mimeTypeDetection()
{
QFETCH(QString, content);
QFETCH(QByteArray, mimeType);

QFile file(content);
file.open(QFile::ReadOnly);
QHttpServerResponse response(file.readAll());
file.close();

QCOMPARE(response.mimeType(), mimeType);
}

QT_END_NAMESPACE

QTEST_MAIN(tst_QHttpServerResponse)

#include "tst_qhttpserverresponse.moc"

0 comments on commit 4f64330

Please sign in to comment.