Permalink
Browse files

post test, test refactoring

  • Loading branch information...
1 parent 9a9ba02 commit 6751c13a0fbe0a9b2b34935bc3bcc52fd0b0a8e7 @reidrac committed Apr 28, 2012
Showing with 83 additions and 36 deletions.
  1. +0 −2 tests/README
  2. +38 −0 tests/base.py
  3. +3 −34 tests/get.py
  4. +41 −0 tests/post.py
  5. +1 −0 tests/run_tests.py
View
@@ -1,2 +0,0 @@
-TODO
-
View
@@ -0,0 +1,38 @@
+
+import json
+import os
+import tempfile
+import unittest
+
+from wsgiref.util import setup_testing_defaults
+
+import wsgi2cgi
+
+class BaseTestCase(unittest.TestCase):
+ def setup_app(self, environ, start_response):
+ wapp = wsgi2cgi.CGI(self.cgi.name, extra_env=self.extra_env)
+ return wapp.application(environ, start_response)
+
+ def start_response(self, status, headers):
+ self.status = status
+ self.headers = headers
+
+ def request_raw(self):
+ app = self.setup_app(self.env, self.start_response)
+ return ''.join(app)
+
+ def request(self):
+ return json.loads(self.request_raw())
+
+ def setUp(self):
+ self.cgi = tempfile.NamedTemporaryFile(delete=False)
+ os.chmod(self.cgi.name, 0700)
+
+ self.env = dict()
+ setup_testing_defaults(self.env)
+
+ self.extra_env = None
+
+ def tearDown(self):
+ os.unlink(self.cgi.name)
+
View
@@ -1,19 +1,15 @@
#!/usr/bin/env python
-import os
-import unittest
-import tempfile
-import json
from StringIO import StringIO
-from wsgiref.util import setup_testing_defaults
+from base import BaseTestCase
import wsgi2cgi
CGI_TEST_JSON = """#!/usr/bin/env python
import os
import json
-print "Content-Type: text/plain\\n\\n"
+print "Content-Type: text/plain\\n"
print json.dumps(dict(os.environ))
"""
@@ -26,34 +22,7 @@
print json.dumps(dict(os.environ))
"""
-class TestGetMethod(unittest.TestCase):
-
- def setup_app(self, environ, start_response):
- wapp = wsgi2cgi.CGI(self.cgi.name, extra_env=self.extra_env)
- return wapp.application(environ, start_response)
-
- def start_response(self, status, headers):
- self.status = status
- self.headers = headers
-
- def request_raw(self):
- app = self.setup_app(self.env, self.start_response)
- return ''.join(app)
-
- def request(self):
- return json.loads(self.request_raw())
-
- def setUp(self):
- self.cgi = tempfile.NamedTemporaryFile(delete=False)
- os.chmod(self.cgi.name, 0700)
-
- self.env = dict()
- setup_testing_defaults(self.env)
-
- self.extra_env = None
-
- def tearDown(self):
- os.unlink(self.cgi.name)
+class TestGetMethod(BaseTestCase):
def test_wsgiref_defaults(self):
self.cgi.write(CGI_TEST_JSON)
View
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+from StringIO import StringIO
+
+from base import BaseTestCase
+import wsgi2cgi
+
+CGI_TEST = """#!/usr/bin/env python
+import os
+import sys
+
+print "Content-Type: text/plain\\n"
+print "METHOD=%s" % os.environ['REQUEST_METHOD']
+indata = sys.stdin.read()
+print indata
+"""
+
+class TestPostMethod(BaseTestCase):
+
+ def test_post(self):
+ self.cgi.write(CGI_TEST)
+ self.cgi.close()
+
+ expected = "Some content to be post\nsome content for the test"
+
+ self.env['REQUEST_METHOD'] = 'POST'
+ self.env['wsgi.input'] = StringIO(expected)
+ self.env['CONTENT_LENGTH'] = str(len(expected))
+
+ response = self.request_raw()
+
+ self.assertEqual(self.status, "200 OK")
+ self.assertEqual(self.headers, [('Content-Type', 'text/plain')])
+
+ self.assertTrue(response.startswith('METHOD=POST\n'))
+ self.assertEqual(response[len('METHOD=POST\n'):], expected + "\n")
+
+
+if __name__ == '__main__':
+ unittest.main()
+
View
@@ -9,6 +9,7 @@
sys.path.append(tests + "/..")
from get import *
+from post import *
if __name__ == '__main__':
unittest.main()

0 comments on commit 6751c13

Please sign in to comment.