Skip to content

Commit aa170ad

Browse files
committedJan 23, 2019
Unit tests for QgsNetworkAccessManager signals
1 parent 535965b commit aa170ad

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
 

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ SET(TESTS
162162
testqgsmeshlayer.cpp
163163
testqgsmeshlayerinterpolator.cpp
164164
testqgsmeshlayerrenderer.cpp
165+
testqgsnetworkaccessmanager.cpp
165166
testqgsnetworkcontentfetcher.cpp
166167
testqgsogcutils.cpp
167168
testqgsogrutils.cpp
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/***************************************************************************
2+
testqgsnetworkaccessmanager.cpp
3+
-----------------------
4+
begin : January 2019
5+
copyright : (C) 2019 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsnetworkcontentfetcher.h"
19+
#include "qgsnetworkaccessmanager.h"
20+
#include "qgsapplication.h"
21+
#include <QObject>
22+
#include "qgstest.h"
23+
#include <QNetworkReply>
24+
25+
class TestQgsNetworkAccessManager : public QObject
26+
{
27+
Q_OBJECT
28+
public:
29+
TestQgsNetworkAccessManager() = default;
30+
31+
private slots:
32+
void initTestCase();// will be called before the first testfunction is executed.
33+
void cleanupTestCase();// will be called after the last testfunction was executed.
34+
void init();// will be called before each testfunction is executed.
35+
void cleanup();// will be called after every testfunction.
36+
void fetchEmptyUrl(); //test fetching blank url
37+
void fetchBadUrl(); //test fetching bad url
38+
void fetchEncodedContent(); //test fetching url content encoded as utf-8
39+
40+
};
41+
42+
void TestQgsNetworkAccessManager::initTestCase()
43+
{
44+
QgsApplication::init();
45+
QgsApplication::initQgis();
46+
}
47+
48+
void TestQgsNetworkAccessManager::cleanupTestCase()
49+
{
50+
QgsApplication::exitQgis();
51+
}
52+
53+
void TestQgsNetworkAccessManager::init()
54+
{
55+
}
56+
57+
void TestQgsNetworkAccessManager::cleanup()
58+
{
59+
}
60+
61+
void TestQgsNetworkAccessManager::fetchEmptyUrl()
62+
{
63+
QObject context;
64+
//test fetching from a blank url
65+
bool loaded = false;
66+
bool gotRequestAboutToBeCreatedSignal = false;
67+
int requestId = -1;
68+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( QgsNetworkRequestParameters params )
69+
{
70+
gotRequestAboutToBeCreatedSignal = true;
71+
requestId = params.requestId();
72+
QVERIFY( requestId > 0 );
73+
QCOMPARE( params.operation(), QNetworkAccessManager::GetOperation );
74+
QCOMPARE( params.request().url(), QUrl() );
75+
} );
76+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ), &context, [&]( QgsNetworkReplyContent reply )
77+
{
78+
QCOMPARE( reply.errorString(), QStringLiteral( "Protocol \"\" is unknown" ) );
79+
QCOMPARE( reply.requestId(), requestId );
80+
loaded = true;
81+
} );
82+
QgsNetworkAccessManager::instance()->get( QNetworkRequest( QUrl() ) );
83+
84+
while ( !loaded )
85+
{
86+
qApp->processEvents();
87+
}
88+
89+
QVERIFY( gotRequestAboutToBeCreatedSignal );
90+
}
91+
92+
void TestQgsNetworkAccessManager::fetchBadUrl()
93+
{
94+
QObject context;
95+
//test fetching from a blank url
96+
bool loaded = false;
97+
bool gotRequestAboutToBeCreatedSignal = false;
98+
int requestId = -1;
99+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( QgsNetworkRequestParameters params )
100+
{
101+
gotRequestAboutToBeCreatedSignal = true;
102+
requestId = params.requestId();
103+
QVERIFY( requestId > 0 );
104+
QCOMPARE( params.operation(), QNetworkAccessManager::GetOperation );
105+
QCOMPARE( params.request().url(), QUrl( QStringLiteral( "http://x" ) ) );
106+
} );
107+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ), &context, [&]( QgsNetworkReplyContent reply )
108+
{
109+
QCOMPARE( reply.errorString(), QStringLiteral( "Host x not found" ) );
110+
QCOMPARE( reply.requestId(), requestId );
111+
loaded = true;
112+
} );
113+
QgsNetworkAccessManager::instance()->get( QNetworkRequest( QUrl( QStringLiteral( "http://x" ) ) ) );
114+
115+
while ( !loaded )
116+
{
117+
qApp->processEvents();
118+
}
119+
120+
QVERIFY( gotRequestAboutToBeCreatedSignal );
121+
}
122+
123+
124+
void TestQgsNetworkAccessManager::fetchEncodedContent()
125+
{
126+
QObject context;
127+
//test fetching from a blank url
128+
bool loaded = false;
129+
bool gotRequestAboutToBeCreatedSignal = false;
130+
int requestId = -1;
131+
QUrl u = QUrl::fromLocalFile( QStringLiteral( TEST_DATA_DIR ) + '/' + "encoded_html.html" );
132+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( QgsNetworkRequestParameters params )
133+
{
134+
gotRequestAboutToBeCreatedSignal = true;
135+
requestId = params.requestId();
136+
QVERIFY( requestId > 0 );
137+
QCOMPARE( params.operation(), QNetworkAccessManager::GetOperation );
138+
QCOMPARE( params.request().url(), u );
139+
} );
140+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ), &context, [&]( QgsNetworkReplyContent reply )
141+
{
142+
QCOMPARE( reply.error(), QNetworkReply::NoError );
143+
QCOMPARE( reply.requestId(), requestId );
144+
QVERIFY( reply.rawHeaderList().contains( "Content-Length" ) );
145+
loaded = true;
146+
} );
147+
QgsNetworkAccessManager::instance()->get( QNetworkRequest( u ) );
148+
149+
while ( !loaded )
150+
{
151+
qApp->processEvents();
152+
}
153+
154+
QVERIFY( gotRequestAboutToBeCreatedSignal );
155+
}
156+
157+
QGSTEST_MAIN( TestQgsNetworkAccessManager )
158+
#include "testqgsnetworkaccessmanager.moc"

0 commit comments

Comments
 (0)