old_contents: from unittest import TestCase
  
  from socketio.server import SocketIOServer
  from socketio.virtsocket import Socket
  
  
  class MockSocketIOServer(object):
      """
      Mock a SocketIO server.
      """
      def __init__(self, *args, **kwargs):
          self.sockets = {'1': Socket()}
  
      def get_socket(self, socket_id=''):
          return self.sockets.get(socket_id)
  
  
  class MockSocketIOhandler(object):
      """
      Mock a SocketIO handler.
      """
      def __init__(self, *args, **kwargs):
          self.server = MockSocketIOServer()
  
  
  class TestSocketAPI(TestCase):
  
      def setUp(self):
          self.server = MockSocketIOServer()
          self.virtsocket = Socket(self.server)
  
      def test__set_namespaces(self):
          namespaces = {'test': 'a'}
          self.virtsocket._set_namespaces(namespaces)
          self.asserEqual(self.virtsocket.namespaces, namespaces)
  
      def test__set_request(self):
          request = {'test': 'a'}
          self.virtsocket._set_request(request)
          self.asserEqual(self.virtsocket.request, request)
  
new_contents: from unittest import TestCase, main
  
  from socketio import SocketIOServer
  from socketio.virtsocket import Socket
  
  
  class MockSocketIOServer(object):
      """Mock a SocketIO server"""
      def __init__(self, *args, **kwargs):
          self.sockets = {}
  
      def get_socket(self, socket_id=''):
          return self.sockets.get(socket_id)
  
  
  class MockSocketIOhandler(object):
      """Mock a SocketIO handler"""
      def __init__(self, *args, **kwargs):
          self.server = MockSocketIOServer()
  
  
  class MockNamespace(object):
      """Mock a Namespace from the namespace module"""
      def __init__(self, environ, name, request=None):
          self.environ = environ
          self.ns_name = name
          self.socket = 'socketio'
  
      def disconnect(self):
          pass
  
  class TestSocketAPI(TestCase):
      """Test the virtual Socket object"""
  
      def setUp(self):
          self.server = MockSocketIOServer()
          self.virtsocket = Socket(self.server)
  
      def test__set_namespaces(self):
          namespaces = {'/': MockNamespace}
          self.virtsocket._set_namespaces(namespaces)
          self.assertEqual(self.virtsocket.namespaces, namespaces)
  
      def test__set_request(self):
          request = {'test': 'a'}
          self.virtsocket._set_request(request)
          self.assertEqual(self.virtsocket.request, request)
  
      def test__set_environ(self):
          environ = []
          self.virtsocket._set_environ(environ)
          self.assertEqual(self.virtsocket.environ, environ)
  
      def test_connected_property(self):
          # not connected
          self.assertFalse(self.virtsocket.connected)
          
          # connected
          self.virtsocket.state = "CONNECTED"
          self.assertTrue(self.virtsocket.connected)
  
      def test_incr_hist(self):
          self.virtsocket.incr_hits()
          self.assertEqual(self.virtsocket.hits, 1)
          self.assertEqual(self.virtsocket.state, self.virtsocket.STATE_CONNECTED)
  
      def test_disconnect(self):
          # kill connected socket
          self.virtsocket.state = "CONNECTED"
          self.virtsocket.active_ns = {'test' : MockNamespace(environ=[], 
                                                              name="m")}
          self.virtsocket.disconnect()
          self.assertEqual(self.virtsocket.state, "CONNECTED")
          self.assertEqual(self.virtsocket.active_ns, {})
  
      def test_kill(self):
          # kill connected socket
          self.virtsocket.state = "CONNECTED"
          self.virtsocket.active_ns = {'test' : MockNamespace(environ=[],
                                                              name="m")}
          self.virtsocket.kill()
          self.assertEqual(self.virtsocket.state, "DISCONNECTING")
  
      def test__receiver_loop(self):
          """Test the loop  """
          # most of the method is tested by test_packet.TestDecode and
          # test_namespace.TestBaseNamespace
          pass
          # self.virtsocket._receiver_loop()
          # self.virtsocket.server_queue.put_nowait_msg('2::')
  
  
  if __name__ == '__main__':
      main()
  
subject: test simple methods of socket module