44import cherrypy
55from ws4py .server .cherrypyserver import WebSocketPlugin , WebSocketTool
66from ws4py .websocket import EchoWebSocket
7+ from ws4py .compat import py3k
78
89class FakeSocket (object ):
910 def settimeout (self , timeout ):
@@ -31,26 +32,26 @@ class App(object):
3132 @cherrypy .expose
3233 def ws (self ):
3334 assert cherrypy .request .ws_handler != None
34-
35+
3536def setup_engine ():
3637 # we don't need a HTTP server for this test
3738 cherrypy .server .unsubscribe ()
3839
3940 cherrypy .config .update ({'log.screen' : False })
40-
41+
4142 cherrypy .engine .websocket = WebSocketPlugin (cherrypy .engine )
4243 cherrypy .engine .websocket .subscribe ()
43-
44+
4445 cherrypy .tools .websocket = WebSocketTool ()
45-
46+
4647 config = {'/ws' : {'tools.websocket.on' : True ,
4748 'tools.websocket.handler_cls' : EchoWebSocket }}
4849 cherrypy .tree .mount (App (), '/' , config )
4950 cherrypy .engine .start ()
5051
5152def teardown_engine ():
5253 cherrypy .engine .exit ()
53-
54+
5455class CherryPyTest (unittest .TestCase ):
5556 def setUp (self ):
5657 setup_engine ()
@@ -65,7 +66,10 @@ def test_plugin(self):
6566 h = EchoWebSocket (s , [], [])
6667 cherrypy .engine .publish ('handle-websocket' , h , ('127.0.0.1' , 0 ))
6768 self .assertEquals (len (cherrypy .engine .websocket .pool ), 1 )
68- k = cherrypy .engine .websocket .pool .keys ()[0 ]
69+ if py3k :
70+ k = list (cherrypy .engine .websocket .pool .keys ())[0 ]
71+ else :
72+ k = cherrypy .engine .websocket .pool .keys ()[0 ]
6973 self .assertTrue (k is h )
7074 self .assertEquals (cherrypy .engine .websocket .pool [k ][1 ], ('127.0.0.1' , 0 ))
7175
0 commit comments