|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2020 The Matrix.org Foundation C.I.C. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import json |
| 16 | +from io import BytesIO |
| 17 | + |
| 18 | +from mock import Mock |
| 19 | + |
| 20 | +from synapse.api.errors import SynapseError |
| 21 | +from synapse.http.servlet import ( |
| 22 | + parse_json_object_from_request, |
| 23 | + parse_json_value_from_request, |
| 24 | +) |
| 25 | + |
| 26 | +from tests import unittest |
| 27 | + |
| 28 | + |
| 29 | +def make_request(content): |
| 30 | + """Make an object that acts enough like a request.""" |
| 31 | + request = Mock(spec=["content"]) |
| 32 | + |
| 33 | + if isinstance(content, dict): |
| 34 | + content = json.dumps(content).encode("utf8") |
| 35 | + |
| 36 | + request.content = BytesIO(content) |
| 37 | + return request |
| 38 | + |
| 39 | + |
| 40 | +class TestServletUtils(unittest.TestCase): |
| 41 | + def test_parse_json_value(self): |
| 42 | + """Basic tests for parse_json_value_from_request.""" |
| 43 | + # Test round-tripping. |
| 44 | + obj = {"foo": 1} |
| 45 | + result = parse_json_value_from_request(make_request(obj)) |
| 46 | + self.assertEqual(result, obj) |
| 47 | + |
| 48 | + # Results don't have to be objects. |
| 49 | + result = parse_json_value_from_request(make_request(b'["foo"]')) |
| 50 | + self.assertEqual(result, ["foo"]) |
| 51 | + |
| 52 | + # Test empty. |
| 53 | + with self.assertRaises(SynapseError): |
| 54 | + parse_json_value_from_request(make_request(b"")) |
| 55 | + |
| 56 | + result = parse_json_value_from_request(make_request(b""), allow_empty_body=True) |
| 57 | + self.assertIsNone(result) |
| 58 | + |
| 59 | + # Invalid UTF-8. |
| 60 | + with self.assertRaises(SynapseError): |
| 61 | + parse_json_value_from_request(make_request(b"\xFF\x00")) |
| 62 | + |
| 63 | + # Invalid JSON. |
| 64 | + with self.assertRaises(SynapseError): |
| 65 | + parse_json_value_from_request(make_request(b"foo")) |
| 66 | + |
| 67 | + with self.assertRaises(SynapseError): |
| 68 | + parse_json_value_from_request(make_request(b'{"foo": Infinity}')) |
| 69 | + |
| 70 | + def test_parse_json_object(self): |
| 71 | + """Basic tests for parse_json_object_from_request.""" |
| 72 | + # Test empty. |
| 73 | + result = parse_json_object_from_request( |
| 74 | + make_request(b""), allow_empty_body=True |
| 75 | + ) |
| 76 | + self.assertEqual(result, {}) |
| 77 | + |
| 78 | + # Test not an object |
| 79 | + with self.assertRaises(SynapseError): |
| 80 | + parse_json_object_from_request(make_request(b'["foo"]')) |
0 commit comments