summaryrefslogtreecommitdiff
path: root/test.py
diff options
context:
space:
mode:
Diffstat (limited to 'test.py')
-rwxr-xr-xtest.py43
1 files changed, 19 insertions, 24 deletions
diff --git a/test.py b/test.py
index f46a512..f2e8910 100755
--- a/test.py
+++ b/test.py
@@ -3,12 +3,7 @@
import unittest
import doctest
import wsgiref.validate
-# Cannot use io module as it is broken in 2.6.
-# Writing a str to a io.StringIO results in an exception.
-try:
- import cStringIO as io
-except ImportError:
- import StringIO as io
+import io
from hashlib import md5
import sys
@@ -27,7 +22,7 @@ class Request:
QUERY_STRING="")
self.environ.update({
"wsgi.version": (1, 0),
- "wsgi.input": io.StringIO(),
+ "wsgi.input": io.BytesIO(),
"wsgi.errors": sys.stderr,
"wsgi.url_scheme": "http",
"wsgi.multithread": False,
@@ -122,14 +117,14 @@ class Result:
self.testcase.fail("header %s not found" % name)
def get_data(self):
- return "".join(self.writtendata) + "".join(self.returneddata)
+ return b"".join(self.writtendata) + b"".join(self.returneddata)
from wsgitools import applications
class StaticContentTest(unittest.TestCase):
def setUp(self):
self.app = applications.StaticContent(
- "200 Found", [("Content-Type", "text/plain")], "nothing")
+ "200 Found", [("Content-Type", "text/plain")], b"nothing")
self.req = Request(self)
def testGet(self):
@@ -146,7 +141,7 @@ class StaticContentTest(unittest.TestCase):
class StaticFileTest(unittest.TestCase):
def setUp(self):
- self.app = applications.StaticFile(io.StringIO("success"), "200 Found",
+ self.app = applications.StaticFile(io.BytesIO(b"success"), "200 Found",
[("Content-Type", "text/plain")])
self.req = Request(self)
@@ -167,7 +162,7 @@ from wsgitools import digest
class AuthDigestMiddlewareTest(unittest.TestCase):
def setUp(self):
self.staticapp = applications.StaticContent(
- "200 Found", [("Content-Type", "text/plain")], "success")
+ "200 Found", [("Content-Type", "text/plain")], b"success")
token_gen = digest.AuthTokenGenerator("foo", lambda _: "baz")
self.app = digest.AuthDigestMiddleware(
wsgiref.validate.validator(self.staticapp), token_gen)
@@ -232,28 +227,28 @@ from wsgitools import middlewares
def writing_application(environ, start_response):
write = start_response("404 Not found", [("Content-Type", "text/plain")])
write = start_response("200 Ok", [("Content-Type", "text/plain")])
- write("first")
- yield ""
- yield "second"
+ write(b"first")
+ yield b""
+ yield b"second"
def write_only_application(environ, start_response):
write = start_response("200 Ok", [("Content-Type", "text/plain")])
- write("first")
- write("second")
- yield ""
+ write(b"first")
+ write(b"second")
+ yield b""
class NoWriteCallableMiddlewareTest(unittest.TestCase):
def testWrite(self):
app = middlewares.NoWriteCallableMiddleware(writing_application)
res = Request(self)(app)
self.assertEqual(res.writtendata, [])
- self.assertEqual("".join(res.returneddata), "firstsecond")
+ self.assertEqual(b"".join(res.returneddata), b"firstsecond")
def testWriteOnly(self):
app = middlewares.NoWriteCallableMiddleware(write_only_application)
res = Request(self)(app)
self.assertEqual(res.writtendata, [])
- self.assertEqual("".join(res.returneddata), "firstsecond")
+ self.assertEqual(b"".join(res.returneddata), b"firstsecond")
class StupidIO:
"""file-like without tell method, so StaticFile is not able to
@@ -273,7 +268,7 @@ class StupidIO:
class ContentLengthMiddlewareTest(unittest.TestCase):
def setUp(self):
- self.staticapp = applications.StaticFile(StupidIO("success"),
+ self.staticapp = applications.StaticFile(StupidIO(b"success"),
"200 Found", [("Content-Type", "text/plain")])
self.app = middlewares.ContentLengthMiddleware(self.staticapp,
maxstore=10)
@@ -296,7 +291,7 @@ class ContentLengthMiddlewareTest(unittest.TestCase):
class BasicAuthMiddlewareTest(unittest.TestCase):
def setUp(self):
self.staticapp = applications.StaticContent(
- "200 Found", [("Content-Type", "text/plain")], "success")
+ "200 Found", [("Content-Type", "text/plain")], b"success")
checkpw = middlewares.DictAuthChecker({"bar": "baz"})
self.app = middlewares.BasicAuthMiddleware(
wsgiref.validate.validator(self.staticapp), checkpw)
@@ -340,13 +335,13 @@ import gzip
class GzipWSGIFilterTest(unittest.TestCase):
def testSimple(self):
app = applications.StaticContent("200 Found",
- [("Content-Type", "text/plain")], "nothing")
+ [("Content-Type", "text/plain")], b"nothing")
app = filters.WSGIFilterMiddleware(app, filters.GzipWSGIFilter)
req = Request(self)
req.environ["HTTP_ACCEPT_ENCODING"] = "gzip"
res = req(app)
- data = gzip.GzipFile(fileobj=io.StringIO(res.get_data())).read()
- self.assertEqual(data, "nothing")
+ data = gzip.GzipFile(fileobj=io.BytesIO(res.get_data())).read()
+ self.assertEqual(data, b"nothing")
def alltests(case):
return unittest.TestLoader().loadTestsFromTestCase(case)