Пример #1
0
    def create_client_frame(b64patch, **kwargs):
        """
        Kind-of hack-y; maybe better to re-factor the Protocol to have a
        frame-encoder method-call? Anyway, makes a throwaway protocol
        encode a frame for us, collects the .sendData call and returns
        the data that would have gone out. Accepts all the kwargs that
        WebSocketClientProtocol.sendFrame() accepts.
        """

        # only real way to inject a "known" secret-key for the headers
        # to line up... :/
        b64patch.return_value = b'QIatSt9QkZPyS4QQfdufO8TgkL0='

        factory = WebSocketClientFactory(protocols=['wamp.2.json'])
        factory.protocol = WebSocketClientProtocol
        factory.doStart()
        proto = factory.buildProtocol(IPv4Address('TCP', '127.0.0.9', 65534))
        proto.transport = MagicMock()
        proto.connectionMade()
        proto.data = mock_handshake_server
        proto.processHandshake()

        data = []

        def collect(d, *args):
            data.append(d)
        proto.sendData = collect

        proto.sendFrame(**kwargs)
        return b''.join(data)
Пример #2
0
    def create_client_frame(b64patch, **kwargs):
        """
        Kind-of hack-y; maybe better to re-factor the Protocol to have a
        frame-encoder method-call? Anyway, makes a throwaway protocol
        encode a frame for us, collects the .sendData call and returns
        the data that would have gone out. Accepts all the kwargs that
        WebSocketClientProtocol.sendFrame() accepts.
        """

        # only real way to inject a "known" secret-key for the headers
        # to line up... :/
        b64patch.return_value = b'QIatSt9QkZPyS4QQfdufO8TgkL0='

        factory = WebSocketClientFactory(protocols=['wamp.2.json'])
        factory.protocol = WebSocketClientProtocol
        factory.doStart()
        proto = factory.buildProtocol(IPv4Address('TCP', '127.0.0.9', 65534))
        proto.transport = MagicMock()
        proto.connectionMade()
        proto.data = mock_handshake_server
        proto.processHandshake()

        data = []

        def collect(d, *args):
            data.append(d)

        proto.sendData = collect

        proto.sendFrame(**kwargs)
        return b''.join(data)
Пример #3
0
    class TestClient(unittest.TestCase):
        def setUp(self):
            self.factory = WebSocketClientFactory(protocols=['wamp.2.json'])
            self.factory.protocol = WebSocketClientProtocol
            self.factory.doStart()

            self.proto = self.factory.buildProtocol(
                IPv4Address('TCP', '127.0.0.1', 65534))
            self.transport = MagicMock()
            self.proto.transport = self.transport
            self.proto.connectionMade()

        def tearDown(self):
            self.factory.doStop()
            # not really necessary, but ...
            del self.factory
            del self.proto

        def test_unclean_timeout_client(self):
            """
            make a delayed call to drop the connection (client-side)
            """

            if False:
                self.proto.debug = True
                self.proto.factory._log = print

            # get to STATE_OPEN
            self.proto.websocket_key = b64decode('6Jid6RgXpH0RVegaNSs/4g==')
            self.proto.data = mock_handshake_server
            self.proto.processHandshake()
            self.assertEqual(self.proto.state,
                             WebSocketServerProtocol.STATE_OPEN)
            self.assertTrue(self.proto.serverConnectionDropTimeout > 0)

            with replace_loop(Clock()) as reactor:
                # now 'do the test' and transition to CLOSING
                self.proto.sendCloseFrame()
                self.proto.onCloseFrame(1000, b"raw reason")

                # check we scheduled a call
                self.assertEqual(len(reactor.calls), 1)
                self.assertEqual(reactor.calls[0].func,
                                 self.proto.onServerConnectionDropTimeout)
                self.assertEqual(reactor.calls[0].getTime(),
                                 self.proto.serverConnectionDropTimeout)

                # now, advance the clock past the call (and thereby
                # execute it)
                reactor.advance(self.proto.closeHandshakeTimeout + 1)

                # we should have called abortConnection
                self.assertEqual("call.abortConnection()",
                                 str(self.proto.transport.method_calls[-1]))
                self.assertTrue(self.proto.transport.abortConnection.called)
                # ...too "internal" for an assert?
                self.assertEqual(self.proto.state,
                                 WebSocketServerProtocol.STATE_CLOSED)
