From c0a0b3710302dc7711c4296c35df8ccf6b91f572 Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Fri, 8 Jun 2012 13:26:20 +0100 Subject: Implement RFC6265 test suite Tests against the test data from the IETF working group https://github.com/abarth/http-state The test data is in the parser.json file, imported from that repository and with one patch applied to make the ordering0001 test case data match the raw files which are used by their python test server. Task-number: QTBUG-18920 Change-Id: I17c1a8d92aef2850907f009667c6574e4c8d0cdb Reviewed-by: Richard J. Moore --- .../qnetworkcookiejar/tst_qnetworkcookiejar.cpp | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp') diff --git a/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp b/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp index 5dffe68386..8b509024e9 100644 --- a/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp +++ b/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp @@ -41,8 +41,13 @@ #include +#include +#include +#include +#include #include #include +#include #include "private/qtldurl_p.h" class tst_QNetworkCookieJar: public QObject @@ -59,6 +64,8 @@ private slots: void effectiveTLDs_data(); void effectiveTLDs(); #endif + void rfc6265_data(); + void rfc6265(); }; QT_BEGIN_NAMESPACE @@ -456,6 +463,92 @@ void tst_QNetworkCookieJar::effectiveTLDs() } #endif +void tst_QNetworkCookieJar::rfc6265_data() +{ + QTest::addColumn("received"); + QTest::addColumn >("sent"); + QTest::addColumn("sentTo"); + + QFile file(QFINDTESTDATA("parser.json")); + QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); + QJsonDocument document; + document = QJsonDocument::fromJson(file.readAll()); + QVERIFY(!document.isNull()); + QVERIFY(document.isArray()); + + foreach (const QJsonValue& testCase, document.array()) { + QJsonObject testObject = testCase.toObject(); + + //"test" - the test case name + QString testCaseName = testObject.value("test").toString(); + if (testCaseName.toLower().startsWith("disabled")) + continue; + + //"received" - the cookies received from the server + QJsonArray received = testObject.value("received").toArray(); + QStringList receivedList; + foreach (const QJsonValue& receivedCookie, received) + receivedList.append(receivedCookie.toString()); + + //"sent" - the cookies sent back to the server + QJsonArray sent = testObject.value("sent").toArray(); + QList sentList; + foreach (const QJsonValue& sentCookie, sent) { + QJsonObject sentCookieObject = sentCookie.toObject(); + QNetworkCookie cookie; + cookie.setName(sentCookieObject.value("name").toString().toUtf8()); + cookie.setValue(sentCookieObject.value("value").toString().toUtf8()); + sentList.append(cookie); + } + + //"sent-to" - the relative url where cookies are sent + QTest::newRow(qPrintable(testCaseName)) << receivedList << sentList << testObject.value("sent-to").toString(); + } +} + +void tst_QNetworkCookieJar::rfc6265() +{ + QFETCH(QStringList, received); + QFETCH(QList, sent); + QFETCH(QString, sentTo); + + QUrl receivedUrl("http://home.example.org:8888/cookie-parser"); + QUrl sentUrl("http://home.example.org:8888/cookie-parser-result"); + if (!sentTo.isEmpty()) + sentUrl = receivedUrl.resolved(sentTo); + + QNetworkCookieJar jar; + QList receivedCookies; + foreach (const QString &cookieLine, received) + receivedCookies.append(QNetworkCookie::parseCookies(cookieLine.toUtf8())); + + jar.setCookiesFromUrl(receivedCookies, receivedUrl); + QList cookiesToSend = jar.cookiesForUrl(sentUrl); + + //compare cookies only using name/value, as the metadata isn't sent over the network + QCOMPARE(cookiesToSend.count(), sent.count()); + bool ok = true; + for (int i = 0; i < cookiesToSend.count(); i++) { + if (cookiesToSend.at(i).name() != sent.at(i).name()) { + ok = false; + break; + } + if (cookiesToSend.at(i).value() != sent.at(i).value()) { + ok = false; + break; + } + } + if (!ok) { + QNetworkRequest r(sentUrl); + r.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookiesToSend)); + QString actual = QString::fromUtf8(r.rawHeader("Cookie")); + r.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(sent)); + QString expected = QString::fromUtf8(r.rawHeader("Cookie")); + + QVERIFY2(ok, qPrintable(QString("Expected: %1\nActual: %2").arg(expected).arg(actual))); + } +} + QTEST_MAIN(tst_QNetworkCookieJar) #include "tst_qnetworkcookiejar.moc" -- cgit v1.2.3