[libcalamares] Add urls only if valid, add tests to check that

This commit is contained in:
Adriaan de Groot 2021-08-24 10:00:42 +02:00
parent 1452b74740
commit c79fc2e6d9
3 changed files with 70 additions and 2 deletions

View File

@ -20,6 +20,8 @@
#include <QThread>
#include <QTimer>
#include <algorithm>
namespace CalamaresUtils
{
namespace Network
@ -205,7 +207,10 @@ Manager::setCheckHasInternetUrl( const QUrl& url )
{
d->m_lastCheckedUrlIndex = -1;
d->m_hasInternetUrls.clear();
d->m_hasInternetUrls.append( url );
if ( url.isValid() )
{
d->m_hasInternetUrls.append( url );
}
}
void
@ -213,12 +218,17 @@ Manager::setCheckHasInternetUrl( const QVector< QUrl >& urls )
{
d->m_lastCheckedUrlIndex = -1;
d->m_hasInternetUrls = urls;
std::remove_if(
d->m_hasInternetUrls.begin(), d->m_hasInternetUrls.end(), []( const QUrl& u ) { return u.isValid(); } );
}
void
Manager::addCheckHasInternetUrl( const QUrl& url )
{
d->m_hasInternetUrls.append( url );
if ( url.isValid() )
{
d->m_hasInternetUrls.append( url );
}
}
/** @brief Does a request asynchronously, returns the (pending) reply

View File

@ -60,3 +60,58 @@ NetworkTests::testPing()
QVERIFY( canPing_www_kde_org );
}
}
void
NetworkTests::testCheckUrl()
{
using namespace CalamaresUtils::Network;
Logger::setupLogLevel( Logger::LOGVERBOSE );
auto& nam = Manager::instance();
{
QUrl u( "http://example.com" );
QVERIFY( u.isValid() );
nam.setCheckHasInternetUrl( u );
QVERIFY( nam.checkHasInternet() );
}
{
QUrl u( "http://nonexistent.example.com" );
QVERIFY( u.isValid() );
nam.setCheckHasInternetUrl( u );
QVERIFY( !nam.checkHasInternet() );
}
{
QUrl u;
QVERIFY( !u.isValid() );
nam.setCheckHasInternetUrl( u );
QVERIFY( !nam.checkHasInternet() );
}
}
void
NetworkTests::testCheckMultiUrl()
{
using namespace CalamaresUtils::Network;
Logger::setupLogLevel( Logger::LOGVERBOSE );
auto& nam = Manager::instance();
{
QUrl u0( "http://example.com" );
QUrl u1( "https://kde.org" );
QVERIFY( u0.isValid() );
QVERIFY( u1.isValid() );
nam.setCheckHasInternetUrl( { u0, u1 } );
QVERIFY( nam.checkHasInternet() );
}
{
QUrl u0( "http://nonexistent.example.com" );
QUrl u1( "http://bogus.example.com" );
QVERIFY( u0.isValid() );
QVERIFY( u1.isValid() );
nam.setCheckHasInternetUrl( { u0, u1 } );
QVERIFY( !nam.checkHasInternet() );
QVERIFY( !nam.checkHasInternet() );
nam.addCheckHasInternetUrl( QUrl( "http://example.com" ) );
QVERIFY( nam.checkHasInternet() );
}
}

View File

@ -24,6 +24,9 @@ private Q_SLOTS:
void testInstance();
void testPing();
void testCheckUrl();
void testCheckMultiUrl();
};
#endif