def _new_protocol_ws(transit_server, log_requests):
    """
    Internal helper for test-suites that need to provide WebSocket
    client/server pairs.

    :returns: a 2-tuple: (iosim.IOPump, protocol)
    """
    ws_factory = WebSocketServerFactory("ws://localhost:4002")
    ws_factory.protocol = WebSocketTransitConnection
    ws_factory.transit = transit_server
    ws_factory.log_requests = log_requests
    ws_protocol = ws_factory.buildProtocol(('127.0.0.1', 0))

    @implementer(IRelayTestClient)
    class TransitWebSocketClientProtocol(WebSocketClientProtocol):
        _received = b""
        connected = False

        def connectionMade(self):
            self.connected = True
            return super(TransitWebSocketClientProtocol, self).connectionMade()

        def connectionLost(self, reason):
            self.connected = False
            return super(TransitWebSocketClientProtocol,
                         self).connectionLost(reason)

        def onMessage(self, data, isBinary):
            self._received = self._received + data

        def send(self, data):
            self.sendMessage(data, True)

        def get_received_data(self):
            return self._received

        def reset_received_data(self):
            self._received = b""

        def disconnect(self):
            self.sendClose(1000, True)

    client_factory = WebSocketClientFactory()
    client_factory.protocol = TransitWebSocketClientProtocol
    client_protocol = client_factory.buildProtocol(('127.0.0.1', 31337))
    client_protocol.disconnect = client_protocol.dropConnection

    pump = iosim.connect(
        ws_protocol,
        iosim.makeFakeServer(ws_protocol),
        client_protocol,
        iosim.makeFakeClient(client_protocol),
    )
    return pump, client_protocol
Пример #5
0
    class TestClient(unittest.TestCase):
        def setUp(self):
            self.factory = WebSocketClientFactory(protocols=['wamp.2.json'])
            self.factory.protocol = WebSocketClientProtocol
            self.factory.doStart()

            self.proto = self.factory.buildProtocol(IPv4Address('TCP', '127.0.0.1', 65534))
            self.transport = MagicMock()
            self.proto.transport = self.transport
            self.proto.connectionMade()

        def tearDown(self):
            self.factory.doStop()
            # not really necessary, but ...
            del self.factory
            del self.proto

        def test_unclean_timeout_client(self):
            """
            make a delayed call to drop the connection (client-side)
            """

            if False:
                self.proto.debug = True
                self.proto.factory._log = print

            # get to STATE_OPEN
            self.proto.websocket_key = b64decode('6Jid6RgXpH0RVegaNSs/4g==')
            self.proto.data = mock_handshake_server
            self.proto.processHandshake()
            self.assertEqual(self.proto.state, WebSocketServerProtocol.STATE_OPEN)
            self.assertTrue(self.proto.serverConnectionDropTimeout > 0)

            with replace_loop(Clock()) as reactor:
                # now 'do the test' and transition to CLOSING
                self.proto.sendCloseFrame()
                self.proto.onCloseFrame(1000, "raw reason")

                # check we scheduled a call
                self.assertEqual(len(reactor.calls), 1)
                self.assertEqual(reactor.calls[0].func, self.proto.onServerConnectionDropTimeout)
                self.assertEqual(reactor.calls[0].getTime(), self.proto.serverConnectionDropTimeout)

                # now, advance the clock past the call (and thereby
                # execute it)
                reactor.advance(self.proto.closeHandshakeTimeout + 1)

                # we should have called abortConnection
                self.assertEqual("call.abortConnection()", str(self.proto.transport.method_calls[-1]))
                self.assertTrue(self.proto.transport.abortConnection.called)
                # ...too "internal" for an assert?
                self.assertEqual(self.proto.state, WebSocketServerProtocol.STATE_CLOSED)
Пример #6
0
	def buildProtocol(self,address):
		proto = WebSocketClientFactory.buildProtocol(self,address)
		self.protocolInstance = proto
		return proto
Пример #7
0
    def buildProtocol(self, address):
        proto = WebSocketClientFactory.buildProtocol(self, address)

        self.connectedProtocol = proto
        return proto
Пример #8
0
 def buildProtocol(self, addr):
     p = WebSocketClientFactory.buildProtocol(self, addr)
     self.autobahn_status_push.setProtocolInstance(p)
     return p
 def buildProtocol(self, addr):
     self._p = WebSocketClientFactory.buildProtocol(self, addr)
     self._p.callback = self.callback
     self._p.health_check_func = self.health_check_func
     return self._p
Пример #10
0
 def buildProtocol(self, addr):
     self._p = WebSocketClientFactory.buildProtocol(self, addr)
     self._p.callback = self.callback
     self._p.health_check_func = self.health_check_func
     return self._p
Пример #11
0
 def buildProtocol(self, addr):
     self.connected_protocol = WebSocketClientFactory.buildProtocol(
         self, addr)
     return self.connected_protocol
Пример #12
0
 def buildProtocol(self, addr):
     print 'build client protocol'
     p = WebSocketClientFactory.buildProtocol(self, addr.host)
     p.postman = self.postman
     return p
Пример #13
0
 def buildProtocol(self, addr):
     proto = WebSocketClientFactory.buildProtocol(self, addr)
     proto.factory = self
     proto.logger = self._logger
     self.ws_proto = proto
     return proto