Forward the methods dealing with the break down of query to QUrlQuery

Now that QUrlQuery exists, these methods are no longer necessary in
QUrl itself. Manipulation of the items should be done using the new
class.

They are now implemented using a temporary QUrlQuery. This is hardly
efficient but it works.

Change-Id: I34820b3101424593d0715841a2057ac3f74d74f0
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Thiago Macieira 2011-09-08 22:05:42 +02:00 committed by Qt by Nokia
parent 1aeb180386
commit 1c2144c39f
5 changed files with 157 additions and 633 deletions

View File

@ -4970,57 +4970,6 @@ bool QUrl::hasQuery() const
return d->hasQuery;
}
/*!
Sets the characters used for delimiting between keys and values,
and between key-value pairs in the URL's query string. The default
value delimiter is '=' and the default pair delimiter is '&'.
\img qurl-querystring.png
\a valueDelimiter will be used for separating keys from values,
and \a pairDelimiter will be used to separate key-value pairs.
Any occurrences of these delimiting characters in the encoded
representation of the keys and values of the query string are
percent encoded.
If \a valueDelimiter is set to '-' and \a pairDelimiter is '/',
the above query string would instead be represented like this:
\snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 4
Calling this function does not change the delimiters of the
current query string. It only affects queryItems(),
setQueryItems() and addQueryItems().
*/
void QUrl::setQueryDelimiters(char valueDelimiter, char pairDelimiter)
{
if (!d) d = new QUrlPrivate;
detach();
d->valueDelimiter = valueDelimiter;
d->pairDelimiter = pairDelimiter;
}
/*!
Returns the character used to delimit between key-value pairs in
the query string of the URL.
*/
char QUrl::queryPairDelimiter() const
{
if (!d) return '&';
return d->pairDelimiter;
}
/*!
Returns the character used to delimit between keys and values in
the query string of the URL.
*/
char QUrl::queryValueDelimiter() const
{
if (!d) return '=';
return d->valueDelimiter;
}
/*!
Sets the query string of the URL to \a query. The string is
inserted as-is, and no further encoding is performed when calling
@ -5049,462 +4998,6 @@ void QUrl::setEncodedQuery(const QByteArray &query)
d->hasQuery = !query.isNull();
}
/*!
Sets the query string of the URL to an encoded version of \a
query. The contents of \a query are converted to a string
internally, each pair delimited by the character returned by
pairDelimiter(), and the key and value are delimited by
valueDelimiter().
\note This method does not encode spaces (ASCII 0x20) as plus (+) signs,
like HTML forms do. If you need that kind of encoding, you must encode
the value yourself and use QUrl::setEncodedQueryItems.
\sa setQueryDelimiters(), queryItems(), setEncodedQueryItems()
*/
void QUrl::setQueryItems(const QList<QPair<QString, QString> > &query)
{
if (!d) d = new QUrlPrivate;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
char alsoEncode[3];
alsoEncode[0] = d->valueDelimiter;
alsoEncode[1] = d->pairDelimiter;
alsoEncode[2] = 0;
QByteArray queryTmp;
for (int i = 0; i < query.size(); i++) {
if (i) queryTmp += d->pairDelimiter;
// query = *( pchar / "/" / "?" )
queryTmp += toPercentEncodingHelper(query.at(i).first, queryExcludeChars, alsoEncode);
queryTmp += d->valueDelimiter;
// query = *( pchar / "/" / "?" )
queryTmp += toPercentEncodingHelper(query.at(i).second, queryExcludeChars, alsoEncode);
}
d->query = queryTmp;
d->hasQuery = !query.isEmpty();
}
/*!
\since 4.4
Sets the query string of the URL to the encoded version of \a
query. The contents of \a query are converted to a string
internally, each pair delimited by the character returned by
pairDelimiter(), and the key and value are delimited by
valueDelimiter().
Note: this function does not verify that the key-value pairs
are properly encoded. It is the caller's responsibility to ensure
that the query delimiters are properly encoded, if any.
\sa setQueryDelimiters(), encodedQueryItems(), setQueryItems()
*/
void QUrl::setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query)
{
if (!d) d = new QUrlPrivate;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
QByteArray queryTmp;
for (int i = 0; i < query.size(); i++) {
if (i) queryTmp += d->pairDelimiter;
queryTmp += query.at(i).first;
queryTmp += d->valueDelimiter;
queryTmp += query.at(i).second;
}
d->query = queryTmp;
d->hasQuery = !query.isEmpty();
}
/*!
Inserts the pair \a key = \a value into the query string of the
URL.
The key/value pair is encoded before it is added to the query. The
pair is converted into separate strings internally. The \a key and
\a value is first encoded into UTF-8 and then delimited by the
character returned by valueDelimiter(). Each key/value pair is
delimited by the character returned by pairDelimiter().
\note This method does not encode spaces (ASCII 0x20) as plus (+) signs,
like HTML forms do. If you need that kind of encoding, you must encode
the value yourself and use QUrl::addEncodedQueryItem.
\sa addEncodedQueryItem()
*/
void QUrl::addQueryItem(const QString &key, const QString &value)
{
if (!d) d = new QUrlPrivate;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
char alsoEncode[3];
alsoEncode[0] = d->valueDelimiter;
alsoEncode[1] = d->pairDelimiter;
alsoEncode[2] = 0;
if (!d->query.isEmpty())
d->query += d->pairDelimiter;
// query = *( pchar / "/" / "?" )
d->query += toPercentEncodingHelper(key, queryExcludeChars, alsoEncode);
d->query += d->valueDelimiter;
// query = *( pchar / "/" / "?" )
d->query += toPercentEncodingHelper(value, queryExcludeChars, alsoEncode);
d->hasQuery = !d->query.isEmpty();
}
/*!
\since 4.4
Inserts the pair \a key = \a value into the query string of the
URL.
Note: this function does not verify that either \a key or \a value
are properly encoded. It is the caller's responsibility to ensure
that the query delimiters are properly encoded, if any.
\sa addQueryItem(), setQueryDelimiters()
*/
void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value)
{
if (!d) d = new QUrlPrivate;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
if (!d->query.isEmpty())
d->query += d->pairDelimiter;
d->query += key;
d->query += d->valueDelimiter;
d->query += value;
d->hasQuery = !d->query.isEmpty();
}
/*!
Returns the query string of the URL, as a map of keys and values.
\note This method does not decode spaces plus (+) signs as spaces (ASCII
0x20), like HTML forms do. If you need that kind of decoding, you must
use QUrl::encodedQueryItems and decode the data yourself.
\sa setQueryItems(), setEncodedQuery()
*/
QList<QPair<QString, QString> > QUrl::queryItems() const
{
if (!d) return QList<QPair<QString, QString> >();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
QList<QPair<QString, QString> > itemMap;
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
QByteArray q(query + pos, valuedelim - pos);
if (valuedelim < end) {
QByteArray v(query + valuedelim + 1, end - valuedelim - 1);
itemMap += qMakePair(fromPercentEncodingMutable(&q),
fromPercentEncodingMutable(&v));
} else {
itemMap += qMakePair(fromPercentEncodingMutable(&q), QString());
}
pos = end + 1;
}
return itemMap;
}
/*!
\since 4.4
Returns the query string of the URL, as a map of encoded keys and values.
\sa setEncodedQueryItems(), setQueryItems(), setEncodedQuery()
*/
QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const
{
if (!d) return QList<QPair<QByteArray, QByteArray> >();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
QList<QPair<QByteArray, QByteArray> > itemMap;
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (valuedelim < end)
itemMap += qMakePair(QByteArray(query + pos, valuedelim - pos),
QByteArray(query + valuedelim + 1, end - valuedelim - 1));
else
itemMap += qMakePair(QByteArray(query + pos, valuedelim - pos), QByteArray());
pos = end + 1;
}
return itemMap;
}
/*!
Returns true if there is a query string pair whose key is equal
to \a key from the URL.
\sa hasEncodedQueryItem()
*/
bool QUrl::hasQueryItem(const QString &key) const
{
if (!d) return false;
return hasEncodedQueryItem(key.toUtf8().toPercentEncoding(queryExcludeChars));
}
/*!
\since 4.4
Returns true if there is a query string pair whose key is equal
to \a key from the URL.
Note: if the encoded \a key does not match the encoded version of
the query, this function will return false. That is, if the
encoded query of this URL is "search=Qt%20Rules", calling this
function with \a key = "%73earch" will return false.
\sa hasQueryItem()
*/
bool QUrl::hasEncodedQueryItem(const QByteArray &key) const
{
if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (key == QByteArray::fromRawData(query + pos, valuedelim - pos))
return true;
pos = end + 1;
}
return false;
}
/*!
Returns the first query string value whose key is equal to \a key
from the URL.
\note This method does not decode spaces plus (+) signs as spaces (ASCII
0x20), like HTML forms do. If you need that kind of decoding, you must
use QUrl::encodedQueryItemValue and decode the data yourself.
\sa allQueryItemValues()
*/
QString QUrl::queryItemValue(const QString &key) const
{
if (!d) return QString();
QByteArray tmp = encodedQueryItemValue(key.toUtf8().toPercentEncoding(queryExcludeChars));
return fromPercentEncodingMutable(&tmp);
}
/*!
\since 4.4
Returns the first query string value whose key is equal to \a key
from the URL.
Note: if the encoded \a key does not match the encoded version of
the query, this function will not work. That is, if the
encoded query of this URL is "search=Qt%20Rules", calling this
function with \a key = "%73earch" will return an empty string.
\sa queryItemValue(), allQueryItemValues()
*/
QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const
{
if (!d) return QByteArray();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (key == QByteArray::fromRawData(query + pos, valuedelim - pos))
return valuedelim < end ?
QByteArray(query + valuedelim + 1, end - valuedelim - 1) : QByteArray();
pos = end + 1;
}
return QByteArray();
}
/*!
Returns the a list of query string values whose key is equal to
\a key from the URL.
\note This method does not decode spaces plus (+) signs as spaces (ASCII
0x20), like HTML forms do. If you need that kind of decoding, you must
use QUrl::allEncodedQueryItemValues and decode the data yourself.
\sa queryItemValue()
*/
QStringList QUrl::allQueryItemValues(const QString &key) const
{
if (!d) return QStringList();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
QByteArray encodedKey = key.toUtf8().toPercentEncoding(queryExcludeChars);
QStringList values;
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (encodedKey == QByteArray::fromRawData(query + pos, valuedelim - pos)) {
QByteArray v(query + valuedelim + 1, end - valuedelim - 1);
values += valuedelim < end ?
fromPercentEncodingMutable(&v)
: QString();
}
pos = end + 1;
}
return values;
}
/*!
\since 4.4
Returns the a list of query string values whose key is equal to
\a key from the URL.
Note: if the encoded \a key does not match the encoded version of
the query, this function will not work. That is, if the
encoded query of this URL is "search=Qt%20Rules", calling this
function with \a key = "%73earch" will return an empty list.
\sa allQueryItemValues(), queryItemValue(), encodedQueryItemValue()
*/
QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const
{
if (!d) return QList<QByteArray>();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
QList<QByteArray> values;
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (key == QByteArray::fromRawData(query + pos, valuedelim - pos))
values += valuedelim < end ?
QByteArray(query + valuedelim + 1, end - valuedelim - 1)
: QByteArray();
pos = end + 1;
}
return values;
}
/*!
Removes the first query string pair whose key is equal to \a key
from the URL.
\sa removeAllQueryItems()
*/
void QUrl::removeQueryItem(const QString &key)
{
if (!d) return;
removeEncodedQueryItem(key.toUtf8().toPercentEncoding(queryExcludeChars));
}
/*!
\since 4.4
Removes the first query string pair whose key is equal to \a key
from the URL.
Note: if the encoded \a key does not match the encoded version of
the query, this function will not work. That is, if the
encoded query of this URL is "search=Qt%20Rules", calling this
function with \a key = "%73earch" will do nothing.
\sa removeQueryItem(), removeAllQueryItems()
*/
void QUrl::removeEncodedQueryItem(const QByteArray &key)
{
if (!d) return;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (key == QByteArray::fromRawData(query + pos, valuedelim - pos)) {
if (end < d->query.size())
++end; // remove additional '%'
d->query.remove(pos, end - pos);
return;
}
pos = end + 1;
}
}
/*!
Removes all the query string pairs whose key is equal to \a key
from the URL.
\sa removeQueryItem()
*/
void QUrl::removeAllQueryItems(const QString &key)
{
if (!d) return;
removeAllEncodedQueryItems(key.toUtf8().toPercentEncoding(queryExcludeChars));
}
/*!
\since 4.4
Removes all the query string pairs whose key is equal to \a key
from the URL.
Note: if the encoded \a key does not match the encoded version of
the query, this function will not work. That is, if the
encoded query of this URL is "search=Qt%20Rules", calling this
function with \a key = "%73earch" will do nothing.
\sa removeQueryItem()
*/
void QUrl::removeAllEncodedQueryItems(const QByteArray &key)
{
if (!d) return;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
detach();
int pos = 0;
const char *query = d->query.constData();
while (pos < d->query.size()) {
int valuedelim, end;
d->queryItem(pos, &valuedelim, &end);
if (key == QByteArray::fromRawData(query + pos, valuedelim - pos)) {
if (end < d->query.size())
++end; // remove additional '%'
d->query.remove(pos, end - pos);
query = d->query.constData(); //required if remove detach;
} else {
pos = end + 1;
}
}
}
/*!
Returns the query string of the URL in percent encoded form.
*/

