/
imgkit-tests.py
executable file
路416 lines (334 loc) 路 14.7 KB
/
imgkit-tests.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# -*- coding: utf-8 -*-
import os
import io
import sys
import codecs
import unittest
# Prepend ../ to PYTHONPATH so that we can import IMGKIT form there.
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.realpath(os.path.join(TESTS_ROOT, '..')))
import imgkit
class TestIMGKitInitialization(unittest.TestCase):
"""Test init"""
def test_html_source(self):
r = imgkit.IMGKit('<h1>Oh hai</h1>', 'string')
self.assertTrue(r.source.isString())
def test_url_source(self):
r = imgkit.IMGKit('http://ya.ru', 'url')
self.assertTrue(r.source.isUrl())
def test_file_source(self):
r = imgkit.IMGKit('fixtures/example.html', 'file')
self.assertTrue(r.source.isFile())
def test_file_object_source(self):
with open('fixtures/example.html') as fl:
r = imgkit.IMGKit(fl, 'file')
self.assertTrue(r.source.isFileObj())
def test_file_source_with_path(self):
r = imgkit.IMGKit('test', 'string')
with io.open('fixtures/example.css') as f:
self.assertTrue(r.source.isFile(path=f))
with codecs.open('fixtures/example.css', encoding='UTF-8') as f:
self.assertTrue(r.source.isFile(path=f))
def test_options_parsing(self):
r = imgkit.IMGKit('html', 'string', options={'format': 'jpg'})
test_command = r.command('test')
idx = test_command.index('--format') # Raise exception in case of not found
self.assertTrue(test_command[idx + 1] == 'jpg')
def test_options_parsing_with_dashes(self):
r = imgkit.IMGKit('html', 'string', options={'--format': 'jpg'})
test_command = r.command('test')
idx = test_command.index('--format') # Raise exception in case of not found
self.assertTrue(test_command[idx + 1] == 'jpg')
def test_options_parsing_with_tuple(self):
options = {
'--custom-header': [
('Accept-Encoding', 'gzip')
]
}
r = imgkit.IMGKit('html', 'string', options=options)
command = r.command()
idx1 = command.index('--custom-header') # Raise exception in case of not found
self.assertTrue(command[idx1 + 1] == 'Accept-Encoding')
self.assertTrue(command[idx1 + 2] == 'gzip')
def test_options_parsing_with_tuple_no_dashes(self):
options = {
'custom-header': [
('Accept-Encoding', 'gzip')
]
}
r = imgkit.IMGKit('html', 'string', options=options)
command = r.command()
idx1 = command.index('--custom-header') # Raise exception in case of not found
self.assertTrue(command[idx1 + 1] == 'Accept-Encoding')
self.assertTrue(command[idx1 + 2] == 'gzip')
def test_repeatable_options(self):
roptions = {
'--format': 'jpg',
'cookies': [
('test_cookie1', 'cookie_value1'),
('test_cookie2', 'cookie_value2'),
]
}
r = imgkit.IMGKit('html', 'string', options=roptions)
test_command = r.command('test')
idx1 = test_command.index('--format') # Raise exception in case of not found
self.assertTrue(test_command[idx1 + 1] == 'jpg')
self.assertTrue(test_command.count('--cookies') == 2)
idx2 = test_command.index('--cookies')
self.assertTrue(test_command[idx2 + 1] == 'test_cookie1')
self.assertTrue(test_command[idx2 + 2] == 'cookie_value1')
idx3 = test_command.index('--cookies', idx2 + 2)
self.assertTrue(test_command[idx3 + 1] == 'test_cookie2')
self.assertTrue(test_command[idx3 + 2] == 'cookie_value2')
def test_custom_config(self):
conf = imgkit.config()
self.assertEqual('imgkit-', conf.meta_tag_prefix)
conf = imgkit.config(meta_tag_prefix='prefix-')
self.assertEqual('prefix-', conf.meta_tag_prefix)
with self.assertRaises(IOError):
imgkit.config(wkhtmltoimage='wrongpath')
class TestIMGKitCommandGeneration(unittest.TestCase):
"""Test command() method"""
def test_command_construction(self):
r = imgkit.IMGKit('html', 'string', options={'format': 'jpg', 'toc-l1-font-size': 12})
command = r.command()
self.assertEqual(command[0], r.wkhtmltoimage)
self.assertEqual(command[command.index('--format') + 1], 'jpg')
self.assertEqual(command[command.index('--toc-l1-font-size') + 1], '12')
def test_lists_of_input_args(self):
urls = ['http://ya.ru', 'http://google.com']
paths = ['fixtures/example.html', 'fixtures/example.html']
r = imgkit.IMGKit(urls, 'url')
r2 = imgkit.IMGKit(paths, 'file')
cmd = r.command()
cmd2 = r2.command()
self.assertEqual(cmd[-3:], ['http://ya.ru', 'http://google.com', '-'])
self.assertEqual(cmd2[-3:], ['fixtures/example.html', 'fixtures/example.html', '-'])
def test_read_source_from_stdin(self):
r = imgkit.IMGKit('html', 'string')
self.assertEqual(r.command()[-2:], ['-', '-'])
def test_url_in_command(self):
r = imgkit.IMGKit('http://ya.ru', 'url')
self.assertEqual(r.command()[-2:], ['http://ya.ru', '-'])
def test_file_path_in_command(self):
path = 'fixtures/example.html'
r = imgkit.IMGKit(path, 'file')
self.assertEqual(r.command()[-2:], [path, '-'])
def test_output_path(self):
out = '/test/test2/out.jpg'
r = imgkit.IMGKit('html', 'string')
self.assertEqual(r.command(out)[-1:], ['/test/test2/out.jpg'])
def test_imgkit_meta_tags(self):
body = """
<html>
<head>
<meta name="imgkit-format" content="jpg"/>
<meta name="imgkit-orientation" content="Landscape"/>
</head>
"""
r = imgkit.IMGKit(body, 'string')
command = r.command()
self.assertEqual(command[command.index('--format') + 1], 'jpg')
self.assertEqual(command[command.index('--orientation') + 1], 'Landscape')
def test_imgkit_meta_tags_in_bad_markup(self):
body = """
<html>
<head>
<meta name="imgkit-format" content="jpg"/>
<meta name="imgkit-orientation" content="Landscape"/>
</head>
<br>
</html>
"""
r = imgkit.IMGKit(body, 'string')
command = r.command()
self.assertEqual(command[command.index('--format') + 1], 'jpg')
self.assertEqual(command[command.index('--orientation') + 1], 'Landscape')
def test_skip_nonimgkit_tags(self):
body = """
<html>
<head>
<meta name="test-page-size" content="Legal"/>
<meta name="imgkit-orientation" content="Landscape"/>
</head>
<br>
</html>
"""
r = imgkit.IMGKit(body, 'string')
command = r.command()
self.assertEqual(command[command.index('--orientation') + 1], 'Landscape')
def test_toc_handling_without_options(self):
r = imgkit.IMGKit('hmtl', 'string', toc={'xsl-style-sheet': 'test.xsl'})
self.assertEqual(r.command()[1], 'toc')
self.assertEqual(r.command()[2], '--xsl-style-sheet')
def test_toc_with_options(self):
options = {
'format': 'jpg',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8"
}
r = imgkit.IMGKit('html', 'string', options=options, toc={'xsl-style-sheet': 'test.xsl'})
command = r.command()
self.assertEqual(command[1 + len(options) * 2], 'toc')
self.assertEqual(command[1 + len(options) * 2 + 1], '--xsl-style-sheet')
def test_cover_without_options(self):
r = imgkit.IMGKit('html', 'string', cover='test.html')
command = r.command()
self.assertEqual(command[1], 'cover')
self.assertEqual(command[2], 'test.html')
def test_cover_with_options(self):
options = {
'format': 'jpg',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8"
}
r = imgkit.IMGKit('html', 'string', options=options, cover='test.html')
command = r.command()
self.assertEqual(command[1 + len(options) * 2], 'cover')
self.assertEqual(command[1 + len(options) * 2 + 1], 'test.html')
def test_cover_and_toc(self):
options = {
'format': 'jpg',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8"
}
r = imgkit.IMGKit('html', 'string', options=options, toc={'xsl-style-sheet': 'test.xsl'}, cover='test.html')
command = r.command()
self.assertEqual(command[-7:], ['toc', '--xsl-style-sheet', 'test.xsl', 'cover', 'test.html', '-', '-'])
def test_cover_and_toc_cover_first(self):
options = {
'format': 'jpg',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8"
}
r = imgkit.IMGKit('html', 'string', options=options, toc={'xsl-style-sheet': 'test.xsl'}, cover='test.html',
cover_first=True)
command = r.command()
self.assertEqual(command[-7:], ['cover', 'test.html', 'toc', '--xsl-style-sheet', 'test.xsl', '-', '-'])
def test_outline_options(self):
options = {
'outline': None,
'outline-depth': 1
}
r = imgkit.IMGKit('ya.ru', 'url', options=options)
cmd = r.command()
# self.assertEqual(cmd[1:], ['--outline', '--outline-depth', '1', 'ya.ru', '-'])
self.assertIn('--outline', cmd)
self.assertEqual(cmd[cmd.index('--outline-depth') + 1], '1')
def test_filter_empty_and_none_values_in_opts(self):
options = {
'outline': '',
'footer-line': None,
'quiet': False
}
r = imgkit.IMGKit('html', 'string', options=options)
cmd = r.command()
self.assertEqual(len(cmd), 6)
class TestIMGKitGeneration(unittest.TestCase):
"""Test to_img() method"""
def setUp(self):
pass
def tearDown(self):
if os.path.exists('out.jpg'):
os.remove('out.jpg')
def test_img_generation(self):
r = imgkit.IMGKit('html', 'string', options={'format': 'jpg'})
pic = r.to_img('out.jpg')
self.assertTrue(pic)
def test_img_generation_xvfb(self):
r = imgkit.IMGKit('html', 'string', options={'format': 'jpg', 'xvfb': ''})
pic = r.to_img('out.jpg')
self.assertTrue(pic)
def test_raise_error_with_invalid_url(self):
r = imgkit.IMGKit('wrongurl', 'url')
with self.assertRaises(IOError):
r.to_img('out.jpg')
def test_raise_error_with_invalid_file_path(self):
paths = ['frongpath.html', 'wrongpath2.html']
with self.assertRaises(IOError):
imgkit.IMGKit('wrongpath.html', 'file')
with self.assertRaises(IOError):
imgkit.IMGKit(paths, 'file')
def test_stylesheet_adding_to_the_head(self):
# TODO rewrite this part of pdfkit.py
r = imgkit.IMGKit('<html><head></head><body>Hai!</body></html>', 'string',
css='fixtures/example.css')
with open('fixtures/example.css') as f:
css = f.read()
r._prepend_css('fixtures/example.css')
self.assertIn('<style>%s</style>' % css, r.source.to_s())
def test_stylesheet_adding_without_head_tag(self):
r = imgkit.IMGKit('<html><body>Hai!</body></html>', 'string',
options={'quiet': None}, css='fixtures/example.css')
with open('fixtures/example.css') as f:
css = f.read()
r._prepend_css('fixtures/example.css')
self.assertIn('<style>%s</style><html>' % css, r.source.to_s())
def test_multiple_stylesheets_adding_to_the_head(self):
# TODO rewrite this part of pdfkit.py
css_files = ['fixtures/example.css', 'fixtures/example2.css']
r = imgkit.IMGKit('<html><head></head><body>Hai!</body></html>', 'string',
css=css_files)
css = []
for css_file in css_files:
with open(css_file) as f:
css.append(f.read())
r._prepend_css(css_files)
self.assertIn('<style>%s</style>' % "\n".join(css), r.source.to_s())
def test_multiple_stylesheet_adding_without_head_tag(self):
css_files = ['fixtures/example.css', 'fixtures/example2.css']
r = imgkit.IMGKit('<html><body>Hai!</body></html>', 'string',
options={'quiet': None}, css=css_files)
css = []
for css_file in css_files:
with open(css_file) as f:
css.append(f.read())
r._prepend_css(css_files)
self.assertIn('<style>%s</style><html>' % "\n".join(css), r.source.to_s())
def test_stylesheet_throw_error_when_url(self):
r = imgkit.IMGKit('http://ya.ru', 'url', css='fixtures/example.css')
with self.assertRaises(r.SourceError):
r.to_img()
def test_stylesheet_adding_to_file_with_option(self):
css = 'fixtures/example.css'
r = imgkit.IMGKit('fixtures/example.html', 'file', css=css)
self.assertEqual(r.css, css)
r._prepend_css(css)
self.assertIn('font-size', r.source.to_s())
def test_wkhtmltoimage_error_handling(self):
r = imgkit.IMGKit('clearlywrongurl.asdf', 'url')
with self.assertRaises(IOError):
r.to_img()
def test_pdf_generation_from_file_like(self):
with open('fixtures/example.html', 'r') as f:
r = imgkit.IMGKit(f, 'file')
output = r.to_img()
self.assertEqual(output[:4], b'\xff\xd8\xff\xe0') # TODO img
def test_raise_error_with_wrong_css_path(self):
css = 'fixtures/wrongpath.css'
r = imgkit.IMGKit('fixtures/example.html', 'file', css=css)
with self.assertRaises(IOError):
r.to_img()
def test_raise_error_if_bad_wkhtmltoimage_option(self):
r = imgkit.IMGKit('<html><body>Hai!</body></html>', 'string',
options={'bad-option': None})
with self.assertRaises(IOError) as cm:
r.to_img()
raised_exception = cm.exception
self.assertRegexpMatches(str(raised_exception),
'^wkhtmltoimage exited with non-zero code 1. error:\nUnknown long argument '
'--bad-option\r?\n')
if __name__ == "__main__":
unittest.main()