Use mediatype.ContentMixin if its also extended

This commit is contained in:
Gavin M. Roy 2015-11-20 14:14:39 -05:00
parent 2fdc020dab
commit f028723e45
8 changed files with 39 additions and 10 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ build
dist
env
*.egg-info
.coverage

View file

@ -79,9 +79,12 @@ exceptions will include the stack traces, etc.
Standardized Error Response Documents
-------------------------------------
Version 0.5.0 also introduced the :class:`~sprockets.http.mixins.ErrorWriter`
class which implements ``write_error`` to provide a standard JSON
document response instead of the default HTML response that Tornado
implements.
class which implements ``write_error`` to provide a standard machine-readable
document response instead of the default HTML response that Tornado implements.
If :class:`~sprockets.mixins.mediatype.ContentMixin` is being used as well,
``write_error`` will use
:meth:`~sprockets.mixins.mediatype.ContentMixin.send_response` to send the
document, otherwise it is sent as JSON.
.. autoclass:: sprockets.http.mixins.ErrorWriter
:members:

View file

@ -2,6 +2,9 @@
Release History
===============
`1.0.1`_ (20 Nov 2015)
----------------------
- Add support for ``sprockets.mixins.mediatype`` in ``sprockets.http.mixins.ErrorWriter``
`1.0.0`_ (20 Nov 2015)
----------------------

View file

@ -1 +1,2 @@
nose>=1.3.1,<2
mock>=1.3,<2

View file

@ -2,7 +2,6 @@
#
import os.path
import sys
import setuptools

View file

@ -1,7 +1,7 @@
import os
version_info = (1, 0, 0)
version_info = (1, 0, 1)
__version__ = '.'.join(str(v) for v in version_info)

View file

@ -78,9 +78,14 @@ class ErrorWriter(object):
"""
Write error bodies out consistently.
Mix this class in to your inheritance chain to include error
bodies as a standard JSON document. The error document has
three simple properties:
Mix this class in to your inheritance chain to include error bodies in a
machine-readable document format.
If :class:`~sprockets.mixins.mediatype.ContentMixin` is also in use, it
will send the error response with it, otherwise the response is sent as
a JSON document.
The error document has three simple properties:
**type**
This is the type of exception that occurred or ``null``.
@ -121,5 +126,9 @@ class ErrorWriter(object):
reason = kwargs.get('reason', _get_http_reason(status_code))
error_body.setdefault('message', reason)
self.set_header('Content-Type', 'application/json')
self.write(json.dumps(error_body).encode('utf-8'))
# If sprockets.mixins.media_type is being used, use it
if hasattr(self, 'send_response'):
self.send_response(error_body)
else:
self.set_header('Content-Type', 'application/json; charset=utf-8')
self.write(json.dumps(error_body).encode('utf-8'))

View file

@ -1,5 +1,6 @@
import logging
import json
import mock
from tornado import httputil, testing, web
@ -161,3 +162,15 @@ class ErrorWriterTests(testing.AsyncHTTPTestCase):
body = self._decode_response(response)
self.assertGreater(len(body['traceback']), 0)
def test_that_mediatype_mixin_is_honored(self):
send_response = mock.Mock()
setattr(examples.StatusHandler, 'send_response', send_response)
response = self.fetch('/status/500')
self.assertEqual(response.code, 500)
send_response.assert_called_once_with({
'type': None,
'message': 'Internal Server Error',
'traceback': None
})
delattr(examples.StatusHandler, 'send_response')