コード例 #1
0
    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEqual(len(manager), 0)

        s = MagicMock()
        s.recv.return_value = Frame(opcode=OPCODE_TEXT,
                                    body=b'hello',
                                    fin=1,
                                    masking_key=os.urandom(4)).build()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEqual(len(manager), 1)
        self.assertTrue(h in manager)

        # the following call to .close() on the
        # websocket object will initiate
        # the closing handshake
        # This next line mocks the response
        # from the client to actually
        # complete the handshake.
        # The manager will then remove the websocket
        # from its pool
        s.recv.return_value = Frame(opcode=OPCODE_CLOSE,
                                    body=b"ok we're done",
                                    fin=1,
                                    masking_key=os.urandom(4)).build()
        h.close()

        # the poller runs a thread, give it time to get there
        time.sleep(1)

        # TODO: Implement a fake poller so that works...
        self.assertEqual(len(manager), 0)
コード例 #2
0
    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEqual(len(manager), 0)

        s = MagicMock(spec=socket.socket)
        s.recv.return_value = Frame(opcode=OPCODE_TEXT, body=b'hello',
                                    fin=1, masking_key=os.urandom(4)).build()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEqual(len(manager), 1)
        self.assertTrue(h in manager)

        # the following call to .close() on the
        # websocket object will initiate
        # the closing handshake
        # This next line mocks the response
        # from the client to actually
        # complete the handshake.
        # The manager will then remove the websocket
        # from its pool
        s.recv.return_value = Frame(opcode=OPCODE_CLOSE, body=b"ok we're done",
                                    fin=1, masking_key=os.urandom(4)).build()
        h.close()

        # the poller runs a thread, give it time to get there
        time.sleep(1)
        
        # TODO: Implement a fake poller so that works...
        self.assertEqual(len(manager), 0)
コード例 #3
0
    def test_plugin(self):
        self.assertEquals(len(cherrypy.engine.websocket.pool), 0)

        s = FakeSocket()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEquals(len(cherrypy.engine.websocket.pool), 1)
        k = cherrypy.engine.websocket.pool.keys()[0]
        self.assertTrue(k is h)
        self.assertEquals(cherrypy.engine.websocket.pool[k][1], ('127.0.0.1', 0))

        self.assertEquals(len(cherrypy.engine.websocket.pool), 1)
        h.close() # shutdown server side of the websocket connection
        h.client_terminated = True # we aren't actually connected so pretend the client shutdown
        cherrypy.engine.publish('main')
        self.assertEquals(len(cherrypy.engine.websocket.pool), 0)
コード例 #4
0
    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEquals(len(manager), 0)

        s = FakeSocket()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEquals(len(manager), 1)
        self.assertTrue(h in manager)

        h.close()
        
        # the poller runs a thread, give it time to get there
        time.sleep(0.5)

        # TODO: Implement a fake poller so that works...
        self.assertEquals(len(manager), 0)
コード例 #5
0
    def test_plugin(self):
        self.assertEquals(len(cherrypy.engine.websocket.pool), 0)

        s = FakeSocket()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEquals(len(cherrypy.engine.websocket.pool), 1)
        k = cherrypy.engine.websocket.pool.keys()[0]
        self.assertTrue(k is h)
        self.assertEquals(cherrypy.engine.websocket.pool[k][1],
                          ('127.0.0.1', 0))

        self.assertEquals(len(cherrypy.engine.websocket.pool), 1)
        h.close()  # shutdown server side of the websocket connection
        h.client_terminated = True  # we aren't actually connected so pretend the client shutdown
        cherrypy.engine.publish('main')
        self.assertEquals(len(cherrypy.engine.websocket.pool), 0)