Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c62a255
commit 3c71405
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsAction. | ||
From build dir, run: ctest -R PyQgsAction -V | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Alessandro Pasotti' | ||
__date__ = '24/11/2021' | ||
__copyright__ = 'Copyright 2021, The QGIS Project' | ||
|
||
import qgis # NOQA switch sip api | ||
|
||
from qgis.core import ( | ||
QgsExpressionContext, | ||
QgsAction, | ||
QgsNetworkAccessManager, | ||
QgsNetworkRequestParameters, | ||
QgsApplication, | ||
) | ||
|
||
from qgis.PyQt.QtCore import QTemporaryDir | ||
|
||
from qgis.testing import start_app, unittest | ||
|
||
import os | ||
import re | ||
import time | ||
import platform | ||
from functools import partial | ||
|
||
start_app() | ||
|
||
|
||
class TestQgsAction(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.body = None | ||
|
||
def _req_logger(self, params): | ||
self.body = bytes(params.content()) | ||
|
||
def test_post_urlencoded_action(self): | ||
"""Test form www urlencoded""" | ||
|
||
def _req_logger(self, params): | ||
self.body = bytes(params.content()) | ||
|
||
QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self)) | ||
|
||
temp_dir = QTemporaryDir() | ||
temp_path = temp_dir.path() | ||
temp_file = os.path.join(temp_path, 'urlencoded.txt') | ||
|
||
action = QgsAction(QgsAction.SubmitUrlEncoded, 'url_encoded', "http://fake_qgis_http_endpoint" + temp_file + r"?[% url_encode(map('a&+b', 'a and plus b', 'a=b', 'a equals b')) %]") | ||
ctx = QgsExpressionContext() | ||
action.run(ctx) | ||
|
||
while not self.body: | ||
QgsApplication.instance().processEvents() | ||
|
||
self.assertEqual(self.body, br"a%26%2Bb=a%20and%20plus%20b&a%3Db=a%20equals%20b") | ||
|
||
def test_post_multipart_action(self): | ||
"""Test multipart""" | ||
|
||
self.body = None | ||
|
||
def _req_logger(self, params): | ||
self.body = bytes(params.content()) | ||
|
||
QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self)) | ||
|
||
temp_dir = QTemporaryDir() | ||
temp_path = temp_dir.path() | ||
temp_file = os.path.join(temp_path, 'multipart.txt') | ||
|
||
action = QgsAction(QgsAction.SubmitUrlMultipart, 'url_encoded', "http://fake_qgis_http_endpoint" + temp_file + r"?[% url_encode(map('a&+b', 'a and plus b', 'a=b', 'a equals b')) %]") | ||
ctx = QgsExpressionContext() | ||
action.run(ctx) | ||
|
||
while not self.body: | ||
QgsApplication.instance().processEvents() | ||
|
||
self.assertEqual(re.sub(r'\.oOo\.[^\r]*', '.oOo.UUID', self.body.decode('utf8')), '\r\n'.join([ | ||
'--boundary_.oOo.UUID', | ||
'Content-Disposition: form-data; name="a&+b"', | ||
'', | ||
'a and plus b', | ||
'--boundary_.oOo.UUID', | ||
'Content-Disposition: form-data; name="a=b"', | ||
'', | ||
'a equals b', | ||
'--boundary_.oOo.UUID', | ||
''])) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |