Skip to content

Commit

Permalink
Add a request.current_service attribute (fixes #105)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Dec 14, 2016
1 parent 3b4c0ae commit 857dce3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ CHANGELOG
2.3.0 (unreleased)
==================

**New features**

- Add a ``request.current_service`` attribute (fixes #105)

**Bug fixes**

- Fix ``cornice.cors.get_cors_preflight_view`` to make it parse
Expand Down
3 changes: 2 additions & 1 deletion cornice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
handle_exceptions,
register_resource_views,
)
from cornice.util import ContentTypePredicate
from cornice.util import ContentTypePredicate, current_service
from pyramid.events import NewRequest
from pyramid.httpexceptions import HTTPNotFound, HTTPForbidden
from pyramid.security import NO_PERMISSION_REQUIRED
Expand Down Expand Up @@ -79,6 +79,7 @@ def includeme(config):
config.add_subscriber(wrap_request, NewRequest)
config.add_renderer('simplejson', util.json_renderer)
config.add_view_predicate('content_type', ContentTypePredicate)
config.add_request_method(current_service, reify=True)

settings = config.get_settings()
if asbool(settings.get('handle_exceptions', True)):
Expand Down
17 changes: 17 additions & 0 deletions cornice/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,20 @@ def func_name(f):
return '{0}.{1}'.format(f.im_class.__name__, f.__name__) # Python 2
else: # pragma: no cover
return f.__name__ # Python 2


def current_service(request):
"""Return the Cornice service matching the specified request.
:returns: the service or None if unmatched.
:rtype: cornice.Service
"""
if request.matched_route:
services = request.registry.cornice_services
pattern = request.matched_route.pattern
try:
service = services[pattern]
except KeyError:
return None
else:
return service
17 changes: 17 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ def test_extract_form_urlencoded_data_is_deprecated(self):
with mock.patch('cornice.util.warnings') as mocked:
util.extract_form_urlencoded_data(mock.MagicMock())
self.assertTrue(mocked.warn.called)


class CurrentServiceTest(unittest.TestCase):

def test_current_service_returns_the_service_for_existing_patterns(self):
request = mock.MagicMock()
request.matched_route.pattern = '/buckets'
request.registry.cornice_services = {'/buckets': mock.sentinel.service}

self.assertEqual(util.current_service(request), mock.sentinel.service)

def test_current_service_returns_none_for_unexisting_patterns(self):
request = mock.MagicMock()
request.matched_route.pattern = '/unexisting'
request.registry.cornice_services = {}

self.assertEqual(util.current_service(request), None)

0 comments on commit 857dce3

Please sign in to comment.