Move the QByteArray-based percent-encoding activities to QByteArray

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 <joao.abecasis@nokia.com>
Reviewed-by: David Faure <faure@kde.org>
This commit is contained in:
Thiago Macieira 2011-09-02 20:09:35 +02:00 committed by Qt by Nokia
parent d78fe5f8d3
commit 8fa2a41bd5
2 changed files with 99 additions and 9 deletions

View File

@ -5193,7 +5193,7 @@ QList<QPair<QByteArray, QByteArray> > 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<QByteArray> 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);
}
/*!

View File

@ -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<QByteArray>("encodedString");
QTest::addColumn<QByteArray>("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<QByteArray>("decodedString");
QTest::addColumn<QByteArray>("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<QByteArray>("original");
QTest::addColumn<QByteArray>("encoded");
QTest::addColumn<QByteArray>("excludeInEncoding");
QTest::addColumn<QByteArray>("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<QByteArray>("str1");