kopia lustrzana https://gitlab.com/marnanel/chapeau
view_for_mimetype() passes *args and **kwargs through to the view.
Tests updated, and tidied a bit.merge-requests/2/merge
rodzic
3d3ce9b0f8
commit
03c8c701ec
|
@ -1,8 +1,15 @@
|
|||
from unittest import TestCase
|
||||
from kepi.tophat_ui.view_for_mimetype import view_for_mimetype
|
||||
|
||||
class DummyRequest:
|
||||
def __init__(self, accept):
|
||||
self.headers = {
|
||||
'Accept': accept,
|
||||
}
|
||||
|
||||
class Tests(TestCase):
|
||||
|
||||
def test_for_mimetype(self):
|
||||
def test_simple(self):
|
||||
|
||||
vfm = view_for_mimetype(
|
||||
[
|
||||
|
@ -12,12 +19,6 @@ class Tests(TestCase):
|
|||
default = lambda req: 'df',
|
||||
)
|
||||
|
||||
class DummyRequest:
|
||||
def __init__(self, accept):
|
||||
self.headers = {
|
||||
'Accept': accept,
|
||||
}
|
||||
|
||||
self.assertEqual(
|
||||
vfm(DummyRequest('text/html')),
|
||||
'th',
|
||||
|
@ -55,6 +56,8 @@ class Tests(TestCase):
|
|||
'df',
|
||||
)
|
||||
|
||||
def test_no_default(self):
|
||||
|
||||
vfm_no_default = view_for_mimetype(
|
||||
[
|
||||
('text', 'html', lambda req: 'th'),
|
||||
|
@ -67,6 +70,11 @@ class Tests(TestCase):
|
|||
'th',
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
vfm_no_default(DummyRequest('application/json')),
|
||||
'aj',
|
||||
)
|
||||
|
||||
# But unknown requests get an HttpResponse object
|
||||
# with status_code==406.
|
||||
|
||||
|
@ -79,3 +87,28 @@ class Tests(TestCase):
|
|||
vfm_no_default(DummyRequest('')).status_code,
|
||||
406,
|
||||
)
|
||||
|
||||
def test_extra_params(self):
|
||||
|
||||
vfm = view_for_mimetype(
|
||||
[
|
||||
('text', 'html', lambda req, a: 'th'+a),
|
||||
('application', 'json', lambda req, a: 'aj'+a),
|
||||
],
|
||||
default = lambda req, a: 'df'+a,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
vfm(DummyRequest('text/html'), '123'),
|
||||
'th123',
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
vfm(DummyRequest('application/json'), '123'),
|
||||
'aj123',
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
vfm(DummyRequest('image/jpeg'), '123'),
|
||||
'df123',
|
||||
)
|
||||
|
|
|
@ -35,12 +35,14 @@ def view_for_mimetype(
|
|||
|
||||
def _view_for_mimetype_inner(
|
||||
request,
|
||||
*args, **kwargs,
|
||||
):
|
||||
|
||||
accept_header = request.headers.get('Accept', '')
|
||||
accept_parsed = parse_accept_header(accept_header)
|
||||
|
||||
logger.debug("Accept=%s -> %s", accept_header, accept_parsed)
|
||||
logger.debug("Views: %s", views)
|
||||
|
||||
for want_type, want_subtype, params, q in accept_parsed:
|
||||
|
||||
|
@ -55,14 +57,18 @@ def view_for_mimetype(
|
|||
logger.debug(' -- found %s/%s: %s',
|
||||
have_type, have_subtype, view)
|
||||
|
||||
result = view(request)
|
||||
result = view(request,
|
||||
*args, **kwargs,
|
||||
)
|
||||
return result
|
||||
|
||||
if default is not None:
|
||||
logger.debug(' -- using default: %s',
|
||||
have_type, have_subtype, view)
|
||||
|
||||
result = default(request)
|
||||
result = default(request,
|
||||
*args, **kwargs,
|
||||
)
|
||||
return result
|
||||
else:
|
||||
logger.debug(' -- none found; 406')
|
||||
|
|
Ładowanie…
Reference in New Issue