Python/BLIPListenerTest.py
author Dan Preston <danpreston@codechemistry.com>
Tue May 05 15:27:20 2009 -0700 (2009-05-05)
changeset 40 423c134d3205
child 51 de59ce19f42e
permissions -rw-r--r--
Tweaked release to be immediate instead of on autorelease pool.
     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 handleRequest(request):
    21             logging.info("Got request!: %r",request)
    22             body = request.body
    23             assert len(body)<32768
    24             assert request.contentType == 'application/octet-stream'
    25             assert int(request['Size']) == len(body)
    26             assert request['User-Agent'] != None
    27             for i in xrange(0,len(request.body)):
    28                 assert ord(body[i]) == i%256
    29             
    30             response = request.response
    31             response.body = request.body
    32             response['Content-Type'] = request.contentType
    33             response.send()
    34         
    35         listener = Listener(46353)
    36         listener.onRequest = handleRequest
    37         logging.info("Listener is waiting...")
    38         
    39         try:
    40             asyncore.loop()
    41         except KeyboardInterrupt:
    42             logging.info("KeyboardInterrupt")
    43 
    44 if __name__ == '__main__':
    45     logging.basicConfig(level=logging.INFO)
    46     unittest.main()