|
| 1 | +/*************************************************************************** |
| 2 | + testqgsogrprovider.cpp - TestQgsOgrProvider |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 10.11.2017 |
| 6 | + copyright : (C) 2017 by Alessandro Pasotti |
| 7 | + email : apasotti at boundlessgeo dot com |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | + |
| 18 | +#include "qgstest.h" |
| 19 | + |
| 20 | +//qgis includes... |
| 21 | +#include <qgis.h> |
| 22 | +#include <qgssettings.h> |
| 23 | +#include <qgsapplication.h> |
| 24 | +#include <qgsproviderregistry.h> |
| 25 | +#include <qgsvectorlayer.h> |
| 26 | +#include <qgsnetworkaccessmanager.h> |
| 27 | + |
| 28 | +#include <QObject> |
| 29 | + |
| 30 | +#include <cpl_conv.h> |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * \ingroup UnitTests |
| 35 | + * This is a unit test for the ogr provider |
| 36 | + */ |
| 37 | +class TestQgsOgrProvider : public QObject |
| 38 | +{ |
| 39 | + Q_OBJECT |
| 40 | + |
| 41 | + private slots: |
| 42 | + void initTestCase();// will be called before the first testfunction is executed. |
| 43 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 44 | + void init() {}// will be called before each testfunction is executed. |
| 45 | + void cleanup() {}// will be called after every testfunction. |
| 46 | + |
| 47 | + void setupProxy(); |
| 48 | + |
| 49 | + private: |
| 50 | + QString mTestDataDir; |
| 51 | + QString mReport; |
| 52 | + signals: |
| 53 | + |
| 54 | + public slots: |
| 55 | +}; |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +//runs before all tests |
| 60 | +void TestQgsOgrProvider::initTestCase() |
| 61 | +{ |
| 62 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 63 | + QgsApplication::init(); |
| 64 | + QgsApplication::initQgis(); |
| 65 | + |
| 66 | + mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt |
| 67 | + mReport = QStringLiteral( "<h1>OGR Provider Tests</h1>\n" ); |
| 68 | +} |
| 69 | + |
| 70 | +//runs after all tests |
| 71 | +void TestQgsOgrProvider::cleanupTestCase() |
| 72 | +{ |
| 73 | + QgsApplication::exitQgis(); |
| 74 | + QString myReportFile = QDir::tempPath() + "/qgistest.html"; |
| 75 | + QFile myFile( myReportFile ); |
| 76 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 77 | + { |
| 78 | + QTextStream myQTextStream( &myFile ); |
| 79 | + myQTextStream << mReport; |
| 80 | + myFile.close(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void TestQgsOgrProvider::setupProxy() |
| 85 | +{ |
| 86 | + |
| 87 | + QgsSettings settings; |
| 88 | + { |
| 89 | + settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true ); |
| 90 | + settings.setValue( QStringLiteral( "proxy/proxyPort" ), QStringLiteral( "1234" ) ); |
| 91 | + settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) ); |
| 92 | + settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) ); |
| 93 | + settings.setValue( QStringLiteral( "proxy/proxyPassword" ), QStringLiteral( "password" ) ); |
| 94 | + settings.setValue( QStringLiteral( "proxy/proxyExcludedUrls" ), QStringLiteral( "http://www.myhost.com|http://www.myotherhost.com" ) ); |
| 95 | + QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache(); |
| 96 | + QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) ); |
| 97 | + QVERIFY( vl.isValid() ); |
| 98 | + const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL ); |
| 99 | + QCOMPARE( proxyConfig, "myproxyhostname.com:1234" ); |
| 100 | + const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL ); |
| 101 | + QCOMPARE( proxyCredentials, "username:password" ); |
| 102 | + } |
| 103 | + |
| 104 | + { |
| 105 | + // Test partial config |
| 106 | + settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true ); |
| 107 | + settings.remove( QStringLiteral( "proxy/proxyPort" ) ); |
| 108 | + settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) ); |
| 109 | + settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) ); |
| 110 | + settings.remove( QStringLiteral( "proxy/proxyPassword" ) ); |
| 111 | + QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache(); |
| 112 | + QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) ); |
| 113 | + QVERIFY( vl.isValid() ); |
| 114 | + const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL ); |
| 115 | + QCOMPARE( proxyConfig, "myproxyhostname.com" ); |
| 116 | + const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL ); |
| 117 | + QCOMPARE( proxyCredentials, "username" ); |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +QGSTEST_MAIN( TestQgsOgrProvider ) |
| 124 | +#include "testqgsogrprovider.moc" |
0 commit comments