Converted httpwrappers tests to unittest.
We have always been at war with doctests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13313 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1b04cb2348
commit
8a10078a03
|
@ -1,476 +1,214 @@
|
||||||
"""
|
import unittest
|
||||||
###################
|
import pickle
|
||||||
# Empty QueryDict #
|
from django.http import QueryDict, HttpResponse, CompatCookie, BadHeaderError
|
||||||
###################
|
|
||||||
|
class QueryDictTests(unittest.TestCase):
|
||||||
>>> q = QueryDict('')
|
def test_missing_key(self):
|
||||||
|
q = QueryDict('')
|
||||||
>>> q['foo']
|
self.assertRaises(KeyError, q.__getitem__, 'foo')
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
def test_immutability(self):
|
||||||
MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
|
q = QueryDict('')
|
||||||
|
self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
|
||||||
>>> q['something'] = 'bar'
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
|
||||||
Traceback (most recent call last):
|
self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
|
||||||
...
|
self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertRaises(AttributeError, q.pop, 'foo')
|
||||||
|
self.assertRaises(AttributeError, q.popitem)
|
||||||
>>> q.get('foo', 'default')
|
self.assertRaises(AttributeError, q.clear)
|
||||||
'default'
|
|
||||||
|
def test_immutable_get_with_default(self):
|
||||||
>>> q.getlist('foo')
|
q = QueryDict('')
|
||||||
[]
|
self.assertEqual(q.get('foo', 'default'), 'default')
|
||||||
|
|
||||||
>>> q.setlist('foo', ['bar', 'baz'])
|
def test_immutable_basic_operations(self):
|
||||||
Traceback (most recent call last):
|
q = QueryDict('')
|
||||||
...
|
self.assertEqual(q.getlist('foo'), [])
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertEqual(q.has_key('foo'), False)
|
||||||
|
self.assertEqual('foo' in q, False)
|
||||||
>>> q.appendlist('foo', ['bar'])
|
self.assertEqual(q.items(), [])
|
||||||
Traceback (most recent call last):
|
self.assertEqual(q.lists(), [])
|
||||||
...
|
self.assertEqual(q.items(), [])
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertEqual(q.keys(), [])
|
||||||
|
self.assertEqual(q.values(), [])
|
||||||
>>> q.has_key('foo')
|
self.assertEqual(len(q), 0)
|
||||||
False
|
self.assertEqual(q.urlencode(), '')
|
||||||
|
|
||||||
>>> 'foo' in q
|
def test_single_key_value(self):
|
||||||
False
|
"""Test QueryDict with one key/value pair"""
|
||||||
|
|
||||||
>>> q.items()
|
q = QueryDict('foo=bar')
|
||||||
[]
|
self.assertEqual(q['foo'], 'bar')
|
||||||
|
self.assertRaises(KeyError, q.__getitem__, 'bar')
|
||||||
>>> q.lists()
|
self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
|
||||||
[]
|
|
||||||
|
self.assertEqual(q.get('foo', 'default'), 'bar')
|
||||||
>>> q.keys()
|
self.assertEqual(q.get('bar', 'default'), 'default')
|
||||||
[]
|
self.assertEqual(q.getlist('foo'), ['bar'])
|
||||||
|
self.assertEqual(q.getlist('bar'), [])
|
||||||
>>> q.values()
|
|
||||||
[]
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
|
||||||
|
self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
|
||||||
>>> len(q)
|
|
||||||
0
|
self.failUnless(q.has_key('foo'))
|
||||||
|
self.failUnless('foo' in q)
|
||||||
>>> q.update({'foo': 'bar'})
|
self.failIf(q.has_key('bar'))
|
||||||
Traceback (most recent call last):
|
self.failIf('bar' in q)
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertEqual(q.items(), [(u'foo', u'bar')])
|
||||||
|
self.assertEqual(q.lists(), [(u'foo', [u'bar'])])
|
||||||
>>> q.pop('foo')
|
self.assertEqual(q.keys(), ['foo'])
|
||||||
Traceback (most recent call last):
|
self.assertEqual(q.values(), ['bar'])
|
||||||
...
|
self.assertEqual(len(q), 1)
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
|
||||||
>>> q.popitem()
|
self.assertRaises(AttributeError, q.pop, 'foo')
|
||||||
Traceback (most recent call last):
|
self.assertRaises(AttributeError, q.popitem)
|
||||||
...
|
self.assertRaises(AttributeError, q.clear)
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
|
||||||
|
|
||||||
>>> q.clear()
|
self.assertEqual(q.urlencode(), 'foo=bar')
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
def test_mutable_copy(self):
|
||||||
AttributeError: This QueryDict instance is immutable
|
"""A copy of a QueryDict is mutable."""
|
||||||
|
q = QueryDict('').copy()
|
||||||
>>> q.setdefault('foo', 'bar')
|
self.assertRaises(KeyError, q.__getitem__, "foo")
|
||||||
Traceback (most recent call last):
|
q['name'] = 'john'
|
||||||
...
|
self.assertEqual(q['name'], 'john')
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
def test_mutable_delete(self):
|
||||||
>>> q.urlencode()
|
q = QueryDict('').copy()
|
||||||
''
|
q['name'] = 'john'
|
||||||
|
del q['name']
|
||||||
###################################
|
self.failIf('name' in q)
|
||||||
# Mutable copy of empty QueryDict #
|
|
||||||
###################################
|
def test_basic_mutable_operations(self):
|
||||||
|
q = QueryDict('').copy()
|
||||||
>>> q = q.copy()
|
q['name'] = 'john'
|
||||||
|
self.assertEqual(q.get('foo', 'default'), 'default')
|
||||||
>>> q['foo']
|
self.assertEqual(q.get('name', 'default'), 'john')
|
||||||
Traceback (most recent call last):
|
self.assertEqual(q.getlist('name'), ['john'])
|
||||||
...
|
self.assertEqual(q.getlist('foo'), [])
|
||||||
MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
|
|
||||||
|
q.setlist('foo', ['bar', 'baz'])
|
||||||
>>> q['name'] = 'john'
|
self.assertEqual(q.get('foo', 'default'), 'baz')
|
||||||
|
self.assertEqual(q.getlist('foo'), ['bar', 'baz'])
|
||||||
>>> q['name']
|
|
||||||
u'john'
|
q.appendlist('foo', 'another')
|
||||||
|
self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another'])
|
||||||
>>> del q['name']
|
self.assertEqual(q['foo'], 'another')
|
||||||
>>> 'name' in q
|
self.failUnless(q.has_key('foo'))
|
||||||
False
|
self.failUnless('foo' in q)
|
||||||
|
|
||||||
>>> q['name'] = 'john'
|
self.assertEqual(q.items(), [(u'foo', u'another'), (u'name', u'john')])
|
||||||
|
self.assertEqual(q.lists(), [(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])])
|
||||||
>>> q.get('foo', 'default')
|
self.assertEqual(q.keys(), [u'foo', u'name'])
|
||||||
'default'
|
self.assertEqual(q.values(), [u'another', u'john'])
|
||||||
|
self.assertEqual(len(q), 2)
|
||||||
>>> q.get('name', 'default')
|
|
||||||
u'john'
|
q.update({'foo': 'hello'})
|
||||||
|
self.assertEqual(q['foo'], 'hello')
|
||||||
>>> q.getlist('name')
|
self.assertEqual(q.get('foo', 'not available'), 'hello')
|
||||||
[u'john']
|
self.assertEqual(q.getlist('foo'), [u'bar', u'baz', u'another', u'hello'])
|
||||||
|
self.assertEqual(q.pop('foo'), [u'bar', u'baz', u'another', u'hello'])
|
||||||
>>> q.getlist('foo')
|
self.assertEqual(q.pop('foo', 'not there'), 'not there')
|
||||||
[]
|
self.assertEqual(q.get('foo', 'not there'), 'not there')
|
||||||
|
self.assertEqual(q.setdefault('foo', 'bar'), 'bar')
|
||||||
>>> q.setlist('foo', ['bar', 'baz'])
|
self.assertEqual(q['foo'], 'bar')
|
||||||
|
self.assertEqual(q.getlist('foo'), ['bar'])
|
||||||
>>> q.get('foo', 'default')
|
self.assertEqual(q.urlencode(), 'foo=bar&name=john')
|
||||||
u'baz'
|
|
||||||
|
q.clear()
|
||||||
>>> q.getlist('foo')
|
self.assertEqual(len(q), 0)
|
||||||
[u'bar', u'baz']
|
|
||||||
|
def test_multiple_keys(self):
|
||||||
>>> q.appendlist('foo', 'another')
|
"""Test QueryDict with two key/value pairs with same keys."""
|
||||||
|
|
||||||
>>> q.getlist('foo')
|
q = QueryDict('vote=yes&vote=no')
|
||||||
[u'bar', u'baz', u'another']
|
|
||||||
|
self.assertEqual(q['vote'], u'no')
|
||||||
>>> q['foo']
|
self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
|
||||||
u'another'
|
|
||||||
|
self.assertEqual(q.get('vote', 'default'), u'no')
|
||||||
>>> q.has_key('foo')
|
self.assertEqual(q.get('foo', 'default'), 'default')
|
||||||
True
|
self.assertEqual(q.getlist('vote'), [u'yes', u'no'])
|
||||||
|
self.assertEqual(q.getlist('foo'), [])
|
||||||
>>> 'foo' in q
|
|
||||||
True
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
|
||||||
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
|
||||||
>>> q.items()
|
self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
|
||||||
[(u'foo', u'another'), (u'name', u'john')]
|
|
||||||
|
self.assertEqual(q.has_key('vote'), True)
|
||||||
>>> q.lists()
|
self.assertEqual('vote' in q, True)
|
||||||
[(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])]
|
self.assertEqual(q.has_key('foo'), False)
|
||||||
|
self.assertEqual('foo' in q, False)
|
||||||
>>> q.keys()
|
self.assertEqual(q.items(), [(u'vote', u'no')])
|
||||||
[u'foo', u'name']
|
self.assertEqual(q.lists(), [(u'vote', [u'yes', u'no'])])
|
||||||
|
self.assertEqual(q.keys(), [u'vote'])
|
||||||
>>> q.values()
|
self.assertEqual(q.values(), [u'no'])
|
||||||
[u'another', u'john']
|
self.assertEqual(len(q), 1)
|
||||||
|
|
||||||
>>> len(q)
|
self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
|
||||||
2
|
self.assertRaises(AttributeError, q.pop, 'foo')
|
||||||
|
self.assertRaises(AttributeError, q.popitem)
|
||||||
>>> q.update({'foo': 'hello'})
|
self.assertRaises(AttributeError, q.clear)
|
||||||
|
self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
|
||||||
# Displays last value
|
self.assertRaises(AttributeError, q.__delitem__, 'vote')
|
||||||
>>> q['foo']
|
|
||||||
u'hello'
|
def test_invalid_input_encoding(self):
|
||||||
|
"""
|
||||||
>>> q.get('foo', 'not available')
|
QueryDicts must be able to handle invalid input encoding (in this
|
||||||
u'hello'
|
case, bad UTF-8 encoding).
|
||||||
|
"""
|
||||||
>>> q.getlist('foo')
|
q = QueryDict('foo=bar&foo=\xff')
|
||||||
[u'bar', u'baz', u'another', u'hello']
|
self.assertEqual(q['foo'], u'\ufffd')
|
||||||
|
self.assertEqual(q.getlist('foo'), [u'bar', u'\ufffd'])
|
||||||
>>> q.pop('foo')
|
|
||||||
[u'bar', u'baz', u'another', u'hello']
|
def test_pickle(self):
|
||||||
|
q = QueryDict('')
|
||||||
>>> q.pop('foo', 'not there')
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
||||||
'not there'
|
self.assertEqual(q == q1, True)
|
||||||
|
q = QueryDict('a=b&c=d')
|
||||||
>>> q.get('foo', 'not there')
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
||||||
'not there'
|
self.assertEqual(q == q1, True)
|
||||||
|
q = QueryDict('a=b&c=d&a=1')
|
||||||
>>> q.setdefault('foo', 'bar')
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
||||||
u'bar'
|
self.assertEqual(q == q1 , True)
|
||||||
|
|
||||||
>>> q['foo']
|
def test_update_from_querydict(self):
|
||||||
u'bar'
|
"""Regression test for #8278: QueryDict.update(QueryDict)"""
|
||||||
|
x = QueryDict("a=1&a=2", mutable=True)
|
||||||
>>> q.getlist('foo')
|
y = QueryDict("a=3&a=4")
|
||||||
[u'bar']
|
x.update(y)
|
||||||
|
self.assertEqual(x.getlist('a'), [u'1', u'2', u'3', u'4'])
|
||||||
>>> q.urlencode()
|
|
||||||
'foo=bar&name=john'
|
class HttpResponseTests(unittest.TestCase):
|
||||||
|
def test_unicode_headers(self):
|
||||||
>>> q.clear()
|
r = HttpResponse()
|
||||||
|
|
||||||
>>> len(q)
|
# If we insert a unicode value it will be converted to an ascii
|
||||||
0
|
r['value'] = u'test value'
|
||||||
|
self.failUnless(isinstance(r['value'], str))
|
||||||
#####################################
|
|
||||||
# QueryDict with one key/value pair #
|
# An error is raised When a unicode object with non-ascii is assigned.
|
||||||
#####################################
|
self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value')
|
||||||
|
|
||||||
>>> q = QueryDict('foo=bar')
|
# The response also converts unicode keys to strings.)
|
||||||
|
r[u'test'] = 'testing key'
|
||||||
>>> q['foo']
|
l = list(r.items())
|
||||||
u'bar'
|
l.sort()
|
||||||
|
self.assertEqual(l[1], ('test', 'testing key'))
|
||||||
>>> q['bar']
|
|
||||||
Traceback (most recent call last):
|
# It will also raise errors for keys with non-ascii data.
|
||||||
...
|
self.assertRaises(UnicodeEncodeError, r.__setitem__, u't\xebst key', 'value')
|
||||||
MultiValueDictKeyError: "Key 'bar' not found in <QueryDict: {u'foo': [u'bar']}>"
|
|
||||||
|
def test_newlines_in_headers(self):
|
||||||
>>> q['something'] = 'bar'
|
# Bug #10188: Do not allow newlines in headers (CR or LF)
|
||||||
Traceback (most recent call last):
|
r = HttpResponse()
|
||||||
...
|
self.assertRaises(BadHeaderError, r.__setitem__, 'test\rstr', 'test')
|
||||||
AttributeError: This QueryDict instance is immutable
|
self.assertRaises(BadHeaderError, r.__setitem__, 'test\nstr', 'test')
|
||||||
|
|
||||||
>>> q.get('foo', 'default')
|
class CookieTests(unittest.TestCase):
|
||||||
u'bar'
|
|
||||||
|
|
||||||
>>> q.get('bar', 'default')
|
|
||||||
'default'
|
|
||||||
|
|
||||||
>>> q.getlist('foo')
|
|
||||||
[u'bar']
|
|
||||||
|
|
||||||
>>> q.getlist('bar')
|
|
||||||
[]
|
|
||||||
|
|
||||||
>>> q.setlist('foo', ['bar', 'baz'])
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.appendlist('foo', ['bar'])
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.has_key('foo')
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> 'foo' in q
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> q.has_key('bar')
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> 'bar' in q
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> q.items()
|
|
||||||
[(u'foo', u'bar')]
|
|
||||||
|
|
||||||
>>> q.lists()
|
|
||||||
[(u'foo', [u'bar'])]
|
|
||||||
|
|
||||||
>>> q.keys()
|
|
||||||
[u'foo']
|
|
||||||
|
|
||||||
>>> q.values()
|
|
||||||
[u'bar']
|
|
||||||
|
|
||||||
>>> len(q)
|
|
||||||
1
|
|
||||||
|
|
||||||
>>> q.update({'foo': 'bar'})
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.pop('foo')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.popitem()
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.clear()
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.setdefault('foo', 'bar')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.urlencode()
|
|
||||||
'foo=bar'
|
|
||||||
|
|
||||||
#####################################################
|
|
||||||
# QueryDict with two key/value pairs with same keys #
|
|
||||||
#####################################################
|
|
||||||
|
|
||||||
>>> q = QueryDict('vote=yes&vote=no')
|
|
||||||
|
|
||||||
>>> q['vote']
|
|
||||||
u'no'
|
|
||||||
|
|
||||||
>>> q['something'] = 'bar'
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.get('vote', 'default')
|
|
||||||
u'no'
|
|
||||||
|
|
||||||
>>> q.get('foo', 'default')
|
|
||||||
'default'
|
|
||||||
|
|
||||||
>>> q.getlist('vote')
|
|
||||||
[u'yes', u'no']
|
|
||||||
|
|
||||||
>>> q.getlist('foo')
|
|
||||||
[]
|
|
||||||
|
|
||||||
>>> q.setlist('foo', ['bar', 'baz'])
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.appendlist('foo', ['bar'])
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.has_key('vote')
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> 'vote' in q
|
|
||||||
True
|
|
||||||
|
|
||||||
>>> q.has_key('foo')
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> 'foo' in q
|
|
||||||
False
|
|
||||||
|
|
||||||
>>> q.items()
|
|
||||||
[(u'vote', u'no')]
|
|
||||||
|
|
||||||
>>> q.lists()
|
|
||||||
[(u'vote', [u'yes', u'no'])]
|
|
||||||
|
|
||||||
>>> q.keys()
|
|
||||||
[u'vote']
|
|
||||||
|
|
||||||
>>> q.values()
|
|
||||||
[u'no']
|
|
||||||
|
|
||||||
>>> len(q)
|
|
||||||
1
|
|
||||||
|
|
||||||
>>> q.update({'foo': 'bar'})
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.pop('foo')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.popitem()
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.clear()
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.setdefault('foo', 'bar')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
>>> q.urlencode()
|
|
||||||
'vote=yes&vote=no'
|
|
||||||
|
|
||||||
>>> del q['vote']
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
AttributeError: This QueryDict instance is immutable
|
|
||||||
|
|
||||||
# QueryDicts must be able to handle invalid input encoding (in this case, bad
|
|
||||||
# UTF-8 encoding).
|
|
||||||
>>> q = QueryDict('foo=bar&foo=\xff')
|
|
||||||
|
|
||||||
>>> q['foo']
|
|
||||||
u'\ufffd'
|
|
||||||
|
|
||||||
>>> q.getlist('foo')
|
|
||||||
[u'bar', u'\ufffd']
|
|
||||||
|
|
||||||
|
|
||||||
########################
|
|
||||||
# Pickling a QueryDict #
|
|
||||||
########################
|
|
||||||
>>> import pickle
|
|
||||||
>>> q = QueryDict('')
|
|
||||||
>>> q1 = pickle.loads(pickle.dumps(q, 2))
|
|
||||||
>>> q == q1
|
|
||||||
True
|
|
||||||
>>> q = QueryDict('a=b&c=d')
|
|
||||||
>>> q1 = pickle.loads(pickle.dumps(q, 2))
|
|
||||||
>>> q == q1
|
|
||||||
True
|
|
||||||
>>> q = QueryDict('a=b&c=d&a=1')
|
|
||||||
>>> q1 = pickle.loads(pickle.dumps(q, 2))
|
|
||||||
>>> q == q1
|
|
||||||
True
|
|
||||||
|
|
||||||
######################################
|
|
||||||
# HttpResponse with Unicode headers #
|
|
||||||
######################################
|
|
||||||
|
|
||||||
>>> r = HttpResponse()
|
|
||||||
|
|
||||||
If we insert a unicode value it will be converted to an ascii
|
|
||||||
string. This makes sure we comply with the HTTP specifications.
|
|
||||||
|
|
||||||
>>> r['value'] = u'test value'
|
|
||||||
>>> isinstance(r['value'], str)
|
|
||||||
True
|
|
||||||
|
|
||||||
An error is raised When a unicode object with non-ascii is assigned.
|
|
||||||
|
|
||||||
>>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
|
|
||||||
|
|
||||||
The response also converts unicode keys to strings.
|
|
||||||
|
|
||||||
>>> r[u'test'] = 'testing key'
|
|
||||||
>>> l = list(r.items())
|
|
||||||
>>> l.sort()
|
|
||||||
>>> l[1]
|
|
||||||
('test', 'testing key')
|
|
||||||
|
|
||||||
It will also raise errors for keys with non-ascii data.
|
|
||||||
|
|
||||||
>>> r[u't\xebst'] = 'testing key' # doctest:+ELLIPSIS
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
|
|
||||||
|
|
||||||
# Bug #10188: Do not allow newlines in headers (CR or LF)
|
|
||||||
>>> r['test\\rstr'] = 'test'
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
BadHeaderError: Header values can't contain newlines (got 'test\\rstr')
|
|
||||||
|
|
||||||
>>> r['test\\nstr'] = 'test'
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
BadHeaderError: Header values can't contain newlines (got 'test\\nstr')
|
|
||||||
|
|
||||||
#
|
|
||||||
# Regression test for #8278: QueryDict.update(QueryDict)
|
|
||||||
#
|
|
||||||
>>> x = QueryDict("a=1&a=2", mutable=True)
|
|
||||||
>>> y = QueryDict("a=3&a=4")
|
|
||||||
>>> x.update(y)
|
|
||||||
>>> x.getlist('a')
|
|
||||||
[u'1', u'2', u'3', u'4']
|
|
||||||
"""
|
|
||||||
|
|
||||||
from django.http import QueryDict, HttpResponse, CompatCookie
|
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
|
|
||||||
class Cookies(TestCase):
|
|
||||||
|
|
||||||
def test_encode(self):
|
def test_encode(self):
|
||||||
"""
|
"""
|
||||||
Test that we don't output tricky characters in encoded value
|
Test that we don't output tricky characters in encoded value
|
||||||
|
@ -502,7 +240,3 @@ class Cookies(TestCase):
|
||||||
c2 = CompatCookie()
|
c2 = CompatCookie()
|
||||||
c2.load(c.output())
|
c2.load(c.output())
|
||||||
self.assertEqual(c['test'].value, c2['test'].value)
|
self.assertEqual(c['test'].value, c2['test'].value)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
Loading…
Reference in New Issue