diff options
author | Tasuku Suzuki <tasuku.suzuki@qbc.io> | 2019-04-13 02:57:22 +0900 |
---|---|---|
committer | Tasuku Suzuki <tasuku.suzuki@qbc.io> | 2019-04-26 16:41:53 +0000 |
commit | 4f64330b39bd528a84241e976baa464c6dc89de1 (patch) | |
tree | 5658b33f0912d4611e6d68b8dc3173479ae6b9da | |
parent | 5147076e5ede09fafa96e147ab41dbb5744b8655 (diff) |
Find proper mime type for QByteArray data
QByteArray can be used for any data that QMimeDatabase understands
Change-Id: I17d8f0060065c0a93fc4a8cf6450bdd11aed49d0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
-rw-r--r-- | src/httpserver/qhttpserverresponse.cpp | 7 | ||||
-rw-r--r-- | tests/auto/auto.pro | 3 | ||||
-rw-r--r-- | tests/auto/qhttpserver/tst_qhttpserver.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponder/index.html | 2 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/empty | 0 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/image.jpeg | bin | 0 -> 5514 bytes | |||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/image.png | bin | 0 -> 1032 bytes | |||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/image.svg | 67 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/text.html | 1 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/data/text.plain | 1 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/qhttpserverresponse.pro | 7 | ||||
-rw-r--r-- | tests/auto/qhttpserverresponse/tst_qhttpserverresponse.cpp | 93 |
12 files changed, 178 insertions, 7 deletions
diff --git a/src/httpserver/qhttpserverresponse.cpp b/src/httpserver/qhttpserverresponse.cpp index cf02795..870be8d 100644 --- a/src/httpserver/qhttpserverresponse.cpp +++ b/src/httpserver/qhttpserverresponse.cpp @@ -33,6 +33,7 @@ #include <QtCore/qjsondocument.h> #include <QtCore/qjsonobject.h> +#include <QtCore/qmimedatabase.h> QT_BEGIN_NAMESPACE @@ -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) { } diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index d95dca9..473a337 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -5,4 +5,5 @@ SUBDIRS = \ qabstracthttpserver \ qhttpserver \ qhttpserverresponder \ - qhttpserverrouter + qhttpserverrouter \ + qhttpserverresponse diff --git a/tests/auto/qhttpserver/tst_qhttpserver.cpp b/tests/auto/qhttpserver/tst_qhttpserver.cpp index 9fb33d1..f8c4aa3 100644 --- a/tests/auto/qhttpserver/tst_qhttpserver.cpp +++ b/tests/auto/qhttpserver/tst_qhttpserver.cpp @@ -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"; @@ -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; } diff --git a/tests/auto/qhttpserverresponder/index.html b/tests/auto/qhttpserverresponder/index.html index 6c70bcf..18ecdcb 100644 --- a/tests/auto/qhttpserverresponder/index.html +++ b/tests/auto/qhttpserverresponder/index.html @@ -1 +1 @@ -<html></html>
\ No newline at end of file +<html></html> diff --git a/tests/auto/qhttpserverresponse/data/empty b/tests/auto/qhttpserverresponse/data/empty new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/empty diff --git a/tests/auto/qhttpserverresponse/data/image.jpeg b/tests/auto/qhttpserverresponse/data/image.jpeg Binary files differnew file mode 100644 index 0000000..a5ee3e1 --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/image.jpeg diff --git a/tests/auto/qhttpserverresponse/data/image.png b/tests/auto/qhttpserverresponse/data/image.png Binary files differnew file mode 100644 index 0000000..39a4a26 --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/image.png diff --git a/tests/auto/qhttpserverresponse/data/image.svg b/tests/auto/qhttpserverresponse/data/image.svg new file mode 100644 index 0000000..cb8989b --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/image.svg @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="462pt" + height="339pt" + viewBox="0 0 462 339" + version="1.1" + id="svg2" + inkscape:version="0.91 r13725" + sodipodi:docname="TheQtCompany_logo_2.svg"> + <metadata + id="metadata20"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <defs + id="defs18" /> + <sodipodi:namedview + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1" + objecttolerance="10" + gridtolerance="10" + guidetolerance="10" + inkscape:pageopacity="0" + inkscape:pageshadow="2" + inkscape:window-width="1536" + inkscape:window-height="801" + id="namedview16" + showgrid="false" + inkscape:zoom="1.1138643" + inkscape:cx="270.58047" + inkscape:cy="174.65092" + inkscape:window-x="-8" + inkscape:window-y="-8" + inkscape:window-maximized="1" + inkscape:current-layer="svg2" /> + <path + fill="#41cd52" + d=" M 63.50 0.00 L 462.00 0.00 L 462.00 274.79 C 440.60 296.26 419.13 317.66 397.61 339.00 L 0.00 339.00 L 0.00 63.39 C 21.08 42.18 42.34 21.13 63.50 0.00 Z" + id="path6" /> + <path + d=" M 122.37 71.33 C 137.50 61.32 156.21 58.79 174.00 58.95 C 190.94 59.16 208.72 62.13 222.76 72.24 C 232.96 79.41 239.59 90.48 244.01 101.93 C 251.16 120.73 253.26 141.03 253.50 161.01 C 253.53 181.13 252.62 201.69 245.96 220.86 C 241.50 233.90 233.01 245.48 221.81 253.52 C 229.87 266.58 238.09 279.54 246.15 292.60 C 236.02 297.27 225.92 301.97 215.78 306.62 C 207.15 292.38 198.56 278.11 189.90 263.89 C 178.19 265.81 166.21 265.66 154.44 264.36 C 140.34 262.67 125.97 258.37 115.09 248.88 C 106.73 241.64 101.48 231.51 97.89 221.21 C 92.01 203.79 90.43 185.25 90.16 166.97 C 90.02 147.21 91.28 127.14 97.24 108.18 C 101.85 93.92 109.48 79.69 122.37 71.33 Z" + id="path8" + fill="#ffffff" /> + <path + d=" M 294.13 70.69 C 304.73 70.68 315.33 70.68 325.93 70.69 C 325.96 84.71 325.92 98.72 325.95 112.74 C 339.50 112.76 353.05 112.74 366.60 112.75 C 366.37 121.85 366.12 130.95 365.86 140.05 C 352.32 140.08 338.79 140.04 325.25 140.07 C 325.28 163.05 325.18 186.03 325.30 209.01 C 325.56 215.30 325.42 221.94 328.19 227.75 C 330.21 232.23 335.65 233.38 340.08 233.53 C 348.43 233.50 356.77 233.01 365.12 232.86 C 365.63 241.22 366.12 249.59 366.60 257.95 C 349.99 260.74 332.56 264.08 316.06 258.86 C 309.11 256.80 302.63 252.19 299.81 245.32 C 294.76 233.63 294.35 220.62 294.13 208.07 C 294.11 185.40 294.13 162.74 294.12 140.07 C 286.73 140.05 279.34 140.08 271.95 140.05 C 271.93 130.96 271.93 121.86 271.95 112.76 C 279.34 112.73 286.72 112.77 294.11 112.74 C 294.14 98.72 294.10 84.71 294.13 70.69 Z" + id="path10" + fill="#ffffff" /> + <path + fill="#41cd52" + d=" M 160.51 87.70 C 170.80 86.36 181.60 86.72 191.34 90.61 C 199.23 93.73 205.93 99.84 209.47 107.58 C 214.90 119.31 216.98 132.26 218.03 145.05 C 219.17 162.07 219.01 179.25 216.66 196.17 C 215.01 206.24 212.66 216.85 205.84 224.79 C 198.92 232.76 188.25 236.18 178.01 236.98 C 167.21 237.77 155.82 236.98 146.07 231.87 C 140.38 228.84 135.55 224.09 132.73 218.27 C 129.31 211.30 127.43 203.69 126.11 196.07 C 122.13 171.91 121.17 146.91 126.61 122.89 C 128.85 113.83 132.11 104.53 138.73 97.70 C 144.49 91.85 152.51 88.83 160.51 87.70 Z" + id="path12" /> +</svg> diff --git a/tests/auto/qhttpserverresponse/data/text.html b/tests/auto/qhttpserverresponse/data/text.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/text.html @@ -0,0 +1 @@ +<html></html> diff --git a/tests/auto/qhttpserverresponse/data/text.plain b/tests/auto/qhttpserverresponse/data/text.plain new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/tests/auto/qhttpserverresponse/data/text.plain @@ -0,0 +1 @@ +Hello World! diff --git a/tests/auto/qhttpserverresponse/qhttpserverresponse.pro b/tests/auto/qhttpserverresponse/qhttpserverresponse.pro new file mode 100644 index 0000000..dc0b359 --- /dev/null +++ b/tests/auto/qhttpserverresponse/qhttpserverresponse.pro @@ -0,0 +1,7 @@ +CONFIG += testcase +TARGET = tst_qhttpserverresponse +SOURCES += tst_qhttpserverresponse.cpp + +QT = httpserver testlib + +TESTDATA += data/ diff --git a/tests/auto/qhttpserverresponse/tst_qhttpserverresponse.cpp b/tests/auto/qhttpserverresponse/tst_qhttpserverresponse.cpp new file mode 100644 index 0000000..e531c48 --- /dev/null +++ b/tests/auto/qhttpserverresponse/tst_qhttpserverresponse.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Tasuku Suzuki <tasuku.suzuki@qbc.io> +** 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" |