summaryrefslogtreecommitdiff
path: root/test.py
diff options
context:
space:
mode:
Diffstat (limited to 'test.py')
-rwxr-xr-xtest.py72
1 files changed, 63 insertions, 9 deletions
diff --git a/test.py b/test.py
index 1183d63..8e628cd 100755
--- a/test.py
+++ b/test.py
@@ -3,6 +3,7 @@
import base64
import unittest
import doctest
+import re
import wsgiref.validate
import io
from hashlib import md5
@@ -36,21 +37,25 @@ class Request:
"""
@type key: str
@type value: str
+ @returns: self
"""
self.environ[key] = value
+ return self
def setmethod(self, request_method):
"""
@type request_method: str
+ @returns: self
"""
- self.setenv("REQUEST_METHOD", request_method)
+ return self.setenv("REQUEST_METHOD", request_method)
def setheader(self, name, value):
"""
@type name: str
@type value: str
+ @returns: self
"""
- self.setenv(name.upper().replace('-', '_'), value)
+ return self.setenv("HTTP_" + name.upper().replace('-', '_'), value)
def copy(self):
req = Request(self.testcase)
@@ -178,14 +183,14 @@ class AuthDigestMiddlewareTest(unittest.TestCase):
def test401garbage(self):
req = self.req.copy()
- req.setheader('http-authorization', 'Garbage')
+ req.setheader('Authorization', 'Garbage')
res = req(self.app)
res.status(401)
res.header("WWW-Authenticate", lambda _: True)
def test401digestgarbage(self):
req = self.req.copy()
- req.setheader('http-authorization', 'Digest ","')
+ req.setheader('Authorization', 'Digest ","')
res = req(self.app)
res.status(401)
res.header("WWW-Authenticate", lambda _: True)
@@ -199,7 +204,7 @@ class AuthDigestMiddlewareTest(unittest.TestCase):
token = md5(str2bytes("bar:foo:%s" % password)).hexdigest()
other = md5(str2bytes("GET:")).hexdigest()
resp = md5(str2bytes("%s:%s:%s" % (token, nonce, other))).hexdigest()
- req.setheader('http-authorization', 'Digest algorithm=md5,nonce="%s",' \
+ req.setheader('Authorization', 'Digest algorithm=md5,nonce="%s",' \
'uri=,username=bar,response="%s"' % (nonce, resp))
res = req(self.app)
res.status(status)
@@ -220,7 +225,7 @@ class AuthDigestMiddlewareTest(unittest.TestCase):
other = md5(str2bytes("GET:")).hexdigest()
resp = "%s:%s:1:qux:auth:%s" % (token, nonce, other)
resp = md5(str2bytes(resp)).hexdigest()
- req.setheader('http-authorization', 'Digest algorithm=md5,nonce="%s",' \
+ req.setheader('Authorization', 'Digest algorithm=md5,nonce="%s",' \
'uri=,username=bar,response="%s",qop=auth,nc=1,' \
'cnonce=qux' % (nonce, resp))
res = req(self.app)
@@ -292,6 +297,37 @@ class ContentLengthMiddlewareTest(unittest.TestCase):
res.status("200 Found")
res.header("Content-length", "7")
+class CachingMiddlewareTest(unittest.TestCase):
+ def setUp(self):
+ self.cached = middlewares.CachingMiddleware(self.app)
+ self.accessed = dict()
+
+ def app(self, environ, start_response):
+ count = self.accessed.get(environ["SCRIPT_NAME"], 0) + 1
+ self.accessed[environ["SCRIPT_NAME"]] = count
+ headers = [("Content-Type", "text/plain")]
+ if "maxage0" in environ["SCRIPT_NAME"]:
+ headers.append(("Cache-Control", "max-age=0"))
+ start_response("200 Found", headers)
+ return ["%d" % count]
+
+ def testCache(self):
+ res = Request(self)(self.cached)
+ res.status(200)
+ self.assertEqual(res.get_data(), "1")
+ res = Request(self)(self.cached)
+ res.status(200)
+ self.assertEqual(res.get_data(), "1")
+
+ def testNoCache(self):
+ res = Request(self)(self.cached)
+ res.status(200)
+ self.assertEqual(res.get_data(), "1")
+ res = Request(self).setheader(
+ "Cache-Control", "max-age=0")(self.cached)
+ res.status(200)
+ self.assertEqual(res.get_data(), "2")
+
class BasicAuthMiddlewareTest(unittest.TestCase):
def setUp(self):
self.staticapp = applications.StaticContent(
@@ -308,14 +344,14 @@ class BasicAuthMiddlewareTest(unittest.TestCase):
def test401garbage(self):
req = self.req.copy()
- req.setheader('http-authorization', 'Garbage')
+ req.setheader('Authorization', 'Garbage')
res = req(self.app)
res.status(401)
res.header("WWW-Authenticate", lambda _: True)
def test401basicgarbage(self):
req = self.req.copy()
- req.setheader('http-authorization', 'Basic ()')
+ req.setheader('Authorization', 'Basic ()')
res = req(self.app)
res.status(401)
res.header("WWW-Authenticate", lambda _: True)
@@ -324,7 +360,7 @@ class BasicAuthMiddlewareTest(unittest.TestCase):
req = self.req.copy()
token = "bar:%s" % password
token = bytes2str(base64.b64encode(str2bytes(token)))
- req.setheader('http-authorization', 'Basic %s' % token)
+ req.setheader('Authorization', 'Basic %s' % token)
res = req(self.app)
res.status(status)
@@ -337,6 +373,22 @@ class BasicAuthMiddlewareTest(unittest.TestCase):
from wsgitools import filters
import gzip
+class RequestLogWSGIFilterTest(unittest.TestCase):
+ def testSimple(self):
+ app = applications.StaticContent("200 Found",
+ [("Content-Type", "text/plain")], "nothing")
+ log = io.StringIO()
+ logfilter = filters.RequestLogWSGIFilter.creator(log)
+ app = filters.WSGIFilterMiddleware(app, logfilter)
+ req = Request(self)
+ req.environ["REMOTE_ADDR"] = "1.2.3.4"
+ req.environ["PATH_INFO"] = "/"
+ req.environ["HTTP_USER_AGENT"] = "wsgitools-test"
+ res = req(app)
+ logged = log.getvalue()
+ self.assert_(re.match(r'^1\.2\.3\.4 - - \[[^]]+\] "GET /" '
+ r'200 7 - "wsgitools-test"', logged))
+
class GzipWSGIFilterTest(unittest.TestCase):
def testSimple(self):
app = applications.StaticContent("200 Found",
@@ -357,8 +409,10 @@ fullsuite.addTest(alltests(StaticContentTest))
fullsuite.addTest(alltests(StaticFileTest))
fullsuite.addTest(alltests(AuthDigestMiddlewareTest))
fullsuite.addTest(alltests(ContentLengthMiddlewareTest))
+fullsuite.addTest(alltests(CachingMiddlewareTest))
fullsuite.addTest(alltests(BasicAuthMiddlewareTest))
fullsuite.addTest(alltests(NoWriteCallableMiddlewareTest))
+fullsuite.addTest(alltests(RequestLogWSGIFilterTest))
fullsuite.addTest(alltests(GzipWSGIFilterTest))
if __name__ == "__main__":