Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 8a10078a0397858a080079b77fe936ccbe677cea 1 parent 1b04cb2
Jacob Kaplan-Moss jacobian authored
Showing with 211 additions and 477 deletions.
  1. +211 −477 tests/regressiontests/httpwrappers/tests.py
688 tests/regressiontests/httpwrappers/tests.py
View
@@ -1,476 +1,214 @@
-"""
-###################
-# Empty QueryDict #
-###################
-
->>> q = QueryDict('')
-
->>> q['foo']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
-
->>> q['something'] = 'bar'
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.get('foo', 'default')
-'default'
-
->>> 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('foo')
-False
-
->>> 'foo' in q
-False
-
->>> q.items()
-[]
-
->>> q.lists()
-[]
-
->>> q.keys()
-[]
-
->>> q.values()
-[]
-
->>> len(q)
-0
-
->>> 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()
-''
-
-###################################
-# Mutable copy of empty QueryDict #
-###################################
-
->>> q = q.copy()
-
->>> q['foo']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
-
->>> q['name'] = 'john'
-
->>> q['name']
-u'john'
-
->>> del q['name']
->>> 'name' in q
-False
-
->>> q['name'] = 'john'
-
->>> q.get('foo', 'default')
-'default'
-
->>> q.get('name', 'default')
-u'john'
-
->>> q.getlist('name')
-[u'john']
-
->>> q.getlist('foo')
-[]
-
->>> q.setlist('foo', ['bar', 'baz'])
-
->>> q.get('foo', 'default')
-u'baz'
-
->>> q.getlist('foo')
-[u'bar', u'baz']
-
->>> q.appendlist('foo', 'another')
-
->>> q.getlist('foo')
-[u'bar', u'baz', u'another']
-
->>> q['foo']
-u'another'
-
->>> q.has_key('foo')
-True
-
->>> 'foo' in q
-True
-
->>> q.items()
-[(u'foo', u'another'), (u'name', u'john')]
-
->>> q.lists()
-[(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])]
-
->>> q.keys()
-[u'foo', u'name']
-
->>> q.values()
-[u'another', u'john']
-
->>> len(q)
-2
-
->>> q.update({'foo': 'hello'})
-
-# Displays last value
->>> q['foo']
-u'hello'
-
->>> q.get('foo', 'not available')
-u'hello'
-
->>> q.getlist('foo')
-[u'bar', u'baz', u'another', u'hello']
-
->>> q.pop('foo')
-[u'bar', u'baz', u'another', u'hello']
-
->>> q.pop('foo', 'not there')
-'not there'
-
->>> q.get('foo', 'not there')
-'not there'
-
->>> q.setdefault('foo', 'bar')
-u'bar'
-
->>> q['foo']
-u'bar'
-
->>> q.getlist('foo')
-[u'bar']
-
->>> q.urlencode()
-'foo=bar&name=john'
-
->>> q.clear()
-
->>> len(q)
-0
-
-#####################################
-# QueryDict with one key/value pair #
-#####################################
-
->>> q = QueryDict('foo=bar')
-
->>> q['foo']
-u'bar'
-
->>> q['bar']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'bar' not found in <QueryDict: {u'foo': [u'bar']}>"
-
->>> q['something'] = 'bar'
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.get('foo', 'default')
-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):
-
+import unittest
+import pickle
+from django.http import QueryDict, HttpResponse, CompatCookie, BadHeaderError
+
+class QueryDictTests(unittest.TestCase):
+ def test_missing_key(self):
+ q = QueryDict('')
+ self.assertRaises(KeyError, q.__getitem__, 'foo')
+
+ def test_immutability(self):
+ q = QueryDict('')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+
+ def test_immutable_get_with_default(self):
+ q = QueryDict('')
+ self.assertEqual(q.get('foo', 'default'), 'default')
+
+ def test_immutable_basic_operations(self):
+ q = QueryDict('')
+ self.assertEqual(q.getlist('foo'), [])
+ self.assertEqual(q.has_key('foo'), False)
+ self.assertEqual('foo' in q, False)
+ self.assertEqual(q.items(), [])
+ self.assertEqual(q.lists(), [])
+ self.assertEqual(q.items(), [])
+ self.assertEqual(q.keys(), [])
+ self.assertEqual(q.values(), [])
+ self.assertEqual(len(q), 0)
+ self.assertEqual(q.urlencode(), '')
+
+ def test_single_key_value(self):
+ """Test QueryDict with one key/value pair"""
+
+ q = QueryDict('foo=bar')
+ self.assertEqual(q['foo'], 'bar')
+ self.assertRaises(KeyError, q.__getitem__, 'bar')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+
+ self.assertEqual(q.get('foo', 'default'), 'bar')
+ self.assertEqual(q.get('bar', 'default'), 'default')
+ self.assertEqual(q.getlist('foo'), ['bar'])
+ self.assertEqual(q.getlist('bar'), [])
+
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+
+ self.failUnless(q.has_key('foo'))
+ self.failUnless('foo' in q)
+ self.failIf(q.has_key('bar'))
+ self.failIf('bar' in q)
+
+ self.assertEqual(q.items(), [(u'foo', u'bar')])
+ self.assertEqual(q.lists(), [(u'foo', [u'bar'])])
+ self.assertEqual(q.keys(), ['foo'])
+ self.assertEqual(q.values(), ['bar'])
+ self.assertEqual(len(q), 1)
+
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+ self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
+
+ self.assertEqual(q.urlencode(), 'foo=bar')
+
+ def test_mutable_copy(self):
+ """A copy of a QueryDict is mutable."""
+ q = QueryDict('').copy()
+ self.assertRaises(KeyError, q.__getitem__, "foo")
+ q['name'] = 'john'
+ self.assertEqual(q['name'], 'john')
+
+ def test_mutable_delete(self):
+ q = QueryDict('').copy()
+ q['name'] = 'john'
+ del q['name']
+ self.failIf('name' in q)
+
+ def test_basic_mutable_operations(self):
+ q = QueryDict('').copy()
+ q['name'] = 'john'
+ self.assertEqual(q.get('foo', 'default'), 'default')
+ self.assertEqual(q.get('name', 'default'), 'john')
+ self.assertEqual(q.getlist('name'), ['john'])
+ self.assertEqual(q.getlist('foo'), [])
+
+ q.setlist('foo', ['bar', 'baz'])
+ self.assertEqual(q.get('foo', 'default'), 'baz')
+ self.assertEqual(q.getlist('foo'), ['bar', 'baz'])
+
+ q.appendlist('foo', 'another')
+ self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another'])
+ self.assertEqual(q['foo'], 'another')
+ self.failUnless(q.has_key('foo'))
+ self.failUnless('foo' in q)
+
+ 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'])])
+ self.assertEqual(q.keys(), [u'foo', u'name'])
+ self.assertEqual(q.values(), [u'another', u'john'])
+ self.assertEqual(len(q), 2)
+
+ q.update({'foo': 'hello'})
+ self.assertEqual(q['foo'], 'hello')
+ self.assertEqual(q.get('foo', 'not available'), 'hello')
+ 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'])
+ 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')
+ self.assertEqual(q['foo'], 'bar')
+ self.assertEqual(q.getlist('foo'), ['bar'])
+ self.assertEqual(q.urlencode(), 'foo=bar&name=john')
+
+ q.clear()
+ self.assertEqual(len(q), 0)
+
+ def test_multiple_keys(self):
+ """Test QueryDict with two key/value pairs with same keys."""
+
+ q = QueryDict('vote=yes&vote=no')
+
+ self.assertEqual(q['vote'], u'no')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+
+ self.assertEqual(q.get('vote', 'default'), u'no')
+ self.assertEqual(q.get('foo', 'default'), 'default')
+ self.assertEqual(q.getlist('vote'), [u'yes', u'no'])
+ self.assertEqual(q.getlist('foo'), [])
+
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+
+ self.assertEqual(q.has_key('vote'), True)
+ self.assertEqual('vote' in q, True)
+ self.assertEqual(q.has_key('foo'), False)
+ self.assertEqual('foo' in q, False)
+ self.assertEqual(q.items(), [(u'vote', u'no')])
+ self.assertEqual(q.lists(), [(u'vote', [u'yes', u'no'])])
+ self.assertEqual(q.keys(), [u'vote'])
+ self.assertEqual(q.values(), [u'no'])
+ self.assertEqual(len(q), 1)
+
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+ self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
+ self.assertRaises(AttributeError, q.__delitem__, 'vote')
+
+ def test_invalid_input_encoding(self):
+ """
+ QueryDicts must be able to handle invalid input encoding (in this
+ case, bad UTF-8 encoding).
+ """
+ q = QueryDict('foo=bar&foo=\xff')
+ self.assertEqual(q['foo'], u'\ufffd')
+ self.assertEqual(q.getlist('foo'), [u'bar', u'\ufffd'])
+
+ def test_pickle(self):
+ q = QueryDict('')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1, True)
+ q = QueryDict('a=b&c=d')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1, True)
+ q = QueryDict('a=b&c=d&a=1')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1 , True)
+
+ def test_update_from_querydict(self):
+ """Regression test for #8278: QueryDict.update(QueryDict)"""
+ x = QueryDict("a=1&a=2", mutable=True)
+ y = QueryDict("a=3&a=4")
+ x.update(y)
+ self.assertEqual(x.getlist('a'), [u'1', u'2', u'3', u'4'])
+
+class HttpResponseTests(unittest.TestCase):
+ def test_unicode_headers(self):
+ r = HttpResponse()
+
+ # If we insert a unicode value it will be converted to an ascii
+ r['value'] = u'test value'
+ self.failUnless(isinstance(r['value'], str))
+
+ # An error is raised When a unicode object with non-ascii is assigned.
+ self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value')
+
+ # The response also converts unicode keys to strings.)
+ r[u'test'] = 'testing key'
+ l = list(r.items())
+ l.sort()
+ self.assertEqual(l[1], ('test', 'testing key'))
+
+ # It will also raise errors for keys with non-ascii data.
+ self.assertRaises(UnicodeEncodeError, r.__setitem__, u't\xebst key', 'value')
+
+ def test_newlines_in_headers(self):
+ # Bug #10188: Do not allow newlines in headers (CR or LF)
+ r = HttpResponse()
+ self.assertRaises(BadHeaderError, r.__setitem__, 'test\rstr', 'test')
+ self.assertRaises(BadHeaderError, r.__setitem__, 'test\nstr', 'test')
+
+class CookieTests(unittest.TestCase):
def test_encode(self):
"""
Test that we don't output tricky characters in encoded value
@@ -502,7 +240,3 @@ def test_decode_2(self):
c2 = CompatCookie()
c2.load(c.output())
self.assertEqual(c['test'].value, c2['test'].value)
-
-if __name__ == "__main__":
- import doctest
- doctest.testmod()
Please sign in to comment.
Something went wrong with that request. Please try again.