2012-07-17 19:58:18 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-05-28 16:39:52 +00:00
|
|
|
import copy
|
2012-10-20 15:40:14 +00:00
|
|
|
import os
|
2010-05-28 16:27:09 +00:00
|
|
|
import pickle
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2012-10-23 20:25:38 +00:00
|
|
|
import warnings
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2012-07-30 20:01:50 +00:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2013-02-18 13:26:33 +00:00
|
|
|
from django.core.signals import request_finished
|
2013-02-18 10:37:26 +00:00
|
|
|
from django.db import close_old_connections
|
2012-07-30 20:01:50 +00:00
|
|
|
from django.http import (QueryDict, HttpResponse, HttpResponseRedirect,
|
2012-08-23 08:56:55 +00:00
|
|
|
HttpResponsePermanentRedirect, HttpResponseNotAllowed,
|
2012-10-20 15:40:14 +00:00
|
|
|
HttpResponseNotModified, StreamingHttpResponse,
|
2012-07-30 20:01:50 +00:00
|
|
|
SimpleCookie, BadHeaderError,
|
|
|
|
parse_cookie)
|
2012-08-23 08:56:55 +00:00
|
|
|
from django.test import TestCase
|
2013-04-20 03:20:01 +00:00
|
|
|
from django.utils.encoding import smart_str, force_text
|
|
|
|
from django.utils.functional import lazy
|
2012-12-08 10:13:52 +00:00
|
|
|
from django.utils._os import upath
|
2012-08-14 07:44:04 +00:00
|
|
|
from django.utils import six
|
2010-05-28 16:27:09 +00:00
|
|
|
|
2013-04-20 03:20:01 +00:00
|
|
|
lazystr = lazy(force_text, six.text_type)
|
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
class QueryDictTests(unittest.TestCase):
|
|
|
|
def test_missing_key(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''))
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertRaises(KeyError, q.__getitem__, 'foo')
|
|
|
|
|
|
|
|
def test_immutability(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''))
|
2010-05-28 16:27:09 +00:00
|
|
|
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)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
def test_immutable_get_with_default(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''))
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(q.get('foo', 'default'), 'default')
|
|
|
|
|
|
|
|
def test_immutable_basic_operations(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''))
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(q.getlist('foo'), [])
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(q.has_key('foo'), False)
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual('foo' in q, False)
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(list(six.iteritems(q)), [])
|
|
|
|
self.assertEqual(list(six.iterlists(q)), [])
|
|
|
|
self.assertEqual(list(six.iterkeys(q)), [])
|
|
|
|
self.assertEqual(list(six.itervalues(q)), [])
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(len(q), 0)
|
|
|
|
self.assertEqual(q.urlencode(), '')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
def test_single_key_value(self):
|
|
|
|
"""Test QueryDict with one key/value pair"""
|
|
|
|
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('foo=bar'))
|
2010-05-28 16:27:09 +00:00
|
|
|
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'), [])
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
|
|
|
|
self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
|
|
|
|
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertTrue(q.has_key('foo'))
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertTrue('foo' in q)
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertFalse(q.has_key('bar'))
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse('bar' in q)
|
2010-05-28 16:27:09 +00:00
|
|
|
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(list(six.iteritems(q)), [('foo', 'bar')])
|
|
|
|
self.assertEqual(list(six.iterlists(q)), [('foo', ['bar'])])
|
|
|
|
self.assertEqual(list(six.iterkeys(q)), ['foo'])
|
|
|
|
self.assertEqual(list(six.itervalues(q)), ['bar'])
|
2010-05-28 16:27:09 +00:00
|
|
|
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')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(q.urlencode(), 'foo=bar')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-12-01 22:25:17 +00:00
|
|
|
def test_urlencode(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''), mutable=True)
|
2010-12-01 22:25:17 +00:00
|
|
|
q['next'] = '/a&b/'
|
|
|
|
self.assertEqual(q.urlencode(), 'next=%2Fa%26b%2F')
|
|
|
|
self.assertEqual(q.urlencode(safe='/'), 'next=/a%26b/')
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''), mutable=True)
|
2012-06-07 16:08:47 +00:00
|
|
|
q['next'] = '/t\xebst&key/'
|
2010-12-01 22:25:17 +00:00
|
|
|
self.assertEqual(q.urlencode(), 'next=%2Ft%C3%ABst%26key%2F')
|
|
|
|
self.assertEqual(q.urlencode(safe='/'), 'next=/t%C3%ABst%26key/')
|
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
def test_mutable_copy(self):
|
|
|
|
"""A copy of a QueryDict is mutable."""
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('')).copy()
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertRaises(KeyError, q.__getitem__, "foo")
|
|
|
|
q['name'] = 'john'
|
|
|
|
self.assertEqual(q['name'], 'john')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
def test_mutable_delete(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('')).copy()
|
2010-05-28 16:27:09 +00:00
|
|
|
q['name'] = 'john'
|
|
|
|
del q['name']
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse('name' in q)
|
2010-05-28 16:27:09 +00:00
|
|
|
|
|
|
|
def test_basic_mutable_operations(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('')).copy()
|
2010-05-28 16:27:09 +00:00
|
|
|
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')
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertTrue(q.has_key('foo'))
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertTrue('foo' in q)
|
2010-05-28 16:27:09 +00:00
|
|
|
|
2012-09-28 07:27:38 +00:00
|
|
|
self.assertListEqual(sorted(list(six.iteritems(q))),
|
|
|
|
[('foo', 'another'), ('name', 'john')])
|
|
|
|
self.assertListEqual(sorted(list(six.iterlists(q))),
|
|
|
|
[('foo', ['bar', 'baz', 'another']), ('name', ['john'])])
|
|
|
|
self.assertListEqual(sorted(list(six.iterkeys(q))),
|
|
|
|
['foo', 'name'])
|
|
|
|
self.assertListEqual(sorted(list(six.itervalues(q))),
|
|
|
|
['another', 'john'])
|
2010-05-28 16:27:09 +00:00
|
|
|
|
|
|
|
q.update({'foo': 'hello'})
|
|
|
|
self.assertEqual(q['foo'], 'hello')
|
|
|
|
self.assertEqual(q.get('foo', 'not available'), 'hello')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another', 'hello'])
|
|
|
|
self.assertEqual(q.pop('foo'), ['bar', 'baz', 'another', 'hello'])
|
2010-05-28 16:27:09 +00:00
|
|
|
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'])
|
2012-10-26 20:09:48 +00:00
|
|
|
self.assertIn(q.urlencode(), ['foo=bar&name=john', 'name=john&foo=bar'])
|
2010-05-28 16:27:09 +00:00
|
|
|
|
|
|
|
q.clear()
|
|
|
|
self.assertEqual(len(q), 0)
|
|
|
|
|
|
|
|
def test_multiple_keys(self):
|
|
|
|
"""Test QueryDict with two key/value pairs with same keys."""
|
|
|
|
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('vote=yes&vote=no'))
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(q['vote'], 'no')
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(q.get('vote', 'default'), 'no')
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(q.get('foo', 'default'), 'default')
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(q.getlist('vote'), ['yes', 'no'])
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(q.getlist('foo'), [])
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
|
|
|
|
self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
|
|
|
|
self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
|
|
|
|
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(q.has_key('vote'), True)
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual('vote' in q, True)
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(q.has_key('foo'), False)
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual('foo' in q, False)
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(list(six.iteritems(q)), [('vote', 'no')])
|
|
|
|
self.assertEqual(list(six.iterlists(q)), [('vote', ['yes', 'no'])])
|
|
|
|
self.assertEqual(list(six.iterkeys(q)), ['vote'])
|
|
|
|
self.assertEqual(list(six.itervalues(q)), ['no'])
|
2010-05-28 16:27:09 +00:00
|
|
|
self.assertEqual(len(q), 1)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
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')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-14 07:44:04 +00:00
|
|
|
def test_invalid_input_encoding(self):
|
|
|
|
"""
|
|
|
|
QueryDicts must be able to handle invalid input encoding (in this
|
|
|
|
case, bad UTF-8 encoding).
|
|
|
|
|
|
|
|
This test doesn't apply under Python 3 because the URL is a string
|
|
|
|
and not a bytestring.
|
|
|
|
"""
|
|
|
|
q = QueryDict(str(b'foo=bar&foo=\xff'))
|
|
|
|
self.assertEqual(q['foo'], '\ufffd')
|
|
|
|
self.assertEqual(q.getlist('foo'), ['bar', '\ufffd'])
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
def test_pickle(self):
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str(''))
|
2010-05-28 16:27:09 +00:00
|
|
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
|
|
|
self.assertEqual(q == q1, True)
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('a=b&c=d'))
|
2010-05-28 16:27:09 +00:00
|
|
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
|
|
|
self.assertEqual(q == q1, True)
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('a=b&c=d&a=1'))
|
2010-05-28 16:27:09 +00:00
|
|
|
q1 = pickle.loads(pickle.dumps(q, 2))
|
2012-07-14 21:07:11 +00:00
|
|
|
self.assertEqual(q == q1, True)
|
2010-05-28 16:27:09 +00:00
|
|
|
|
|
|
|
def test_update_from_querydict(self):
|
|
|
|
"""Regression test for #8278: QueryDict.update(QueryDict)"""
|
2012-08-14 07:44:04 +00:00
|
|
|
x = QueryDict(str("a=1&a=2"), mutable=True)
|
|
|
|
y = QueryDict(str("a=3&a=4"))
|
2010-05-28 16:27:09 +00:00
|
|
|
x.update(y)
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(x.getlist('a'), ['1', '2', '3', '4'])
|
2010-05-28 16:27:09 +00:00
|
|
|
|
2010-05-28 16:39:52 +00:00
|
|
|
def test_non_default_encoding(self):
|
|
|
|
"""#13572 - QueryDict with a non-default encoding"""
|
2012-08-14 07:44:04 +00:00
|
|
|
q = QueryDict(str('cur=%A4'), encoding='iso-8859-15')
|
|
|
|
self.assertEqual(q.encoding, 'iso-8859-15')
|
|
|
|
self.assertEqual(list(six.iteritems(q)), [('cur', '€')])
|
|
|
|
self.assertEqual(q.urlencode(), 'cur=%A4')
|
2010-10-11 12:55:17 +00:00
|
|
|
q = q.copy()
|
2012-08-14 07:44:04 +00:00
|
|
|
self.assertEqual(q.encoding, 'iso-8859-15')
|
|
|
|
self.assertEqual(list(six.iteritems(q)), [('cur', '€')])
|
|
|
|
self.assertEqual(q.urlencode(), 'cur=%A4')
|
|
|
|
self.assertEqual(copy.copy(q).encoding, 'iso-8859-15')
|
|
|
|
self.assertEqual(copy.deepcopy(q).encoding, 'iso-8859-15')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
class HttpResponseTests(unittest.TestCase):
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2012-09-07 23:08:57 +00:00
|
|
|
def test_headers_type(self):
|
|
|
|
r = HttpResponse()
|
2010-09-11 01:41:53 +00:00
|
|
|
|
2012-09-07 23:08:57 +00:00
|
|
|
# The following tests explicitly test types in addition to values
|
|
|
|
# because in Python 2 u'foo' == b'foo'.
|
|
|
|
|
|
|
|
# ASCII unicode or bytes values are converted to native strings.
|
|
|
|
r['key'] = 'test'
|
|
|
|
self.assertEqual(r['key'], str('test'))
|
|
|
|
self.assertIsInstance(r['key'], str)
|
|
|
|
r['key'] = 'test'.encode('ascii')
|
|
|
|
self.assertEqual(r['key'], str('test'))
|
|
|
|
self.assertIsInstance(r['key'], str)
|
|
|
|
|
|
|
|
# Latin-1 unicode or bytes values are also converted to native strings.
|
|
|
|
r['key'] = 'café'
|
|
|
|
self.assertEqual(r['key'], smart_str('café', 'latin-1'))
|
|
|
|
self.assertIsInstance(r['key'], str)
|
|
|
|
r['key'] = 'café'.encode('latin-1')
|
|
|
|
self.assertEqual(r['key'], smart_str('café', 'latin-1'))
|
|
|
|
self.assertIsInstance(r['key'], str)
|
|
|
|
|
|
|
|
# Other unicode values are MIME-encoded (there's no way to pass them as bytes).
|
|
|
|
r['key'] = '†'
|
|
|
|
self.assertEqual(r['key'], str('=?utf-8?b?4oCg?='))
|
|
|
|
self.assertIsInstance(r['key'], str)
|
|
|
|
|
|
|
|
# The response also converts unicode or bytes keys to strings, but requires
|
|
|
|
# them to contain ASCII
|
|
|
|
r = HttpResponse()
|
2012-10-26 20:09:48 +00:00
|
|
|
del r['Content-Type']
|
2012-09-07 23:08:57 +00:00
|
|
|
r['foo'] = 'bar'
|
|
|
|
l = list(r.items())
|
2012-10-26 20:09:48 +00:00
|
|
|
self.assertEqual(len(l), 1)
|
2012-09-07 23:08:57 +00:00
|
|
|
self.assertEqual(l[0], ('foo', 'bar'))
|
|
|
|
self.assertIsInstance(l[0][0], str)
|
2010-09-11 01:41:53 +00:00
|
|
|
|
2012-09-07 23:08:57 +00:00
|
|
|
r = HttpResponse()
|
2012-10-26 20:09:48 +00:00
|
|
|
del r['Content-Type']
|
2012-09-07 23:08:57 +00:00
|
|
|
r[b'foo'] = 'bar'
|
2010-05-28 16:27:09 +00:00
|
|
|
l = list(r.items())
|
2012-10-26 20:09:48 +00:00
|
|
|
self.assertEqual(len(l), 1)
|
2012-09-07 23:08:57 +00:00
|
|
|
self.assertEqual(l[0], ('foo', 'bar'))
|
|
|
|
self.assertIsInstance(l[0][0], str)
|
|
|
|
|
|
|
|
r = HttpResponse()
|
|
|
|
self.assertRaises(UnicodeError, r.__setitem__, 'føø', 'bar')
|
|
|
|
self.assertRaises(UnicodeError, r.__setitem__, 'føø'.encode('utf-8'), 'bar')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-09-06 18:47:08 +00:00
|
|
|
def test_long_line(self):
|
|
|
|
# Bug #20889: long lines trigger newlines to be added to headers
|
|
|
|
# (which is not allowed due to bug #10188)
|
|
|
|
h = HttpResponse()
|
|
|
|
f = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz a\xcc\x88'.encode('latin-1')
|
|
|
|
f = f.decode('utf-8')
|
2013-09-09 13:54:08 +00:00
|
|
|
h['Content-Disposition'] = 'attachment; filename="%s"' % f
|
2010-05-28 16:27:09 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2011-06-16 16:34:46 +00:00
|
|
|
def test_dict_behavior(self):
|
|
|
|
"""
|
|
|
|
Test for bug #14020: Make HttpResponse.get work like dict.get
|
|
|
|
"""
|
|
|
|
r = HttpResponse()
|
|
|
|
self.assertEqual(r.get('test'), None)
|
|
|
|
|
2011-09-14 23:27:35 +00:00
|
|
|
def test_non_string_content(self):
|
|
|
|
#Bug 16494: HttpResponse should behave consistently with non-strings
|
|
|
|
r = HttpResponse(12345)
|
2012-08-11 20:29:18 +00:00
|
|
|
self.assertEqual(r.content, b'12345')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
|
|
|
#test content via property
|
|
|
|
r = HttpResponse()
|
|
|
|
r.content = 12345
|
2012-08-11 20:29:18 +00:00
|
|
|
self.assertEqual(r.content, b'12345')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
|
|
|
def test_iter_content(self):
|
|
|
|
r = HttpResponse(['abc', 'def', 'ghi'])
|
2012-08-11 20:29:18 +00:00
|
|
|
self.assertEqual(r.content, b'abcdefghi')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
|
|
|
#test iter content via property
|
|
|
|
r = HttpResponse()
|
|
|
|
r.content = ['idan', 'alex', 'jacob']
|
2012-08-11 20:29:18 +00:00
|
|
|
self.assertEqual(r.content, b'idanalexjacob')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
|
|
|
r = HttpResponse()
|
|
|
|
r.content = [1, 2, 3]
|
2012-08-11 20:29:18 +00:00
|
|
|
self.assertEqual(r.content, b'123')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
2013-06-29 13:41:57 +00:00
|
|
|
#test odd inputs
|
2011-09-14 23:27:35 +00:00
|
|
|
r = HttpResponse()
|
2012-08-11 20:29:18 +00:00
|
|
|
r.content = ['1', '2', 3, '\u079e']
|
2011-09-14 23:27:35 +00:00
|
|
|
#'\xde\x9e' == unichr(1950).encode('utf-8')
|
2012-05-19 15:43:34 +00:00
|
|
|
self.assertEqual(r.content, b'123\xde\x9e')
|
2011-09-14 23:27:35 +00:00
|
|
|
|
|
|
|
#with Content-Encoding header
|
2012-10-24 21:41:45 +00:00
|
|
|
r = HttpResponse()
|
2011-09-14 23:27:35 +00:00
|
|
|
r['Content-Encoding'] = 'winning'
|
2012-10-24 21:41:45 +00:00
|
|
|
r.content = [b'abc', b'def']
|
|
|
|
self.assertEqual(r.content, b'abcdef')
|
|
|
|
self.assertRaises(TypeError if six.PY3 else UnicodeEncodeError,
|
2013-06-29 13:41:57 +00:00
|
|
|
setattr, r, 'content', ['\u079e'])
|
2011-09-14 23:27:35 +00:00
|
|
|
|
2012-10-24 09:33:56 +00:00
|
|
|
# .content can safely be accessed multiple times.
|
2012-10-23 20:25:38 +00:00
|
|
|
r = HttpResponse(iter(['hello', 'world']))
|
|
|
|
self.assertEqual(r.content, r.content)
|
|
|
|
self.assertEqual(r.content, b'helloworld')
|
2012-10-24 09:33:56 +00:00
|
|
|
# accessing the iterator works (once) after accessing .content
|
|
|
|
self.assertEqual(b''.join(r), b'helloworld')
|
|
|
|
self.assertEqual(b''.join(r), b'')
|
|
|
|
# accessing .content still works
|
|
|
|
self.assertEqual(r.content, b'helloworld')
|
|
|
|
|
2013-06-29 13:41:57 +00:00
|
|
|
# Accessing .content also works if the response was iterated first.
|
2012-10-24 09:33:56 +00:00
|
|
|
r = HttpResponse(iter(['hello', 'world']))
|
2013-06-29 13:41:57 +00:00
|
|
|
self.assertEqual(b''.join(r), b'helloworld')
|
|
|
|
self.assertEqual(r.content, b'helloworld')
|
2012-10-23 20:25:38 +00:00
|
|
|
|
2013-06-29 13:41:57 +00:00
|
|
|
# Additional content can be written to the response.
|
2012-10-24 09:33:56 +00:00
|
|
|
r = HttpResponse(iter(['hello', 'world']))
|
|
|
|
self.assertEqual(r.content, b'helloworld')
|
2012-10-23 20:25:38 +00:00
|
|
|
r.write('!')
|
|
|
|
self.assertEqual(r.content, b'helloworld!')
|
|
|
|
|
2012-10-24 09:33:56 +00:00
|
|
|
def test_iterator_isnt_rewound(self):
|
|
|
|
# Regression test for #13222
|
|
|
|
r = HttpResponse('abc')
|
|
|
|
i = iter(r)
|
|
|
|
self.assertEqual(list(i), [b'abc'])
|
|
|
|
self.assertEqual(list(i), [])
|
2012-10-23 20:25:38 +00:00
|
|
|
|
2013-04-20 03:20:01 +00:00
|
|
|
def test_lazy_content(self):
|
|
|
|
r = HttpResponse(lazystr('helloworld'))
|
|
|
|
self.assertEqual(r.content, b'helloworld')
|
|
|
|
|
2012-07-17 19:58:18 +00:00
|
|
|
def test_file_interface(self):
|
|
|
|
r = HttpResponse()
|
|
|
|
r.write(b"hello")
|
|
|
|
self.assertEqual(r.tell(), 5)
|
|
|
|
r.write("привет")
|
|
|
|
self.assertEqual(r.tell(), 17)
|
|
|
|
|
|
|
|
r = HttpResponse(['abc'])
|
2012-10-23 20:25:38 +00:00
|
|
|
r.write('def')
|
|
|
|
self.assertEqual(r.tell(), 6)
|
|
|
|
self.assertEqual(r.content, b'abcdef')
|
2012-07-17 19:58:18 +00:00
|
|
|
|
2013-08-24 16:08:05 +00:00
|
|
|
# with Content-Encoding header
|
|
|
|
r = HttpResponse()
|
|
|
|
r['Content-Encoding'] = 'winning'
|
|
|
|
r.write(b'abc')
|
|
|
|
r.write(b'def')
|
|
|
|
self.assertEqual(r.content, b'abcdef')
|
|
|
|
|
2012-07-30 20:01:50 +00:00
|
|
|
def test_unsafe_redirect(self):
|
|
|
|
bad_urls = [
|
|
|
|
'data:text/html,<script>window.alert("xss")</script>',
|
|
|
|
'mailto:test@example.com',
|
|
|
|
'file:///etc/passwd',
|
|
|
|
]
|
|
|
|
for url in bad_urls:
|
|
|
|
self.assertRaises(SuspiciousOperation,
|
|
|
|
HttpResponseRedirect, url)
|
|
|
|
self.assertRaises(SuspiciousOperation,
|
|
|
|
HttpResponsePermanentRedirect, url)
|
|
|
|
|
2012-08-23 08:56:55 +00:00
|
|
|
class HttpResponseSubclassesTests(TestCase):
|
|
|
|
def test_redirect(self):
|
|
|
|
response = HttpResponseRedirect('/redirected/')
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
# Test that standard HttpResponse init args can be used
|
|
|
|
response = HttpResponseRedirect('/redirected/',
|
|
|
|
content='The resource has temporarily moved',
|
|
|
|
content_type='text/html')
|
|
|
|
self.assertContains(response, 'The resource has temporarily moved', status_code=302)
|
2013-02-13 08:55:43 +00:00
|
|
|
# Test that url attribute is right
|
|
|
|
self.assertEqual(response.url, response['Location'])
|
2012-08-23 08:56:55 +00:00
|
|
|
|
2013-04-20 03:20:01 +00:00
|
|
|
def test_redirect_lazy(self):
|
|
|
|
"""Make sure HttpResponseRedirect works with lazy strings."""
|
|
|
|
r = HttpResponseRedirect(lazystr('/redirected/'))
|
|
|
|
self.assertEqual(r.url, '/redirected/')
|
|
|
|
|
2012-08-22 16:06:03 +00:00
|
|
|
def test_not_modified(self):
|
|
|
|
response = HttpResponseNotModified()
|
|
|
|
self.assertEqual(response.status_code, 304)
|
|
|
|
# 304 responses should not have content/content-type
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
response.content = "Hello dear"
|
|
|
|
self.assertNotIn('content-type', response)
|
|
|
|
|
2012-08-23 08:56:55 +00:00
|
|
|
def test_not_allowed(self):
|
|
|
|
response = HttpResponseNotAllowed(['GET'])
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
# Test that standard HttpResponse init args can be used
|
|
|
|
response = HttpResponseNotAllowed(['GET'],
|
|
|
|
content='Only the GET method is allowed',
|
|
|
|
content_type='text/html')
|
|
|
|
self.assertContains(response, 'Only the GET method is allowed', status_code=405)
|
2012-08-22 16:06:03 +00:00
|
|
|
|
2012-10-20 15:40:14 +00:00
|
|
|
class StreamingHttpResponseTests(TestCase):
|
|
|
|
def test_streaming_response(self):
|
|
|
|
r = StreamingHttpResponse(iter(['hello', 'world']))
|
|
|
|
|
|
|
|
# iterating over the response itself yields bytestring chunks.
|
|
|
|
chunks = list(r)
|
|
|
|
self.assertEqual(chunks, [b'hello', b'world'])
|
|
|
|
for chunk in chunks:
|
|
|
|
self.assertIsInstance(chunk, six.binary_type)
|
|
|
|
|
|
|
|
# and the response can only be iterated once.
|
|
|
|
self.assertEqual(list(r), [])
|
|
|
|
|
|
|
|
# even when a sequence that can be iterated many times, like a list,
|
|
|
|
# is given as content.
|
|
|
|
r = StreamingHttpResponse(['abc', 'def'])
|
|
|
|
self.assertEqual(list(r), [b'abc', b'def'])
|
|
|
|
self.assertEqual(list(r), [])
|
|
|
|
|
|
|
|
# streaming responses don't have a `content` attribute.
|
|
|
|
self.assertFalse(hasattr(r, 'content'))
|
|
|
|
|
|
|
|
# and you can't accidentally assign to a `content` attribute.
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
r.content = 'xyz'
|
|
|
|
|
|
|
|
# but they do have a `streaming_content` attribute.
|
|
|
|
self.assertTrue(hasattr(r, 'streaming_content'))
|
|
|
|
|
|
|
|
# that exists so we can check if a response is streaming, and wrap or
|
|
|
|
# replace the content iterator.
|
|
|
|
r.streaming_content = iter(['abc', 'def'])
|
|
|
|
r.streaming_content = (chunk.upper() for chunk in r.streaming_content)
|
|
|
|
self.assertEqual(list(r), [b'ABC', b'DEF'])
|
|
|
|
|
|
|
|
# coercing a streaming response to bytes doesn't return a complete HTTP
|
|
|
|
# message like a regular response does. it only gives us the headers.
|
|
|
|
r = StreamingHttpResponse(iter(['hello', 'world']))
|
|
|
|
self.assertEqual(
|
|
|
|
six.binary_type(r), b'Content-Type: text/html; charset=utf-8')
|
|
|
|
|
|
|
|
# and this won't consume its content.
|
|
|
|
self.assertEqual(list(r), [b'hello', b'world'])
|
|
|
|
|
|
|
|
# additional content cannot be written to the response.
|
|
|
|
r = StreamingHttpResponse(iter(['hello', 'world']))
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
r.write('!')
|
|
|
|
|
|
|
|
# and we can't tell the current position.
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
r.tell()
|
|
|
|
|
|
|
|
class FileCloseTests(TestCase):
|
2013-02-18 13:26:33 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Disable the request_finished signal during this test
|
|
|
|
# to avoid interfering with the database connection.
|
2013-02-18 10:37:26 +00:00
|
|
|
request_finished.disconnect(close_old_connections)
|
2013-02-18 13:26:33 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-18 10:37:26 +00:00
|
|
|
request_finished.connect(close_old_connections)
|
2013-02-18 13:26:33 +00:00
|
|
|
|
2012-10-20 15:40:14 +00:00
|
|
|
def test_response(self):
|
2012-12-08 10:13:52 +00:00
|
|
|
filename = os.path.join(os.path.dirname(upath(__file__)), 'abc.txt')
|
2012-10-20 15:40:14 +00:00
|
|
|
|
|
|
|
# file isn't closed until we close the response.
|
|
|
|
file1 = open(filename)
|
|
|
|
r = HttpResponse(file1)
|
|
|
|
self.assertFalse(file1.closed)
|
|
|
|
r.close()
|
|
|
|
self.assertTrue(file1.closed)
|
|
|
|
|
|
|
|
# don't automatically close file when we finish iterating the response.
|
|
|
|
file1 = open(filename)
|
|
|
|
r = HttpResponse(file1)
|
|
|
|
self.assertFalse(file1.closed)
|
2012-10-23 20:25:38 +00:00
|
|
|
with warnings.catch_warnings():
|
2012-12-25 20:07:27 +00:00
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
2012-10-23 20:25:38 +00:00
|
|
|
list(r)
|
2012-10-20 15:40:14 +00:00
|
|
|
self.assertFalse(file1.closed)
|
|
|
|
r.close()
|
|
|
|
self.assertTrue(file1.closed)
|
|
|
|
|
|
|
|
# when multiple file are assigned as content, make sure they are all
|
|
|
|
# closed with the response.
|
|
|
|
file1 = open(filename)
|
|
|
|
file2 = open(filename)
|
|
|
|
r = HttpResponse(file1)
|
|
|
|
r.content = file2
|
|
|
|
self.assertFalse(file1.closed)
|
|
|
|
self.assertFalse(file2.closed)
|
|
|
|
r.close()
|
|
|
|
self.assertTrue(file1.closed)
|
|
|
|
self.assertTrue(file2.closed)
|
|
|
|
|
|
|
|
def test_streaming_response(self):
|
2012-12-08 10:13:52 +00:00
|
|
|
filename = os.path.join(os.path.dirname(upath(__file__)), 'abc.txt')
|
2012-10-20 15:40:14 +00:00
|
|
|
|
|
|
|
# file isn't closed until we close the response.
|
|
|
|
file1 = open(filename)
|
|
|
|
r = StreamingHttpResponse(file1)
|
|
|
|
self.assertFalse(file1.closed)
|
|
|
|
r.close()
|
|
|
|
self.assertTrue(file1.closed)
|
|
|
|
|
|
|
|
# when multiple file are assigned as content, make sure they are all
|
|
|
|
# closed with the response.
|
|
|
|
file1 = open(filename)
|
|
|
|
file2 = open(filename)
|
|
|
|
r = StreamingHttpResponse(file1)
|
|
|
|
r.streaming_content = file2
|
|
|
|
self.assertFalse(file1.closed)
|
|
|
|
self.assertFalse(file2.closed)
|
|
|
|
r.close()
|
|
|
|
self.assertTrue(file1.closed)
|
|
|
|
self.assertTrue(file2.closed)
|
|
|
|
|
2010-05-28 16:27:09 +00:00
|
|
|
class CookieTests(unittest.TestCase):
|
2010-01-23 23:13:00 +00:00
|
|
|
def test_encode(self):
|
|
|
|
"""
|
|
|
|
Test that we don't output tricky characters in encoded value
|
|
|
|
"""
|
2011-01-24 20:35:46 +00:00
|
|
|
c = SimpleCookie()
|
2010-01-23 23:13:00 +00:00
|
|
|
c['test'] = "An,awkward;value"
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(";" not in c.output().rstrip(';')) # IE compat
|
|
|
|
self.assertTrue("," not in c.output().rstrip(';')) # Safari compat
|
2010-01-23 23:13:00 +00:00
|
|
|
|
|
|
|
def test_decode(self):
|
|
|
|
"""
|
|
|
|
Test that we can still preserve semi-colons and commas
|
|
|
|
"""
|
2011-01-24 20:35:46 +00:00
|
|
|
c = SimpleCookie()
|
2010-01-23 23:13:00 +00:00
|
|
|
c['test'] = "An,awkward;value"
|
2011-01-24 20:35:46 +00:00
|
|
|
c2 = SimpleCookie()
|
2010-01-23 23:13:00 +00:00
|
|
|
c2.load(c.output())
|
|
|
|
self.assertEqual(c['test'].value, c2['test'].value)
|
|
|
|
|
|
|
|
def test_decode_2(self):
|
|
|
|
"""
|
|
|
|
Test that we haven't broken normal encoding
|
|
|
|
"""
|
2011-01-24 20:35:46 +00:00
|
|
|
c = SimpleCookie()
|
2012-05-19 15:43:34 +00:00
|
|
|
c['test'] = b"\xf0"
|
2011-01-24 20:35:46 +00:00
|
|
|
c2 = SimpleCookie()
|
2010-01-23 23:13:00 +00:00
|
|
|
c2.load(c.output())
|
|
|
|
self.assertEqual(c['test'].value, c2['test'].value)
|
2011-02-13 02:24:05 +00:00
|
|
|
|
|
|
|
def test_nonstandard_keys(self):
|
|
|
|
"""
|
|
|
|
Test that a single non-standard cookie name doesn't affect all cookies. Ticket #13007.
|
|
|
|
"""
|
|
|
|
self.assertTrue('good_cookie' in parse_cookie('good_cookie=yes;bad:cookie=yes').keys())
|
2011-06-25 16:18:40 +00:00
|
|
|
|
|
|
|
def test_repeated_nonstandard_keys(self):
|
|
|
|
"""
|
|
|
|
Test that a repeated non-standard name doesn't affect all cookies. Ticket #15852
|
|
|
|
"""
|
2012-10-25 23:50:25 +00:00
|
|
|
self.assertTrue('good_cookie' in parse_cookie('a:=b; a:=c; good_cookie=yes').keys())
|
2011-07-08 12:07:54 +00:00
|
|
|
|
|
|
|
def test_httponly_after_load(self):
|
|
|
|
"""
|
|
|
|
Test that we can use httponly attribute on cookies that we load
|
|
|
|
"""
|
|
|
|
c = SimpleCookie()
|
|
|
|
c.load("name=val")
|
|
|
|
c['name']['httponly'] = True
|
|
|
|
self.assertTrue(c['name']['httponly'])
|
|
|
|
|
2013-01-11 20:09:33 +00:00
|
|
|
def test_load_dict(self):
|
|
|
|
c = SimpleCookie()
|
|
|
|
c.load({'name': 'val'})
|
|
|
|
self.assertEqual(c['name'].value, 'val')
|
2013-09-09 09:40:37 +00:00
|
|
|
|
|
|
|
@unittest.skipUnless(six.PY2, "PY3 throws an exception on invalid cookie keys.")
|
|
|
|
def test_bad_cookie(self):
|
|
|
|
"""
|
|
|
|
Regression test for #18403
|
|
|
|
"""
|
|
|
|
r = HttpResponse()
|
|
|
|
r.set_cookie("a:.b/", 1)
|
|
|
|
self.assertEqual(len(r.cookies.bad_cookies), 1)
|