summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIra Lun <sammyrosajoe@gmail.com>2017-08-29 21:26:09 +0100
committerIra Lun <sammyrosajoe@gmail.com>2017-08-29 21:26:09 +0100
commitbc7530fcbfb836e017be2adcfa19fe2151d190d0 (patch)
treee0252c244f17e3e94b6322f4d35bec433eece151
parente77caed85ebb2d7edef97257d989653fcba20963 (diff)
downloadwebob-bc7530fcbfb836e017be2adcfa19fe2151d190d0.tar.gz
Add create_accept_header, docs and tests.
-rw-r--r--docs/api/webob.txt2
-rw-r--r--src/webob/acceptparse.py22
-rw-r--r--tests/test_acceptparse.py21
3 files changed, 45 insertions, 0 deletions
diff --git a/docs/api/webob.txt b/docs/api/webob.txt
index 2f3111d..b3905ba 100644
--- a/docs/api/webob.txt
+++ b/docs/api/webob.txt
@@ -9,6 +9,8 @@ Accept-*
.. automodule:: webob.acceptparse
+.. autofunction:: create_accept_header
+
.. autoclass:: Accept
:members: parse
diff --git a/src/webob/acceptparse.py b/src/webob/acceptparse.py
index d2b6007..02c0cb6 100644
--- a/src/webob/acceptparse.py
+++ b/src/webob/acceptparse.py
@@ -1485,6 +1485,28 @@ class AcceptInvalidHeader(_AcceptInvalidOrNoHeader):
return AcceptNoHeader()
+def create_accept_header(header_value):
+ """
+ Create an object representing the ``Accept`` header in a request.
+
+ :param header_value: (``str``) header value
+ :return: If `header_value` is ``None``, an :class:`AcceptNoHeader`
+ instance.
+
+ | If `header_value` is a valid ``Accept`` header, an
+ :class:`AcceptValidHeader` instance.
+
+ | If `header_value` is an invalid ``Accept`` header, an
+ :class:`AcceptInvalidHeader` instance.
+ """
+ if header_value is None:
+ return AcceptNoHeader()
+ try:
+ return AcceptValidHeader(header_value=header_value)
+ except ValueError:
+ return AcceptInvalidHeader(header_value=header_value)
+
+
class NilAccept(object):
"""
Represents a generic ``Accept-*`` style header when it is not present in
diff --git a/tests/test_acceptparse.py b/tests/test_acceptparse.py
index 49f3f37..a8c435e 100644
--- a/tests/test_acceptparse.py
+++ b/tests/test_acceptparse.py
@@ -27,6 +27,7 @@ from webob.acceptparse import (
accept_property,
create_accept_charset_header,
create_accept_encoding_header,
+ create_accept_header,
create_accept_language_header,
MIMEAccept,
NilAccept,
@@ -2230,6 +2231,26 @@ class TestAcceptInvalidHeader(object):
assert returned == 1.0
+class TestCreateAcceptHeader(object):
+ def test_header_value_is_None(self):
+ header_value = None
+ returned = create_accept_header(header_value=header_value)
+ assert isinstance(returned, AcceptNoHeader)
+ assert returned.header_value == header_value
+
+ def test_header_value_is_valid(self):
+ header_value = 'text/html, text/plain;q=0.9'
+ returned = create_accept_header(header_value=header_value)
+ assert isinstance(returned, AcceptValidHeader)
+ assert returned.header_value == header_value
+
+ @pytest.mark.parametrize('header_value', [', ', 'noslash'])
+ def test_header_value_is_invalid(self, header_value):
+ returned = create_accept_header(header_value=header_value)
+ assert isinstance(returned, AcceptInvalidHeader)
+ assert returned.header_value == header_value
+
+
class TestAcceptCharset(object):
@pytest.mark.parametrize('value', [
'',