Skip to content

Commit

Permalink
Merge branch 'fix-error-handler'
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-shawley committed Jun 27, 2019
2 parents 6dec435 + 6fc7941 commit 5a970a3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
7 changes: 6 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
---------

* `1.0.1`_ (27 Jun 2019)

- Fix errant usage of :class:`tornado.web.ErrorHandler`

* `1.0.0`_ (22 Apr 2019)

- Adjust supported Python versions
Expand Down Expand Up @@ -42,7 +46,8 @@ Changelog
- Add :class:`glinda.testing.services.Request`
- Add :class:`glinda.testing.services.Response`

.. _Next Release: https://github.com/dave-shawley/glinda/compare/1.0.0...master
.. _Next Release: https://github.com/dave-shawley/glinda/compare/1.0.1...master
.. _1.0.1: https://github.com/dave-shawley/glinda/compare/1.0.0...1.0.1
.. _1.0.0: https://github.com/dave-shawley/glinda/compare/0.1.0...1.0.0
.. _0.1.0: https://github.com/dave-shawley/glinda/compare/0.0.3...0.1.0
.. _0.0.3: https://github.com/dave-shawley/glinda/compare/0.0.2...0.0.3
Expand Down
2 changes: 1 addition & 1 deletion glinda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (1, 0, 0)
version_info = (1, 0, 1)
__version__ = '.'.join(str(x) for x in version_info)
25 changes: 23 additions & 2 deletions glinda/testing/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""
import collections
import logging
import socket

from tornado import gen, httpserver, httputil, web
Expand Down Expand Up @@ -179,6 +180,8 @@ def __init__(self, name, add_resource_callback):
"""
super(Service, self).__init__()
self.name = name
self.logger = logging.getLogger('.'.join([
__package__, 'Service', name]))
self.add_resource_callback = add_resource_callback

self.acceptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
Expand All @@ -191,6 +194,8 @@ def __init__(self, name, add_resource_callback):
self._responses = collections.defaultdict(list)
self._endpoints = set()

self.logger.info('listening on %s', self.host)

def add_endpoint(self, *path):
"""
Add an endpoint without configuring a response.
Expand All @@ -211,6 +216,7 @@ def _register_endpoint(self, path):
:param path: quoted resource path
"""
if path not in self._endpoints:
self.logger.info('adding endpoint for %s', path)
self.add_resource_callback(self, path)
self._endpoints.add(path)

Expand All @@ -234,6 +240,8 @@ def record_request(self, request):
client request made to one of the services endpoints
"""
self.logger.debug('processing request: method=%s path=%s',
request.method, request.path)
req = Request(request.method, request.path)
req.body = request.body
req.headers.update(request.headers)
Expand Down Expand Up @@ -277,8 +285,16 @@ def get_next_response(self, tornado_request):
"""
key = tornado_request.method, tornado_request.path
try:
return self._responses[key].pop(0)
response = self._responses[key].pop(0)
self.logger.debug('returning response for %s %s: %r',
tornado_request.method, tornado_request.path,
response)
return response
except IndexError:
self.logger.error(
'failed to find response for %s %s: response keys=%r',
tornado_request.method, tornado_request.uri,
list(self._responses.keys()))
raise web.HTTPError(456, 'Unexpected request - %s %s',
tornado_request.method, tornado_request.uri,
reason='Test Configuration Error')
Expand Down Expand Up @@ -345,7 +361,7 @@ class _Application(web.Application):

def __init__(self):
# overridden to install a default handler
super(_Application, self).__init__([web.url('/', web.ErrorHandler)])
super(_Application, self).__init__([web.url('/', _ErrorHandler)])

def add_resource(self, service, resource):
"""
Expand All @@ -363,6 +379,11 @@ def add_resource(self, service, resource):
self.default_router.rules.insert(-1, handler)


class _ErrorHandler(web.RequestHandler):
def prepare(self):
raise web.HTTPError(status_code=456, reason='Unexpected Request')


class _ServiceHandler(web.RequestHandler):
"""
Individual service endpoint.
Expand Down
18 changes: 18 additions & 0 deletions tests/testing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ def test_that_endpoint_responds_with_programmed_response(self):
response = yield client.fetch(service.url_for('resource:test'))
self.assertEqual(response.code, 222)

@tornado.testing.gen_test
def test_that_empty_path_is_usable(self):
service = self.service_layer['service']
service.add_response(services.Request('GET', '/'),
services.Response(222))

client = httpclient.AsyncHTTPClient()
response = yield client.fetch(service.url_for('/'))
self.assertEqual(response.code, 222)

@tornado.testing.gen_test
def test_that_error_handler_works(self):
service = self.service_layer['service']

client = httpclient.AsyncHTTPClient()
response = yield client.fetch(service.url_for('/'), raise_error=False)
self.assertEqual(response.code, 456)


class RequestRecordingTests(tornado.testing.AsyncTestCase):

Expand Down

0 comments on commit 5a970a3

Please sign in to comment.