Python/BLIPListenerTest.py
author Jens Alfke <jens@mooseyard.com>
Sat May 16 14:10:15 2009 -0700 (2009-05-16)
changeset 47 60f2b46d9a3b
child 51 de59ce19f42e
permissions -rw-r--r--
* Fixed #9: compilation error with iPhone 3.0 SDK.
* MYBonjourRegistration now allows you to set a TXT dictionary with non-NSData key values; they'll be translated to UTF-8 object descriptions. Useful for NSStrings and NSNumbers.
     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()