test_server.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. from http import HTTPStatus
  16. from typing import Tuple
  17. from twisted.internet.defer import Deferred
  18. from twisted.web.resource import Resource
  19. from synapse.api.errors import Codes, RedirectException, SynapseError
  20. from synapse.config.server import parse_listener_def
  21. from synapse.http.server import (
  22. DirectServeHtmlResource,
  23. DirectServeJsonResource,
  24. JsonResource,
  25. OptionsResource,
  26. cancellable,
  27. )
  28. from synapse.http.site import SynapseRequest, SynapseSite
  29. from synapse.logging.context import make_deferred_yieldable
  30. from synapse.types import JsonDict
  31. from synapse.util import Clock
  32. from tests import unittest
  33. from tests.http.server._base import EndpointCancellationTestHelperMixin
  34. from tests.server import (
  35. FakeSite,
  36. ThreadedMemoryReactorClock,
  37. make_request,
  38. setup_test_homeserver,
  39. )
  40. class JsonResourceTests(unittest.TestCase):
  41. def setUp(self):
  42. self.reactor = ThreadedMemoryReactorClock()
  43. self.hs_clock = Clock(self.reactor)
  44. self.homeserver = setup_test_homeserver(
  45. self.addCleanup,
  46. federation_http_client=None,
  47. clock=self.hs_clock,
  48. reactor=self.reactor,
  49. )
  50. def test_handler_for_request(self):
  51. """
  52. JsonResource.handler_for_request gives correctly decoded URL args to
  53. the callback, while Twisted will give the raw bytes of URL query
  54. arguments.
  55. """
  56. got_kwargs = {}
  57. def _callback(request, **kwargs):
  58. got_kwargs.update(kwargs)
  59. return 200, kwargs
  60. res = JsonResource(self.homeserver)
  61. res.register_paths(
  62. "GET",
  63. [re.compile("^/_matrix/foo/(?P<room_id>[^/]*)$")],
  64. _callback,
  65. "test_servlet",
  66. )
  67. make_request(
  68. self.reactor,
  69. FakeSite(res, self.reactor),
  70. b"GET",
  71. b"/_matrix/foo/%E2%98%83?a=%E2%98%83",
  72. )
  73. self.assertEqual(got_kwargs, {"room_id": "\N{SNOWMAN}"})
  74. def test_callback_direct_exception(self):
  75. """
  76. If the web callback raises an uncaught exception, it will be translated
  77. into a 500.
  78. """
  79. def _callback(request, **kwargs):
  80. raise Exception("boo")
  81. res = JsonResource(self.homeserver)
  82. res.register_paths(
  83. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  84. )
  85. channel = make_request(
  86. self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
  87. )
  88. self.assertEqual(channel.result["code"], b"500")
  89. def test_callback_indirect_exception(self):
  90. """
  91. If the web callback raises an uncaught exception in a Deferred, it will
  92. be translated into a 500.
  93. """
  94. def _throw(*args):
  95. raise Exception("boo")
  96. def _callback(request, **kwargs):
  97. d = Deferred()
  98. d.addCallback(_throw)
  99. self.reactor.callLater(0.5, d.callback, True)
  100. return make_deferred_yieldable(d)
  101. res = JsonResource(self.homeserver)
  102. res.register_paths(
  103. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  104. )
  105. channel = make_request(
  106. self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
  107. )
  108. self.assertEqual(channel.result["code"], b"500")
  109. def test_callback_synapseerror(self):
  110. """
  111. If the web callback raises a SynapseError, it returns the appropriate
  112. status code and message set in it.
  113. """
  114. def _callback(request, **kwargs):
  115. raise SynapseError(403, "Forbidden!!one!", Codes.FORBIDDEN)
  116. res = JsonResource(self.homeserver)
  117. res.register_paths(
  118. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  119. )
  120. channel = make_request(
  121. self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foo"
  122. )
  123. self.assertEqual(channel.result["code"], b"403")
  124. self.assertEqual(channel.json_body["error"], "Forbidden!!one!")
  125. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  126. def test_no_handler(self):
  127. """
  128. If there is no handler to process the request, Synapse will return 400.
  129. """
  130. def _callback(request, **kwargs):
  131. """
  132. Not ever actually called!
  133. """
  134. self.fail("shouldn't ever get here")
  135. res = JsonResource(self.homeserver)
  136. res.register_paths(
  137. "GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet"
  138. )
  139. channel = make_request(
  140. self.reactor, FakeSite(res, self.reactor), b"GET", b"/_matrix/foobar"
  141. )
  142. self.assertEqual(channel.result["code"], b"400")
  143. self.assertEqual(channel.json_body["error"], "Unrecognized request")
  144. self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")
  145. def test_head_request(self):
  146. """
  147. JsonResource.handler_for_request gives correctly decoded URL args to
  148. the callback, while Twisted will give the raw bytes of URL query
  149. arguments.
  150. """
  151. def _callback(request, **kwargs):
  152. return 200, {"result": True}
  153. res = JsonResource(self.homeserver)
  154. res.register_paths(
  155. "GET",
  156. [re.compile("^/_matrix/foo$")],
  157. _callback,
  158. "test_servlet",
  159. )
  160. # The path was registered as GET, but this is a HEAD request.
  161. channel = make_request(
  162. self.reactor, FakeSite(res, self.reactor), b"HEAD", b"/_matrix/foo"
  163. )
  164. self.assertEqual(channel.result["code"], b"200")
  165. self.assertNotIn("body", channel.result)
  166. class OptionsResourceTests(unittest.TestCase):
  167. def setUp(self):
  168. self.reactor = ThreadedMemoryReactorClock()
  169. class DummyResource(Resource):
  170. isLeaf = True
  171. def render(self, request):
  172. return request.path
  173. # Setup a resource with some children.
  174. self.resource = OptionsResource()
  175. self.resource.putChild(b"res", DummyResource())
  176. def _make_request(self, method, path):
  177. """Create a request from the method/path and return a channel with the response."""
  178. # Create a site and query for the resource.
  179. site = SynapseSite(
  180. "test",
  181. "site_tag",
  182. parse_listener_def({"type": "http", "port": 0}),
  183. self.resource,
  184. "1.0",
  185. max_request_body_size=1234,
  186. reactor=self.reactor,
  187. )
  188. # render the request and return the channel
  189. channel = make_request(self.reactor, site, method, path, shorthand=False)
  190. return channel
  191. def test_unknown_options_request(self):
  192. """An OPTIONS requests to an unknown URL still returns 204 No Content."""
  193. channel = self._make_request(b"OPTIONS", b"/foo/")
  194. self.assertEqual(channel.result["code"], b"204")
  195. self.assertNotIn("body", channel.result)
  196. # Ensure the correct CORS headers have been added
  197. self.assertTrue(
  198. channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
  199. "has CORS Origin header",
  200. )
  201. self.assertTrue(
  202. channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
  203. "has CORS Methods header",
  204. )
  205. self.assertTrue(
  206. channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
  207. "has CORS Headers header",
  208. )
  209. def test_known_options_request(self):
  210. """An OPTIONS requests to an known URL still returns 204 No Content."""
  211. channel = self._make_request(b"OPTIONS", b"/res/")
  212. self.assertEqual(channel.result["code"], b"204")
  213. self.assertNotIn("body", channel.result)
  214. # Ensure the correct CORS headers have been added
  215. self.assertTrue(
  216. channel.headers.hasHeader(b"Access-Control-Allow-Origin"),
  217. "has CORS Origin header",
  218. )
  219. self.assertTrue(
  220. channel.headers.hasHeader(b"Access-Control-Allow-Methods"),
  221. "has CORS Methods header",
  222. )
  223. self.assertTrue(
  224. channel.headers.hasHeader(b"Access-Control-Allow-Headers"),
  225. "has CORS Headers header",
  226. )
  227. def test_unknown_request(self):
  228. """A non-OPTIONS request to an unknown URL should 404."""
  229. channel = self._make_request(b"GET", b"/foo/")
  230. self.assertEqual(channel.result["code"], b"404")
  231. def test_known_request(self):
  232. """A non-OPTIONS request to an known URL should query the proper resource."""
  233. channel = self._make_request(b"GET", b"/res/")
  234. self.assertEqual(channel.result["code"], b"200")
  235. self.assertEqual(channel.result["body"], b"/res/")
  236. class WrapHtmlRequestHandlerTests(unittest.TestCase):
  237. class TestResource(DirectServeHtmlResource):
  238. callback = None
  239. async def _async_render_GET(self, request):
  240. await self.callback(request)
  241. def setUp(self):
  242. self.reactor = ThreadedMemoryReactorClock()
  243. def test_good_response(self):
  244. async def callback(request):
  245. request.write(b"response")
  246. request.finish()
  247. res = WrapHtmlRequestHandlerTests.TestResource()
  248. res.callback = callback
  249. channel = make_request(
  250. self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
  251. )
  252. self.assertEqual(channel.result["code"], b"200")
  253. body = channel.result["body"]
  254. self.assertEqual(body, b"response")
  255. def test_redirect_exception(self):
  256. """
  257. If the callback raises a RedirectException, it is turned into a 30x
  258. with the right location.
  259. """
  260. async def callback(request, **kwargs):
  261. raise RedirectException(b"/look/an/eagle", 301)
  262. res = WrapHtmlRequestHandlerTests.TestResource()
  263. res.callback = callback
  264. channel = make_request(
  265. self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
  266. )
  267. self.assertEqual(channel.result["code"], b"301")
  268. headers = channel.result["headers"]
  269. location_headers = [v for k, v in headers if k == b"Location"]
  270. self.assertEqual(location_headers, [b"/look/an/eagle"])
  271. def test_redirect_exception_with_cookie(self):
  272. """
  273. If the callback raises a RedirectException which sets a cookie, that is
  274. returned too
  275. """
  276. async def callback(request, **kwargs):
  277. e = RedirectException(b"/no/over/there", 304)
  278. e.cookies.append(b"session=yespls")
  279. raise e
  280. res = WrapHtmlRequestHandlerTests.TestResource()
  281. res.callback = callback
  282. channel = make_request(
  283. self.reactor, FakeSite(res, self.reactor), b"GET", b"/path"
  284. )
  285. self.assertEqual(channel.result["code"], b"304")
  286. headers = channel.result["headers"]
  287. location_headers = [v for k, v in headers if k == b"Location"]
  288. self.assertEqual(location_headers, [b"/no/over/there"])
  289. cookies_headers = [v for k, v in headers if k == b"Set-Cookie"]
  290. self.assertEqual(cookies_headers, [b"session=yespls"])
  291. def test_head_request(self):
  292. """A head request should work by being turned into a GET request."""
  293. async def callback(request):
  294. request.write(b"response")
  295. request.finish()
  296. res = WrapHtmlRequestHandlerTests.TestResource()
  297. res.callback = callback
  298. channel = make_request(
  299. self.reactor, FakeSite(res, self.reactor), b"HEAD", b"/path"
  300. )
  301. self.assertEqual(channel.result["code"], b"200")
  302. self.assertNotIn("body", channel.result)
  303. class CancellableDirectServeJsonResource(DirectServeJsonResource):
  304. def __init__(self, clock: Clock):
  305. super().__init__()
  306. self.clock = clock
  307. @cancellable
  308. async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
  309. await self.clock.sleep(1.0)
  310. return HTTPStatus.OK, {"result": True}
  311. async def _async_render_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
  312. await self.clock.sleep(1.0)
  313. return HTTPStatus.OK, {"result": True}
  314. class CancellableDirectServeHtmlResource(DirectServeHtmlResource):
  315. ERROR_TEMPLATE = "{code} {msg}"
  316. def __init__(self, clock: Clock):
  317. super().__init__()
  318. self.clock = clock
  319. @cancellable
  320. async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, bytes]:
  321. await self.clock.sleep(1.0)
  322. return HTTPStatus.OK, b"ok"
  323. async def _async_render_POST(self, request: SynapseRequest) -> Tuple[int, bytes]:
  324. await self.clock.sleep(1.0)
  325. return HTTPStatus.OK, b"ok"
  326. class DirectServeJsonResourceCancellationTests(EndpointCancellationTestHelperMixin):
  327. """Tests for `DirectServeJsonResource` cancellation."""
  328. def setUp(self):
  329. self.reactor = ThreadedMemoryReactorClock()
  330. self.clock = Clock(self.reactor)
  331. self.resource = CancellableDirectServeJsonResource(self.clock)
  332. self.site = FakeSite(self.resource, self.reactor)
  333. def test_cancellable_disconnect(self) -> None:
  334. """Test that handlers with the `@cancellable` flag can be cancelled."""
  335. channel = make_request(
  336. self.reactor, self.site, "GET", "/sleep", await_result=False
  337. )
  338. self._test_disconnect(
  339. self.reactor,
  340. channel,
  341. expect_cancellation=True,
  342. expected_body={"error": "Request cancelled", "errcode": Codes.UNKNOWN},
  343. )
  344. def test_uncancellable_disconnect(self) -> None:
  345. """Test that handlers without the `@cancellable` flag cannot be cancelled."""
  346. channel = make_request(
  347. self.reactor, self.site, "POST", "/sleep", await_result=False
  348. )
  349. self._test_disconnect(
  350. self.reactor,
  351. channel,
  352. expect_cancellation=False,
  353. expected_body={"result": True},
  354. )
  355. class DirectServeHtmlResourceCancellationTests(EndpointCancellationTestHelperMixin):
  356. """Tests for `DirectServeHtmlResource` cancellation."""
  357. def setUp(self):
  358. self.reactor = ThreadedMemoryReactorClock()
  359. self.clock = Clock(self.reactor)
  360. self.resource = CancellableDirectServeHtmlResource(self.clock)
  361. self.site = FakeSite(self.resource, self.reactor)
  362. def test_cancellable_disconnect(self) -> None:
  363. """Test that handlers with the `@cancellable` flag can be cancelled."""
  364. channel = make_request(
  365. self.reactor, self.site, "GET", "/sleep", await_result=False
  366. )
  367. self._test_disconnect(
  368. self.reactor,
  369. channel,
  370. expect_cancellation=True,
  371. expected_body=b"499 Request cancelled",
  372. )
  373. def test_uncancellable_disconnect(self) -> None:
  374. """Test that handlers without the `@cancellable` flag cannot be cancelled."""
  375. channel = make_request(
  376. self.reactor, self.site, "POST", "/sleep", await_result=False
  377. )
  378. self._test_disconnect(
  379. self.reactor, channel, expect_cancellation=False, expected_body=b"ok"
  380. )