forked from aquavitae/docker-weasyprint
-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.py
101 lines (74 loc) · 2.67 KB
/
test.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
97
98
99
100
101
#!/usr/bin/env python3.5
import json
import re
import subprocess
import unittest
from urllib.request import Request, urlopen
html_data = '''
<!DOCTYPE html>
<html>
<head>
<title>Test File</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
'''
def request_factory(path='/'):
url = 'http://127.0.0.1:5001%s' % path
headers = {
'Content-Type': 'application/html'
}
return Request(url, data=html_data.encode('utf-8'), headers=headers, method='POST')
class TestPdf(unittest.TestCase):
def setUp(self):
request = request_factory('/pdf?filename=sample.pdf')
self.response = urlopen(request)
def tearDown(self):
self.response.close()
def test_response_code(self):
self.assertEqual(self.response.getcode(), 200)
def test_headers(self):
headers = dict(self.response.info())
self.assertEqual(headers['Content-Type'], 'application/pdf')
self.assertEqual(headers['Content-Disposition'], 'inline;filename=sample.pdf')
def test_body(self):
self.assertEqual(self.response.read()[:4], b'%PDF')
class TestPng(unittest.TestCase):
def setUp(self):
request = request_factory('/png?filename=sample.png')
self.response = urlopen(request)
def tearDown(self):
self.response.close()
def test_response_code(self):
self.assertEqual(self.response.getcode(), 200)
def test_headers(self):
headers = dict(self.response.info())
self.assertEqual(headers['Content-Type'], 'image/png')
self.assertEqual(headers['Content-Disposition'], 'inline;filename=sample.png')
def test_body(self):
self.assertEqual(self.response.read()[:4], b'\x89PNG')
class TestMultiple(unittest.TestCase):
def setUp(self):
url = 'http://127.0.0.1:5001/multiple?filename=sample.pdf'
headers = {
'Content-Type': 'application/json'
}
data = json.dumps([html_data, html_data]).encode('utf-8')
request = Request(url, data=data, headers=headers, method='POST')
self.response = urlopen(request)
def tearDown(self):
self.response.close()
def test_response_code(self):
self.assertEqual(self.response.getcode(), 200)
def test_headers(self):
headers = dict(self.response.info())
self.assertEqual(headers['Content-Type'], 'application/pdf')
self.assertEqual(headers['Content-Disposition'], 'inline;filename=sample.pdf')
def test_body(self):
data = self.response.read()
pages = re.findall(rb'<<\s+/Type\s+/Page\b', data, re.MULTILINE|re.DOTALL)
self.assertEqual(len(pages), 2)
if __name__ == '__main__':
unittest.main()