Python/BLIPListenerTest.py
changeset 45 8efb48eabd08
child 51 de59ce19f42e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Python/BLIPListenerTest.py	Sun May 10 19:00:50 2009 -0700
     1.3 @@ -0,0 +1,46 @@
     1.4 +#!/usr/bin/env python
     1.5 +# encoding: utf-8
     1.6 +"""
     1.7 +BLIPListenerTest.py
     1.8 +
     1.9 +Created by Jens Alfke on 2008-06-04.
    1.10 +This source file is test/example code, and is in the public domain.
    1.11 +"""
    1.12 +
    1.13 +from BLIP import Listener
    1.14 +
    1.15 +import asyncore
    1.16 +import logging
    1.17 +import unittest
    1.18 +
    1.19 +
    1.20 +class BLIPListenerTest(unittest.TestCase):
    1.21 +    
    1.22 +    def testListener(self):
    1.23 +        def handleRequest(request):
    1.24 +            logging.info("Got request!: %r",request)
    1.25 +            body = request.body
    1.26 +            assert len(body)<32768
    1.27 +            assert request.contentType == 'application/octet-stream'
    1.28 +            assert int(request['Size']) == len(body)
    1.29 +            assert request['User-Agent'] != None
    1.30 +            for i in xrange(0,len(request.body)):
    1.31 +                assert ord(body[i]) == i%256
    1.32 +            
    1.33 +            response = request.response
    1.34 +            response.body = request.body
    1.35 +            response['Content-Type'] = request.contentType
    1.36 +            response.send()
    1.37 +        
    1.38 +        listener = Listener(46353)
    1.39 +        listener.onRequest = handleRequest
    1.40 +        logging.info("Listener is waiting...")
    1.41 +        
    1.42 +        try:
    1.43 +            asyncore.loop()
    1.44 +        except KeyboardInterrupt:
    1.45 +            logging.info("KeyboardInterrupt")
    1.46 +
    1.47 +if __name__ == '__main__':
    1.48 +    logging.basicConfig(level=logging.INFO)
    1.49 +    unittest.main()