| 1 |
import platform |
| 2 |
import unittest |
| 3 |
from datetime import datetime, timezone |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from django.test import SimpleTestCase |
| 7 |
from django.utils.datastructures import MultiValueDict |
| 8 |
from django.utils.http import ( |
| 9 |
MAX_URL_LENGTH, |
| 10 |
base36_to_int, |
| 11 |
content_disposition_header, |
| 12 |
escape_leading_slashes, |
| 13 |
http_date, |
| 14 |
int_to_base36, |
| 15 |
is_same_domain, |
| 16 |
parse_etags, |
| 17 |
parse_header_parameters, |
| 18 |
parse_http_date, |
| 19 |
quote_etag, |
| 20 |
url_has_allowed_host_and_scheme, |
| 21 |
urlencode, |
| 22 |
urlsafe_base64_decode, |
| 23 |
urlsafe_base64_encode, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class URLEncodeTests(SimpleTestCase): |
| 28 |
cannot_encode_none_msg = ( |
| 29 |
"Cannot encode None for key 'a' in a query string. Did you mean to " |
| 30 |
"pass an empty string or omit the value?" |
| 31 |
) |
| 32 |
|
| 33 |
def test_tuples(self): |
| 34 |
self.assertEqual(urlencode((("a", 1), ("b", 2), ("c", 3))), "a=1&b=2&c=3") |
| 35 |
|
| 36 |
def test_dict(self): |
| 37 |
result = urlencode({"a": 1, "b": 2, "c": 3}) |
| 38 |
self.assertEqual(result, "a=1&b=2&c=3") |
| 39 |
|
| 40 |
def test_dict_containing_sequence_not_doseq(self): |
| 41 |
self.assertEqual(urlencode({"a": [1, 2]}, doseq=False), "a=%5B1%2C+2%5D") |
| 42 |
|
| 43 |
def test_dict_containing_tuple_not_doseq(self): |
| 44 |
self.assertEqual(urlencode({"a": (1, 2)}, doseq=False), "a=%281%2C+2%29") |
| 45 |
|
| 46 |
def test_custom_iterable_not_doseq(self): |
| 47 |
class IterableWithStr: |
| 48 |
def __str__(self): |
| 49 |
return "custom" |
| 50 |
|
| 51 |
def __iter__(self): |
| 52 |
yield from range(0, 3) |
| 53 |
|
| 54 |
self.assertEqual(urlencode({"a": IterableWithStr()}, doseq=False), "a=custom") |
| 55 |
|
| 56 |
def test_dict_containing_sequence_doseq(self): |
| 57 |
self.assertEqual(urlencode({"a": [1, 2]}, doseq=True), "a=1&a=2") |
| 58 |
|
| 59 |
def test_dict_containing_empty_sequence_doseq(self): |
| 60 |
self.assertEqual(urlencode({"a": []}, doseq=True), "") |
| 61 |
|
| 62 |
def test_multivaluedict(self): |
| 63 |
result = urlencode( |
| 64 |
MultiValueDict( |
| 65 |
{ |
| 66 |
"name": ["Adrian", "Simon"], |
| 67 |
"position": ["Developer"], |
| 68 |
} |
| 69 |
), |
| 70 |
doseq=True, |
| 71 |
) |
| 72 |
self.assertEqual(result, "name=Adrian&name=Simon&position=Developer") |
| 73 |
|
| 74 |
def test_dict_with_bytes_values(self): |
| 75 |
self.assertEqual(urlencode({"a": b"abc"}, doseq=True), "a=abc") |
| 76 |
|
| 77 |
def test_dict_with_sequence_of_bytes(self): |
| 78 |
self.assertEqual( |
| 79 |
urlencode({"a": [b"spam", b"eggs", b"bacon"]}, doseq=True), |
| 80 |
"a=spam&a=eggs&a=bacon", |
| 81 |
) |
| 82 |
|
| 83 |
def test_dict_with_bytearray(self): |
| 84 |
self.assertEqual(urlencode({"a": bytearray(range(2))}, doseq=True), "a=0&a=1") |
| 85 |
|
| 86 |
def test_generator(self): |
| 87 |
self.assertEqual(urlencode({"a": range(2)}, doseq=True), "a=0&a=1") |
| 88 |
self.assertEqual(urlencode({"a": range(2)}, doseq=False), "a=range%280%2C+2%29") |
| 89 |
|
| 90 |
def test_none(self): |
| 91 |
with self.assertRaisesMessage(TypeError, self.cannot_encode_none_msg): |
| 92 |
urlencode({"a": None}) |
| 93 |
|
| 94 |
def test_none_in_sequence(self): |
| 95 |
with self.assertRaisesMessage(TypeError, self.cannot_encode_none_msg): |
| 96 |
urlencode({"a": [None]}, doseq=True) |
| 97 |
|
| 98 |
def test_none_in_generator(self): |
| 99 |
def gen(): |
| 100 |
yield None |
| 101 |
|
| 102 |
with self.assertRaisesMessage(TypeError, self.cannot_encode_none_msg): |
| 103 |
urlencode({"a": gen()}, doseq=True) |
| 104 |
|
| 105 |
|
| 106 |
class Base36IntTests(SimpleTestCase): |
| 107 |
def test_roundtrip(self): |
| 108 |
for n in [0, 1, 1000, 1000000]: |
| 109 |
self.assertEqual(n, base36_to_int(int_to_base36(n))) |
| 110 |
|
| 111 |
def test_negative_input(self): |
| 112 |
with self.assertRaisesMessage(ValueError, "Negative base36 conversion input."): |
| 113 |
int_to_base36(-1) |
| 114 |
|
| 115 |
def test_to_base36_errors(self): |
| 116 |
for n in ["1", "foo", {1: 2}, (1, 2, 3), 3.141]: |
| 117 |
with self.assertRaises(TypeError): |
| 118 |
int_to_base36(n) |
| 119 |
|
| 120 |
def test_invalid_literal(self): |
| 121 |
for n in ["#", " "]: |
| 122 |
with self.assertRaisesMessage( |
| 123 |
ValueError, "invalid literal for int() with base 36: '%s'" % n |
| 124 |
): |
| 125 |
base36_to_int(n) |
| 126 |
|
| 127 |
def test_input_too_large(self): |
| 128 |
with self.assertRaisesMessage(ValueError, "Base36 input too large"): |
| 129 |
base36_to_int("1" * 14) |
| 130 |
|
| 131 |
def test_to_int_errors(self): |
| 132 |
for n in [123, {1: 2}, (1, 2, 3), 3.141]: |
| 133 |
with self.assertRaises(TypeError): |
| 134 |
base36_to_int(n) |
| 135 |
|
| 136 |
def test_values(self): |
| 137 |
for n, b36 in [(0, "0"), (1, "1"), (42, "16"), (818469960, "django")]: |
| 138 |
self.assertEqual(int_to_base36(n), b36) |
| 139 |
self.assertEqual(base36_to_int(b36), n) |
| 140 |
|
| 141 |
|
| 142 |
class URLHasAllowedHostAndSchemeTests(unittest.TestCase): |
| 143 |
def test_bad_urls(self): |
| 144 |
bad_urls = ( |
| 145 |
"http://example.com", |
| 146 |
"http:///example.com", |
| 147 |
"https://example.com", |
| 148 |
"ftp://example.com", |
| 149 |
r"\\example.com", |
| 150 |
r"\\\example.com", |
| 151 |
r"/\\/example.com", |
| 152 |
r"\\\example.com", |
| 153 |
r"\\example.com", |
| 154 |
r"\\//example.com", |
| 155 |
r"/\/example.com", |
| 156 |
r"\/example.com", |
| 157 |
r"/\example.com", |
| 158 |
"http:///example.com", |
| 159 |
r"http:/\//example.com", |
| 160 |
r"http:\/example.com", |
| 161 |
r"http:/\example.com", |
| 162 |
'javascript:alert("XSS")', |
| 163 |
"\njavascript:alert(x)", |
| 164 |
"java\nscript:alert(x)", |
| 165 |
"\x08//example.com", |
| 166 |
r"http://otherserver\@example.com", |
| 167 |
r"http:\\testserver\@example.com", |
| 168 |
r"http://testserver\me:[email protected]", |
| 169 |
r"http://testserver\@example.com", |
| 170 |
r"http:\\testserver\confirm\[email protected]", |
| 171 |
"http:999999999", |
| 172 |
"ftp:9999999999", |
| 173 |
"\n", |
| 174 |
"http://[2001:cdba:0000:0000:0000:0000:3257:9652/", |
| 175 |
"http://2001:cdba:0000:0000:0000:0000:3257:9652]/", |
| 176 |
) |
| 177 |
for bad_url in bad_urls: |
| 178 |
with self.subTest(url=bad_url): |
| 179 |
self.assertIs( |
| 180 |
url_has_allowed_host_and_scheme( |
| 181 |
bad_url, allowed_hosts={"testserver", "testserver2"} |
| 182 |
), |
| 183 |
False, |
| 184 |
) |
| 185 |
|
| 186 |
def test_good_urls(self): |
| 187 |
good_urls = ( |
| 188 |
"/view/?param=http://example.com", |
| 189 |
"/view/?param=https://example.com", |
| 190 |
"/view?param=ftp://example.com", |
| 191 |
"view/?param=//example.com", |
| 192 |
"https://testserver/", |
| 193 |
"HTTPS://testserver/", |
| 194 |
"//testserver/", |
| 195 |
"http://testserver/[email protected]", |
| 196 |
"/url%20with%20spaces/", |
| 197 |
"path/http:2222222222", |
| 198 |
) |
| 199 |
for good_url in good_urls: |
| 200 |
with self.subTest(url=good_url): |
| 201 |
self.assertIs( |
| 202 |
url_has_allowed_host_and_scheme( |
| 203 |
good_url, allowed_hosts={"otherserver", "testserver"} |
| 204 |
), |
| 205 |
True, |
| 206 |
) |
| 207 |
|
| 208 |
def test_basic_auth(self): |
| 209 |
# Valid basic auth credentials are allowed. |
| 210 |
self.assertIs( |
| 211 |
url_has_allowed_host_and_scheme( |
| 212 |
r"http://user:pass@testserver/", allowed_hosts={"user:pass@testserver"} |
| 213 |
), |
| 214 |
True, |
| 215 |
) |
| 216 |
|
| 217 |
def test_no_allowed_hosts(self): |
| 218 |
# A path without host is allowed. |
| 219 |
self.assertIs( |
| 220 |
url_has_allowed_host_and_scheme( |
| 221 |
"/confirm/[email protected]", allowed_hosts=None |
| 222 |
), |
| 223 |
True, |
| 224 |
) |
| 225 |
# Basic auth without host is not allowed. |
| 226 |
self.assertIs( |
| 227 |
url_has_allowed_host_and_scheme( |
| 228 |
r"http://testserver\@example.com", allowed_hosts=None |
| 229 |
), |
| 230 |
False, |
| 231 |
) |
| 232 |
|
| 233 |
def test_allowed_hosts_str(self): |
| 234 |
self.assertIs( |
| 235 |
url_has_allowed_host_and_scheme( |
| 236 |
"http://good.com/good", allowed_hosts="good.com" |
| 237 |
), |
| 238 |
True, |
| 239 |
) |
| 240 |
self.assertIs( |
| 241 |
url_has_allowed_host_and_scheme( |
| 242 |
"http://good.co/evil", allowed_hosts="good.com" |
| 243 |
), |
| 244 |
False, |
| 245 |
) |
| 246 |
|
| 247 |
def test_secure_param_https_urls(self): |
| 248 |
secure_urls = ( |
| 249 |
"https://example.com/p", |
| 250 |
"HTTPS://example.com/p", |
| 251 |
"/view/?param=http://example.com", |
| 252 |
) |
| 253 |
for url in secure_urls: |
| 254 |
with self.subTest(url=url): |
| 255 |
self.assertIs( |
| 256 |
url_has_allowed_host_and_scheme( |
| 257 |
url, allowed_hosts={"example.com"}, require_https=True |
| 258 |
), |
| 259 |
True, |
| 260 |
) |
| 261 |
|
| 262 |
def test_secure_param_non_https_urls(self): |
| 263 |
insecure_urls = ( |
| 264 |
"http://example.com/p", |
| 265 |
"ftp://example.com/p", |
| 266 |
"//example.com/p", |
| 267 |
) |
| 268 |
for url in insecure_urls: |
| 269 |
with self.subTest(url=url): |
| 270 |
self.assertIs( |
| 271 |
url_has_allowed_host_and_scheme( |
| 272 |
url, allowed_hosts={"example.com"}, require_https=True |
| 273 |
), |
| 274 |
False, |
| 275 |
) |
| 276 |
|
| 277 |
def test_max_url_length(self): |
| 278 |
allowed_host = "example.com" |
| 279 |
max_extra_characters = "é" * (MAX_URL_LENGTH - len(allowed_host) - 1) |
| 280 |
max_length_boundary_url = f"{allowed_host}/{max_extra_characters}" |
| 281 |
cases = [ |
| 282 |
(max_length_boundary_url, True), |
| 283 |
(max_length_boundary_url + "ú", False), |
| 284 |
] |
| 285 |
for url, expected in cases: |
| 286 |
with self.subTest(url=url): |
| 287 |
self.assertIs( |
| 288 |
url_has_allowed_host_and_scheme(url, allowed_hosts={allowed_host}), |
| 289 |
expected, |
| 290 |
) |
| 291 |
|
| 292 |
|
| 293 |
class URLSafeBase64Tests(unittest.TestCase): |
| 294 |
def test_roundtrip(self): |
| 295 |
bytestring = b"foo" |
| 296 |
encoded = urlsafe_base64_encode(bytestring) |
| 297 |
decoded = urlsafe_base64_decode(encoded) |
| 298 |
self.assertEqual(bytestring, decoded) |
| 299 |
|
| 300 |
|
| 301 |
class IsSameDomainTests(unittest.TestCase): |
| 302 |
def test_good(self): |
| 303 |
for pair in ( |
| 304 |
("example.com", "example.com"), |
| 305 |
("example.com", ".example.com"), |
| 306 |
("foo.example.com", ".example.com"), |
| 307 |
("example.com:8888", "example.com:8888"), |
| 308 |
("example.com:8888", ".example.com:8888"), |
| 309 |
("foo.example.com:8888", ".example.com:8888"), |
| 310 |
): |
| 311 |
self.assertIs(is_same_domain(*pair), True) |
| 312 |
|
| 313 |
def test_bad(self): |
| 314 |
for pair in ( |
| 315 |
("example2.com", "example.com"), |
| 316 |
("foo.example.com", "example.com"), |
| 317 |
("example.com:9999", "example.com:8888"), |
| 318 |
("foo.example.com:8888", ""), |
| 319 |
): |
| 320 |
self.assertIs(is_same_domain(*pair), False) |
| 321 |
|
| 322 |
|
| 323 |
class ETagProcessingTests(unittest.TestCase): |
| 324 |
def test_parsing(self): |
| 325 |
self.assertEqual( |
| 326 |
parse_etags(r'"" , "etag", "e\\tag", W/"weak"'), |
| 327 |
['""', '"etag"', r'"e\\tag"', 'W/"weak"'], |
| 328 |
) |
| 329 |
self.assertEqual(parse_etags("*"), ["*"]) |
| 330 |
|
| 331 |
# Ignore RFC 2616 ETags that are invalid according to RFC 9110. |
| 332 |
self.assertEqual(parse_etags(r'"etag", "e\"t\"ag"'), ['"etag"']) |
| 333 |
|
| 334 |
def test_quoting(self): |
| 335 |
self.assertEqual(quote_etag("etag"), '"etag"') # unquoted |
| 336 |
self.assertEqual(quote_etag('"etag"'), '"etag"') # quoted |
| 337 |
self.assertEqual(quote_etag('W/"etag"'), 'W/"etag"') # quoted, weak |
| 338 |
|
| 339 |
|
| 340 |
class HttpDateProcessingTests(unittest.TestCase): |
| 341 |
def test_http_date(self): |
| 342 |
t = 1167616461.0 |
| 343 |
self.assertEqual(http_date(t), "Mon, 01 Jan 2007 01:54:21 GMT") |
| 344 |
|
| 345 |
def test_parsing_rfc1123(self): |
| 346 |
parsed = parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT") |
| 347 |
self.assertEqual( |
| 348 |
datetime.fromtimestamp(parsed, timezone.utc), |
| 349 |
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc), |
| 350 |
) |
| 351 |
|
| 352 |
@unittest.skipIf(platform.architecture()[0] == "32bit", "The Year 2038 problem.") |
| 353 |
@mock.patch("django.utils.http.datetime") |
| 354 |
def test_parsing_rfc850(self, mocked_datetime): |
| 355 |
mocked_datetime.side_effect = datetime |
| 356 |
now_1 = datetime(2019, 11, 6, 8, 49, 37, tzinfo=timezone.utc) |
| 357 |
now_2 = datetime(2020, 11, 6, 8, 49, 37, tzinfo=timezone.utc) |
| 358 |
now_3 = datetime(2048, 11, 6, 8, 49, 37, tzinfo=timezone.utc) |
| 359 |
tests = ( |
| 360 |
( |
| 361 |
now_1, |
| 362 |
"Tuesday, 31-Dec-69 08:49:37 GMT", |
| 363 |
datetime(2069, 12, 31, 8, 49, 37, tzinfo=timezone.utc), |
| 364 |
), |
| 365 |
( |
| 366 |
now_1, |
| 367 |
"Tuesday, 10-Nov-70 08:49:37 GMT", |
| 368 |
datetime(1970, 11, 10, 8, 49, 37, tzinfo=timezone.utc), |
| 369 |
), |
| 370 |
( |
| 371 |
now_1, |
| 372 |
"Sunday, 06-Nov-94 08:49:37 GMT", |
| 373 |
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc), |
| 374 |
), |
| 375 |
( |
| 376 |
now_2, |
| 377 |
"Wednesday, 31-Dec-70 08:49:37 GMT", |
| 378 |
datetime(2070, 12, 31, 8, 49, 37, tzinfo=timezone.utc), |
| 379 |
), |
| 380 |
( |
| 381 |
now_2, |
| 382 |
"Friday, 31-Dec-71 08:49:37 GMT", |
| 383 |
datetime(1971, 12, 31, 8, 49, 37, tzinfo=timezone.utc), |
| 384 |
), |
| 385 |
( |
| 386 |
now_3, |
| 387 |
"Sunday, 31-Dec-00 08:49:37 GMT", |
| 388 |
datetime(2000, 12, 31, 8, 49, 37, tzinfo=timezone.utc), |
| 389 |
), |
| 390 |
( |
| 391 |
now_3, |
| 392 |
"Friday, 31-Dec-99 08:49:37 GMT", |
| 393 |
datetime(1999, 12, 31, 8, 49, 37, tzinfo=timezone.utc), |
| 394 |
), |
| 395 |
) |
| 396 |
for now, rfc850str, expected_date in tests: |
| 397 |
with self.subTest(rfc850str=rfc850str): |
| 398 |
mocked_datetime.now.return_value = now |
| 399 |
parsed = parse_http_date(rfc850str) |
| 400 |
mocked_datetime.now.assert_called_once_with(tz=timezone.utc) |
| 401 |
self.assertEqual( |
| 402 |
datetime.fromtimestamp(parsed, timezone.utc), |
| 403 |
expected_date, |
| 404 |
) |
| 405 |
mocked_datetime.reset_mock() |
| 406 |
|
| 407 |
def test_parsing_asctime(self): |
| 408 |
parsed = parse_http_date("Sun Nov 6 08:49:37 1994") |
| 409 |
self.assertEqual( |
| 410 |
datetime.fromtimestamp(parsed, timezone.utc), |
| 411 |
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc), |
| 412 |
) |
| 413 |
|
| 414 |
def test_parsing_asctime_nonascii_digits(self): |
| 415 |
"""Non-ASCII unicode decimals raise an error.""" |
| 416 |
with self.assertRaises(ValueError): |
| 417 |
parse_http_date("Sun Nov 6 08:49:37 1994") |
| 418 |
with self.assertRaises(ValueError): |
| 419 |
parse_http_date("Sun Nov 12 08:49:37 1994") |
| 420 |
|
| 421 |
def test_parsing_year_less_than_70(self): |
| 422 |
parsed = parse_http_date("Sun Nov 6 08:49:37 0037") |
| 423 |
self.assertEqual( |
| 424 |
datetime.fromtimestamp(parsed, timezone.utc), |
| 425 |
datetime(2037, 11, 6, 8, 49, 37, tzinfo=timezone.utc), |
| 426 |
) |
| 427 |
|
| 428 |
|
| 429 |
class EscapeLeadingSlashesTests(unittest.TestCase): |
| 430 |
def test(self): |
| 431 |
tests = ( |
| 432 |
("//example.com", "/%2Fexample.com"), |
| 433 |
("//", "/%2F"), |
| 434 |
) |
| 435 |
for url, expected in tests: |
| 436 |
with self.subTest(url=url): |
| 437 |
self.assertEqual(escape_leading_slashes(url), expected) |
| 438 |
|
| 439 |
|
| 440 |
class ParseHeaderParameterTests(unittest.TestCase): |
| 441 |
def test_basic(self): |
| 442 |
tests = [ |
| 443 |
("text/plain", ("text/plain", {})), |
| 444 |
("text/vnd.just.made.this.up ; ", ("text/vnd.just.made.this.up", {})), |
| 445 |
("text/plain;charset=us-ascii", ("text/plain", {"charset": "us-ascii"})), |
| 446 |
( |
| 447 |
'text/plain ; charset="us-ascii"', |
| 448 |
("text/plain", {"charset": "us-ascii"}), |
| 449 |
), |
| 450 |
( |
| 451 |
'text/plain ; charset="us-ascii"; another=opt', |
| 452 |
("text/plain", {"charset": "us-ascii", "another": "opt"}), |
| 453 |
), |
| 454 |
( |
| 455 |
'attachment; filename="silly.txt"', |
| 456 |
("attachment", {"filename": "silly.txt"}), |
| 457 |
), |
| 458 |
( |
| 459 |
'attachment; filename="strange;name"', |
| 460 |
("attachment", {"filename": "strange;name"}), |
| 461 |
), |
| 462 |
( |
| 463 |
'attachment; filename="strange;name";size=123;', |
| 464 |
("attachment", {"filename": "strange;name", "size": "123"}), |
| 465 |
), |
| 466 |
( |
| 467 |
'form-data; name="files"; filename="fo\\"o;bar"', |
| 468 |
("form-data", {"name": "files", "filename": 'fo"o;bar'}), |
| 469 |
), |
| 470 |
] |
| 471 |
for header, expected in tests: |
| 472 |
with self.subTest(header=header): |
| 473 |
self.assertEqual(parse_header_parameters(header), expected) |
| 474 |
|
| 475 |
def test_rfc2231_parsing(self): |
| 476 |
test_data = ( |
| 477 |
( |
| 478 |
"Content-Type: application/x-stuff; " |
| 479 |
"title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A", |
| 480 |
"This is ***fun***", |
| 481 |
), |
| 482 |
( |
| 483 |
"Content-Type: application/x-stuff; title*=UTF-8''foo-%c3%a4.html", |
| 484 |
"foo-ä.html", |
| 485 |
), |
| 486 |
( |
| 487 |
"Content-Type: application/x-stuff; title*=iso-8859-1''foo-%E4.html", |
| 488 |
"foo-ä.html", |
| 489 |
), |
| 490 |
) |
| 491 |
for raw_line, expected_title in test_data: |
| 492 |
parsed = parse_header_parameters(raw_line) |
| 493 |
self.assertEqual(parsed[1]["title"], expected_title) |
| 494 |
|
| 495 |
def test_rfc2231_wrong_title(self): |
| 496 |
""" |
| 497 |
Test wrongly formatted RFC 2231 headers (missing double single quotes). |
| 498 |
Parsing should not crash (#24209). |
| 499 |
""" |
| 500 |
test_data = ( |
| 501 |
( |
| 502 |
"Content-Type: application/x-stuff; " |
| 503 |
"title*='This%20is%20%2A%2A%2Afun%2A%2A%2A", |
| 504 |
"'This%20is%20%2A%2A%2Afun%2A%2A%2A", |
| 505 |
), |
| 506 |
("Content-Type: application/x-stuff; title*='foo.html", "'foo.html"), |
| 507 |
("Content-Type: application/x-stuff; title*=bar.html", "bar.html"), |
| 508 |
) |
| 509 |
for raw_line, expected_title in test_data: |
| 510 |
parsed = parse_header_parameters(raw_line) |
| 511 |
self.assertEqual(parsed[1]["title"], expected_title) |
| 512 |
|
| 513 |
|
| 514 |
class ContentDispositionHeaderTests(unittest.TestCase): |
| 515 |
def test_basic(self): |
| 516 |
tests = ( |
| 517 |
((False, None), None), |
| 518 |
((False, "example"), 'inline; filename="example"'), |
| 519 |
((True, None), "attachment"), |
| 520 |
((True, "example"), 'attachment; filename="example"'), |
| 521 |
( |
| 522 |
(True, '"example" file\\name'), |
| 523 |
'attachment; filename="\\"example\\" file\\\\name"', |
| 524 |
), |
| 525 |
((True, "espécimen"), "attachment; filename*=utf-8''esp%C3%A9cimen"), |
| 526 |
( |
| 527 |
(True, '"espécimen" filename'), |
| 528 |
"attachment; filename*=utf-8''%22esp%C3%A9cimen%22%20filename", |
| 529 |
), |
| 530 |
((True, "some\nfile"), "attachment; filename*=utf-8''some%0Afile"), |
| 531 |
) |
| 532 |
|
| 533 |
for (is_attachment, filename), expected in tests: |
| 534 |
with self.subTest(is_attachment=is_attachment, filename=filename): |
| 535 |
self.assertEqual( |
| 536 |
content_disposition_header(is_attachment, filename), expected |
| 537 |
) |