spec/pdfkit_spec.rb in pdfkit-0.7.0 vs spec/pdfkit_spec.rb in pdfkit-0.8.0
- old
+ new
@@ -1,10 +1,11 @@
#encoding: UTF-8
require 'spec_helper'
describe PDFKit do
- context "initialization" do
+ describe "initialization" do
+ # Source
it "should accept HTML as the source" do
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
expect(pdfkit.source).to be_html
expect(pdfkit.source.to_s).to eq('<h1>Oh Hai</h1>')
end
@@ -20,40 +21,151 @@
pdfkit = PDFKit.new(File.new(file_path))
expect(pdfkit.source).to be_file
expect(pdfkit.source.to_s).to eq(file_path)
end
- it "should parse the options into a cmd line friedly format" do
+ # Options
+ ## options keys
+ it "drops options without values" do
+ pdfkit = PDFKit.new('html', :page_size => nil)
+ expect(pdfkit.options).not_to have_key('--page-size')
+ end
+
+ it "transforms keys into command-line arguments" do
pdfkit = PDFKit.new('html', :page_size => 'Letter')
expect(pdfkit.options).to have_key('--page-size')
end
- it "should parse complex options into a cmd line friedly format" do
+ it "transforms complex keys into command-line arguments" do
pdfkit = PDFKit.new('html', :replace => {'value' => 'something else'} )
expect(pdfkit.options).to have_key('--replace')
end
- it "should provide default options" do
+ it "drops options with false or falsey values" do
+ pdfkit = PDFKit.new('html', disable_smart_shrinking: false)
+ expect(pdfkit.options).not_to have_key('--disable-smart-shrinking')
+ end
+
+ ## options values
+ it "parses string option values into strings" do
+ pdfkit = PDFKit.new('html', :page_size => 'Letter')
+ expect(pdfkit.options['--page-size']).to eql 'Letter'
+ end
+
+ it "drops option values of 'true'" do
+ pdfkit = PDFKit.new('html', disable_smart_shrinking: true)
+ expect(pdfkit.options).to have_key('--disable-smart-shrinking')
+ expect(pdfkit.options['--disable-smart-shrinking']).to be_nil
+ end
+
+ it "parses unknown value formats by transforming them into strings" do
+ pdfkit = PDFKit.new('html', image_dpi: 300)
+ expect(pdfkit.options['--image-dpi']).to eql '300'
+ end
+
+ it "parses hash option values into an array" do
+ pdfkit = PDFKit.new('html', :replace => {'value' => 'something else'} )
+ expect(pdfkit.options['--replace']).to eql ['value', 'something else']
+ end
+
+ it "flattens hash options into the key" do
+ pdfkit = PDFKit.new('html', :cookie => {:cookie_name1 => :cookie_val1, :cookie_name2 => :cookie_val2})
+ expect(pdfkit.options).not_to have_key('--cookie')
+ expect(pdfkit.options[['--cookie', 'cookie_name1']]).to eql 'cookie_val1'
+ expect(pdfkit.options[['--cookie', 'cookie_name2']]).to eql 'cookie_val2'
+ end
+
+ it "parses array option values into a string" do
+ pdfkit = PDFKit.new('html', :replace => ['value', 'something else'] )
+ expect(pdfkit.options['--replace']).to eql ['value', 'something else']
+ end
+
+ it "flattens array options" do
+ pdfkit = PDFKit.new('html', :cookie => [[:cookie_name1, :cookie_val1], [:cookie_name2, :cookie_val2]])
+ expect(pdfkit.options).not_to have_key('--cookie')
+ expect(pdfkit.options[['--cookie', 'cookie_name1']]).to eql 'cookie_val1'
+ expect(pdfkit.options[['--cookie', 'cookie_name2']]).to eql 'cookie_val2'
+ end
+
+ ## default options
+ it "provides default options" do
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
['--margin-top', '--margin-right', '--margin-bottom', '--margin-left'].each do |option|
expect(pdfkit.options).to have_key(option)
end
end
- it "should default to 'UTF-8' encoding" do
+ it "allows overriding default options" do
+ pdfkit = PDFKit.new('html', :page_size => 'A4')
+ expect(pdfkit.options['--page-size']).to eql 'A4'
+ end
+
+ it "defaults to 'UTF-8' encoding" do
pdfkit = PDFKit.new('Captación')
expect(pdfkit.options['--encoding']).to eq('UTF-8')
end
- it "should not have any stylesheedt by default" do
+ it "handles repeatable values which are strings" do
+ pdfkit = PDFKit.new('html', allow: 'http://myapp.com')
+ expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
+ expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
+ end
+
+ it "handles repeatable values which are hashes" do
+ pdfkit = PDFKit.new('html', allow: { 'http://myapp.com' => nil, 'http://google.com' => nil })
+ expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
+ expect(pdfkit.options).to have_key ['--allow', 'http://google.com']
+ expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
+ expect(pdfkit.options[['--allow', 'http://google.com']]).to eql nil
+ end
+
+ it "handles repeatable values which are arrays" do
+ pdfkit = PDFKit.new('html', allow: ['http://myapp.com', 'http://google.com'])
+ expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
+ expect(pdfkit.options).to have_key ['--allow', 'http://google.com']
+ expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
+ expect(pdfkit.options[['--allow', 'http://google.com']]).to eql nil
+ end
+
+ # Stylesheets
+ it "has no stylesheet by default" do
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
expect(pdfkit.stylesheets).to be_empty
end
+
+ it "should not prepend cover with --" do
+ pdfkit = PDFKit.new('html', "cover" => 'http://google.com')
+ expect(pdfkit.options).to have_key('cover')
+ end
+
+ it "should not prepend toc with --" do
+ pdfkit = PDFKit.new('html', 'toc' => '')
+ expect(pdfkit.options).to have_key('toc')
+ end
+
+ it "should handle special params passed as symbols" do
+ pdfkit = PDFKit.new('html', {toc: true})
+ expect(pdfkit.options).to have_key('toc')
+ end
end
- context "command" do
- it "should contstruct the correct command" do
+ describe "#options" do
+ # #options is an attr_reader, but it doesn't really matter. See these two examples:
+ it "cannot be externally overwritten entirely" do
+ pdfkit = PDFKit.new('html', :page_size => 'A4')
+ expect{ pdfkit.options = {} }.to raise_error(NoMethodError)
+ end
+
+ it "has attributes that are externally mutable" do
+ pdfkit = PDFKit.new('html', :page_size => 'A4')
+ pdfkit.options['--page-size'] = 'Letter'
+ expect(pdfkit.options['--page-size']).to eql 'Letter'
+ end
+ end
+
+ describe "#command" do
+ it "should construct the correct command" do
pdfkit = PDFKit.new('html', :page_size => 'Letter', :toc_l1_font_size => 12, :replace => {'foo' => 'bar'})
command = pdfkit.command
expect(command).to include "wkhtmltopdf"
expect(command).to include "--page-size Letter"
expect(command).to include "--toc-l1-font-size 12"
@@ -121,11 +233,11 @@
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
pdfkit = PDFKit.new(File.new(file_path))
expect(pdfkit.command).to match /#{file_path} -$/
end
- it "should specify the path for the ouput if a apth is given" do
+ it "should specify the path for the ouput if a path is given" do
file_path = "/path/to/output.pdf"
pdfkit = PDFKit.new("html")
expect(pdfkit.command(file_path)).to match /#{file_path}$/
end
@@ -221,11 +333,11 @@
it "should not use quiet" do
pdfkit = PDFKit.new('html', quiet: false)
expect(pdfkit.command).not_to include '--quiet'
end
- it "should use quiet option by defautl" do
+ it "should use quiet option by default" do
pdfkit = PDFKit.new('html')
expect(pdfkit.command).to include '--quiet'
end
it "should not use quiet option in verbose mode" do
@@ -239,23 +351,36 @@
PDFKit.configure do |config|
config.verbose = false
end
end
+ it "should not use quiet option in verbose mode when option of quiet is configured" do
+ PDFKit.configure do |config|
+ config.verbose = true
+ config.default_options[:quiet] = true
+ end
+
+ pdfkit = PDFKit.new('html')
+ expect(pdfkit.command).not_to include '--quiet'
+
+ PDFKit.configure do |config|
+ config.verbose = false
+ end
+ end
end
- context "#to_pdf" do
+ describe "#to_pdf" do
it "should not read the contents of the pdf when saving it as a file" do
file_path = "/my/file/path.pdf"
pdfkit = PDFKit.new('html', :page_size => 'Letter')
mock_pdf = double
expect(mock_pdf).to receive(:puts)
expect(mock_pdf).not_to receive(:gets) # do no read the contents when given a file path
expect(mock_pdf).to receive(:close_write)
-
+
expect(IO).to receive(:popen) do |args, mode, &block|
block.call(mock_pdf)
end
expect(::File).to receive(:size).with(file_path).and_return(50)
@@ -321,27 +446,27 @@
end
#NOTICE: This test is failed if use wkhtmltopdf-binary (0.9.9.1)
it "should throw an error if it is unable to connect" do
pdfkit = PDFKit.new("http://google.com/this-should-not-be-found/404.html")
- expect { pdfkit.to_pdf }.to raise_error /exitstatus=2/
+ expect { pdfkit.to_pdf }.to raise_error /exitstatus=1/
end
it "should not throw an error if it is unable to connect", pending: 'this test works for wkhtmltopdf-binary (0.9.9.1)' do
pdfkit = PDFKit.new("http://localhost/this-should-not-be-found/404.html")
pdf = pdfkit.to_pdf
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
end
it "should generate PDF if there are missing assets" do
- pdfkit = PDFKit.new("<html><body><img alt='' src='http://example.com/surely-it-doesnt-exist.gif' /></body></html>", ignore_load_errors: true)
+ pdfkit = PDFKit.new("<html><body><img alt='' src='http://example.com/surely-it-doesnt-exist.gif' /></body></html>")
pdf = pdfkit.to_pdf
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
end
end
- context "#to_file" do
+ describe "#to_file" do
before do
@file_path = File.join(SPEC_ROOT,'fixtures','test.pdf')
File.delete(@file_path) if File.exist?(@file_path)
end
@@ -358,10 +483,10 @@
it "should not truncate data (in Ruby 1.8.6)" do
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
pdfkit = PDFKit.new(File.new(file_path))
pdf_data = pdfkit.to_pdf
- file = pdfkit.to_file(@file_path)
+ pdfkit.to_file(@file_path)
file_data = open(@file_path, 'rb') {|io| io.read }
expect(pdf_data.size).to eq(file_data.size)
end
end