Browse Source

Merge "Use WebOb 1.8.1"

Zuul 6 months ago
parent
commit
3e97060669

+ 1
- 24
glance/common/wsgi.py View File

@@ -53,12 +53,6 @@ from glance import i18n
53 53
 from glance.i18n import _, _LE, _LI, _LW
54 54
 
55 55
 
56
-try:
57
-    from webob.acceptparse import AcceptLanguageValidHeader  # noqa
58
-    USING_WEBOB_1_8 = True
59
-except ImportError:
60
-    USING_WEBOB_1_8 = False
61
-
62 56
 bind_opts = [
63 57
     cfg.HostAddressOpt('bind_host',
64 58
                        default='0.0.0.0',
@@ -1048,18 +1042,7 @@ class Request(webob.Request):
1048 1042
         else:
1049 1043
             return content_type
1050 1044
 
1051
-    def _best_match_language_1_7(self):
1052
-        """Determines best available locale from the Accept-Language header.
1053
-
1054
-        :returns: the best language match or None if the 'Accept-Language'
1055
-                  header was not available in the request.
1056
-        """
1057
-        if not self.accept_language:
1058
-            return None
1059
-        langs = i18n.get_available_languages('glance')
1060
-        return self.accept_language.best_match(langs)
1061
-
1062
-    def _best_match_language_1_8(self):
1045
+    def best_match_language(self):
1063 1046
         """Determines best available locale from the Accept-Language header.
1064 1047
 
1065 1048
         :returns: the best language match or None if the 'Accept-Language'
@@ -1076,12 +1059,6 @@ class Request(webob.Request):
1076 1059
             best_match = None
1077 1060
         return best_match
1078 1061
 
1079
-    def best_match_language(self):
1080
-        if USING_WEBOB_1_8:
1081
-            return self._best_match_language_1_8()
1082
-        else:
1083
-            return self._best_match_language_1_7()
1084
-
1085 1062
     def get_range_from_request(self, image_size):
1086 1063
         """Return the `Range` in a request."""
1087 1064
 

+ 21
- 41
glance/tests/unit/common/test_wsgi.py View File

@@ -184,31 +184,20 @@ class RequestTest(test_utils.BaseTestCase):
184 184
         req = wsgi.Request.blank('/', headers={'Accept-Language': 'unknown'})
185 185
         self.assertIsNone(req.best_match_language())
186 186
 
187
-    def test_best_match_language_unknown(self):
188
-        # Test that we are actually invoking language negotiation by webop
187
+    @mock.patch.object(webob.acceptparse.AcceptLanguageValidHeader, 'lookup')
188
+    def test_best_match_language_unknown(self, mock_lookup):
189
+        # Test that we are actually invoking language negotiation by WebOb
189 190
         request = wsgi.Request.blank('/')
190 191
         accepted = 'unknown-lang'
191 192
         request.headers = {'Accept-Language': accepted}
192 193
 
193
-        # TODO(rosmaita): simplify when lower_constraints has webob >= 1.8.1
194
-        try:
195
-            from webob.acceptparse import AcceptLanguageValidHeader  # noqa
196
-            cls = webob.acceptparse.AcceptLanguageValidHeader
197
-            funcname = 'lookup'
198
-            # Bug #1765748: see comment in code in the function under test
199
-            # to understand why this is the correct return value for the
200
-            # webob 1.8.x mock
201
-            retval = 'fake_LANG'
202
-        except ImportError:
203
-            cls = webob.acceptparse.AcceptLanguage
204
-            funcname = 'best_match'
205
-            retval = None
206
-
207
-        with mock.patch.object(cls, funcname) as mocked_function:
208
-            mocked_function.return_value = retval
209
-
210
-            self.assertIsNone(request.best_match_language())
211
-            mocked_function.assert_called_once()
194
+        # Bug #1765748: see comment in code in the function under test
195
+        # to understand why this is the correct return value for the
196
+        # webob 1.8.x mock
197
+        mock_lookup.return_value = 'fake_LANG'
198
+
199
+        self.assertIsNone(request.best_match_language())
200
+        mock_lookup.assert_called_once()
212 201
 
213 202
         # If Accept-Language is missing or empty, match should be None
214 203
         request.headers = {'Accept-Language': ''}
@@ -389,27 +378,18 @@ class ResourceTest(test_utils.BaseTestCase):
389 378
                               resource, request)
390 379
         self.assertEqual(message_es, str(e))
391 380
 
381
+    @mock.patch.object(webob.acceptparse.AcceptLanguageValidHeader, 'lookup')
392 382
     @mock.patch.object(i18n, 'translate')
393
-    def test_translate_exception(self, mock_translate):
394
-        # TODO(rosmaita): simplify when lower_constraints has webob >= 1.8.1
395
-        try:
396
-            from webob.acceptparse import AcceptLanguageValidHeader  # noqa
397
-            cls = webob.acceptparse.AcceptLanguageValidHeader
398
-            funcname = 'lookup'
399
-        except ImportError:
400
-            cls = webob.acceptparse.AcceptLanguage
401
-            funcname = 'best_match'
402
-
403
-        with mock.patch.object(cls, funcname) as mocked_function:
404
-            mock_translate.return_value = 'No Encontrado'
405
-            mocked_function.return_value = 'de'
406
-
407
-            req = wsgi.Request.blank('/tests/123')
408
-            req.headers["Accept-Language"] = "de"
409
-
410
-            e = webob.exc.HTTPNotFound(explanation='Not Found')
411
-            e = wsgi.translate_exception(req, e)
412
-            self.assertEqual('No Encontrado', e.explanation)
383
+    def test_translate_exception(self, mock_translate, mock_lookup):
384
+        mock_translate.return_value = 'No Encontrado'
385
+        mock_lookup.return_value = 'de'
386
+
387
+        req = wsgi.Request.blank('/tests/123')
388
+        req.headers["Accept-Language"] = "de"
389
+
390
+        e = webob.exc.HTTPNotFound(explanation='Not Found')
391
+        e = wsgi.translate_exception(req, e)
392
+        self.assertEqual('No Encontrado', e.explanation)
413 393
 
414 394
     def test_response_headers_encoded(self):
415 395
         # prepare environment

+ 1
- 1
lower-constraints.txt View File

@@ -138,7 +138,7 @@ unittest2==1.1.0
138 138
 urllib3==1.22
139 139
 vine==1.1.4
140 140
 voluptuous==0.11.1
141
-WebOb==1.7.1
141
+WebOb==1.8.1
142 142
 whereto===0.3.0
143 143
 wrapt==1.10.11
144 144
 WSME==0.8.0

+ 12
- 0
releasenotes/notes/use-webob-1.8.1-5c3cd1b1382f063e.yaml View File

@@ -0,0 +1,12 @@
1
+---
2
+other:
3
+  - |
4
+    Negotiation of the 'Accept-Language' header now follows the "Lookup"
5
+    matching scheme described in `RFC 4647, section 3.4
6
+    <https://tools.ietf.org/html/rfc4647.html#section-3.4>`_. The
7
+    "Lookup" scheme is one of the algorithms suggested in `RFC 7231,
8
+    section 5.3.5
9
+    <https://tools.ietf.org/html/rfc7231.html#section-5.3.5>`_. (This is
10
+    due to a change in an underlying library, which previously used a
11
+    matching scheme that did not conform to `RFC 7231
12
+    <https://tools.ietf.org/html/rfc7231.html>`_.)

+ 1
- 1
requirements.txt View File

@@ -10,7 +10,7 @@ SQLAlchemy!=1.1.5,!=1.1.6,!=1.1.7,!=1.1.8,>=1.0.10 # MIT
10 10
 eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT
11 11
 PasteDeploy>=1.5.0 # MIT
12 12
 Routes>=2.3.1 # MIT
13
-WebOb>=1.7.1 # MIT
13
+WebOb>=1.8.1 # MIT
14 14
 sqlalchemy-migrate>=0.11.0 # Apache-2.0
15 15
 sqlparse>=0.2.2 # BSD
16 16
 alembic>=0.8.10 # MIT

Loading…
Cancel
Save