"""Tests for wsgi.stub http://www.decafbad.com mailto:l.m.orchard@pobox.com Share and Enjoy """ import unittest from md5 import md5 from StringIO import StringIO from random import random from wsgi.stub import WSGIStub def simplestApp(environ, start_response): start_response('200 OK',[('Content-type','text/plain')]) return ['Hello world!'] class TestCase(unittest.TestCase): def setUp(self): """Create some reusable objects for tests.""" def tearDown(self): """ """ pass def testCreate(self): """Test result of WSGIStub creation.""" stub = WSGIStub(simplestApp) def testSimpleGET(self): """Try running the simplest app.""" stub = WSGIStub(simplestApp) stub.run() stub_output = stub.stdout.getvalue().split('\r\n') test_output = [ 'Status: 200 OK', 'Content-type: text/plain', '', 'Hello world!' ] self.assertEqual(test_output, stub_output, "Mismatch between test run (%s) and stub output (%s)" % \ (test_output, stub_output)) def testComplexGET(self): """Try running the simplest app with complex GET query.""" stub = WSGIStub(simplestApp) stub.SCRIPT_NAME = "/foo/bar" stub.run( uri="http://localhost/foo/bar/thing/baz?xyzzy=quux&wang=doodle" ) e = stub.environ self.assertEqual(e['QUERY_STRING'], 'xyzzy=quux&wang=doodle', "Query string mismatch: %s" % e['QUERY_STRING']) self.assertEqual(e['PATH_INFO'], '/thing/baz', "Unexpected PATH_INFO: %s" % e['PATH_INFO']) def testPUT(self): """Try running a PUT query.""" def postApp(environ, start_response): environ['test.input_hash'] = \ md5(environ['wsgi.input'].read()).hexdigest() start_response('200 OK',[('Content-type','text/plain')]) return ['Hello world!'] post_data = """ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque ultricies. Morbi vitae pede et felis mattis scelerisque. Ut ante. Nunc at felis. Duis iaculis arcu sed mauris. """ post_data_hash = md5(post_data).hexdigest() post_data_len = len(post_data) stub = WSGIStub(postApp) stub.run( method="POST", uri="http://localhost/foo/thing/baz?xyzzy=quux", stdin=StringIO(post_data) ) e = stub.environ self.assertEqual(e['test.input_hash'], post_data_hash, "Supplied input and processed input hashes should match.") self.assertEqual(int(e['CONTENT_LENGTH']), post_data_len, "Expected CONTENT_LENGTH of %s, found %s" % \ (post_data_len, e['CONTENT_LENGTH']) ) if __name__ == '__main__': unittest.main()