View File

@ -44,7 +44,6 @@
#include <QtCore/qbytearray.h>
#include <QtCore/qobjectdefs.h>
#include <QtCore/qpair.h>
#include <QtCore/qstring.h>
#include <QtCore/qhash.h>
@ -156,31 +155,8 @@ public:
QByteArray encodedPath() const;
bool hasQuery() const;
void setEncodedQuery(const QByteArray &query);
QByteArray encodedQuery() const;
void setQueryDelimiters(char valueDelimiter, char pairDelimiter);
char queryValueDelimiter() const;
char queryPairDelimiter() const;
void setQueryItems(const QList<QPair<QString, QString> > &query);
void addQueryItem(const QString &key, const QString &value);
QList<QPair<QString, QString> > queryItems() const;
bool hasQueryItem(const QString &key) const;
QString queryItemValue(const QString &key) const;
QStringList allQueryItemValues(const QString &key) const;
void removeQueryItem(const QString &key);
void removeAllQueryItems(const QString &key);
void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query);
void addEncodedQueryItem(const QByteArray &key, const QByteArray &value);
QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const;
bool hasEncodedQueryItem(const QByteArray &key) const;
QByteArray encodedQueryItemValue(const QByteArray &key) const;
QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const;
void removeEncodedQueryItem(const QByteArray &key);
void removeAllEncodedQueryItems(const QByteArray &key);
void setEncodedQuery(const QByteArray &query);
void setFragment(const QString &fragment);
QString fragment() const;
@ -218,6 +194,15 @@ public:
{ return fromAce(punycode); }
QT_DEPRECATED static QByteArray toPunycode(const QString &string)
{ return toAce(string); }
QT_DEPRECATED inline void setQueryItems(const QList<QPair<QString, QString> > &qry);
QT_DEPRECATED inline void addQueryItem(const QString &key, const QString &value);
QT_DEPRECATED inline QList<QPair<QString, QString> > queryItems() const;
QT_DEPRECATED inline bool hasQueryItem(const QString &key) const;
QT_DEPRECATED inline QString queryItemValue(const QString &key) const;
QT_DEPRECATED inline QStringList allQueryItemValues(const QString &key) const;
QT_DEPRECATED inline void removeQueryItem(const QString &key);
QT_DEPRECATED inline void removeAllQueryItems(const QString &key);
#endif
static QString fromAce(const QByteArray &);
@ -262,6 +247,10 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
QT_END_NAMESPACE
#if QT_DEPRECATED_SINCE(5,0)
# include <QtCore/qurlquery.h>
#endif
QT_END_HEADER
#endif // QURL_H

View File

@ -46,6 +46,10 @@
#include <QtCore/qshareddata.h>
#include <QtCore/qurl.h>
#if QT_DEPRECATED_SINCE(5,0)
#include <QtCore/qstringlist.h>
#endif
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
@ -110,6 +114,25 @@ public:
Q_DECLARE_TYPEINFO(QUrlQuery, Q_MOVABLE_TYPE);
Q_DECLARE_SHARED(QUrlQuery)
#if QT_DEPRECATED_SINCE(5,0)
inline void QUrl::setQueryItems(const QList<QPair<QString, QString> > &qry)
{ QUrlQuery q(*this); q.setQueryItems(qry); setQuery(q.query()); }
inline void QUrl::addQueryItem(const QString &key, const QString &value)
{ QUrlQuery q(*this); q.addQueryItem(key, value); setQuery(q.query()); }
inline QList<QPair<QString, QString> > QUrl::queryItems() const
{ return QUrlQuery(*this).queryItems(); }
inline bool QUrl::hasQueryItem(const QString &key) const
{ return QUrlQuery(*this).hasQueryItem(key); }
inline QString QUrl::queryItemValue(const QString &key) const
{ return QUrlQuery(*this).queryItemValue(key); }
inline QStringList QUrl::allQueryItemValues(const QString &key) const
{ return QUrlQuery(*this).allQueryItemValues(key); }
inline void QUrl::removeQueryItem(const QString &key)
{ QUrlQuery q(*this); q.removeQueryItem(key); setQuery(q.query()); }
inline void QUrl::removeAllQueryItems(const QString &key)
{ QUrlQuery q(*this); q.removeAllQueryItems(key); }
#endif
QT_END_NAMESPACE
QT_END_HEADER

