Fixed #23977 -- Added setdefault() method to HttpResponse
This commit is contained in:
parent
d4e449d730
commit
059c9ab24c
|
@ -261,6 +261,11 @@ class HttpResponseBase(six.Iterator):
|
||||||
if httponly:
|
if httponly:
|
||||||
self.cookies[key]['httponly'] = True
|
self.cookies[key]['httponly'] = True
|
||||||
|
|
||||||
|
def setdefault(self, key, value):
|
||||||
|
"""Sets a header unless it has already been set."""
|
||||||
|
if key not in self:
|
||||||
|
self[key] = value
|
||||||
|
|
||||||
def set_signed_cookie(self, key, value, salt='', **kwargs):
|
def set_signed_cookie(self, key, value, salt='', **kwargs):
|
||||||
value = signing.get_cookie_signer(salt=key + salt).sign(value)
|
value = signing.get_cookie_signer(salt=key + salt).sign(value)
|
||||||
return self.set_cookie(key, value, **kwargs)
|
return self.set_cookie(key, value, **kwargs)
|
||||||
|
|
|
@ -708,6 +708,12 @@ Methods
|
||||||
Returns ``True`` or ``False`` based on a case-insensitive check for a
|
Returns ``True`` or ``False`` based on a case-insensitive check for a
|
||||||
header with the given name.
|
header with the given name.
|
||||||
|
|
||||||
|
.. method:: HttpResponse.setdefault(header, value)
|
||||||
|
|
||||||
|
.. versionadded:: 1.8
|
||||||
|
|
||||||
|
Sets a header unless it has already been set.
|
||||||
|
|
||||||
.. method:: HttpResponse.set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False)
|
.. method:: HttpResponse.set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False)
|
||||||
|
|
||||||
Sets a cookie. The parameters are the same as in the
|
Sets a cookie. The parameters are the same as in the
|
||||||
|
|
|
@ -483,6 +483,10 @@ Requests and Responses
|
||||||
like :meth:`~django.http.HttpResponse.getvalue` so that instances can be used
|
like :meth:`~django.http.HttpResponse.getvalue` so that instances can be used
|
||||||
as stream objects.
|
as stream objects.
|
||||||
|
|
||||||
|
* The new :meth:`HttpResponse.setdefault()
|
||||||
|
<django.http.HttpResponse.setdefault>` method allows setting a header unless
|
||||||
|
it has already been set.
|
||||||
|
|
||||||
Tests
|
Tests
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,20 @@ class HttpResponseBaseTests(SimpleTestCase):
|
||||||
with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance cannot tell its position'):
|
with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance cannot tell its position'):
|
||||||
r.tell()
|
r.tell()
|
||||||
|
|
||||||
|
def test_setdefault(self):
|
||||||
|
"""
|
||||||
|
HttpResponseBase.setdefault() should not change an existing header
|
||||||
|
and should be case insensitive.
|
||||||
|
"""
|
||||||
|
r = HttpResponseBase()
|
||||||
|
|
||||||
|
r['Header'] = 'Value'
|
||||||
|
r.setdefault('header', 'changed')
|
||||||
|
self.assertEqual(r['header'], 'Value')
|
||||||
|
|
||||||
|
r.setdefault('x-header', 'DefaultValue')
|
||||||
|
self.assertEqual(r['X-Header'], 'DefaultValue')
|
||||||
|
|
||||||
|
|
||||||
class HttpResponseTests(SimpleTestCase):
|
class HttpResponseTests(SimpleTestCase):
|
||||||
def test_status_code(self):
|
def test_status_code(self):
|
||||||
|
|
Loading…
Reference in New Issue