def handle_http_replication_attempt(self) -> SynapseRequest: """Asserts that a connection attempt was made to the master HS on the HTTP replication port, then proxies it to the master HS object to be handled. Returns: The request object received by master HS. """ # We should have an outbound connection attempt. clients = self.reactor.tcpClients self.assertEqual(len(clients), 1) (host, port, client_factory, _timeout, _bindAddress) = clients.pop(0) self.assertEqual(host, "1.2.3.4") self.assertEqual(port, 8765) # Set up client side protocol client_protocol = client_factory.buildProtocol(None) # Set up the server side protocol channel = self.site.buildProtocol(None) # hook into the channel's request factory so that we can keep a record # of the requests requests: List[SynapseRequest] = [] real_request_factory = channel.requestFactory def request_factory(*args, **kwargs): request = real_request_factory(*args, **kwargs) requests.append(request) return request channel.requestFactory = request_factory # Connect client to server and vice versa. client_to_server_transport = FakeTransport(channel, self.reactor, client_protocol) client_protocol.makeConnection(client_to_server_transport) server_to_client_transport = FakeTransport(client_protocol, self.reactor, channel) channel.makeConnection(server_to_client_transport) # The request will now be processed by `self.site` and the response # streamed back. self.reactor.advance(0) # We tear down the connection so it doesn't get reused without our # knowledge. server_to_client_transport.loseConnection() client_to_server_transport.loseConnection() # there should have been exactly one request self.assertEqual(len(requests), 1) return requests[0]
def handle_http_replication_attempt(self) -> SynapseRequest: """Asserts that a connection attempt was made to the master HS on the HTTP replication port, then proxies it to the master HS object to be handled. Returns: The request object received by master HS. """ # We should have an outbound connection attempt. clients = self.reactor.tcpClients self.assertEqual(len(clients), 1) (host, port, client_factory, _timeout, _bindAddress) = clients.pop(0) self.assertEqual(host, "1.2.3.4") self.assertEqual(port, 8765) # Set up client side protocol client_protocol = client_factory.buildProtocol(None) request_factory = OneShotRequestFactory() # Set up the server side protocol channel = _PushHTTPChannel(self.reactor) channel.requestFactory = request_factory channel.site = self.site # Connect client to server and vice versa. client_to_server_transport = FakeTransport( channel, self.reactor, client_protocol ) client_protocol.makeConnection(client_to_server_transport) server_to_client_transport = FakeTransport( client_protocol, self.reactor, channel ) channel.makeConnection(server_to_client_transport) # The request will now be processed by `self.site` and the response # streamed back. self.reactor.advance(0) # We tear down the connection so it doesn't get reused without our # knowledge. server_to_client_transport.loseConnection() client_to_server_transport.loseConnection() return request_factory.request