Commits

Jeffrey Gelens  committed b2d649c

Added optional wsgi app call to response. Also improved testing.

  • Participants
  • Parent commits 11c3aaf

Comments (0)

Files changed (2)

File geventwebsocket/handler.py

         self.websocket_connection = False
         super(WebSocketHandler, self).__init__(*args, **kwargs)
 
-    def handle_one_response(self):
+    def handle_one_response(self, call_wsgi_app=True):
         # In case the client doesn't want to initialize a WebSocket connection
         # we will proceed with the default PyWSGI functionality.
         if self.environ.get("HTTP_CONNECTION") != "Upgrade" or \
         else:
             raise Exception("Version not supported")
 
-        return self.application(self.environ, self.start_response)
+        if call_wsgi_app:
+            return self.application(self.environ, self.start_response)
+        else:
+            return
 
     def write(self, data):
         if self.websocket_connection:
         challenge += key3
 
         return md5(challenge).digest()
+
+    def wait(self):
+        return self.websocket.wait()
+
+    def send(self, message):
+        return self.websocket.send(message)

File tests/test__websocket.py

             elif CONTENT_LENGTH in headers:
                 num = int(headers[CONTENT_LENGTH])
                 self.body = fd.read(num)
-            else:
-                self.body = fd.read(16)
+            #else:
+            #    self.body = fd.read(16)
         except:
             print 'Response.read failed to read the body:\n%s' % self
             raise
                 if message is None:
                     break
                 ws.send(message)
-                ws.close_connection()
+
             return []
 
     def test_basic(self):
         "^n:ds[4U"
 
         fd.write(headers)
-        fd.write(self.message)
 
-        response = read_http(fd, code=101, body="8jKS'y:G*Co,Wxa-", reason="Web Socket Protocol Handshake")
+        response = read_http(fd, code=101, reason="Web Socket Protocol Handshake")
         response.assertHeader("Upgrade", "WebSocket")
         response.assertHeader("Connection", "Upgrade")
         response.assertHeader("Sec-WebSocket-Origin", "http://localhost")
         response.assertHeader("Sec-WebSocket-Location", "ws://localhost/echo")
         response.assertHeader("Sec-WebSocket-Protocol", "test")
+        assert fd.read(16) == "8jKS'y:G*Co,Wxa-"
 
-        message = fd.read()
+        fd.write(self.message)
+        message = fd.read(len(self.message))
         assert message == self.message, \
                'Unexpected message: %r (expected %r)\n%s' % (message, self.message, self)
 
         fd.close()
 
+    def test_10000_messages(self):
+        fd = self.connect().makefile(bufsize=1)
+        headers = "" \
+        "GET /echo HTTP/1.1\r\n" \
+        "Host: localhost\r\n" \
+        "Connection: Upgrade\r\n" \
+        "Sec-WebSocket-Key2: 12998 5 Y3 1  .P00\r\n" \
+        "Sec-WebSocket-Protocol: test\r\n" \
+        "Upgrade: WebSocket\r\n" \
+        "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n" \
+        "Origin: http://localhost\r\n\r\n" \
+        "^n:ds[4U"
+
+        fd.write(headers)
+
+        response = read_http(fd, code=101, reason="Web Socket Protocol Handshake")
+        response.assertHeader("Upgrade", "WebSocket")
+        response.assertHeader("Connection", "Upgrade")
+        response.assertHeader("Sec-WebSocket-Origin", "http://localhost")
+        response.assertHeader("Sec-WebSocket-Location", "ws://localhost/echo")
+        response.assertHeader("Sec-WebSocket-Protocol", "test")
+        assert fd.read(16) == "8jKS'y:G*Co,Wxa-"
+
+        for i in xrange(10000):
+            fd.write(self.message)
+            message = fd.read(len(self.message))
+
+            assert message == self.message, \
+                   'Unexpected message: %r (expected %r)\n%s' % (message, self.message, self)
+
+
+        fd.close()
+
     def test_badrequest(self):
         fd = self.connect().makefile(bufsize=1)
         fd.write('GET /echo HTTP/1.1\r\nHost: localhost\r\n\r\n')
         "Origin: http://example.com\r\n\r\n"
 
         fd.write(headers)
+        response = read_http(fd, code=101, reason="Web Socket Protocol Handshake")
+
         fd.write(self.message)
-        response = read_http(fd, code=101, body=self.message, reason="Web Socket Protocol Handshake")
+        message = fd.read(len(self.message))
+        assert message == self.message, \
+               'Unexpected message: %r (expected %r)\n%s' % (message, self.message, self)
 
         fd.close()