Python/CloseTestPong.py
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 51 de59ce19f42e
permissions -rw-r--r--
* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
     1 # CloseTestPong.py
     2 # Tests the closing negotiation facilities of the BLIP 1.1 protocol
     3 
     4 from BLIP import Listener
     5 
     6 import logging
     7 import asyncore
     8 import unittest
     9 
    10 class CloseTestPong(unittest.TestCase):
    11     
    12     def shouldClose(self):
    13         logging.info("Allowed to close.")
    14         return True
    15     
    16     def handleConnection(self, conn):
    17         logging.info("Accepted connection.")
    18         conn.onCloseRequest = self.shouldClose
    19     
    20     def handleRequest(self, req):
    21         resp = req.response
    22         resp.body = "Pong"
    23         resp.send()
    24     
    25     def testClose(self):
    26         listen = Listener(1337)
    27         listen.onConnected = self.handleConnection
    28         listen.onRequest = self.handleRequest
    29         
    30         try:
    31             asyncore.loop()
    32         except KeyboardInterrupt:
    33             pass
    34 
    35 
    36 if __name__ == '__main__':
    37     logging.basicConfig(level=logging.INFO)
    38     unittest.main()