From 6801b595308651a4d103b2e1c45d4f5387e151e5 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 7 Jan 2009 13:09:22 +0000 Subject: Add a 'post' argument to the DummyRequest ctor. --- repoze/bfg/testing.py | 25 +++++++++++++++++++------ repoze/bfg/tests/test_testing.py | 28 +++++++++++++++++++++------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 3480184bd..5a90033c7 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -306,17 +306,26 @@ class DummyModel: return inst class DummyRequest: - """ A dummy request object (imitates a :term:`WebOb` ``Request`` - object). The named constructor arguments correspond to their - WebOb equivalents. Extra keyword arguments are assigned as - attributes of the request itself.""" + """ A dummy request object (imitates a :term:`WebOb` ``Request`` object). + + The ``params``, ``environ``, ``headers``, ``path``, and ``cookies`` + arguments correspond to their WebOb equivalents. + + The ``post`` argument, if passed, populates the request's + ``POST`` attribute, but *not* ``params``, in order to allow testing + that the app accepts data for a given view only from POST requests. + This argument also sets ``self.method`` to "POST". + + Extra keyword arguments are assigned as attributes of the request + itself. + """ implements(IRequest) method = 'GET' application_url = 'http://example.com' host = 'example.com:80' content_length = 0 def __init__(self, params=None, environ=None, headers=None, path='/', - cookies=None, **kw): + cookies=None, post=None, **kw): if environ is None: environ = {} if params is None: @@ -330,7 +339,11 @@ class DummyRequest: self.params = params self.cookies = cookies self.GET = params - self.POST = params + if post is not None: + self.method = 'POST' + self.POST = post + else: + self.POST = params self.host_url = self.application_url self.path_url = self.application_url self.url = self.application_url diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index e0faf1855..38d41be06 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -363,6 +363,12 @@ class TestDummyRequest(unittest.TestCase): self.assertEqual(request.subpath, []) self.assertEqual(request.context, None) + def test_params_explicit(self): + request = self._makeOne(params = {'foo':'bar'}) + self.assertEqual(request.params['foo'], 'bar') + self.assertEqual(request.GET['foo'], 'bar') + self.assertEqual(request.POST['foo'], 'bar') + def test_environ_explicit(self): request = self._makeOne(environ = {'PATH_INFO':'/foo'}) self.assertEqual(request.environ['PATH_INFO'], '/foo') @@ -379,19 +385,27 @@ class TestDummyRequest(unittest.TestCase): request = self._makeOne(cookies = {'type': 'gingersnap'}) self.assertEqual(request.cookies['type'], 'gingersnap') - def test_kwargs(self): - request = self._makeOne(water = 1) - self.assertEqual(request.water, 1) - - def test_with_post(self): + def test_post_explicit(self): POST = {'foo': 'bar', 'baz': 'qux'} request = self._makeOne(post=POST) self.assertEqual(request.method, 'POST') self.assertEqual(request.POST, POST) - # Unlike a normal request, *don't* put explict POST data into params: - # doing so masks a possible XSS bug in the app. + # N.B.: Unlike a normal request, passing 'post' should *not* put + # explict POST data into params: doing so masks a possible + # XSS bug in the app. Tests for apps which don't care about + # the distinction should just use 'params'. self.assertEqual(request.params, {}) + def test_post_empty_shadows_params(self): + request = self._makeOne(params={'foo': 'bar'}, post={}) + self.assertEqual(request.method, 'POST') + self.assertEqual(request.params.get('foo'), 'bar') + self.assertEqual(request.POST.get('foo'), None) + + def test_kwargs(self): + request = self._makeOne(water = 1) + self.assertEqual(request.water, 1) + class TestDummyTemplateRenderer(unittest.TestCase): def _getTargetClass(self): from repoze.bfg.testing import DummyTemplateRenderer -- cgit v1.2.3