def test_lists_of_input_args(self): urls = ['http://ya.ru', 'http://google.com'] paths = ['fixtures/example.html', 'fixtures/example.html'] r = wkhtmltox.wkhtmltopdf(urls, 'url') r2 = wkhtmltox.wkhtmltopdf(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_stylesheet_throw_error_when_url(self): r = wkhtmltox.wkhtmltopdf('http://ya.ru', 'url', css='fixtures/example.css') with self.assertRaises(r.ImproperSourceError): r.to_pdf()
def test_cover_without_options(self): r = wkhtmltox.wkhtmltopdf('html', 'string', cover='test.html') command = r.command() self.assertEqual(command[1], 'cover') self.assertEqual(command[2], 'test.html')
def test_repeatable_options(self): roptions = { '--page-size': 'Letter', 'cookies': [ ('test_cookie1', 'cookie_value1'), ('test_cookie2', 'cookie_value2'), ] } r = wkhtmltox.wkhtmltopdf('html', 'string', options=roptions) test_command = r.command('test') idx1 = test_command.index( '--page-size') # Raise exception in case of not found self.assertTrue(test_command[idx1 + 1] == 'Letter') 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_options_parsing(self): r = wkhtmltox.wkhtmltopdf('html', 'string', options={'page-size': 'Letter'}) test_command = r.command('test') idx = test_command.index( '--page-size') # Raise exception in case of not found self.assertTrue(test_command[idx + 1] == 'Letter')
def test_options_parsing_with_tuple_no_dashes(self): options = {'custom-header': [('Accept-Encoding', 'gzip')]} r = wkhtmltox.wkhtmltopdf('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_outline_options(self): options = {'outline': None, 'outline-depth': 1} r = wkhtmltox.wkhtmltopdf('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_stylesheet_adding_without_head_tag(self): r = wkhtmltox.wkhtmltopdf('<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_command_construction(self): r = wkhtmltox.wkhtmltopdf('html', 'string', options={ 'page-size': 'Letter', 'toc-l1-font-size': 12 }) command = r.command() self.assertEqual(command[0], r.wkhtmltopdf) self.assertEqual(command[command.index('--page-size') + 1], 'Letter') self.assertEqual(command[command.index('--toc-l1-font-size') + 1], '12')
def test_raise_error_if_bad_wkhtmltopdf_option(self): r = wkhtmltox.wkhtmltopdf('<html><body>Hai!</body></html>', 'string', options={'bad-option': None}) with self.assertRaises(IOError) as cm: r.to_pdf() raised_exception = cm.exception self.assertRegexpMatches( str(raised_exception), '^wkhtmltopdf exited with non-zero code 1. error:\nUnknown long argument --bad-option\r?\n' )
def test_stylesheet_adding_to_the_head(self): #TODO rewrite this part of wkhtmltox.py r = wkhtmltox.wkhtmltopdf( '<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_wkhtmltox_meta_tags(self): body = """ <html> <head> <meta name="wkhtmltox-page-size" content="Legal"/> <meta name="wkhtmltox-orientation" content="Landscape"/> </head> """ r = wkhtmltox.wkhtmltopdf(body, 'string') command = r.command() self.assertEqual(command[command.index('--page-size') + 1], 'Legal') self.assertEqual(command[command.index('--orientation') + 1], 'Landscape')
def test_multiple_stylesheet_adding_without_head_tag(self): css_files = ['fixtures/example.css', 'fixtures/example2.css'] r = wkhtmltox.wkhtmltopdf('<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_multiple_stylesheets_adding_to_the_head(self): #TODO rewrite this part of wkhtmltox.py css_files = ['fixtures/example.css', 'fixtures/example2.css'] r = wkhtmltox.wkhtmltopdf( '<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_cover_with_options(self): options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8" } r = wkhtmltox.wkhtmltopdf('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 = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8" } r = wkhtmltox.wkhtmltopdf('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_toc_with_options(self): options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8" } r = wkhtmltox.wkhtmltopdf('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_raise_error_with_invalid_url(self): r = wkhtmltox.wkhtmltopdf('wrongurl', 'url') with self.assertRaises(IOError): r.to_pdf('out.pdf')
def test_filter_empty_and_none_values_in_opts(self): options = {'outline': '', 'footer-line': None, 'quiet': False} r = wkhtmltox.wkhtmltopdf('html', 'string', options=options) cmd = r.command() self.assertEqual(len(cmd), 6)
def test_pdf_generation(self): r = wkhtmltox.wkhtmltopdf('html', 'string', options={'page-size': 'Letter'}) pdf = r.to_pdf('out.pdf') self.assertTrue(pdf)
def test_raise_error_with_wrong_css_path(self): css = 'fixtures/wrongpath.css' r = wkhtmltox.wkhtmltopdf('fixtures/example.html', 'file', css=css) with self.assertRaises(IOError): r.to_pdf()
def test_pdf_generation_from_file_like(self): with open('fixtures/example.html', 'r') as f: r = wkhtmltox.wkhtmltopdf(f, 'file') output = r.to_pdf() self.assertEqual(output[:4].decode('utf-8'), '%PDF')
def test_wkhtmltopdf_error_handling(self): r = wkhtmltox.wkhtmltopdf('clearlywrongurl.asdf', 'url') with self.assertRaises(IOError): r.to_pdf()
def test_stylesheet_adding_to_file_with_option(self): css = 'fixtures/example.css' r = wkhtmltox.wkhtmltopdf('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_url_source(self): r = wkhtmltox.wkhtmltopdf('http://ya.ru', 'url') self.assertTrue(r.source.isUrl())
def test_toc_handling_without_options(self): r = wkhtmltox.wkhtmltopdf('hmtl', 'string', toc={'xsl-style-sheet': 'test.xsl'}) self.assertEqual(r.command()[1], 'toc') self.assertEqual(r.command()[2], '--xsl-style-sheet')
def test_file_source_with_path(self): r = wkhtmltox.wkhtmltopdf('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_file_object_source(self): with open('fixtures/example.html') as fl: r = wkhtmltox.wkhtmltopdf(fl, 'file') self.assertTrue(r.source.isFileObj())
def test_file_source(self): r = wkhtmltox.wkhtmltopdf('fixtures/example.html', 'file') self.assertTrue(r.source.isFile())
def test_raise_error_with_invalid_file_path(self): paths = ['frongpath.html', 'wrongpath2.html'] with self.assertRaises(IOError): wkhtmltox.wkhtmltopdf('wrongpath.html', 'file') with self.assertRaises(IOError): wkhtmltox.wkhtmltopdf(paths, 'file')