From 8fa2a41bd5fb0e21c3cbda3d76eba77a922bded2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 2 Sep 2011 20:09:35 +0200 Subject: [PATCH] Move the QByteArray-based percent-encoding activities to QByteArray MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copy the unit tests that related to percent-encoding to tst_qbytearray.cpp and use public functions to execute QUrl::fromPercentEncoded and QUrl::toPercentEncoded. Change-Id: I6639ea566d82dabeb91280177a854e89e18f6f8d Reviewed-by: João Abecasis Reviewed-by: David Faure --- src/corelib/io/qurl.cpp | 17 ++-- .../tools/qbytearray/tst_qbytearray.cpp | 91 +++++++++++++++++++ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 1629f5745d..4b9e4d6782 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -5193,7 +5193,7 @@ QList > QUrl::encodedQueryItems() const bool QUrl::hasQueryItem(const QString &key) const { if (!d) return false; - return hasEncodedQueryItem(toPercentEncoding(key, queryExcludeChars)); + return hasEncodedQueryItem(key.toUtf8().toPercentEncoding(queryExcludeChars)); } /*! @@ -5239,7 +5239,7 @@ bool QUrl::hasEncodedQueryItem(const QByteArray &key) const QString QUrl::queryItemValue(const QString &key) const { if (!d) return QString(); - QByteArray tmp = encodedQueryItemValue(toPercentEncoding(key, queryExcludeChars)); + QByteArray tmp = encodedQueryItemValue(key.toUtf8().toPercentEncoding(queryExcludeChars)); return fromPercentEncodingMutable(&tmp); } @@ -5289,7 +5289,7 @@ QStringList QUrl::allQueryItemValues(const QString &key) const if (!d) return QStringList(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); - QByteArray encodedKey = toPercentEncoding(key, queryExcludeChars); + QByteArray encodedKey = key.toUtf8().toPercentEncoding(queryExcludeChars); QStringList values; int pos = 0; @@ -5353,7 +5353,7 @@ QList QUrl::allEncodedQueryItemValues(const QByteArray &key) const void QUrl::removeQueryItem(const QString &key) { if (!d) return; - removeEncodedQueryItem(toPercentEncoding(key, queryExcludeChars)); + removeEncodedQueryItem(key.toUtf8().toPercentEncoding(queryExcludeChars)); } /*! @@ -5399,7 +5399,7 @@ void QUrl::removeEncodedQueryItem(const QByteArray &key) void QUrl::removeAllQueryItems(const QString &key) { if (!d) return; - removeAllEncodedQueryItems(toPercentEncoding(key, queryExcludeChars)); + removeAllEncodedQueryItems(key.toUtf8().toPercentEncoding(queryExcludeChars)); } /*! @@ -5726,8 +5726,7 @@ QString QUrl::toString(FormattingOptions options) const if (!(options & QUrl::RemoveQuery) && d->hasQuery) { url += QLatin1Char('?'); - // query is already encoded, but possibly more than necessary. - url += toPrettyPercentEncoding(fromPercentEncoding(d->query), true); + url += QString::fromUtf8(QByteArray::fromPercentEncoding(d->query)); } if (!(options & QUrl::RemoveFragment) && d->hasFragment) { url += QLatin1Char('#'); @@ -5808,7 +5807,7 @@ QByteArray QUrl::toEncoded(FormattingOptions options) const */ QString QUrl::fromPercentEncoding(const QByteArray &input) { - return fromPercentEncodingHelper(input); + return QString::fromUtf8(QByteArray::fromPercentEncoding(input)); } /*! @@ -5825,7 +5824,7 @@ QString QUrl::fromPercentEncoding(const QByteArray &input) */ QByteArray QUrl::toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include) { - return toPercentEncodingHelper(input, exclude.constData(), include.constData()); + return input.toUtf8().toPercentEncoding(exclude, include); } /*! diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 63900b0c55..2c8aa4d62a 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -123,6 +123,12 @@ private slots: void toFromHex_data(); void toFromHex(); void toFromPercentEncoding(); + void fromPercentEncoding_data(); + void fromPercentEncoding(); + void toPercentEncoding_data(); + void toPercentEncoding(); + void toPercentEncoding2_data(); + void toPercentEncoding2(); void compare_data(); void compare(); @@ -1340,6 +1346,91 @@ void tst_QByteArray::toFromPercentEncoding() QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isNull()); } +void tst_QByteArray::fromPercentEncoding_data() +{ + QTest::addColumn("encodedString"); + QTest::addColumn("decodedString"); + + QTest::newRow("NormalString") << QByteArray("filename") << QByteArray("filename"); + QTest::newRow("NormalStringEncoded") << QByteArray("file%20name") << QByteArray("file name"); + QTest::newRow("JustEncoded") << QByteArray("%20") << QByteArray(" "); + QTest::newRow("HTTPUrl") << QByteArray("http://qt.nokia.com") << QByteArray("http://qt.nokia.com"); + QTest::newRow("HTTPUrlEncoded") << QByteArray("http://qt%20nokia%20com") << QByteArray("http://qt nokia com"); + QTest::newRow("EmptyString") << QByteArray("") << QByteArray(""); + QTest::newRow("Task27166") << QByteArray("Fran%C3%A7aise") << QByteArray("Française"); +} + +void tst_QByteArray::fromPercentEncoding() +{ + QFETCH(QByteArray, encodedString); + QFETCH(QByteArray, decodedString); + + QCOMPARE(QByteArray::fromPercentEncoding(encodedString), decodedString); +} + +void tst_QByteArray::toPercentEncoding_data() +{ + QTest::addColumn("decodedString"); + QTest::addColumn("encodedString"); + + QTest::newRow("NormalString") << QByteArray("filename") << QByteArray("filename"); + QTest::newRow("NormalStringEncoded") << QByteArray("file name") << QByteArray("file%20name"); + QTest::newRow("JustEncoded") << QByteArray(" ") << QByteArray("%20"); + QTest::newRow("HTTPUrl") << QByteArray("http://qt.nokia.com") << QByteArray("http%3A//qt.nokia.com"); + QTest::newRow("HTTPUrlEncoded") << QByteArray("http://qt nokia com") << QByteArray("http%3A//qt%20nokia%20com"); + QTest::newRow("EmptyString") << QByteArray("") << QByteArray(""); + QTest::newRow("Task27166") << QByteArray("Française") << QByteArray("Fran%C3%A7aise"); +} + +void tst_QByteArray::toPercentEncoding() +{ + QFETCH(QByteArray, decodedString); + QFETCH(QByteArray, encodedString); + + QCOMPARE(decodedString.toPercentEncoding("/.").constData(), encodedString.constData()); +} + +void tst_QByteArray::toPercentEncoding2_data() +{ + QTest::addColumn("original"); + QTest::addColumn("encoded"); + QTest::addColumn("excludeInEncoding"); + QTest::addColumn("includeInEncoding"); + + QTest::newRow("test_01") << QByteArray("abcdevghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678-._~") + << QByteArray("abcdevghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678-._~") + << QByteArray("") + << QByteArray(""); + QTest::newRow("test_02") << QByteArray("{\t\n\r^\"abc}") + << QByteArray("%7B%09%0A%0D%5E%22abc%7D") + << QByteArray("") + << QByteArray(""); + QTest::newRow("test_03") << QByteArray("://?#[]@!$&'()*+,;=") + << QByteArray("%3A%2F%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D") + << QByteArray("") + << QByteArray(""); + QTest::newRow("test_04") << QByteArray("://?#[]@!$&'()*+,;=") + << QByteArray("%3A%2F%2F%3F%23%5B%5D%40!$&'()*+,;=") + << QByteArray("!$&'()*+,;=") + << QByteArray(""); + QTest::newRow("test_05") << QByteArray("abcd") + << QByteArray("a%62%63d") + << QByteArray("") + << QByteArray("bc"); +} + +void tst_QByteArray::toPercentEncoding2() +{ + QFETCH(QByteArray, original); + QFETCH(QByteArray, encoded); + QFETCH(QByteArray, excludeInEncoding); + QFETCH(QByteArray, includeInEncoding); + + QByteArray encodedData = original.toPercentEncoding(excludeInEncoding, includeInEncoding); + QCOMPARE(encodedData.constData(), encoded.constData()); + QCOMPARE(original, QByteArray::fromPercentEncoding(encodedData)); +} + void tst_QByteArray::compare_data() { QTest::addColumn("str1");