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_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_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_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_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_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_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_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_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_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_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_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_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_stylesheet_adding_to_the_head(self): 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>{}</style>'.format(css), r.source.to_s())
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_img_generation_xvfb(self): r = imgkit.IMGKit('html', 'string', options={ 'format': 'jpg', 'xvfb': '' }) pic = r.to_img('out.jpg') self.assertTrue(pic)
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_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')
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_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)
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_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_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_stylesheet_adding_to_the_head(self): 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>{}</style>".format(css), r.source.to_s())
def test_multiple_stylesheets_adding_to_the_head(self): 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>{}</style>'.format("\n".join(css)), r.source.to_s())
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_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_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>{}</style><html>".format(css), r.source.to_s())
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_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')