def make_request(method, path, content=b""): """ Make a web request using the given method and path, feed it the content, and return the Request and the Channel underneath. """ if not isinstance(method, bytes): method = method.encode('ascii') if not isinstance(path, bytes): path = path.encode('ascii') # Decorate it to be the full path if not path.startswith(b"/_matrix"): path = b"/_matrix/client/r0/" + path path = path.replace(b"//", b"/") if isinstance(content, text_type): content = content.encode('utf8') site = FakeSite() channel = FakeChannel() req = SynapseRequest(site, channel) req.process = lambda: b"" req.content = BytesIO(content) req.requestReceived(method, path, b"1.1") return req, channel
def test_with_request_context(self): """ Information from the logging context request should be added to the JSON response. """ handler = logging.StreamHandler(self.output) handler.setFormatter(JsonFormatter()) handler.addFilter(LoggingContextFilter()) logger = self.get_logger(handler) # A full request isn't needed here. site = Mock(spec=["site_tag", "server_version_string", "getResourceFor"]) site.site_tag = "test-site" site.server_version_string = "Server v1" site.reactor = Mock() request = SynapseRequest(FakeChannel(site, None), site) # Call requestReceived to finish instantiating the object. request.content = BytesIO() # Partially skip some of the internal processing of SynapseRequest. request._started_processing = Mock() request.request_metrics = Mock(spec=["name"]) with patch.object(Request, "render"): request.requestReceived(b"POST", b"/_matrix/client/versions", b"1.1") # Also set the requester to ensure the processing works. request.requester = "@foo:test" with LoggingContext( request.get_request_id(), parent_context=request.logcontext ): logger.info("Hello there, %s!", "wally") log = self.get_log_line() # The terse logger includes additional request information, if possible. expected_log_keys = [ "log", "level", "namespace", "request", "ip_address", "site_tag", "requester", "authenticated_entity", "method", "url", "protocol", "user_agent", ] self.assertCountEqual(log.keys(), expected_log_keys) self.assertEqual(log["log"], "Hello there, wally!") self.assertTrue(log["request"].startswith("POST-")) self.assertEqual(log["ip_address"], "127.0.0.1") self.assertEqual(log["site_tag"], "test-site") self.assertEqual(log["requester"], "@foo:test") self.assertEqual(log["authenticated_entity"], "@foo:test") self.assertEqual(log["method"], "POST") self.assertEqual(log["url"], "/_matrix/client/versions") self.assertEqual(log["protocol"], "1.1") self.assertEqual(log["user_agent"], "")
def post_json(destination, path, data): self.assertEqual(destination, self.hs.hostname) self.assertEqual( path, "/_matrix/key/v2/query", ) channel = FakeChannel(self.site, self.reactor) req = SynapseRequest(channel) req.content = BytesIO(encode_canonical_json(data)) req.requestReceived( b"POST", path.encode("utf-8"), b"1.1", ) wait_until_result(self.reactor, req) self.assertEqual(channel.code, 200) resp = channel.json_body return resp
def make_notary_request(self, server_name: str, key_id: str) -> dict: """Send a GET request to the test server requesting the given key. Checks that the response is a 200 and returns the decoded json body. """ channel = FakeChannel(self.site, self.reactor) req = SynapseRequest(channel) req.content = BytesIO(b"") req.requestReceived( b"GET", b"/_matrix/key/v2/query/%s/%s" % (server_name.encode("utf-8"), key_id.encode("utf-8")), b"1.1", ) channel.await_result() self.assertEqual(channel.code, 200) resp = channel.json_body return resp
def make_request(method, path, content=b""): """ Make a web request using the given method and path, feed it the content, and return the Request and the Channel underneath. """ if isinstance(content, text_type): content = content.encode('utf8') site = FakeSite() channel = FakeChannel() req = SynapseRequest(site, channel) req.process = lambda: b"" req.content = BytesIO(content) req.requestReceived(method, path, b"1.1") return req, channel
async def post_json( destination: str, path: str, data: Optional[JsonDict] = None) -> Union[JsonDict, list]: self.assertEqual(destination, self.hs.hostname) self.assertEqual( path, "/_matrix/key/v2/query", ) channel = FakeChannel(self.site, self.reactor) # channel is a `FakeChannel` but `HTTPChannel` is expected req = SynapseRequest(channel, self.site) # type: ignore[arg-type] req.content = BytesIO(encode_canonical_json(data)) req.requestReceived( b"POST", path.encode("utf-8"), b"1.1", ) channel.await_result() self.assertEqual(channel.code, 200) resp = channel.json_body return resp