mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
Allow access to content of POST requests in QgsNetworkReplyContent
This commit is contained in:
parent
8120e4a54f
commit
009ee572d8
@ -30,7 +30,8 @@ Default constructor.
|
||||
|
||||
QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
|
||||
const QNetworkRequest &request,
|
||||
int requestId );
|
||||
int requestId,
|
||||
const QByteArray &content = QByteArray() );
|
||||
%Docstring
|
||||
Constructor for QgsNetworkRequestParameters, with the specified network
|
||||
``operation`` and original ``request``.
|
||||
@ -57,6 +58,12 @@ Returns a string identifying the thread which the request originated from.
|
||||
int requestId() const;
|
||||
%Docstring
|
||||
Returns a unique ID identifying the request.
|
||||
%End
|
||||
|
||||
QByteArray content() const;
|
||||
%Docstring
|
||||
Returns the request's content. This is only used for POST or PUT operation
|
||||
requests.
|
||||
%End
|
||||
|
||||
};
|
||||
|
@ -32,6 +32,7 @@
|
||||
|
||||
#include <QUrl>
|
||||
#include <QTimer>
|
||||
#include <QBuffer>
|
||||
#include <QNetworkReply>
|
||||
#include <QThreadStorage>
|
||||
#include <QAuthenticator>
|
||||
@ -206,8 +207,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
|
||||
|
||||
static QAtomicInt sRequestId = 0;
|
||||
const int requestId = ++sRequestId;
|
||||
QByteArray content;
|
||||
if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
|
||||
{
|
||||
content = buffer->buffer();
|
||||
}
|
||||
|
||||
emit requestAboutToBeCreated( QgsNetworkRequestParameters( op, req, requestId ) );
|
||||
emit requestAboutToBeCreated( QgsNetworkRequestParameters( op, req, requestId, content ) );
|
||||
Q_NOWARN_DEPRECATED_PUSH
|
||||
emit requestAboutToBeCreated( op, req, outgoingData );
|
||||
Q_NOWARN_DEPRECATED_POP
|
||||
@ -423,10 +429,11 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
|
||||
// QgsNetworkRequestParameters
|
||||
//
|
||||
|
||||
QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation, const QNetworkRequest &request, int requestId )
|
||||
QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation, const QNetworkRequest &request, int requestId, const QByteArray &content )
|
||||
: mOperation( operation )
|
||||
, mRequest( request )
|
||||
, mOriginatingThreadId( QStringLiteral( "0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) ) )
|
||||
, mRequestId( requestId )
|
||||
, mContent( content )
|
||||
{
|
||||
}
|
||||
|
@ -50,7 +50,8 @@ class CORE_EXPORT QgsNetworkRequestParameters
|
||||
*/
|
||||
QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
|
||||
const QNetworkRequest &request,
|
||||
int requestId );
|
||||
int requestId,
|
||||
const QByteArray &content = QByteArray() );
|
||||
|
||||
/**
|
||||
* Returns the request operation, e.g. GET or POST.
|
||||
@ -75,12 +76,19 @@ class CORE_EXPORT QgsNetworkRequestParameters
|
||||
*/
|
||||
int requestId() const { return mRequestId; }
|
||||
|
||||
/**
|
||||
* Returns the request's content. This is only used for POST or PUT operation
|
||||
* requests.
|
||||
*/
|
||||
QByteArray content() const { return mContent; }
|
||||
|
||||
private:
|
||||
|
||||
QNetworkAccessManager::Operation mOperation;
|
||||
QNetworkRequest mRequest;
|
||||
QString mOriginatingThreadId;
|
||||
int mRequestId = 0;
|
||||
QByteArray mContent;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -59,6 +59,7 @@ class TestQgsNetworkAccessManager : public QObject
|
||||
void fetchEmptyUrl(); //test fetching blank url
|
||||
void fetchBadUrl(); //test fetching bad url
|
||||
void fetchEncodedContent(); //test fetching url content encoded as utf-8
|
||||
void fetchPost();
|
||||
void fetchBadSsl();
|
||||
void fetchTimeout();
|
||||
|
||||
@ -235,6 +236,44 @@ void TestQgsNetworkAccessManager::fetchEncodedContent()
|
||||
thread->deleteLater();
|
||||
}
|
||||
|
||||
void TestQgsNetworkAccessManager::fetchPost()
|
||||
{
|
||||
if ( QgsTest::isTravis() )
|
||||
QSKIP( "This test is disabled on Travis CI environment" );
|
||||
|
||||
QObject context;
|
||||
//test fetching from a blank url
|
||||
bool loaded = false;
|
||||
bool gotRequestAboutToBeCreatedSignal = false;
|
||||
int requestId = -1;
|
||||
QUrl u = QUrl::fromLocalFile( QStringLiteral( TEST_DATA_DIR ) + '/' + "encoded_html.html" );
|
||||
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( const QgsNetworkRequestParameters & params )
|
||||
{
|
||||
gotRequestAboutToBeCreatedSignal = true;
|
||||
requestId = params.requestId();
|
||||
QVERIFY( requestId > 0 );
|
||||
QCOMPARE( params.operation(), QNetworkAccessManager::GetOperation );
|
||||
QCOMPARE( params.request().url(), u );
|
||||
QCOMPARE( params.content(), QByteArray( "a=b&c=d" ) );
|
||||
} );
|
||||
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ), &context, [&]( const QgsNetworkReplyContent & reply )
|
||||
{
|
||||
QCOMPARE( reply.error(), QNetworkReply::NoError );
|
||||
QCOMPARE( reply.requestId(), requestId );
|
||||
QVERIFY( reply.rawHeaderList().contains( "Content-Length" ) );
|
||||
QCOMPARE( reply.request().url(), u );
|
||||
loaded = true;
|
||||
} );
|
||||
QgsNetworkAccessManager::instance()->post( QNetworkRequest( u ), QByteArray( "a=b&c=d" ) );
|
||||
|
||||
while ( !loaded )
|
||||
{
|
||||
qApp->processEvents();
|
||||
}
|
||||
|
||||
QVERIFY( gotRequestAboutToBeCreatedSignal );
|
||||
}
|
||||
|
||||
void TestQgsNetworkAccessManager::fetchBadSsl()
|
||||
{
|
||||
if ( QgsTest::isTravis() )
|
||||
|
Loading…
x
Reference in New Issue
Block a user