1
from datetime import datetime
4
from django.http import HttpResponse, utils
5
from django.test import RequestFactory
6
from django.utils.datastructures import MultiValueDict
7
from django.utils import http
8
from django.utils import six
9
from django.utils import unittest
12
class TestUtilsHttp(unittest.TestCase):
14
def test_same_origin_true(self):
16
self.assertTrue(http.same_origin('http://foo.com/', 'http://foo.com/'))
17
# One with trailing slash - see #15617
18
self.assertTrue(http.same_origin('http://foo.com', 'http://foo.com/'))
19
self.assertTrue(http.same_origin('http://foo.com/', 'http://foo.com'))
21
self.assertTrue(http.same_origin('https://foo.com:8000', 'https://foo.com:8000/'))
23
def test_same_origin_false(self):
25
self.assertFalse(http.same_origin('http://foo.com', 'https://foo.com'))
27
self.assertFalse(http.same_origin('http://foo.com', 'http://goo.com'))
28
# Different host again
29
self.assertFalse(http.same_origin('http://foo.com', 'http://foo.com.evil.com'))
31
self.assertFalse(http.same_origin('http://foo.com:8000', 'http://foo.com:8001'))
33
def test_urlencode(self):
35
result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
36
self.assertEqual(result, 'a=1&b=2&c=3')
39
result = http.urlencode({ 'a': 1, 'b': 2, 'c': 3})
40
acceptable_results = [
41
# Need to allow all of these as dictionaries have to be treated as
50
self.assertTrue(result in acceptable_results)
51
result = http.urlencode({'a': [1, 2]}, doseq=False)
52
self.assertEqual(result, 'a=%5B%271%27%2C+%272%27%5D')
53
result = http.urlencode({'a': [1, 2]}, doseq=True)
54
self.assertEqual(result, 'a=1&a=2')
55
result = http.urlencode({'a': []}, doseq=True)
56
self.assertEqual(result, '')
59
result = http.urlencode(MultiValueDict({
60
'name': ['Adrian', 'Simon'],
61
'position': ['Developer']
63
acceptable_results = [
64
# MultiValueDicts are similarly unordered
65
'name=Adrian&name=Simon&position=Developer',
66
'position=Developer&name=Adrian&name=Simon'
68
self.assertTrue(result in acceptable_results)
70
def test_fix_IE_for_vary(self):
72
Regression for #16632.
74
`fix_IE_for_vary` shouldn't crash when there's no Content-Type header.
77
# functions to generate responses
78
def response_with_unsafe_content_type():
79
r = HttpResponse(content_type="text/unsafe")
83
def no_content_response_with_unsafe_content_type():
84
# 'Content-Type' always defaulted, so delete it
85
r = response_with_unsafe_content_type()
89
# request with & without IE user agent
92
ie_request = rf.get('/', HTTP_USER_AGENT='MSIE')
94
# not IE, unsafe_content_type
95
response = response_with_unsafe_content_type()
96
utils.fix_IE_for_vary(request, response)
97
self.assertTrue('Vary' in response)
99
# IE, unsafe_content_type
100
response = response_with_unsafe_content_type()
101
utils.fix_IE_for_vary(ie_request, response)
102
self.assertFalse('Vary' in response)
105
response = no_content_response_with_unsafe_content_type()
106
utils.fix_IE_for_vary(request, response)
107
self.assertTrue('Vary' in response)
110
response = no_content_response_with_unsafe_content_type()
111
utils.fix_IE_for_vary(ie_request, response)
112
self.assertFalse('Vary' in response)
114
def test_base36(self):
116
for n in [0, 1, 1000, 1000000]:
117
self.assertEqual(n, http.base36_to_int(http.int_to_base36(n)))
119
self.assertEqual(sys.maxint, http.base36_to_int(http.int_to_base36(sys.maxint)))
122
self.assertRaises(ValueError, http.int_to_base36, -1)
124
self.assertRaises(ValueError, http.int_to_base36, sys.maxint + 1)
125
for n in ['1', 'foo', {1: 2}, (1, 2, 3), 3.141]:
126
self.assertRaises(TypeError, http.int_to_base36, n)
129
self.assertRaises(ValueError, http.base36_to_int, n)
130
for n in [123, {1: 2}, (1, 2, 3), 3.141]:
131
self.assertRaises(TypeError, http.base36_to_int, n)
133
# more explicit output testing
134
for n, b36 in [(0, '0'), (1, '1'), (42, '16'), (818469960, 'django')]:
135
self.assertEqual(http.int_to_base36(n), b36)
136
self.assertEqual(http.base36_to_int(b36), n)
139
class ETagProcessingTests(unittest.TestCase):
140
def testParsing(self):
141
etags = http.parse_etags(r'"", "etag", "e\"t\"ag", "e\\tag", W/"weak"')
142
self.assertEqual(etags, ['', 'etag', 'e"t"ag', r'e\tag', 'weak'])
144
def testQuoting(self):
145
quoted_etag = http.quote_etag(r'e\t"ag')
146
self.assertEqual(quoted_etag, r'"e\\t\"ag"')
149
class HttpDateProcessingTests(unittest.TestCase):
150
def testParsingRfc1123(self):
151
parsed = http.parse_http_date('Sun, 06 Nov 1994 08:49:37 GMT')
152
self.assertEqual(datetime.utcfromtimestamp(parsed),
153
datetime(1994, 11, 6, 8, 49, 37))
155
def testParsingRfc850(self):
156
parsed = http.parse_http_date('Sunday, 06-Nov-94 08:49:37 GMT')
157
self.assertEqual(datetime.utcfromtimestamp(parsed),
158
datetime(1994, 11, 6, 8, 49, 37))
160
def testParsingAsctime(self):
161
parsed = http.parse_http_date('Sun Nov 6 08:49:37 1994')
162
self.assertEqual(datetime.utcfromtimestamp(parsed),
163
datetime(1994, 11, 6, 8, 49, 37))