Skip to content

Commit

Permalink
fix the dummy request to support the new accept apis
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Oct 18, 2018
1 parent 66a767f commit d3fe147
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,14 @@
unreleased
==========

Bug Fixes
---------

- Fix the ``pyramid.testing.DummyRequest`` to support the new
``request.accept`` API so that ``acceptable_offers`` is available even
when code sets the value to a string.
See https://github.com/Pylons/pyramid/pull/3396

1.10a1 (2018-10-15)
===================

Expand Down
13 changes: 7 additions & 6 deletions docs/whatsnew-1.10.rst
Expand Up @@ -87,14 +87,19 @@ Deprecations
Backward Incompatibilities
--------------------------

- On Python 3.4+ the ``repoze.lru`` dependency is dropped. If you were using this package directly in your apps you should make sure that you are depending on it directly within your project.
- Removed ``pyramid.config.Configurator.set_request_property`` which had been deprecated since :app:`Pyramid` 1.5.
Instead use :meth:`pyramid.config.Configurator.add_request_method` with ``reify=True`` or ``property=True``.
See https://github.com/Pylons/pyramid/pull/3368

- On Python 3.4+ the ``repoze.lru`` dependency is dropped.
If you were using this package directly in your apps you should make sure that you are depending on it directly within your project.
See https://github.com/Pylons/pyramid/pull/3140

- Remove the ``permission`` argument from :meth:`pyramid.config.Configurator.add_route`.
This was an argument left over from a feature removed in :app:`Pyramid` 1.5 and has had no effect since then.
See https://github.com/Pylons/pyramid/pull/3299

- Modify the builtin session implementations to set ``SameSite='Lax'`` on cookies.
- Modified the builtin session implementations to set ``SameSite='Lax'`` on cookies.
This affects :func:`pyramid.session.BaseCookieSessionFactory`, :func:`pyramid.session.SignedCookieSessionFactory`, and :func:`pyramid.session.UnencryptedCookieSessionFactoryConfig`.
See https://github.com/Pylons/pyramid/pull/3300

Expand All @@ -104,10 +109,6 @@ Backward Incompatibilities
- :meth:`pyramid.config.Configurator.add_notfound_view` uses default redirect class exception :class:`pyramid.httpexceptions.HTTPTemporaryRedirect` instead of previous :class:`pyramid.httpexceptions.HTTPFound`.
See https://github.com/Pylons/pyramid/pull/3328

- Removed ``pyramid.config.Configurator.set_request_property`` which had been deprecated since :app:`Pyramid` 1.5.
Instead use :meth:`pyramid.config.Configurator.add_request_method` with ``reify=True`` or ``property=True``.
See https://github.com/Pylons/pyramid/pull/3368

- Removed the ``principal`` keyword argument from :func:`pyramid.security.remember` which had been deprecated since :app:`Pyramid` 1.6 and replaced by the ``userid`` argument.
See https://github.com/Pylons/pyramid/pull/3369

Expand Down
18 changes: 18 additions & 0 deletions src/pyramid/testing.py
Expand Up @@ -2,6 +2,8 @@
import os
from contextlib import contextmanager

from webob.acceptparse import create_accept_header

from zope.interface import implementer, alsoProvides

from pyramid.interfaces import IRequest, ISession
Expand Down Expand Up @@ -340,6 +342,7 @@ class DummyRequest(
charset = 'UTF-8'
script_name = ''
_registry = None
_accept = None
request_iface = IRequest

def __init__(
Expand All @@ -350,6 +353,7 @@ def __init__(
path='/',
cookies=None,
post=None,
accept=None,
**kw
):
if environ is None:
Expand Down Expand Up @@ -388,6 +392,7 @@ def __init__(
self.virtual_root = None
self.marshalled = params # repoze.monty
self.session = DummySession()
self.accept = accept
self.__dict__.update(kw)

def _get_registry(self):
Expand All @@ -403,6 +408,19 @@ def _del_registry(self):

registry = property(_get_registry, _set_registry, _del_registry)

def _set_accept(self, value):
self._accept = create_accept_header(value)

def _get_accept(self):
if self._accept is None:
self._accept = create_accept_header(None)
return self._accept

def _del_accept(self):
self._accept = None

accept = property(_get_accept, _set_accept, _del_accept)

@reify
def response(self):
f = _get_response_factory(self.registry)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_testing.py
Expand Up @@ -303,6 +303,29 @@ def test_response_without_responsefactory(self):
self.assertEqual(resp.__class__, Response)
self.assertTrue(request.response is resp) # reified

def test_default_accept(self):
request = self._makeOne()
self.assertEqual(
request.accept.acceptable_offers(['text/html']),
[('text/html', 1.0)],
)

request.accept = 'text/plain'
self.assertEqual(request.accept.acceptable_offers(['text/html']), [])

del request.accept
self.assertEqual(
request.accept.acceptable_offers(['text/html']),
[('text/html', 1.0)],
)

def test_accept__init__(self):
request = self._makeOne(accept='text/plain')
self.assertEqual(
request.accept.acceptable_offers(['text/html', 'text/plain']),
[('text/plain', 1.0)],
)


class TestDummyTemplateRenderer(unittest.TestCase):
def _getTargetClass(self,):
Expand Down

0 comments on commit d3fe147

Please sign in to comment.