2018-09-20 02:28:18 -04:00
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
import re
|
2022-05-11 07:24:48 -04:00
|
|
|
from http import HTTPStatus
|
|
|
|
from typing import Tuple
|
2018-09-20 02:28:18 -04:00
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
from twisted.internet.defer import Deferred
|
2018-09-20 02:28:18 -04:00
|
|
|
from twisted.web.resource import Resource
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2020-01-15 10:58:55 -05:00
|
|
|
from synapse.api.errors import Codes, RedirectException, SynapseError
|
2020-06-16 07:44:07 -04:00
|
|
|
from synapse.config.server import parse_listener_def
|
2022-05-11 07:24:48 -04:00
|
|
|
from synapse.http.server import (
|
|
|
|
DirectServeHtmlResource,
|
|
|
|
DirectServeJsonResource,
|
|
|
|
JsonResource,
|
|
|
|
OptionsResource,
|
|
|
|
cancellable,
|
|
|
|
)
|
|
|
|
from synapse.http.site import SynapseRequest, SynapseSite
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable
|
2022-05-11 07:24:48 -04:00
|
|
|
from synapse.types import JsonDict
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util import Clock
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
from tests import unittest
|
2022-05-11 07:24:48 -04:00
|
|
|
from tests.http.server._base import EndpointCancellationTestHelperMixin
|
2019-01-24 05:31:54 -05:00
|
|
|
from tests.server import (
|
2020-11-13 17:39:09 -05:00
|
|
|
FakeSite,
|
2019-01-24 05:31:54 -05:00
|
|
|
ThreadedMemoryReactorClock,
|
|
|
|
make_request,
|
|
|
|
setup_test_homeserver,
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
class JsonResourceTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2019-01-24 05:31:54 -05:00
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
2018-06-27 05:37:24 -04:00
|
|
|
self.hs_clock = Clock(self.reactor)
|
|
|
|
self.homeserver = setup_test_homeserver(
|
2020-12-02 11:09:24 -05:00
|
|
|
self.addCleanup,
|
|
|
|
federation_http_client=None,
|
|
|
|
clock=self.hs_clock,
|
|
|
|
reactor=self.reactor,
|
2018-06-27 05:37:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_handler_for_request(self):
|
|
|
|
"""
|
|
|
|
JsonResource.handler_for_request gives correctly decoded URL args to
|
|
|
|
the callback, while Twisted will give the raw bytes of URL query
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
got_kwargs = {}
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
got_kwargs.update(kwargs)
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, kwargs
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
2018-07-17 06:43:18 -04:00
|
|
|
res.register_paths(
|
2019-07-24 08:07:35 -04:00
|
|
|
"GET",
|
|
|
|
[re.compile("^/_matrix/foo/(?P<room_id>[^/]*)$")],
|
|
|
|
_callback,
|
|
|
|
"test_servlet",
|
2018-07-17 06:43:18 -04:00
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2020-12-15 09:54:41 -05:00
|
|
|
make_request(
|
2021-09-24 06:01:25 -04:00
|
|
|
self.reactor,
|
|
|
|
FakeSite(res, self.reactor),
|
|
|
|
b"GET",
|
|
|
|
b"/_matrix/foo/%E2%98%83?a=%E2%98%83",
|
2018-11-06 11:00:00 -05:00
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.assertEqual(got_kwargs, {"room_id": "\N{SNOWMAN}"})
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def test_callback_direct_exception(self):
|
|
|
|
"""
|
|
|
|
If the web callback raises an uncaught exception, it will be translated
|
|
|
|
into a 500.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
raise Exception("boo")
|
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
2019-07-24 08:07:35 -04:00
|
|
|
res.register_paths(
|
|
|
|
"GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"500")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def test_callback_indirect_exception(self):
|
|
|
|
"""
|
|
|
|
If the web callback raises an uncaught exception in a Deferred, it will
|
|
|
|
be translated into a 500.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _throw(*args):
|
|
|
|
raise Exception("boo")
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(_throw)
|
2021-09-24 06:01:25 -04:00
|
|
|
self.reactor.callLater(0.5, d.callback, True)
|
2018-11-15 17:46:51 -05:00
|
|
|
return make_deferred_yieldable(d)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
2019-07-24 08:07:35 -04:00
|
|
|
res.register_paths(
|
|
|
|
"GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"500")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def test_callback_synapseerror(self):
|
|
|
|
"""
|
|
|
|
If the web callback raises a SynapseError, it returns the appropriate
|
|
|
|
status code and message set in it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
raise SynapseError(403, "Forbidden!!one!", Codes.FORBIDDEN)
|
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
2019-07-24 08:07:35 -04:00
|
|
|
res.register_paths(
|
|
|
|
"GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403")
|
2018-08-08 22:22:01 -04:00
|
|
|
self.assertEqual(channel.json_body["error"], "Forbidden!!one!")
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def test_no_handler(self):
|
|
|
|
"""
|
|
|
|
If there is no handler to process the request, Synapse will return 400.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
"""
|
|
|
|
Not ever actually called!
|
|
|
|
"""
|
|
|
|
self.fail("shouldn't ever get here")
|
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
2019-07-24 08:07:35 -04:00
|
|
|
res.register_paths(
|
|
|
|
"GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foobar"
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"400")
|
2018-08-08 22:22:01 -04:00
|
|
|
self.assertEqual(channel.json_body["error"], "Unrecognized request")
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")
|
2018-09-20 02:28:18 -04:00
|
|
|
|
2020-08-03 08:45:42 -04:00
|
|
|
def test_head_request(self):
|
|
|
|
"""
|
|
|
|
JsonResource.handler_for_request gives correctly decoded URL args to
|
|
|
|
the callback, while Twisted will give the raw bytes of URL query
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _callback(request, **kwargs):
|
|
|
|
return 200, {"result": True}
|
|
|
|
|
|
|
|
res = JsonResource(self.homeserver)
|
|
|
|
res.register_paths(
|
2021-02-16 17:32:34 -05:00
|
|
|
"GET",
|
|
|
|
[re.compile("^/_matrix/foo$")],
|
|
|
|
_callback,
|
|
|
|
"test_servlet",
|
2020-08-03 08:45:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# The path was registered as GET, but this is a HEAD request.
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"HEAD", b"/_matrix/foo"
|
|
|
|
)
|
2020-08-03 08:45:42 -04:00
|
|
|
|
|
|
|
self.assertEqual(channel.result["code"], b"200")
|
|
|
|
self.assertNotIn("body", channel.result)
|
|
|
|
|
2018-09-20 02:28:18 -04:00
|
|
|
|
2020-05-22 09:30:07 -04:00
|
|
|
class OptionsResourceTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
|
|
|
|
class DummyResource(Resource):
|
|
|
|
isLeaf = True
|
|
|
|
|
|
|
|
def render(self, request):
|
|
|
|
return request.path
|
|
|
|
|
|
|
|
# Setup a resource with some children.
|
|
|
|
self.resource = OptionsResource()
|
|
|
|
self.resource.putChild(b"res", DummyResource())
|
|
|
|
|
|
|
|
def _make_request(self, method, path):
|
|
|
|
"""Create a request from the method/path and return a channel with the response."""
|
|
|
|
# Create a site and query for the resource.
|
2020-06-16 07:44:07 -04:00
|
|
|
site = SynapseSite(
|
|
|
|
"test",
|
|
|
|
"site_tag",
|
|
|
|
parse_listener_def({"type": "http", "port": 0}),
|
|
|
|
self.resource,
|
|
|
|
"1.0",
|
2021-04-23 14:20:44 -04:00
|
|
|
max_request_body_size=1234,
|
2021-04-23 12:06:47 -04:00
|
|
|
reactor=self.reactor,
|
2020-06-16 07:44:07 -04:00
|
|
|
)
|
2020-11-13 17:39:09 -05:00
|
|
|
|
2020-11-15 17:49:21 -05:00
|
|
|
# render the request and return the channel
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(self.reactor, site, method, path, shorthand=False)
|
2020-05-22 09:30:07 -04:00
|
|
|
return channel
|
|
|
|
|
|
|
|
def test_unknown_options_request(self):
|
2020-07-24 07:08:07 -04:00
|
|
|
"""An OPTIONS requests to an unknown URL still returns 204 No Content."""
|
2020-05-22 09:30:07 -04:00
|
|
|
channel = self._make_request(b"OPTIONS", b"/foo/")
|
2020-07-24 07:08:07 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"204")
|
|
|
|
self.assertNotIn("body", channel.result)
|
2020-05-22 09:30:07 -04:00
|
|
|
|
2020-05-22 12:42:39 -04:00
|
|
|
# Ensure the correct CORS headers have been added
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
|
|
|
|
"has CORS Origin header",
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
|
|
|
|
"has CORS Methods header",
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
|
|
|
|
"has CORS Headers header",
|
|
|
|
)
|
|
|
|
|
2020-05-22 09:30:07 -04:00
|
|
|
def test_known_options_request(self):
|
2020-07-24 07:08:07 -04:00
|
|
|
"""An OPTIONS requests to an known URL still returns 204 No Content."""
|
2020-05-22 09:30:07 -04:00
|
|
|
channel = self._make_request(b"OPTIONS", b"/res/")
|
2020-07-24 07:08:07 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"204")
|
|
|
|
self.assertNotIn("body", channel.result)
|
2020-05-22 09:30:07 -04:00
|
|
|
|
2020-05-22 12:42:39 -04:00
|
|
|
# Ensure the correct CORS headers have been added
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
|
|
|
|
"has CORS Origin header",
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
|
|
|
|
"has CORS Methods header",
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
|
|
|
|
"has CORS Headers header",
|
|
|
|
)
|
|
|
|
|
2020-05-22 09:30:07 -04:00
|
|
|
def test_unknown_request(self):
|
|
|
|
"""A non-OPTIONS request to an unknown URL should 404."""
|
|
|
|
channel = self._make_request(b"GET", b"/foo/")
|
|
|
|
self.assertEqual(channel.result["code"], b"404")
|
|
|
|
|
|
|
|
def test_known_request(self):
|
|
|
|
"""A non-OPTIONS request to an known URL should query the proper resource."""
|
|
|
|
channel = self._make_request(b"GET", b"/res/")
|
|
|
|
self.assertEqual(channel.result["code"], b"200")
|
|
|
|
self.assertEqual(channel.result["body"], b"/res/")
|
|
|
|
|
|
|
|
|
2020-01-15 10:58:55 -05:00
|
|
|
class WrapHtmlRequestHandlerTests(unittest.TestCase):
|
2020-07-03 14:02:19 -04:00
|
|
|
class TestResource(DirectServeHtmlResource):
|
2020-01-15 10:58:55 -05:00
|
|
|
callback = None
|
|
|
|
|
|
|
|
async def _async_render_GET(self, request):
|
2020-07-03 14:02:19 -04:00
|
|
|
await self.callback(request)
|
2020-01-15 10:58:55 -05:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
|
|
|
|
def test_good_response(self):
|
2020-08-03 08:45:42 -04:00
|
|
|
async def callback(request):
|
2020-01-15 10:58:55 -05:00
|
|
|
request.write(b"response")
|
|
|
|
request.finish()
|
|
|
|
|
|
|
|
res = WrapHtmlRequestHandlerTests.TestResource()
|
|
|
|
res.callback = callback
|
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
|
|
|
|
)
|
2020-01-15 10:58:55 -05:00
|
|
|
|
|
|
|
self.assertEqual(channel.result["code"], b"200")
|
|
|
|
body = channel.result["body"]
|
|
|
|
self.assertEqual(body, b"response")
|
|
|
|
|
|
|
|
def test_redirect_exception(self):
|
|
|
|
"""
|
|
|
|
If the callback raises a RedirectException, it is turned into a 30x
|
|
|
|
with the right location.
|
|
|
|
"""
|
|
|
|
|
2020-08-03 08:45:42 -04:00
|
|
|
async def callback(request, **kwargs):
|
2020-01-15 10:58:55 -05:00
|
|
|
raise RedirectException(b"/look/an/eagle", 301)
|
|
|
|
|
|
|
|
res = WrapHtmlRequestHandlerTests.TestResource()
|
|
|
|
res.callback = callback
|
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
|
|
|
|
)
|
2020-01-15 10:58:55 -05:00
|
|
|
|
|
|
|
self.assertEqual(channel.result["code"], b"301")
|
|
|
|
headers = channel.result["headers"]
|
|
|
|
location_headers = [v for k, v in headers if k == b"Location"]
|
|
|
|
self.assertEqual(location_headers, [b"/look/an/eagle"])
|
|
|
|
|
|
|
|
def test_redirect_exception_with_cookie(self):
|
|
|
|
"""
|
|
|
|
If the callback raises a RedirectException which sets a cookie, that is
|
|
|
|
returned too
|
|
|
|
"""
|
|
|
|
|
2020-08-03 08:45:42 -04:00
|
|
|
async def callback(request, **kwargs):
|
2020-01-15 10:58:55 -05:00
|
|
|
e = RedirectException(b"/no/over/there", 304)
|
|
|
|
e.cookies.append(b"session=yespls")
|
|
|
|
raise e
|
|
|
|
|
|
|
|
res = WrapHtmlRequestHandlerTests.TestResource()
|
|
|
|
res.callback = callback
|
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
|
|
|
|
)
|
2020-01-15 10:58:55 -05:00
|
|
|
|
|
|
|
self.assertEqual(channel.result["code"], b"304")
|
|
|
|
headers = channel.result["headers"]
|
|
|
|
location_headers = [v for k, v in headers if k == b"Location"]
|
|
|
|
self.assertEqual(location_headers, [b"/no/over/there"])
|
|
|
|
cookies_headers = [v for k, v in headers if k == b"Set-Cookie"]
|
|
|
|
self.assertEqual(cookies_headers, [b"session=yespls"])
|
2020-08-03 08:45:42 -04:00
|
|
|
|
|
|
|
def test_head_request(self):
|
|
|
|
"""A head request should work by being turned into a GET request."""
|
|
|
|
|
|
|
|
async def callback(request):
|
|
|
|
request.write(b"response")
|
|
|
|
request.finish()
|
|
|
|
|
|
|
|
res = WrapHtmlRequestHandlerTests.TestResource()
|
|
|
|
res.callback = callback
|
|
|
|
|
2021-09-24 06:01:25 -04:00
|
|
|
channel = make_request(
|
|
|
|
self.reactor, FakeSite(res, self.reactor), b"HEAD", b"/path"
|
|
|
|
)
|
2020-08-03 08:45:42 -04:00
|
|
|
|
|
|
|
self.assertEqual(channel.result["code"], b"200")
|
|
|
|
self.assertNotIn("body", channel.result)
|
2022-05-11 07:24:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CancellableDirectServeJsonResource(DirectServeJsonResource):
|
|
|
|
def __init__(self, clock: Clock):
|
|
|
|
super().__init__()
|
|
|
|
self.clock = clock
|
|
|
|
|
|
|
|
@cancellable
|
|
|
|
async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, {"result": True}
|
|
|
|
|
|
|
|
async def _async_render_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, {"result": True}
|
|
|
|
|
|
|
|
|
|
|
|
class CancellableDirectServeHtmlResource(DirectServeHtmlResource):
|
|
|
|
ERROR_TEMPLATE = "{code} {msg}"
|
|
|
|
|
|
|
|
def __init__(self, clock: Clock):
|
|
|
|
super().__init__()
|
|
|
|
self.clock = clock
|
|
|
|
|
|
|
|
@cancellable
|
|
|
|
async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, bytes]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, b"ok"
|
|
|
|
|
|
|
|
async def _async_render_POST(self, request: SynapseRequest) -> Tuple[int, bytes]:
|
|
|
|
await self.clock.sleep(1.0)
|
|
|
|
return HTTPStatus.OK, b"ok"
|
|
|
|
|
|
|
|
|
|
|
|
class DirectServeJsonResourceCancellationTests(EndpointCancellationTestHelperMixin):
|
|
|
|
"""Tests for `DirectServeJsonResource` cancellation."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
self.clock = Clock(self.reactor)
|
|
|
|
self.resource = CancellableDirectServeJsonResource(self.clock)
|
|
|
|
self.site = FakeSite(self.resource, self.reactor)
|
|
|
|
|
|
|
|
def test_cancellable_disconnect(self) -> None:
|
|
|
|
"""Test that handlers with the `@cancellable` flag can be cancelled."""
|
|
|
|
channel = make_request(
|
|
|
|
self.reactor, self.site, "GET", "/sleep", await_result=False
|
|
|
|
)
|
|
|
|
self._test_disconnect(
|
|
|
|
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 = make_request(
|
|
|
|
self.reactor, self.site, "POST", "/sleep", await_result=False
|
|
|
|
)
|
|
|
|
self._test_disconnect(
|
|
|
|
self.reactor,
|
|
|
|
channel,
|
|
|
|
expect_cancellation=False,
|
|
|
|
expected_body={"result": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DirectServeHtmlResourceCancellationTests(EndpointCancellationTestHelperMixin):
|
|
|
|
"""Tests for `DirectServeHtmlResource` cancellation."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
self.clock = Clock(self.reactor)
|
|
|
|
self.resource = CancellableDirectServeHtmlResource(self.clock)
|
|
|
|
self.site = FakeSite(self.resource, self.reactor)
|
|
|
|
|
|
|
|
def test_cancellable_disconnect(self) -> None:
|
|
|
|
"""Test that handlers with the `@cancellable` flag can be cancelled."""
|
|
|
|
channel = make_request(
|
|
|
|
self.reactor, self.site, "GET", "/sleep", await_result=False
|
|
|
|
)
|
|
|
|
self._test_disconnect(
|
|
|
|
self.reactor,
|
|
|
|
channel,
|
|
|
|
expect_cancellation=True,
|
|
|
|
expected_body=b"499 Request cancelled",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_uncancellable_disconnect(self) -> None:
|
|
|
|
"""Test that handlers without the `@cancellable` flag cannot be cancelled."""
|
|
|
|
channel = make_request(
|
|
|
|
self.reactor, self.site, "POST", "/sleep", await_result=False
|
|
|
|
)
|
|
|
|
self._test_disconnect(
|
|
|
|
self.reactor, channel, expect_cancellation=False, expected_body=b"ok"
|
|
|
|
)
|