summaryrefslogtreecommitdiff
path: root/src/webob
diff options
context:
space:
mode:
authorIra Lun <sammyrosajoe@gmail.com>2017-08-29 19:46:39 +0100
committerIra Lun <sammyrosajoe@gmail.com>2017-08-29 19:49:17 +0100
commit29b6956245cf907bc063c8eb9a95c003e962e860 (patch)
tree5d155c29ed1315223e8ec8df074242a877554112 /src/webob
parentc87520a180e76b1454a23280603cc9911381aa9b (diff)
downloadwebob-29b6956245cf907bc063c8eb9a95c003e962e860.tar.gz
Add accept_encoding_property and tests.
Diffstat (limited to 'src/webob')
-rw-r--r--src/webob/acceptparse.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/webob/acceptparse.py b/src/webob/acceptparse.py
index 7733d27..db63a34 100644
--- a/src/webob/acceptparse.py
+++ b/src/webob/acceptparse.py
@@ -1241,6 +1241,67 @@ def create_accept_encoding_header(header_value):
return AcceptEncodingInvalidHeader(header_value=header_value)
+def accept_encoding_property():
+ doc = """
+ Property representing the ``Accept-Encoding`` header.
+
+ (:rfc:`RFC 7231, section 5.3.4 <7231#section-5.3.4>`)
+
+ The header value in the request environ is parsed and a new object
+ representing the header is created every time we *get* the value of the
+ property. (*set* and *del* change the header value in the request
+ environ, and do not involve parsing.)
+ """
+
+ ENVIRON_KEY = 'HTTP_ACCEPT_ENCODING'
+
+ def fget(request):
+ """Get an object representing the header in the request."""
+ return create_accept_encoding_header(
+ header_value=request.environ.get(ENVIRON_KEY)
+ )
+
+ def fset(request, value):
+ """
+ Set the corresponding key in the request environ.
+
+ `value` can be:
+
+ * ``None``
+ * a ``str`` header value
+ * a ``dict``, with content-coding, ``identity`` or ``*`` ``str``\ s as
+ keys, and qvalue ``float``\ s as values
+ * a ``tuple`` or ``list``, where each item is either a header element
+ ``str``, or a (content-coding/``identity``/``*``, qvalue) ``tuple``
+ or ``list``
+ * an :class:`AcceptEncodingValidHeader`,
+ :class:`AcceptEncodingNoHeader`, or
+ :class:`AcceptEncodingInvalidHeader` instance
+ * object of any other type that returns a value for ``__str__``
+ """
+ if value is None or isinstance(value, AcceptEncodingNoHeader):
+ fdel(request=request)
+ else:
+ if isinstance(
+ value, (AcceptEncodingValidHeader, AcceptEncodingInvalidHeader)
+ ):
+ header_value = value.header_value
+ else:
+ header_value = AcceptEncoding._python_value_to_header_str(
+ value=value,
+ )
+ request.environ[ENVIRON_KEY] = header_value
+
+ def fdel(request):
+ """Delete the corresponding key from the request environ."""
+ try:
+ del request.environ[ENVIRON_KEY]
+ except KeyError:
+ pass
+
+ return property(fget, fset, fdel, textwrap.dedent(doc))
+
+
class AcceptLanguage(object):
"""
Represent an ``Accept-Language`` header.