~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/regressiontests/utils/http.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from datetime import datetime
2
 
import sys
3
 
 
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
10
 
 
11
 
 
12
 
class TestUtilsHttp(unittest.TestCase):
13
 
 
14
 
    def test_same_origin_true(self):
15
 
        # Identical
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'))
20
 
        # With port
21
 
        self.assertTrue(http.same_origin('https://foo.com:8000', 'https://foo.com:8000/'))
22
 
 
23
 
    def test_same_origin_false(self):
24
 
        # Different scheme
25
 
        self.assertFalse(http.same_origin('http://foo.com', 'https://foo.com'))
26
 
        # Different host
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'))
30
 
        # Different port
31
 
        self.assertFalse(http.same_origin('http://foo.com:8000', 'http://foo.com:8001'))
32
 
 
33
 
    def test_urlencode(self):
34
 
        # 2-tuples (the norm)
35
 
        result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
36
 
        self.assertEqual(result, 'a=1&b=2&c=3')
37
 
 
38
 
        # A dictionary
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
42
 
            # unordered
43
 
            'a=1&b=2&c=3',
44
 
            'a=1&c=3&b=2',
45
 
            'b=2&a=1&c=3',
46
 
            'b=2&c=3&a=1',
47
 
            'c=3&a=1&b=2',
48
 
            'c=3&b=2&a=1'
49
 
        ]
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, '')
57
 
 
58
 
        # A MultiValueDict
59
 
        result = http.urlencode(MultiValueDict({
60
 
            'name': ['Adrian', 'Simon'],
61
 
            'position': ['Developer']
62
 
        }), doseq=True)
63
 
        acceptable_results = [
64
 
            # MultiValueDicts are similarly unordered
65
 
            'name=Adrian&name=Simon&position=Developer',
66
 
            'position=Developer&name=Adrian&name=Simon'
67
 
        ]
68
 
        self.assertTrue(result in acceptable_results)
69
 
 
70
 
    def test_fix_IE_for_vary(self):
71
 
        """
72
 
        Regression for #16632.
73
 
 
74
 
        `fix_IE_for_vary` shouldn't crash when there's no Content-Type header.
75
 
        """
76
 
 
77
 
        # functions to generate responses
78
 
        def response_with_unsafe_content_type():
79
 
            r = HttpResponse(content_type="text/unsafe")
80
 
            r['Vary'] = 'Cookie'
81
 
            return r
82
 
 
83
 
        def no_content_response_with_unsafe_content_type():
84
 
            # 'Content-Type' always defaulted, so delete it
85
 
            r = response_with_unsafe_content_type()
86
 
            del r['Content-Type']
87
 
            return r
88
 
 
89
 
        # request with & without IE user agent
90
 
        rf = RequestFactory()
91
 
        request = rf.get('/')
92
 
        ie_request = rf.get('/', HTTP_USER_AGENT='MSIE')
93
 
 
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)
98
 
 
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)
103
 
 
104
 
        # not IE, no_content
105
 
        response = no_content_response_with_unsafe_content_type()
106
 
        utils.fix_IE_for_vary(request, response)
107
 
        self.assertTrue('Vary' in response)
108
 
 
109
 
        # IE, no_content
110
 
        response = no_content_response_with_unsafe_content_type()
111
 
        utils.fix_IE_for_vary(ie_request, response)
112
 
        self.assertFalse('Vary' in response)
113
 
 
114
 
    def test_base36(self):
115
 
        # reciprocity works
116
 
        for n in [0, 1, 1000, 1000000]:
117
 
            self.assertEqual(n, http.base36_to_int(http.int_to_base36(n)))
118
 
        if not six.PY3:
119
 
            self.assertEqual(sys.maxint, http.base36_to_int(http.int_to_base36(sys.maxint)))
120
 
 
121
 
        # bad input
122
 
        self.assertRaises(ValueError, http.int_to_base36, -1)
123
 
        if not six.PY3:
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)
127
 
 
128
 
        for n in ['#', ' ']:
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)
132
 
 
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)
137
 
 
138
 
 
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'])
143
 
 
144
 
    def testQuoting(self):
145
 
        quoted_etag = http.quote_etag(r'e\t"ag')
146
 
        self.assertEqual(quoted_etag, r'"e\\t\"ag"')
147
 
 
148
 
 
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))
154
 
 
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))
159
 
 
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))