summaryrefslogtreecommitdiff
path: root/dummyserver/handlers.py
diff options
context:
space:
mode:
Diffstat (limited to 'dummyserver/handlers.py')
-rw-r--r--dummyserver/handlers.py75
1 files changed, 57 insertions, 18 deletions
diff --git a/dummyserver/handlers.py b/dummyserver/handlers.py
index 72faa1aa..53fbe4a8 100644
--- a/dummyserver/handlers.py
+++ b/dummyserver/handlers.py
@@ -9,7 +9,7 @@ import time
import zlib
from io import BytesIO
-from tornado.wsgi import HTTPRequest
+from tornado.web import RequestHandler
try:
from urllib.parse import urlsplit
@@ -21,23 +21,34 @@ log = logging.getLogger(__name__)
class Response(object):
def __init__(self, body='', status='200 OK', headers=None):
- if not isinstance(body, bytes):
- body = body.encode('utf8')
-
self.body = body
self.status = status
self.headers = headers or [("Content-type", "text/plain")]
- def __call__(self, environ, start_response):
- start_response(self.status, self.headers)
- return [self.body]
+ def __call__(self, request_handler):
+ status, reason = self.status.split(' ', 1)
+ request_handler.set_status(int(status), reason)
+ for header,value in self.headers:
+ request_handler.add_header(header,value)
+
+ # chunked
+ if isinstance(self.body, list):
+ for item in self.body:
+ if not isinstance(item, bytes):
+ item = item.encode('utf8')
+ request_handler.write(item)
+ request_handler.flush()
+ else:
+ body = self.body
+ if not isinstance(body, bytes):
+ body = body.encode('utf8')
+ request_handler.write(body)
-class WSGIHandler(object):
- pass
+RETRY_TEST_NAMES = collections.defaultdict(int)
-class TestingApp(WSGIHandler):
+class TestingApp(RequestHandler):
"""
Simple app that performs various operations, useful for testing an HTTP
library.
@@ -46,10 +57,25 @@ class TestingApp(WSGIHandler):
it exists. Status code 200 indicates success, 400 indicates failure. Each
method has its own conditions for success/failure.
"""
- def __call__(self, environ, start_response):
- """ Call the correct method in this class based on the incoming URI """
- req = HTTPRequest(environ)
+ def get(self):
+ """ Handle GET requests """
+ self._call_method()
+
+ def post(self):
+ """ Handle POST requests """
+ self._call_method()
+ def put(self):
+ """ Handle PUT requests """
+ self._call_method()
+
+ def options(self):
+ """ Handle OPTIONS requests """
+ self._call_method()
+
+ def _call_method(self):
+ """ Call the correct method in this class based on the incoming URI """
+ req = self.request
req.params = {}
for k, v in req.arguments.items():
req.params[k] = next(iter(v))
@@ -60,13 +86,14 @@ class TestingApp(WSGIHandler):
target = path[1:].replace('/', '_')
method = getattr(self, target, self.index)
+
resp = method(req)
if dict(resp.headers).get('Connection') == 'close':
# FIXME: Can we kill the connection somehow?
pass
- return resp(environ, start_response)
+ resp(self)
def index(self, _request):
"Render simple message"
@@ -184,15 +211,27 @@ class TestingApp(WSGIHandler):
return Response("test-name header not set",
status="400 Bad Request")
- if not hasattr(self, 'retry_test_names'):
- self.retry_test_names = collections.defaultdict(int)
- self.retry_test_names[test_name] += 1
+ RETRY_TEST_NAMES[test_name] += 1
- if self.retry_test_names[test_name] >= 2:
+ if RETRY_TEST_NAMES[test_name] >= 2:
return Response("Retry successful!")
else:
return Response("need to keep retrying!", status="418 I'm A Teapot")
+ def chunked(self, request):
+ return Response(['123'] * 4)
+
+ def chunked_gzip(self, request):
+ chunks = []
+ compressor = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS)
+
+ for uncompressed in [b'123'] * 4:
+ chunks.append(compressor.compress(uncompressed))
+
+ chunks.append(compressor.flush())
+
+ return Response(chunks, headers=[('Content-Encoding', 'gzip')])
+
def shutdown(self, request):
sys.exit()