Python/BLIPListenerTest.py
author morrowa
Thu Jul 02 19:58:11 2009 -0700 (2009-07-02)
changeset 56 6c3b5372a307
parent 51 de59ce19f42e
permissions -rw-r--r--
Removed unnecessary files. Toned down logging. Added null logging handler to BLIP so client code doesn't have to use logging. Modified test drivers to work against Cocoa versions.
     1 #!/usr/bin/env python
     2 # encoding: utf-8
     3 """
     4 BLIPListenerTest.py
     5 
     6 Created by Jens Alfke on 2008-06-04.
     7 This source file is test/example code, and is in the public domain.
     8 """
     9 
    10 from BLIP import Listener
    11 
    12 import asyncore
    13 import logging
    14 import unittest
    15 
    16 
    17 class BLIPListenerTest(unittest.TestCase):
    18     
    19     def testListener(self):
    20         def handleConnection(conn):
    21             logging.info("Got new connection: %r", conn)
    22             conn.ListenerTestNumRequests = 0
    23         
    24         def handleRequest(request):
    25             logging.info("Got request!: %r",request)
    26             request.connection.ListenerTestNumRequests += 1
    27             body = request.body
    28             assert len(body)<32768
    29             assert request.contentType == 'application/octet-stream'
    30             assert int(request['Size']) == len(body)
    31             assert request['User-Agent'] != None
    32             for i in xrange(0,len(request.body)):
    33                 assert ord(body[i]) == i%256
    34             
    35             response = request.response
    36             response.body = request.body
    37             response['Content-Type'] = request.contentType
    38             response.send()
    39             if request.connection.ListenerTestNumRequests >= 50:
    40                 request.connection.close()
    41         
    42         listener = Listener(46353)
    43         listener.onConnected = handleConnection
    44         listener.onRequest = handleRequest
    45         logging.info("Listener is waiting...")
    46         
    47         try:
    48             asyncore.loop()
    49         except KeyboardInterrupt:
    50             logging.info("KeyboardInterrupt")
    51 
    52 if __name__ == '__main__':
    53     logging.basicConfig(level=logging.INFO)
    54     unittest.main()