|
| 1 | +""" |
| 2 | +Script evaluation tests. |
| 3 | +""" |
| 4 | + |
| 5 | +from . import integ_test_base |
| 6 | +import json |
| 7 | +import gzip |
| 8 | +import os |
| 9 | +import requests |
| 10 | +import string |
| 11 | + |
| 12 | + |
| 13 | +class TestMaxRequestSize(integ_test_base.IntegTestBase): |
| 14 | + def _get_config_file_name(self) -> str: |
| 15 | + """ |
| 16 | + Generates config file. Overwrite this function for tests to |
| 17 | + run against not default state file. |
| 18 | +
|
| 19 | + Returns |
| 20 | + ------- |
| 21 | + str |
| 22 | + Absolute path to config file. |
| 23 | + """ |
| 24 | + config_file = open(os.path.join(self.tmp_dir, "test.conf"), "w+") |
| 25 | + config_file.write( |
| 26 | + "[TabPy]\n" |
| 27 | + f"TABPY_QUERY_OBJECT_PATH = {self.tmp_dir}/query_objects\n" |
| 28 | + f"TABPY_PORT = {self._get_port()}\n" |
| 29 | + f"TABPY_GZIP_ENABLE = TRUE\n" |
| 30 | + f"TABPY_STATE_PATH = {self.tmp_dir}\n" |
| 31 | + "TABPY_MAX_REQUEST_SIZE_MB = 1\n" |
| 32 | + ) |
| 33 | + |
| 34 | + pwd_file = self._get_pwd_file() |
| 35 | + if pwd_file is not None: |
| 36 | + pwd_file = os.path.abspath(pwd_file) |
| 37 | + config_file.write(f"TABPY_PWD_FILE = {pwd_file}\n") |
| 38 | + |
| 39 | + transfer_protocol = self._get_transfer_protocol() |
| 40 | + if transfer_protocol is not None: |
| 41 | + config_file.write(f"TABPY_TRANSFER_PROTOCOL = {transfer_protocol}\n") |
| 42 | + |
| 43 | + cert_file_name = self._get_certificate_file_name() |
| 44 | + if cert_file_name is not None: |
| 45 | + cert_file_name = os.path.abspath(cert_file_name) |
| 46 | + config_file.write(f"TABPY_CERTIFICATE_FILE = {cert_file_name}\n") |
| 47 | + |
| 48 | + key_file_name = self._get_key_file_name() |
| 49 | + if key_file_name is not None: |
| 50 | + key_file_name = os.path.abspath(key_file_name) |
| 51 | + config_file.write(f"TABPY_KEY_FILE = {key_file_name}\n") |
| 52 | + |
| 53 | + evaluate_timeout = self._get_evaluate_timeout() |
| 54 | + if evaluate_timeout is not None: |
| 55 | + config_file.write(f"TABPY_EVALUATE_TIMEOUT = {evaluate_timeout}\n") |
| 56 | + |
| 57 | + config_file.close() |
| 58 | + |
| 59 | + self.delete_config_file = True |
| 60 | + return config_file.name |
| 61 | + |
| 62 | + def test_payload_exceeds_max_request_size_evaluate(self): |
| 63 | + size_mb = 2 |
| 64 | + num_chars = size_mb * 1024 * 1024 |
| 65 | + large_string = string.printable * (num_chars // len(string.printable)) |
| 66 | + large_string += string.printable[:num_chars % len(string.printable)] |
| 67 | + |
| 68 | + payload = { |
| 69 | + "data": { "_arg1": large_string }, |
| 70 | + "script": "return _arg1" |
| 71 | + } |
| 72 | + headers = { |
| 73 | + "Content-Type": "application/json", |
| 74 | + } |
| 75 | + url = self._get_url() + "/evaluate" |
| 76 | + response = requests.request("POST", url, data=json.dumps(payload).encode('utf-8'), |
| 77 | + headers=headers) |
| 78 | + result = json.loads(response.text) |
| 79 | + self.assertEqual(413, response.status_code) |
| 80 | + |
0 commit comments