View File

@ -114,11 +114,8 @@ private slots:
void isRelative_data();
void isRelative();
void setQueryItems();
void queryItems();
void hasQuery_data();
void hasQuery();
void hasQueryItem_data();
void hasQueryItem();
void nameprep();
void isValid();
void schemeValidator_data();
@ -157,8 +154,6 @@ private slots:
void toEncodedNotUsingUninitializedPath();
void emptyAuthorityRemovesExistingAuthority();
void acceptEmptyAuthoritySegments();
void removeAllEncodedQueryItems_data();
void removeAllEncodedQueryItems();
};
// Testing get/set functions
@ -1416,8 +1411,6 @@ void tst_QUrl::symmetry()
QCOMPARE(url.path(), QString::fromLatin1("/pub"));
// this will be encoded ...
QCOMPARE(url.encodedQuery().constData(), QString::fromLatin1("a=b&a=d%C3%B8&a=f").toLatin1().constData());
// unencoded
QCOMPARE(url.allQueryItemValues("a").join(""), QString::fromUtf8("bdøf"));
QCOMPARE(url.fragment(), QString::fromUtf8("vræl"));
QUrl onlyHost("//qt.nokia.com");
@ -1577,56 +1570,6 @@ void tst_QUrl::setQueryItems()
"/ole&du>anne+jørgen=sant/prosent>%25#top"));
}
void tst_QUrl::queryItems()
{
QUrl url;
QVERIFY(!url.hasQuery());
QList<QPair<QString, QString> > newItems;
newItems += qMakePair(QString("2"), QString("b"));
newItems += qMakePair(QString("1"), QString("a"));
newItems += qMakePair(QString("3"), QString("c"));
newItems += qMakePair(QString("4"), QString("a b"));
newItems += qMakePair(QString("5"), QString("&"));
newItems += qMakePair(QString("foo bar"), QString("hello world"));
newItems += qMakePair(QString("foo+bar"), QString("hello+world"));
newItems += qMakePair(QString("tex"), QString("a + b = c"));
url.setQueryItems(newItems);
QVERIFY(url.hasQuery());
QList<QPair<QString, QString> > setItems = url.queryItems();
QVERIFY(newItems == setItems);
url.addQueryItem("1", "z");
QVERIFY(url.hasQueryItem("1"));
QCOMPARE(url.queryItemValue("1").toLatin1().constData(), "a");
url.addQueryItem("1", "zz");
QStringList expected;
expected += "a";
expected += "z";
expected += "zz";
QCOMPARE(expected, url.allQueryItemValues("1"));
url.removeQueryItem("1");
QCOMPARE(url.allQueryItemValues("1").size(), 2);
QCOMPARE(url.queryItemValue("1").toLatin1().constData(), "z");
url.removeAllQueryItems("1");
QVERIFY(!url.hasQueryItem("1"));
QCOMPARE(url.queryItemValue("4").toLatin1().constData(), "a b");
QCOMPARE(url.queryItemValue("5").toLatin1().constData(), "&");
QCOMPARE(url.queryItemValue("tex").toLatin1().constData(), "a + b = c");
QCOMPARE(url.queryItemValue("foo bar").toLatin1().constData(), "hello world");
url.setUrl("http://www.google.com/search?q=a+b");
QCOMPARE(url.queryItemValue("q"), QString("a+b"));
url.setUrl("http://www.google.com/search?q=a=b"); // invalid, but should be tolerated
QCOMPARE(url.queryItemValue("q"), QString("a=b"));
}
void tst_QUrl::hasQuery_data()
{
QTest::addColumn<QString>("url");
@ -1656,27 +1599,6 @@ void tst_QUrl::hasQuery()
QCOMPARE(qurl.encodedQuery().isNull(), !trueFalse);
}
void tst_QUrl::hasQueryItem_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("item");
QTest::addColumn<bool>("trueFalse");
QTest::newRow("no query items") << "http://www.foo.bar" << "baz" << false;
QTest::newRow("query item: hello") << "http://www.foo.bar?hello=world" << "hello" << true;
QTest::newRow("no query item: world") << "http://www.foo.bar?hello=world" << "world" << false;
QTest::newRow("query item: qt") << "http://www.foo.bar?hello=world&qt=rocks" << "qt" << true;
}
void tst_QUrl::hasQueryItem()
{
QFETCH(QString, url);
QFETCH(QString, item);
QFETCH(bool, trueFalse);
QCOMPARE(QUrl(url).hasQueryItem(item), trueFalse);
}
void tst_QUrl::nameprep()
{
QUrl url(QString::fromUtf8("http://www.fu""\xc3""\x9f""ball.de/"));
@ -2568,28 +2490,5 @@ void tst_QUrl::effectiveTLDs()
QCOMPARE(domain.topLevelDomain(), TLD);
}
void tst_QUrl::removeAllEncodedQueryItems_data()
{
QTest::addColumn<QUrl>("url");
QTest::addColumn<QByteArray>("key");
QTest::addColumn<QUrl>("result");
QTest::newRow("test1") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("bbb") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&ccc=c");
QTest::newRow("test2") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("aaa") << QUrl::fromEncoded("http://qt.nokia.com/foo?bbb=b&ccc=c");
// QTest::newRow("test3") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("ccc") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b");
QTest::newRow("test4") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("b%62b") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c");
QTest::newRow("test5") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c") << QByteArray("b%62b") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&ccc=c");
QTest::newRow("test6") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c") << QByteArray("bbb") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c");
}
void tst_QUrl::removeAllEncodedQueryItems()
{
QFETCH(QUrl, url);
QFETCH(QByteArray, key);
QFETCH(QUrl, result);
url.removeAllEncodedQueryItems(key);
QCOMPARE(url, result);
}
QTEST_MAIN(tst_QUrl)
#include "tst_qurl.moc"

View File

@ -72,6 +72,12 @@ private Q_SLOTS:
void encodedParsing_data();
void encodedParsing();
void differentDelimiters();
// old tests from tst_qurl.cpp
// add new tests above
void old_queryItems();
void old_hasQueryItem_data();
void old_hasQueryItem();
};
static QString prettyElement(const QString &key, const QString &value)
@ -230,6 +236,14 @@ void tst_QUrlQuery::constructing()
empty.setQueryDelimiters('(', ')');
QCOMPARE(empty.queryValueDelimiter(), QChar(QLatin1Char('(')));
QCOMPARE(empty.queryPairDelimiter(), QChar(QLatin1Char(')')));
QList<QPair<QString, QString> > query;
query += qMakePair(QString("type"), QString("login"));
query += qMakePair(QString("name"), QString::fromUtf8("åge nissemannsen"));
query += qMakePair(QString("ole&du"), QString::fromUtf8("anne+jørgen=sant"));
query += qMakePair(QString("prosent"), QString("%"));
copy.setQueryItems(query);
QVERIFY(!copy.isEmpty());
}
void tst_QUrlQuery::addRemove()
@ -690,6 +704,112 @@ void tst_QUrlQuery::differentDelimiters()
}
}
void tst_QUrlQuery::old_queryItems()
{
// test imported from old tst_qurl.cpp
QUrlQuery url;
QList<QPair<QString, QString> > newItems;
newItems += qMakePair(QString("1"), QString("a"));
newItems += qMakePair(QString("2"), QString("b"));
newItems += qMakePair(QString("3"), QString("c"));
newItems += qMakePair(QString("4"), QString("a b"));
newItems += qMakePair(QString("5"), QString("&"));
newItems += qMakePair(QString("foo bar"), QString("hello world"));
newItems += qMakePair(QString("foo+bar"), QString("hello+world"));
newItems += qMakePair(QString("tex"), QString("a + b = c"));
url.setQueryItems(newItems);
QVERIFY(!url.isEmpty());
QList<QPair<QString, QString> > setItems = url.queryItems();
QVERIFY(newItems == setItems);
url.addQueryItem("1", "z");
#if 0
// undefined behaviour in the new QUrlQuery
QVERIFY(url.hasQueryItem("1"));
QCOMPARE(url.queryItemValue("1").toLatin1().constData(), "a");
url.addQueryItem("1", "zz");
QStringList expected;
expected += "a";
expected += "z";
expected += "zz";
QCOMPARE(url.allQueryItemValues("1"), expected);
url.removeQueryItem("1");
QCOMPARE(url.allQueryItemValues("1").size(), 2);
QCOMPARE(url.queryItemValue("1").toLatin1().constData(), "z");
#endif
url.removeAllQueryItems("1");
QVERIFY(!url.hasQueryItem("1"));
QCOMPARE(url.queryItemValue("4").toLatin1().constData(), "a b");
QCOMPARE(url.queryItemValue("5").toLatin1().constData(), "&");
QCOMPARE(url.queryItemValue("tex").toLatin1().constData(), "a + b = c");
QCOMPARE(url.queryItemValue("foo bar").toLatin1().constData(), "hello world");
//url.setUrl("http://www.google.com/search?q=a+b");
url.setQuery("q=a+b");
QCOMPARE(url.queryItemValue("q"), QString("a+b"));
//url.setUrl("http://www.google.com/search?q=a=b"); // invalid, but should be tolerated
url.setQuery("q=a=b");
QCOMPARE(url.queryItemValue("q"), QString("a=b"));
}
void tst_QUrlQuery::old_hasQueryItem_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("item");
QTest::addColumn<bool>("trueFalse");
// the old tests started with "http://www.foo.bar"
QTest::newRow("no query items") << "" << "baz" << false;
QTest::newRow("query item: hello") << "hello=world" << "hello" << true;
QTest::newRow("no query item: world") << "hello=world" << "world" << false;
QTest::newRow("query item: qt") << "hello=world&qt=rocks" << "qt" << true;
}
void tst_QUrlQuery::old_hasQueryItem()
{
QFETCH(QString, url);
QFETCH(QString, item);
QFETCH(bool, trueFalse);
QCOMPARE(QUrlQuery(url).hasQueryItem(item), trueFalse);
}
#if 0
// this test doesn't make sense anymore
void tst_QUrl::removeAllEncodedQueryItems_data()
{
QTest::addColumn<QUrl>("url");
QTest::addColumn<QByteArray>("key");
QTest::addColumn<QUrl>("result");
QTest::newRow("test1") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("bbb") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&ccc=c");
QTest::newRow("test2") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("aaa") << QUrl::fromEncoded("http://qt.nokia.com/foo?bbb=b&ccc=c");
// QTest::newRow("test3") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("ccc") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b");
QTest::newRow("test4") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c") << QByteArray("b%62b") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&bbb=b&ccc=c");
QTest::newRow("test5") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c") << QByteArray("b%62b") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&ccc=c");
QTest::newRow("test6") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c") << QByteArray("bbb") << QUrl::fromEncoded("http://qt.nokia.com/foo?aaa=a&b%62b=b&ccc=c");
}
void tst_QUrl::removeAllEncodedQueryItems()
{
QFETCH(QUrl, url);
QFETCH(QByteArray, key);
QFETCH(QUrl, result);
url.removeAllEncodedQueryItems(key);
QCOMPARE(url, result);
}
#endif
QTEST_APPLESS_MAIN(tst_QUrlQuery)
#include "tst_qurlquery.moc"