2020-09-10 20:55:25 +02:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-09-10 20:55:25 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import json
|
2022-05-11 13:25:13 +02:00
|
|
|
from http import HTTPStatus
|
2020-09-10 20:55:25 +02:00
|
|
|
from io import BytesIO
|
2023-02-07 01:20:04 +01:00
|
|
|
from typing import Tuple, Union
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
2020-09-10 20:55:25 +02:00
|
|
|
|
2022-05-11 13:25:13 +02:00
|
|
|
from synapse.api.errors import Codes, SynapseError
|
2020-09-10 20:55:25 +02:00
|
|
|
from synapse.http.servlet import (
|
2022-05-11 13:25:13 +02:00
|
|
|
RestServlet,
|
2020-09-10 20:55:25 +02:00
|
|
|
parse_json_object_from_request,
|
|
|
|
parse_json_value_from_request,
|
|
|
|
)
|
2022-05-11 13:25:13 +02:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.rest.client._base import client_patterns
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.types import JsonDict
|
2022-08-31 13:16:05 +02:00
|
|
|
from synapse.util.cancellation import cancellable
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
from tests import unittest
|
2022-06-07 19:17:32 +02:00
|
|
|
from tests.http.server._base import test_disconnect
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def make_request(content: Union[bytes, JsonDict]) -> Mock:
|
2020-09-10 20:55:25 +02:00
|
|
|
"""Make an object that acts enough like a request."""
|
2022-10-07 18:39:45 +02:00
|
|
|
request = Mock(spec=["method", "uri", "content"])
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
if isinstance(content, dict):
|
|
|
|
content = json.dumps(content).encode("utf8")
|
|
|
|
|
2022-10-07 18:39:45 +02:00
|
|
|
request.method = bytes("STUB_METHOD", "ascii")
|
|
|
|
request.uri = bytes("/test_stub_uri", "ascii")
|
2020-09-10 20:55:25 +02:00
|
|
|
request.content = BytesIO(content)
|
|
|
|
return request
|
|
|
|
|
|
|
|
|
|
|
|
class TestServletUtils(unittest.TestCase):
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_parse_json_value(self) -> None:
|
2020-09-10 20:55:25 +02:00
|
|
|
"""Basic tests for parse_json_value_from_request."""
|
|
|
|
# Test round-tripping.
|
|
|
|
obj = {"foo": 1}
|
2022-05-23 13:23:26 +02:00
|
|
|
result1 = parse_json_value_from_request(make_request(obj))
|
|
|
|
self.assertEqual(result1, obj)
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
# Results don't have to be objects.
|
2022-05-23 13:23:26 +02:00
|
|
|
result2 = parse_json_value_from_request(make_request(b'["foo"]'))
|
|
|
|
self.assertEqual(result2, ["foo"])
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
# Test empty.
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
parse_json_value_from_request(make_request(b""))
|
|
|
|
|
2022-05-23 13:23:26 +02:00
|
|
|
result3 = parse_json_value_from_request(
|
|
|
|
make_request(b""), allow_empty_body=True
|
|
|
|
)
|
|
|
|
self.assertIsNone(result3)
|
2020-09-10 20:55:25 +02:00
|
|
|
|
|
|
|
# Invalid UTF-8.
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
parse_json_value_from_request(make_request(b"\xFF\x00"))
|
|
|
|
|
|
|
|
# Invalid JSON.
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
parse_json_value_from_request(make_request(b"foo"))
|
|
|
|
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
parse_json_value_from_request(make_request(b'{"foo": Infinity}'))
|
|
|
|
|
2023-02-07 01:20:04 +01:00
|
|
|
def test_parse_json_object(self) -> None:
|
2020-09-10 20:55:25 +02:00
|
|
|
"""Basic tests for parse_json_object_from_request."""
|
|
|
|
# Test empty.
|
|
|
|
result = parse_json_object_from_request(
|
|
|
|
make_request(b""), allow_empty_body=True
|
|
|
|
)
|
|
|
|
self.assertEqual(result, {})
|
|
|
|
|
|
|
|
# Test not an object
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
parse_json_object_from_request(make_request(b'["foo"]'))
|
2022-05-11 13:25:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CancellableRestServlet(RestServlet):
|
|
|
|
"""A `RestServlet` with a mix of cancellable and uncancellable handlers."""
|
|
|
|
|
|
|
|
PATTERNS = client_patterns("/sleep$")
|
|
|
|
|
|
|
|
def __init__(self, hs: HomeServer):
|
|
|
|
super().__init__()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
|
|
|
@cancellable
|
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, {"result": True}
|
|
|
|
|
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, {"result": True}
|
|
|
|
|
|
|
|
|
2022-06-07 19:17:32 +02:00
|
|
|
class TestRestServletCancellation(unittest.HomeserverTestCase):
|
2022-05-11 13:25:13 +02:00
|
|
|
"""Tests for `RestServlet` cancellation."""
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
lambda hs, http_server: CancellableRestServlet(hs).register(http_server)
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_cancellable_disconnect(self) -> None:
|
|
|
|
"""Test that handlers with the `@cancellable` flag can be cancelled."""
|
|
|
|
channel = self.make_request("GET", "/sleep", await_result=False)
|
2022-06-07 19:17:32 +02:00
|
|
|
test_disconnect(
|
2022-05-11 13:25:13 +02:00
|
|
|
self.reactor,
|
|
|
|
channel,
|
|
|
|
expect_cancellation=True,
|
|
|
|
expected_body={"error": "Request cancelled", "errcode": Codes.UNKNOWN},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_uncancellable_disconnect(self) -> None:
|
|
|
|
"""Test that handlers without the `@cancellable` flag cannot be cancelled."""
|
|
|
|
channel = self.make_request("POST", "/sleep", await_result=False)
|
2022-06-07 19:17:32 +02:00
|
|
|
test_disconnect(
|
2022-05-11 13:25:13 +02:00
|
|
|
self.reactor,
|
|
|
|
channel,
|
|
|
|
expect_cancellation=False,
|
|
|
|
expected_body={"result": True},
|
|
|
|
)
|