|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsAction. |
| 3 | +
|
| 4 | +From build dir, run: ctest -R PyQgsAction -V |
| 5 | +
|
| 6 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 2 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +""" |
| 11 | +__author__ = 'Alessandro Pasotti' |
| 12 | +__date__ = '24/11/2021' |
| 13 | +__copyright__ = 'Copyright 2021, The QGIS Project' |
| 14 | + |
| 15 | +import qgis # NOQA switch sip api |
| 16 | + |
| 17 | +from qgis.core import ( |
| 18 | + QgsExpressionContext, |
| 19 | + QgsAction, |
| 20 | + QgsNetworkAccessManager, |
| 21 | + QgsNetworkRequestParameters, |
| 22 | + QgsApplication, |
| 23 | +) |
| 24 | + |
| 25 | +from qgis.PyQt.QtCore import QTemporaryDir |
| 26 | + |
| 27 | +from qgis.testing import start_app, unittest |
| 28 | + |
| 29 | +import os |
| 30 | +import re |
| 31 | +import time |
| 32 | +import platform |
| 33 | +from functools import partial |
| 34 | + |
| 35 | +start_app() |
| 36 | + |
| 37 | + |
| 38 | +class TestQgsAction(unittest.TestCase): |
| 39 | + |
| 40 | + def setUp(self): |
| 41 | + self.body = None |
| 42 | + |
| 43 | + def _req_logger(self, params): |
| 44 | + self.body = bytes(params.content()) |
| 45 | + |
| 46 | + def test_post_urlencoded_action(self): |
| 47 | + """Test form www urlencoded""" |
| 48 | + |
| 49 | + def _req_logger(self, params): |
| 50 | + self.body = bytes(params.content()) |
| 51 | + |
| 52 | + QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self)) |
| 53 | + |
| 54 | + temp_dir = QTemporaryDir() |
| 55 | + temp_path = temp_dir.path() |
| 56 | + temp_file = os.path.join(temp_path, 'urlencoded.txt') |
| 57 | + |
| 58 | + 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')) %]") |
| 59 | + ctx = QgsExpressionContext() |
| 60 | + action.run(ctx) |
| 61 | + |
| 62 | + while not self.body: |
| 63 | + QgsApplication.instance().processEvents() |
| 64 | + |
| 65 | + self.assertEqual(self.body, br"a%26%2Bb=a%20and%20plus%20b&a%3Db=a%20equals%20b") |
| 66 | + |
| 67 | + def test_post_multipart_action(self): |
| 68 | + """Test multipart""" |
| 69 | + |
| 70 | + self.body = None |
| 71 | + |
| 72 | + def _req_logger(self, params): |
| 73 | + self.body = bytes(params.content()) |
| 74 | + |
| 75 | + QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self)) |
| 76 | + |
| 77 | + temp_dir = QTemporaryDir() |
| 78 | + temp_path = temp_dir.path() |
| 79 | + temp_file = os.path.join(temp_path, 'multipart.txt') |
| 80 | + |
| 81 | + 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')) %]") |
| 82 | + ctx = QgsExpressionContext() |
| 83 | + action.run(ctx) |
| 84 | + |
| 85 | + while not self.body: |
| 86 | + QgsApplication.instance().processEvents() |
| 87 | + |
| 88 | + self.assertEqual(re.sub(r'\.oOo\.[^\r]*', '.oOo.UUID', self.body.decode('utf8')), '\r\n'.join([ |
| 89 | + '--boundary_.oOo.UUID', |
| 90 | + 'Content-Disposition: form-data; name="a&+b"', |
| 91 | + '', |
| 92 | + 'a and plus b', |
| 93 | + '--boundary_.oOo.UUID', |
| 94 | + 'Content-Disposition: form-data; name="a=b"', |
| 95 | + '', |
| 96 | + 'a equals b', |
| 97 | + '--boundary_.oOo.UUID', |
| 98 | + ''])) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments