|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import decimal
|
| 6 | +import os |
6 | 7 | import unittest
|
7 |
| - |
| 8 | +from parameterized import parameterized |
8 | 9 | from awslambdaric.lambda_runtime_marshaller import to_json
|
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestLambdaRuntimeMarshaller(unittest.TestCase):
|
| 13 | + execution_envs = ( |
| 14 | + "AWS_Lambda_python3.12", |
| 15 | + "AWS_Lambda_python3.11", |
| 16 | + "AWS_Lambda_python3.10", |
| 17 | + "AWS_Lambda_python3.9", |
| 18 | + ) |
| 19 | + |
| 20 | + envs_lambda_marshaller_ensure_ascii_false = {"AWS_Lambda_python3.12"} |
| 21 | + |
| 22 | + execution_envs_lambda_marshaller_ensure_ascii_true = tuple( |
| 23 | + set(execution_envs).difference(envs_lambda_marshaller_ensure_ascii_false) |
| 24 | + ) |
| 25 | + execution_envs_lambda_marshaller_ensure_ascii_false = tuple( |
| 26 | + envs_lambda_marshaller_ensure_ascii_false |
| 27 | + ) |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + self.org_os_environ = os.environ |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + os.environ = self.org_os_environ |
| 34 | + |
12 | 35 | def test_to_json_decimal_encoding(self):
|
13 | 36 | response = to_json({"pi": decimal.Decimal("3.14159")})
|
14 | 37 | self.assertEqual('{"pi": 3.14159}', response)
|
@@ -38,10 +61,22 @@ def test_json_serializer_is_not_default_json(self):
|
38 | 61 | self.assertFalse(hasattr(stock_json, "YOLO"))
|
39 | 62 | self.assertTrue(hasattr(simplejson, "YOLO"))
|
40 | 63 |
|
41 |
| - def test_to_json_unicode_encoding(self): |
| 64 | + @parameterized.expand(execution_envs_lambda_marshaller_ensure_ascii_false) |
| 65 | + def test_to_json_unicode_not_escaped_encoding(self, execution_env): |
| 66 | + os.environ = {"AWS_EXECUTION_ENV": execution_env} |
42 | 67 | response = to_json({"price": "£1.00"})
|
43 | 68 | self.assertEqual('{"price": "£1.00"}', response)
|
44 | 69 | self.assertNotEqual('{"price": "\\u00a31.00"}', response)
|
45 | 70 | self.assertEqual(
|
46 | 71 | 19, len(response.encode("utf-8"))
|
47 | 72 | ) # would be 23 bytes if a unicode escape was returned
|
| 73 | + |
| 74 | + @parameterized.expand(execution_envs_lambda_marshaller_ensure_ascii_true) |
| 75 | + def test_to_json_unicode_is_escaped_encoding(self, execution_env): |
| 76 | + os.environ = {"AWS_EXECUTION_ENV": execution_env} |
| 77 | + response = to_json({"price": "£1.00"}) |
| 78 | + self.assertEqual('{"price": "\\u00a31.00"}', response) |
| 79 | + self.assertNotEqual('{"price": "£1.00"}', response) |
| 80 | + self.assertEqual( |
| 81 | + 23, len(response.encode("utf-8")) |
| 82 | + ) # would be 19 bytes if a escaped was returned |
0 commit comments