tests/auto/qabstractnetworkcache/tst_qabstractnetworkcache.cpp
branchRCL_3
changeset 4 3b1da2848fc7
parent 0 1918ee327afb
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 
    42 
    43 #include <QtTest/QtTest>
    43 #include <QtTest/QtTest>
    44 #include <QtGui/QtGui>
       
    45 #include <QtNetwork/QtNetwork>
    44 #include <QtNetwork/QtNetwork>
    46 #include "../../shared/util.h"
    45 #include "../../shared/util.h"
    47 #include "../network-settings.h"
    46 #include "../network-settings.h"
    48 
    47 
    49 #define TESTFILE QString("http://%1/qtest/cgi-bin/").arg(QtNetworkSettings::serverName())
    48 #define TESTFILE QString("http://%1/qtest/cgi-bin/").arg(QtNetworkSettings::serverName())
    81 public:
    80 public:
    82     NetworkDiskCache(QObject *parent = 0)
    81     NetworkDiskCache(QObject *parent = 0)
    83         : QNetworkDiskCache(parent)
    82         : QNetworkDiskCache(parent)
    84         , gotData(false)
    83         , gotData(false)
    85     {
    84     {
    86         QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation)
    85         QString location = QDir::tempPath() + QLatin1String("/tst_qnetworkdiskcache/");
    87                                     + QLatin1String("/qnetworkdiskcache/");
       
    88         setCacheDirectory(location);
    86         setCacheDirectory(location);
    89         clear();
    87         clear();
    90     }
    88     }
    91 
    89 
    92     QIODevice *data(const QUrl &url)
    90     QIODevice *data(const QUrl &url)
   134     QTest::newRow("500-3") << QNetworkRequest::PreferCache << "httpcachetest_expires500.cgi" << true;
   132     QTest::newRow("500-3") << QNetworkRequest::PreferCache << "httpcachetest_expires500.cgi" << true;
   135 
   133 
   136     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires200.cgi" << AlwaysFalse;
   134     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_expires200.cgi" << AlwaysFalse;
   137     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires200.cgi" << false;
   135     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_expires200.cgi" << false;
   138     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires200.cgi" << AlwaysTrue;
   136     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_expires200.cgi" << AlwaysTrue;
   139     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_expires200.cgi" << true;
   137     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_expires200.cgi" << false;
   140 }
   138 }
   141 
   139 
   142 void tst_QAbstractNetworkCache::expires()
   140 void tst_QAbstractNetworkCache::expires()
   143 {
   141 {
   144     check();
   142     check();
   156     QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified304.cgi" << true;
   154     QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified304.cgi" << true;
   157 
   155 
   158     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified200.cgi" << AlwaysFalse;
   156     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_lastModified200.cgi" << AlwaysFalse;
   159     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified200.cgi" << false;
   157     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_lastModified200.cgi" << false;
   160     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified200.cgi" << AlwaysTrue;
   158     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_lastModified200.cgi" << AlwaysTrue;
   161     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified200.cgi" << true;
   159     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_lastModified200.cgi" << false;
   162 }
   160 }
   163 
   161 
   164 void tst_QAbstractNetworkCache::lastModified()
   162 void tst_QAbstractNetworkCache::lastModified()
   165 {
   163 {
   166     check();
   164     check();
   178     QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_etag304.cgi" << true;
   176     QTest::newRow("304-3") << QNetworkRequest::PreferCache << "httpcachetest_etag304.cgi" << true;
   179 
   177 
   180     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag200.cgi" << AlwaysFalse;
   178     QTest::newRow("200-0") << QNetworkRequest::AlwaysNetwork << "httpcachetest_etag200.cgi" << AlwaysFalse;
   181     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag200.cgi" << false;
   179     QTest::newRow("200-1") << QNetworkRequest::PreferNetwork << "httpcachetest_etag200.cgi" << false;
   182     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag200.cgi" << AlwaysTrue;
   180     QTest::newRow("200-2") << QNetworkRequest::AlwaysCache << "httpcachetest_etag200.cgi" << AlwaysTrue;
   183     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_etag200.cgi" << true;
   181     QTest::newRow("200-3") << QNetworkRequest::PreferCache << "httpcachetest_etag200.cgi" << false;
   184 }
   182 }
   185 
   183 
   186 void tst_QAbstractNetworkCache::etag()
   184 void tst_QAbstractNetworkCache::etag()
   187 {
   185 {
   188     check();
   186     check();
   205 
   203 
   206     QTest::newRow("304-1") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysFalse;
   204     QTest::newRow("304-1") << QNetworkRequest::AlwaysNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysFalse;
   207     QTest::newRow("304-2") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
   205     QTest::newRow("304-2") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
   208     QTest::newRow("304-3") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysTrue;
   206     QTest::newRow("304-3") << QNetworkRequest::AlwaysCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << AlwaysTrue;
   209     QTest::newRow("304-4") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
   207     QTest::newRow("304-4") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol.cgi?max-age=1000, must-revalidate" << true;
       
   208 
       
   209     // see QTBUG-7060
       
   210     //QTest::newRow("nokia-boston") << QNetworkRequest::PreferNetwork << "http://waplabdc.nokia-boston.com/browser/users/venkat/cache/Cache_directives/private_1b.asp" << true;
       
   211     QTest::newRow("304-2b") << QNetworkRequest::PreferNetwork << "httpcachetest_cachecontrol200.cgi?private, max-age=1000" << true;
       
   212     QTest::newRow("304-4b") << QNetworkRequest::PreferCache << "httpcachetest_cachecontrol200.cgi?private, max-age=1000" << true;
   210 }
   213 }
   211 
   214 
   212 void tst_QAbstractNetworkCache::cacheControl()
   215 void tst_QAbstractNetworkCache::cacheControl()
   213 {
   216 {
   214     check();
   217     check();
   223     QNetworkAccessManager manager;
   226     QNetworkAccessManager manager;
   224     NetworkDiskCache *diskCache = new NetworkDiskCache(&manager);
   227     NetworkDiskCache *diskCache = new NetworkDiskCache(&manager);
   225     manager.setCache(diskCache);
   228     manager.setCache(diskCache);
   226     QCOMPARE(diskCache->gotData, false);
   229     QCOMPARE(diskCache->gotData, false);
   227 
   230 
   228     QNetworkRequest request(QUrl(TESTFILE + url));
   231     QUrl realUrl = url.contains("://") ? url : TESTFILE + url;
       
   232     QNetworkRequest request(realUrl);
   229 
   233 
   230     // prime the cache
   234     // prime the cache
   231     QNetworkReply *reply = manager.get(request);
   235     QNetworkReply *reply = manager.get(request);
   232     QSignalSpy downloaded1(reply, SIGNAL(finished()));
   236     QSignalSpy downloaded1(reply, SIGNAL(finished()));
   233     QTRY_COMPARE(downloaded1.count(), 1);
   237     QTRY_COMPARE(downloaded1.count(), 1);