forked from deanishe/alfred-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web_http_encoding.py
More file actions
68 lines (48 loc) · 1.86 KB
/
test_web_http_encoding.py
File metadata and controls
68 lines (48 loc) · 1.86 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: fileencoding=utf-8
"""
test_encoding.py
Created by deanishe@deanishe.net on 2014-08-17.
Copyright (c) 2014 deanishe@deanishe.net
MIT Licence. See http://opensource.org/licenses/MIT
"""
from __future__ import print_function
import os
import pytest
import pytest_localserver
from workflow import web
TEST_DATA = [
# Baidu sends charset header *and* uses meta HTML tag
('baidu.html', {'Content-Type': 'text/html; charset=utf-8'}, 'utf-8'),
# Document specifies us-ascii
('us-ascii.xml', {'Content-Type': 'application/xml'}, 'us-ascii'),
# Document specifies UTF-8
('utf8.xml', {'Content-Type': 'application/xml'}, 'utf-8'),
# Document specifies no encoding; application/xml defaults to UTF-8
# (in this library. There is no standard encoding defined.)
('no-encoding.xml', {'Content-Type': 'application/xml'}, 'utf-8'),
# application/json is UTF-8
('utf8.json', {'Content-Type': 'application/json'}, 'utf-8'),
]
def test_web_encoding(httpserver):
"""Test web encoding"""
test_data = []
for filename, headers, encoding in TEST_DATA:
p = os.path.join(os.path.dirname(__file__), 'data', filename)
test_data.append((p, headers, encoding))
p2 = '{0}.gz'.format(p)
if os.path.exists(p2):
h2 = headers.copy()
h2['Content-Encoding'] = 'gzip'
test_data.append((p2, h2, encoding))
for filepath, headers, encoding in test_data:
print('filepath={0!r}, headers={1!r}, encoding={2!r}'.format(
filepath, headers, encoding))
content = open(filepath).read()
httpserver.serve_content(content, headers=headers)
r = web.get(httpserver.url)
r.raise_for_status()
assert r.encoding == encoding
if __name__ == '__main__': # pragma: no cover
pytest.main([__file__])