File: httputil_test.py

package info (click to toggle)
python-tornado 2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,536 kB
  • sloc: python: 12,755; ansic: 64; xml: 26; sql: 25; makefile: 17
file content (222 lines) | stat: -rw-r--r-- 7,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python


from __future__ import absolute_import, division, with_statement
from tornado.httputil import url_concat, parse_multipart_form_data, HTTPHeaders
from tornado.escape import utf8
from tornado.testing import LogTrapTestCase
from tornado.util import b
import logging
import unittest


class TestUrlConcat(unittest.TestCase):

    def test_url_concat_no_query_params(self):
        url = url_concat(
                "https://localhost/path",
                [('y', 'y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?y=y&z=z")

    def test_url_concat_encode_args(self):
        url = url_concat(
                "https://localhost/path",
                [('y', '/y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?y=%2Fy&z=z")

    def test_url_concat_trailing_q(self):
        url = url_concat(
                "https://localhost/path?",
                [('y', 'y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?y=y&z=z")

    def test_url_concat_q_with_no_trailing_amp(self):
        url = url_concat(
                "https://localhost/path?x",
                [('y', 'y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?x&y=y&z=z")

    def test_url_concat_trailing_amp(self):
        url = url_concat(
                "https://localhost/path?x&",
                [('y', 'y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?x&y=y&z=z")

    def test_url_concat_mult_params(self):
        url = url_concat(
                "https://localhost/path?a=1&b=2",
                [('y', 'y'), ('z', 'z')],
                )
        self.assertEqual(url, "https://localhost/path?a=1&b=2&y=y&z=z")

    def test_url_concat_no_params(self):
        url = url_concat(
            "https://localhost/path?r=1&t=2",
            [],
            )
        self.assertEqual(url, "https://localhost/path?r=1&t=2")


class MultipartFormDataTest(LogTrapTestCase):
    def test_file_upload(self):
        data = b("""\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--""").replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        file = files["files"][0]
        self.assertEqual(file["filename"], "ab.txt")
        self.assertEqual(file["body"], b("Foo"))

    def test_unquoted_names(self):
        # quotes are optional unless special characters are present
        data = b("""\
--1234
Content-Disposition: form-data; name=files; filename=ab.txt

Foo
--1234--""").replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        file = files["files"][0]
        self.assertEqual(file["filename"], "ab.txt")
        self.assertEqual(file["body"], b("Foo"))

    def test_special_filenames(self):
        filenames = ['a;b.txt',
                     'a"b.txt',
                     'a";b.txt',
                     'a;"b.txt',
                     'a";";.txt',
                     'a\\"b.txt',
                     'a\\b.txt',
                     ]
        for filename in filenames:
            logging.info("trying filename %r", filename)
            data = """\
--1234
Content-Disposition: form-data; name="files"; filename="%s"

Foo
--1234--""" % filename.replace('\\', '\\\\').replace('"', '\\"')
            data = utf8(data.replace("\n", "\r\n"))
            args = {}
            files = {}
            parse_multipart_form_data(b("1234"), data, args, files)
            file = files["files"][0]
            self.assertEqual(file["filename"], filename)
            self.assertEqual(file["body"], b("Foo"))

    def test_boundary_starts_and_ends_with_quotes(self):
        data = b('''\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--''').replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b('"1234"'), data, args, files)
        file = files["files"][0]
        self.assertEqual(file["filename"], "ab.txt")
        self.assertEqual(file["body"], b("Foo"))

    def test_missing_headers(self):
        data = b('''\
--1234

Foo
--1234--''').replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        self.assertEqual(files, {})

    def test_invalid_content_disposition(self):
        data = b('''\
--1234
Content-Disposition: invalid; name="files"; filename="ab.txt"

Foo
--1234--''').replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        self.assertEqual(files, {})

    def test_line_does_not_end_with_correct_line_break(self):
        data = b('''\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo--1234--''').replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        self.assertEqual(files, {})

    def test_content_disposition_header_without_name_parameter(self):
        data = b("""\
--1234
Content-Disposition: form-data; filename="ab.txt"

Foo
--1234--""").replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        self.assertEqual(files, {})

    def test_data_after_final_boundary(self):
        # The spec requires that data after the final boundary be ignored.
        # http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
        # In practice, some libraries include an extra CRLF after the boundary.
        data = b("""\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--
""").replace(b("\n"), b("\r\n"))
        args = {}
        files = {}
        parse_multipart_form_data(b("1234"), data, args, files)
        file = files["files"][0]
        self.assertEqual(file["filename"], "ab.txt")
        self.assertEqual(file["body"], b("Foo"))


class HTTPHeadersTest(unittest.TestCase):
    def test_multi_line(self):
        # Lines beginning with whitespace are appended to the previous line
        # with any leading whitespace replaced by a single space.
        # Note that while multi-line headers are a part of the HTTP spec,
        # their use is strongly discouraged.
        data = """\
Foo: bar
 baz
Asdf: qwer
\tzxcv
Foo: even
     more
     lines
""".replace("\n", "\r\n")
        headers = HTTPHeaders.parse(data)
        self.assertEqual(headers["asdf"], "qwer zxcv")
        self.assertEqual(headers.get_list("asdf"), ["qwer zxcv"])
        self.assertEqual(headers["Foo"], "bar baz,even more lines")
        self.assertEqual(headers.get_list("foo"), ["bar baz", "even more lines"])
        self.assertEqual(sorted(list(headers.get_all())),
                         [("Asdf", "qwer zxcv"),
                          ("Foo", "bar baz"),
                          ("Foo", "even more lines")])