-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_rfc6266.py
97 lines (74 loc) · 2.92 KB
/
test_rfc6266.py
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
# vim: set fileencoding=utf-8 sw=4 ts=4 et :
from rfc6266 import (
parse_headers, parse_httplib2_response, parse_requests_response,
build_header)
import pytest
def test_parsing():
assert parse_headers(None).disposition == 'inline'
assert parse_headers('attachment').disposition == 'attachment'
assert parse_headers('attachment; key=val').assocs['key'] == 'val'
assert parse_headers(
'attachment; filename=simple').filename_unsafe == 'simple'
# test ISO-8859-1
fname = parse_headers(u'attachment; filename="oyé"').filename_unsafe
assert fname == u'oyé', repr(fname)
cd = parse_headers(
'attachment; filename="EURO rates";'
' filename*=utf-8\'\'%e2%82%ac%20rates')
assert cd.filename_unsafe == u'€ rates'
@pytest.mark.skipif("(3,0) <= sys.version_info < (3,3)")
def test_httplib2(httpserver):
httplib2 = pytest.importorskip('httplib2')
http = httplib2.Http()
httpserver.serve_content('eep', headers={
'Content-Disposition': 'attachment; filename="a b="'})
resp, content = http.request(httpserver.url)
assert parse_httplib2_response(resp).filename_unsafe == 'a b='
@pytest.mark.skipif("(3,0) <= sys.version_info < (3,3)")
def test_requests(httpserver):
requests = pytest.importorskip('requests')
httpserver.serve_content('eep', headers={
'Content-Disposition': 'attachment; filename="a b="'})
resp = requests.get(httpserver.url)
assert parse_requests_response(resp).filename_unsafe == 'a b='
def test_location_fallback():
assert parse_headers(
None, location='https://foo/bar%c3%a9.py'
).filename_unsafe == u'baré.py'
assert parse_headers(
None, location='https://foo/'
).filename_unsafe == u''
assert parse_headers(
None, location='https://foo/%C3%A9toil%C3%A9/'
).filename_unsafe == u'étoilé'
def test_strict():
# Trailing ; means the header is rejected
assert parse_headers('attachment;').disposition == 'inline'
assert parse_headers('attachment; key=val;').disposition == 'inline'
try:
cd = parse_headers(
'attachment; filename="spa ced";')
except ValueError:
assert True
else:
assert False, cd
def test_relaxed():
assert parse_headers(
'attachment;', relaxed=True).disposition == 'attachment'
assert parse_headers(
'attachment; key=val;', relaxed=True).disposition == 'attachment'
cd = parse_headers(
'attachment; filename="spa ced";',
relaxed=True)
assert cd.filename_unsafe == u'spa ced'
def test_roundtrip():
def roundtrip(filename):
return parse_headers(build_header(filename)).filename_unsafe
def assert_roundtrip(filename):
assert roundtrip(filename) == filename
assert_roundtrip('a b')
assert_roundtrip('a b')
assert_roundtrip('a b ')
assert_roundtrip(' a b')
assert_roundtrip('a\"b')
assert_roundtrip(u'aéio o♥u"qfsdf!')