void tst_QUrlQuery::multiAddRemove() { QUrlQuery query; { // one item, two values query.addQueryItem("a", "b"); query.addQueryItem("a", "c"); QVERIFY(!query.isEmpty()); QVERIFY(query.hasQueryItem("a")); // returns the first one QVERIFY(query.queryItemValue("a") == "b"); // order is the order we set them in QVERIFY(query.allQueryItemValues("a") == QStringList() << "b" << "c"); } { // add another item, two values query.addQueryItem("A", "B"); query.addQueryItem("A", "C"); QVERIFY(query.hasQueryItem("A")); QVERIFY(query.hasQueryItem("a")); QVERIFY(query.queryItemValue("a") == "b"); QVERIFY(query.allQueryItemValues("a") == QStringList() << "b" << "c"); QVERIFY(query.queryItemValue("A") == "B"); QVERIFY(query.allQueryItemValues("A") == QStringList() << "B" << "C"); } { // remove one of the original items query.removeQueryItem("a"); QVERIFY(query.hasQueryItem("a")); // it must have removed the first one QVERIFY(query.queryItemValue("a") == "c"); } { // remove the items we added later query.removeAllQueryItems("A"); QVERIFY(!query.isEmpty()); QVERIFY(!query.hasQueryItem("A")); } { // add one element to the current, then remove them query.addQueryItem("a", "d"); query.removeAllQueryItems("a"); QVERIFY(!query.hasQueryItem("a")); QVERIFY(query.isEmpty()); } }
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")); }