Пример #1
0
    def test_client_connect_auto(self):
        """
        A server will automatically accept client connections by default.
        """
        fake_server = FakeServer(self.server_factory)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        client = self.successResultOf(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_connected(conn, client)
Пример #2
0
    def test_client_connect_auto(self):
        """
        A server will automatically accept client connections by default.
        """
        fake_server = FakeServer(self.server_factory)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        client = self.successResultOf(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_connected(conn, client)
Пример #3
0
    def test_reject_connection_with_reason(self):
        """
        Connections can be rejected with a custom reason.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        conn.reject_connection(UnknownHostError())
        self.assertNoResult(connected_d)
        self.failureResultOf(client_d, UnknownHostError)
        self.assert_connection_rejected(conn)
Пример #4
0
    def test_reject_connection(self):
        """
        Connections can be rejected manually if desired.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        conn.reject_connection()
        self.assertNoResult(connected_d)
        self.failureResultOf(client_d, ConnectionRefusedError)
        self.assert_connection_rejected(conn)
Пример #5
0
    def test_reject_connection_with_reason(self):
        """
        Connections can be rejected with a custom reason.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        conn.reject_connection(UnknownHostError())
        self.assertNoResult(connected_d)
        self.failureResultOf(client_d, UnknownHostError)
        self.assert_connection_rejected(conn)
Пример #6
0
    def test_reject_connection(self):
        """
        Connections can be rejected manually if desired.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        conn.reject_connection()
        self.assertNoResult(connected_d)
        self.failureResultOf(client_d, ConnectionRefusedError)
        self.assert_connection_rejected(conn)
Пример #7
0
    def test_client_connect_hook(self):
        """
        An on_connect function can be passed to the server to be called
        whenever a connection is made.
        """
        def on_connect(conn):
            conn.hook_was_called = True
            conn.client_id_from_hook = id(conn.client_protocol)
            conn.server_id_from_hook = id(conn.server_protocol)

        fake_server = FakeServer(self.server_factory, on_connect=on_connect)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        client = self.successResultOf(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_connected(conn, client)
        self.assertEqual(conn.hook_was_called, True)
        self.assertEqual(conn.client_id_from_hook, id(client))
        self.assertEqual(conn.server_id_from_hook, id(conn.server_protocol))
Пример #8
0
    def test_accept_connection(self):
        """
        Connections can be accepted manually if desired.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        accepted_d = conn.accept_connection()
        self.successResultOf(accepted_d)
        self.successResultOf(connected_d)
        client = self.successResultOf(client_d)
        self.assert_connected(conn, client)
Пример #9
0
    def test_client_connect_hook(self):
        """
        An on_connect function can be passed to the server to be called
        whenever a connection is made.
        """
        def on_connect(conn):
            conn.hook_was_called = True
            conn.client_id_from_hook = id(conn.client_protocol)
            conn.server_id_from_hook = id(conn.server_protocol)

        fake_server = FakeServer(self.server_factory, on_connect=on_connect)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        client = self.successResultOf(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_connected(conn, client)
        self.assertEqual(conn.hook_was_called, True)
        self.assertEqual(conn.client_id_from_hook, id(client))
        self.assertEqual(conn.server_id_from_hook, id(conn.server_protocol))
Пример #10
0
    def test_accept_connection(self):
        """
        Connections can be accepted manually if desired.
        """
        fake_server = FakeServer(self.server_factory, auto_accept=False)
        conn_d = fake_server.await_connection()
        self.assertNoResult(conn_d)

        client_d = fake_server.endpoint.connect(self.client_factory)
        self.assertNoResult(client_d)
        conn = self.successResultOf(conn_d)
        self.assert_pending(conn)

        connected_d = conn.await_connected()
        self.assertNoResult(connected_d)

        accepted_d = conn.accept_connection()
        self.successResultOf(accepted_d)
        self.successResultOf(connected_d)
        client = self.successResultOf(client_d)
        self.assert_connected(conn, client)
Пример #11
0
    def test_server_for_protocol(self):
        """
        A FakeServer can also be constructed from a a protocol class instead of
        a factory.
        """
        class MyProtocol(Protocol):
            pass
        fake_server = FakeServer.for_protocol(MyProtocol)
        self.assertEqual(fake_server.server_factory.protocol, MyProtocol)
        self.assertNotEqual(fake_server.server_factory.protocol, Protocol)

        yield fake_server.endpoint.connect(self.client_factory)
        conn = yield fake_server.await_connection()
        self.assertIsInstance(conn.server_protocol, MyProtocol)
Пример #12
0
    def test_server_for_protocol(self):
        """
        A FakeServer can also be constructed from a a protocol class instead of
        a factory.
        """
        class MyProtocol(Protocol):
            pass

        fake_server = FakeServer.for_protocol(MyProtocol)
        self.assertEqual(fake_server.server_factory.protocol, MyProtocol)
        self.assertNotEqual(fake_server.server_factory.protocol, Protocol)

        yield fake_server.endpoint.connect(self.client_factory)
        conn = yield fake_server.await_connection()
        self.assertIsInstance(conn.server_protocol, MyProtocol)
Пример #13
0
    def test_server_abort(self):
        """
        If the server aborts, the client's connection is also lost.
        """
        fake_server = FakeServer(self.server_factory)
        conn, client = self.connect_client(fake_server)
        finished_d = conn.await_finished()
        self.assertNoResult(finished_d)

        # The disconnection gets scheduled, but doesn't actually happen until
        # the next reactor tick.
        conn.server_protocol.transport.abortConnection()
        self.assert_connected(conn, client)
        self.assertNoResult(finished_d)

        # Allow the reactor to run so the disconnection gets processed.
        yield wait0()
        self.assert_disconnected(conn, server_reason=ConnectionAborted)
        self.successResultOf(finished_d)
Пример #14
0
    def test_send_client_to_server(self):
        """
        Bytes can be sent from the client to the server.
        """
        fake_server = FakeServer(self.server_factory)
        conn, client = self.connect_client(fake_server)
        server = conn.server_protocol
        self.assertEqual(server.received, b"")

        # Bytes sent, but not received until reactor runs.
        d = client.write(b"foo")
        self.assertEqual(server.received, b"")
        yield d
        self.assertEqual(server.received, b"foo")

        client.write(b"bar")
        d = client.write(b"baz")
        self.assertEqual(server.received, b"foo")
        yield d
        self.assertEqual(server.received, b"foobarbaz")
Пример #15
0
    def test_send_server_to_client(self):
        """
        Bytes can be sent from the server to the client.
        """
        fake_server = FakeServer(self.server_factory)
        conn, client = self.connect_client(fake_server)
        server = conn.server_protocol
        self.assertEqual(server.received, b"")

        # Bytes sent, but not received until reactor runs.
        d = server.write(b"foo")
        self.assertEqual(client.received, b"")
        yield d
        self.assertEqual(client.received, b"foo")

        # Send two sets of bytes at once, waiting for the second.
        server.write(b"bar")
        d = server.write(b"baz")
        self.assertEqual(client.received, b"foo")
        yield d
        self.assertEqual(client.received, b"foobarbaz")