From d3fe14781747539c470089208fa7aeb1b2cbbd6f Mon Sep 17 00:00:00 2001
From: Michael Merickel <michael@merickel.org>
Date: Thu, 18 Oct 2018 03:45:01 +0200
Subject: [PATCH] fix the dummy request to support the new accept apis

---
 docs/whatsnew-1.10.rst |   13 +++---
 tests/test_testing.py  |   23 +++++++++++
 CHANGES.rst            |   11 +++++
 src/pyramid/testing.py |   18 +++++++++
 4 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index cc70dca..d04e841 100644
--- a/CHANGES.rst
+++ b/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)
 ===================
 
diff --git a/docs/whatsnew-1.10.rst b/docs/whatsnew-1.10.rst
index b14771c..53eed6f 100644
--- a/docs/whatsnew-1.10.rst
+++ b/docs/whatsnew-1.10.rst
@@ -87,14 +87,19 @@
 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
 
@@ -103,10 +108,6 @@
 
 - :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
diff --git a/src/pyramid/testing.py b/src/pyramid/testing.py
index e2549f0..ea86f58 100644
--- a/src/pyramid/testing.py
+++ b/src/pyramid/testing.py
@@ -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
@@ -340,6 +342,7 @@
     charset = 'UTF-8'
     script_name = ''
     _registry = None
+    _accept = None
     request_iface = IRequest
 
     def __init__(
@@ -350,6 +353,7 @@
         path='/',
         cookies=None,
         post=None,
+        accept=None,
         **kw
     ):
         if environ is None:
@@ -388,6 +392,7 @@
         self.virtual_root = None
         self.marshalled = params  # repoze.monty
         self.session = DummySession()
+        self.accept = accept
         self.__dict__.update(kw)
 
     def _get_registry(self):
@@ -403,6 +408,19 @@
 
     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)
diff --git a/tests/test_testing.py b/tests/test_testing.py
index 90e30c9..16c94ee 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -303,6 +303,29 @@
         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,):

--
Gitblit v1.9.3