Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use a request type instead of a custom predicate

  • Loading branch information...
commit 582e1c4c17724663fff293cee41544cfa836f201 1 parent c042371
@mcdonc mcdonc authored
Showing with 23 additions and 33 deletions.
  1. +13 −7 pyramid_xmlrpc/__init__.py
  2. +10 −26 pyramid_xmlrpc/tests.py
View
20 pyramid_xmlrpc/__init__.py
@@ -2,6 +2,8 @@
import xmlrpclib
from zope.interface import implements
+from zope.interface import alsoProvides
+from zope.interface import Interface
from zope.interface import providedBy
from pyramid.interfaces import IViewClassifier
@@ -143,6 +145,8 @@ def __init__(self, name='', request_type=None, for_=None, permission=None,
context=None, view_mapper=MapplyViewMapper,
renderer='xmlrpc'):
self.name = name
+ if request_type is None:
+ request_type = IXMLRPCRequest
self.request_type = request_type
self.context = context or for_
self.permission = permission
@@ -159,7 +163,7 @@ def __init__(self, name='', request_type=None, for_=None, permission=None,
self.custom_predicates = custom_predicates
self.renderer = renderer
self.view_mapper = view_mapper
- self.custom_predicates = tuple(custom_predicates) + (is_xmlrpc_request,)
+ self.custom_predicates = custom_predicates
def xmlrpc_renderer_factory(info):
def _render(value, system):
@@ -207,18 +211,20 @@ def view(context, request):
request.__dict__.update(info)
return view(inner_context, request)
-def is_xmlrpc_request(context, request):
- return bool(getattr(request, 'is_xmlrpc', False))
+class IXMLRPCRequest(Interface):
+ # marker interface
+ pass
def _set_xmlrpc_params(event, override):
request = event.request
+ headers = request.headers
if (request.content_type == 'text/xml'
and request.method == 'POST'
- and not 'soapaction' in request.headers
- and not 'x-pyramid-avoid-xmlrpc' in request.headers):
+ and not 'soapaction' in headers
+ and not 'x-pyramid-avoid-xmlrpc' in headers):
params, method = parse_xmlrpc_request(request)
request.xmlrpc_params, request.xmlrpc_method = params, method
- request.is_xmlrpc = True
+ alsoProvides(request, IXMLRPCRequest)
if override:
request.override_renderer = 'xmlrpc'
return True
@@ -247,7 +253,7 @@ def omnipresent(config):
config.add_view(
xmlrpc_traversal_view,
renderer='xmlrpc',
- custom_predicates=(is_xmlrpc_request,)
+ request_type=IXMLRPCRequest,
)
includeme = limited
View
36 pyramid_xmlrpc/tests.py
@@ -172,13 +172,14 @@ def _makeOne(self, **kw):
from pyramid_xmlrpc import xmlrpc_config
return xmlrpc_config(**kw)
- def test_it_no_custom_predicates(self):
+ def test_it_no_request_type(self):
+ from pyramid_xmlrpc import IXMLRPCRequest
inst = self._makeOne()
- self.assertEqual(len(inst.custom_predicates), 1)
+ self.assertEqual(inst.request_type, IXMLRPCRequest)
- def test_it_with_custom_predicates(self):
- inst = self._makeOne(custom_predicates=(1,))
- self.assertEqual(len(inst.custom_predicates), 2)
+ def test_it_with_request_type(self):
+ inst = self._makeOne(request_type=1)
+ self.assertEqual(inst.request_type, 1)
class Test_xmlrpc_renderer_factory(unittest.TestCase):
def _callFUT(self, info):
@@ -290,25 +291,6 @@ def theview(request):
result = self._callFUT(context, request)
self.assertEqual(result, '123')
-class Test_is_xmlrpc_request(unittest.TestCase):
- def _callFUT(self, context, request):
- from pyramid_xmlrpc import is_xmlrpc_request
- return is_xmlrpc_request(context, request)
-
- def test_true(self):
- request = testing.DummyRequest()
- request.is_xmlrpc = True
- self.assertEqual(self._callFUT(None, request), True)
-
- def test_false(self):
- request = testing.DummyRequest()
- request.is_xmlrpc = False
- self.assertEqual(self._callFUT(None, request), False)
-
- def test_missing_false(self):
- request = testing.DummyRequest()
- self.assertEqual(self._callFUT(None, request), False)
-
class Test__set_xmlrpc_params(unittest.TestCase):
def _callFUT(self, event, override):
from pyramid_xmlrpc import _set_xmlrpc_params
@@ -356,6 +338,7 @@ def test_false_avoid_xmlrpc(self):
def test_true_no_override(self):
import xmlrpclib
+ from pyramid_xmlrpc import IXMLRPCRequest
request = testing.DummyRequest()
request.content_type = 'text/xml'
request.method = 'POST'
@@ -365,13 +348,14 @@ def test_true_no_override(self):
event.request = request
result = self._callFUT(event, False)
self.assertEqual(result, True)
- self.assertEqual(request.is_xmlrpc, True)
+ self.failUnless(IXMLRPCRequest.providedBy(request))
self.assertEqual(request.xmlrpc_params, ('a',))
self.assertEqual(request.xmlrpc_method, None)
self.assertEqual(getattr(request, 'override_renderer', None), None)
def test_true_with_override(self):
import xmlrpclib
+ from pyramid_xmlrpc import IXMLRPCRequest
request = testing.DummyRequest()
request.content_type = 'text/xml'
request.method = 'POST'
@@ -381,7 +365,7 @@ def test_true_with_override(self):
event.request = request
result = self._callFUT(event, True)
self.assertEqual(result, True)
- self.assertEqual(request.is_xmlrpc, True)
+ self.failUnless(IXMLRPCRequest.providedBy(request))
self.assertEqual(request.xmlrpc_params, ('a',))
self.assertEqual(request.xmlrpc_method, None)
self.assertEqual(request.override_renderer, 'xmlrpc')
Please sign in to comment.
Something went wrong with that request. Please try again.