2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2015-01-28 12:35:27 +00:00
|
|
|
from datetime import datetime
|
2012-02-16 00:58:49 +00:00
|
|
|
|
2016-07-27 03:45:07 +00:00
|
|
|
from django.test import ignore_warnings
|
2016-12-01 10:38:01 +00:00
|
|
|
from django.utils import http
|
2012-08-03 16:46:30 +00:00
|
|
|
from django.utils.datastructures import MultiValueDict
|
2016-07-27 03:45:07 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango21Warning
|
2011-03-15 20:37:09 +00:00
|
|
|
|
2012-09-26 19:10:17 +00:00
|
|
|
|
2011-03-15 20:37:09 +00:00
|
|
|
class TestUtilsHttp(unittest.TestCase):
|
|
|
|
|
2011-04-22 12:01:41 +00:00
|
|
|
def test_urlencode(self):
|
|
|
|
# 2-tuples (the norm)
|
|
|
|
result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
|
|
|
|
self.assertEqual(result, 'a=1&b=2&c=3')
|
2012-06-14 09:32:40 +00:00
|
|
|
|
2011-04-22 12:01:41 +00:00
|
|
|
# A dictionary
|
2013-10-14 19:13:14 +00:00
|
|
|
result = http.urlencode({'a': 1, 'b': 2, 'c': 3})
|
2011-04-22 12:01:41 +00:00
|
|
|
acceptable_results = [
|
|
|
|
# Need to allow all of these as dictionaries have to be treated as
|
|
|
|
# unordered
|
|
|
|
'a=1&b=2&c=3',
|
|
|
|
'a=1&c=3&b=2',
|
|
|
|
'b=2&a=1&c=3',
|
|
|
|
'b=2&c=3&a=1',
|
|
|
|
'c=3&a=1&b=2',
|
|
|
|
'c=3&b=2&a=1'
|
|
|
|
]
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(result, acceptable_results)
|
2012-06-14 09:32:40 +00:00
|
|
|
result = http.urlencode({'a': [1, 2]}, doseq=False)
|
|
|
|
self.assertEqual(result, 'a=%5B%271%27%2C+%272%27%5D')
|
|
|
|
result = http.urlencode({'a': [1, 2]}, doseq=True)
|
|
|
|
self.assertEqual(result, 'a=1&a=2')
|
|
|
|
result = http.urlencode({'a': []}, doseq=True)
|
|
|
|
self.assertEqual(result, '')
|
|
|
|
|
2011-04-22 12:01:41 +00:00
|
|
|
# A MultiValueDict
|
|
|
|
result = http.urlencode(MultiValueDict({
|
|
|
|
'name': ['Adrian', 'Simon'],
|
|
|
|
'position': ['Developer']
|
|
|
|
}), doseq=True)
|
|
|
|
acceptable_results = [
|
|
|
|
# MultiValueDicts are similarly unordered
|
|
|
|
'name=Adrian&name=Simon&position=Developer',
|
|
|
|
'position=Developer&name=Adrian&name=Simon'
|
|
|
|
]
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(result, acceptable_results)
|
2011-12-11 08:58:14 +00:00
|
|
|
|
2012-02-16 00:58:49 +00:00
|
|
|
def test_base36(self):
|
|
|
|
# reciprocity works
|
2012-08-03 16:46:30 +00:00
|
|
|
for n in [0, 1, 1000, 1000000]:
|
2012-02-16 00:58:49 +00:00
|
|
|
self.assertEqual(n, http.base36_to_int(http.int_to_base36(n)))
|
|
|
|
|
|
|
|
# bad input
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
http.int_to_base36(-1)
|
2012-08-03 16:46:30 +00:00
|
|
|
for n in ['1', 'foo', {1: 2}, (1, 2, 3), 3.141]:
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
http.int_to_base36(n)
|
2012-08-03 16:46:30 +00:00
|
|
|
|
2012-02-16 00:58:49 +00:00
|
|
|
for n in ['#', ' ']:
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
http.base36_to_int(n)
|
2012-08-03 16:46:30 +00:00
|
|
|
for n in [123, {1: 2}, (1, 2, 3), 3.141]:
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
http.base36_to_int(n)
|
2012-02-16 00:58:49 +00:00
|
|
|
|
|
|
|
# more explicit output testing
|
2012-02-16 01:10:21 +00:00
|
|
|
for n, b36 in [(0, '0'), (1, '1'), (42, '16'), (818469960, 'django')]:
|
2012-02-16 00:58:49 +00:00
|
|
|
self.assertEqual(http.int_to_base36(n), b36)
|
|
|
|
self.assertEqual(http.base36_to_int(b36), n)
|
2012-09-26 19:10:17 +00:00
|
|
|
|
2014-05-12 11:38:39 +00:00
|
|
|
def test_is_safe_url(self):
|
2016-04-08 02:04:45 +00:00
|
|
|
bad_urls = (
|
|
|
|
'http://example.com',
|
|
|
|
'http:///example.com',
|
|
|
|
'https://example.com',
|
|
|
|
'ftp://example.com',
|
|
|
|
r'\\example.com',
|
|
|
|
r'\\\example.com',
|
|
|
|
r'/\\/example.com',
|
|
|
|
r'\\\example.com',
|
|
|
|
r'\\example.com',
|
|
|
|
r'\\//example.com',
|
|
|
|
r'/\/example.com',
|
|
|
|
r'\/example.com',
|
|
|
|
r'/\example.com',
|
|
|
|
'http:///example.com',
|
2016-09-16 16:15:00 +00:00
|
|
|
r'http:/\//example.com',
|
|
|
|
r'http:\/example.com',
|
|
|
|
r'http:/\example.com',
|
2016-04-08 02:04:45 +00:00
|
|
|
'javascript:alert("XSS")',
|
|
|
|
'\njavascript:alert(x)',
|
|
|
|
'\x08//example.com',
|
|
|
|
r'http://otherserver\@example.com',
|
|
|
|
r'http:\\testserver\@example.com',
|
|
|
|
r'http://testserver\me:pass@example.com',
|
|
|
|
r'http://testserver\@example.com',
|
|
|
|
r'http:\\testserver\confirm\me@example.com',
|
2017-03-14 14:46:53 +00:00
|
|
|
'http:999999999',
|
|
|
|
'ftp:9999999999',
|
2016-04-08 02:04:45 +00:00
|
|
|
'\n',
|
|
|
|
)
|
|
|
|
for bad_url in bad_urls:
|
2016-07-27 03:45:07 +00:00
|
|
|
with ignore_warnings(category=RemovedInDjango21Warning):
|
|
|
|
self.assertFalse(http.is_safe_url(bad_url, host='testserver'), "%s should be blocked" % bad_url)
|
|
|
|
self.assertFalse(
|
|
|
|
http.is_safe_url(bad_url, allowed_hosts={'testserver', 'testserver2'}),
|
|
|
|
"%s should be blocked" % bad_url,
|
|
|
|
)
|
2016-04-08 02:04:45 +00:00
|
|
|
|
|
|
|
good_urls = (
|
|
|
|
'/view/?param=http://example.com',
|
|
|
|
'/view/?param=https://example.com',
|
|
|
|
'/view?param=ftp://example.com',
|
|
|
|
'view/?param=//example.com',
|
|
|
|
'https://testserver/',
|
|
|
|
'HTTPS://testserver/',
|
|
|
|
'//testserver/',
|
|
|
|
'http://testserver/confirm?email=me@example.com',
|
|
|
|
'/url%20with%20spaces/',
|
2017-03-14 14:46:53 +00:00
|
|
|
'path/http:2222222222',
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
|
|
|
for good_url in good_urls:
|
2016-07-27 03:45:07 +00:00
|
|
|
with ignore_warnings(category=RemovedInDjango21Warning):
|
|
|
|
self.assertTrue(http.is_safe_url(good_url, host='testserver'), "%s should be allowed" % good_url)
|
|
|
|
self.assertTrue(
|
|
|
|
http.is_safe_url(good_url, allowed_hosts={'otherserver', 'testserver'}),
|
|
|
|
"%s should be allowed" % good_url,
|
|
|
|
)
|
2016-03-04 14:41:52 +00:00
|
|
|
|
2016-02-22 21:47:01 +00:00
|
|
|
# Valid basic auth credentials are allowed.
|
2016-07-27 03:45:07 +00:00
|
|
|
self.assertTrue(http.is_safe_url(r'http://user:pass@testserver/', allowed_hosts={'user:pass@testserver'}))
|
2016-02-22 21:47:01 +00:00
|
|
|
# A path without host is allowed.
|
|
|
|
self.assertTrue(http.is_safe_url('/confirm/me@example.com'))
|
|
|
|
# Basic auth without host is not allowed.
|
|
|
|
self.assertFalse(http.is_safe_url(r'http://testserver\@example.com'))
|
2014-05-12 11:38:39 +00:00
|
|
|
|
2016-08-19 11:23:13 +00:00
|
|
|
def test_is_safe_url_secure_param_https_urls(self):
|
|
|
|
secure_urls = (
|
|
|
|
'https://example.com/p',
|
|
|
|
'HTTPS://example.com/p',
|
|
|
|
'/view/?param=http://example.com',
|
|
|
|
)
|
|
|
|
for url in secure_urls:
|
2016-07-27 03:45:07 +00:00
|
|
|
self.assertTrue(http.is_safe_url(url, allowed_hosts={'example.com'}, require_https=True))
|
2016-08-19 11:23:13 +00:00
|
|
|
|
|
|
|
def test_is_safe_url_secure_param_non_https_urls(self):
|
|
|
|
not_secure_urls = (
|
|
|
|
'http://example.com/p',
|
|
|
|
'ftp://example.com/p',
|
|
|
|
'//example.com/p',
|
|
|
|
)
|
|
|
|
for url in not_secure_urls:
|
2016-07-27 03:45:07 +00:00
|
|
|
self.assertFalse(http.is_safe_url(url, allowed_hosts={'example.com'}, require_https=True))
|
2016-08-19 11:23:13 +00:00
|
|
|
|
2014-08-21 11:53:22 +00:00
|
|
|
def test_urlsafe_base64_roundtrip(self):
|
|
|
|
bytestring = b'foo'
|
|
|
|
encoded = http.urlsafe_base64_encode(bytestring)
|
|
|
|
decoded = http.urlsafe_base64_decode(encoded)
|
|
|
|
self.assertEqual(bytestring, decoded)
|
|
|
|
|
2014-09-23 12:45:59 +00:00
|
|
|
def test_urlquote(self):
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(http.urlquote('Paris & Orl\xe9ans'), 'Paris%20%26%20Orl%C3%A9ans')
|
|
|
|
self.assertEqual(http.urlquote('Paris & Orl\xe9ans', safe="&"), 'Paris%20&%20Orl%C3%A9ans')
|
|
|
|
self.assertEqual(http.urlunquote('Paris%20%26%20Orl%C3%A9ans'), 'Paris & Orl\xe9ans')
|
|
|
|
self.assertEqual(http.urlunquote('Paris%20&%20Orl%C3%A9ans'), 'Paris & Orl\xe9ans')
|
|
|
|
self.assertEqual(http.urlquote_plus('Paris & Orl\xe9ans'), 'Paris+%26+Orl%C3%A9ans')
|
|
|
|
self.assertEqual(http.urlquote_plus('Paris & Orl\xe9ans', safe="&"), 'Paris+&+Orl%C3%A9ans')
|
|
|
|
self.assertEqual(http.urlunquote_plus('Paris+%26+Orl%C3%A9ans'), 'Paris & Orl\xe9ans')
|
|
|
|
self.assertEqual(http.urlunquote_plus('Paris+&+Orl%C3%A9ans'), 'Paris & Orl\xe9ans')
|
2014-09-23 12:45:59 +00:00
|
|
|
|
2015-03-17 09:52:55 +00:00
|
|
|
def test_is_same_domain_good(self):
|
|
|
|
for pair in (
|
|
|
|
('example.com', 'example.com'),
|
|
|
|
('example.com', '.example.com'),
|
|
|
|
('foo.example.com', '.example.com'),
|
|
|
|
('example.com:8888', 'example.com:8888'),
|
|
|
|
('example.com:8888', '.example.com:8888'),
|
|
|
|
('foo.example.com:8888', '.example.com:8888'),
|
|
|
|
):
|
|
|
|
self.assertTrue(http.is_same_domain(*pair))
|
|
|
|
|
|
|
|
def test_is_same_domain_bad(self):
|
|
|
|
for pair in (
|
|
|
|
('example2.com', 'example.com'),
|
|
|
|
('foo.example.com', 'example.com'),
|
|
|
|
('example.com:9999', 'example.com:8888'),
|
|
|
|
):
|
|
|
|
self.assertFalse(http.is_same_domain(*pair))
|
|
|
|
|
2012-09-26 19:10:17 +00:00
|
|
|
|
|
|
|
class ETagProcessingTests(unittest.TestCase):
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_parsing(self):
|
2016-09-01 13:32:20 +00:00
|
|
|
self.assertEqual(
|
|
|
|
http.parse_etags(r'"" , "etag", "e\\tag", W/"weak"'),
|
|
|
|
['""', '"etag"', r'"e\\tag"', 'W/"weak"']
|
|
|
|
)
|
|
|
|
self.assertEqual(http.parse_etags('*'), ['*'])
|
|
|
|
|
|
|
|
# Ignore RFC 2616 ETags that are invalid according to RFC 7232.
|
|
|
|
self.assertEqual(http.parse_etags(r'"etag", "e\"t\"ag"'), ['"etag"'])
|
2012-09-26 19:10:17 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_quoting(self):
|
2016-09-01 13:32:20 +00:00
|
|
|
self.assertEqual(http.quote_etag('etag'), '"etag"') # unquoted
|
|
|
|
self.assertEqual(http.quote_etag('"etag"'), '"etag"') # quoted
|
|
|
|
self.assertEqual(http.quote_etag('W/"etag"'), 'W/"etag"') # quoted, weak
|
2012-09-26 19:10:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HttpDateProcessingTests(unittest.TestCase):
|
2014-09-23 12:45:59 +00:00
|
|
|
def test_http_date(self):
|
|
|
|
t = 1167616461.0
|
|
|
|
self.assertEqual(http.http_date(t), 'Mon, 01 Jan 2007 01:54:21 GMT')
|
|
|
|
|
|
|
|
def test_cookie_date(self):
|
|
|
|
t = 1167616461.0
|
|
|
|
self.assertEqual(http.cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT')
|
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_parsing_rfc1123(self):
|
2012-09-26 19:10:17 +00:00
|
|
|
parsed = http.parse_http_date('Sun, 06 Nov 1994 08:49:37 GMT')
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
|
2012-09-26 19:10:17 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_parsing_rfc850(self):
|
2012-09-26 19:10:17 +00:00
|
|
|
parsed = http.parse_http_date('Sunday, 06-Nov-94 08:49:37 GMT')
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
|
2012-09-26 19:10:17 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_parsing_asctime(self):
|
2012-09-26 19:10:17 +00:00
|
|
|
parsed = http.parse_http_date('Sun Nov 6 08:49:37 1994')
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
|