QNetworkReply *reply,
bool synchronous,
QUrl &url,
- QUrl *urlForLastAuthentication)
+ QUrl *urlForLastAuthentication,
+ bool allowAuthenticationReuse)
{
Q_Q(QNetworkAccessManager);
// don't try the cache for the same URL twice in a row
// being called twice for the same URL means the authentication failed
// also called when last URL is empty, e.g. on first call
- if (urlForLastAuthentication->isEmpty()
- || url != *urlForLastAuthentication) {
+ if (allowAuthenticationReuse && (urlForLastAuthentication->isEmpty()
+ || url != *urlForLastAuthentication)) {
// if credentials are included in the url, then use them
if (!url.userName().isEmpty()
&& !url.password().isEmpty()) {
*urlForLastAuthentication = url;
emit q->authenticationRequired(reply, authenticator);
- authenticationManager->cacheCredentials(url, authenticator);
+ if (allowAuthenticationReuse)
+ authenticationManager->cacheCredentials(url, authenticator);
}
#ifndef QT_NO_NETWORKPROXY
void tst_QNetworkReply::httpWithNoCredentialUsage()
{
- QNetworkRequest request(QUrl("http://httptest:httptest@" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
- // Do not use credentials
- request.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual);
QNetworkAccessManager manager;
- QNetworkReplyPtr reply = manager.get(request);
- qRegisterMetaType<QNetworkReply*>("QNetworkReply*");
- qRegisterMetaType<QAuthenticator*>("QAuthenticator*");
QSignalSpy authSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
QSignalSpy finishedSpy(&manager, SIGNAL(finished(QNetworkReply*)));
- qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
- QSignalSpy errorSpy(reply, SIGNAL(error(QNetworkReply::NetworkError)));
- connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
- QTestEventLoop::instance().enterLoop(10);
- QVERIFY(!QTestEventLoop::instance().timeout());
+ // Get with credentials, to preload authentication cache
+ {
+ QNetworkRequest request(QUrl("http://httptest:httptest@" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
+ QNetworkReplyPtr reply = manager.get(request);
+ QVERIFY(waitForFinish(reply) == Success);
+ // credentials in URL, so don't expect authentication signal
+ QCOMPARE(authSpy.count(), 0);
+ QCOMPARE(finishedSpy.count(), 1);
+ finishedSpy.clear();
+ }
- // We check if authenticationRequired was emitted, however we do not anything in it so it should be 401
- QCOMPARE(authSpy.count(), 1);
- QCOMPARE(finishedSpy.count(), 1);
- QCOMPARE(errorSpy.count(), 1);
+ // Get with cached credentials (normal usage)
+ {
+ QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
+ QNetworkReplyPtr reply = manager.get(request);
+ QVERIFY(waitForFinish(reply) == Success);
+ // credentials in cache, so don't expect authentication signal
+ QCOMPARE(authSpy.count(), 0);
+ QCOMPARE(finishedSpy.count(), 1);
+ finishedSpy.clear();
+ }
- QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
+ // Do not use cached credentials (webkit cross origin usage)
+ {
+ QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
+ request.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual);
+ QNetworkReplyPtr reply = manager.get(request);
+
+ QSignalSpy errorSpy(reply, SIGNAL(error(QNetworkReply::NetworkError)));
+
+ connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
+ QTestEventLoop::instance().enterLoop(10);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ // We check if authenticationRequired was emitted, however we do not anything in it so it should be 401
+ QCOMPARE(authSpy.count(), 1);
+ QCOMPARE(finishedSpy.count(), 1);
+ QCOMPARE(errorSpy.count(), 1);
+
+ QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
+ }
}
void tst_QNetworkReply::qtbug15311doubleContentLength()