spec/imgkit_spec.rb in imgkit-1.3.6 vs spec/imgkit_spec.rb in imgkit-1.3.7
- old
+ new
@@ -1,45 +1,45 @@
-require 'spec_helper'
+require 'spec_helper'
describe IMGKit do
context "initialization" do
it "should accept HTML as the source" do
imgkit = IMGKit.new('<h1>Oh Hai</h1>')
imgkit.source.should be_html
imgkit.source.to_s.should == '<h1>Oh Hai</h1>'
end
-
+
it "should accept a URL as the source" do
imgkit = IMGKit.new('http://google.com')
imgkit.source.should be_url
imgkit.source.to_s.should == 'http://google.com'
end
-
+
it "should accept a File as the source" do
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
imgkit = IMGKit.new(File.new(file_path))
imgkit.source.should be_file
imgkit.source.to_s.should == file_path
end
-
+
it "should provide no default options" do
imgkit = IMGKit.new('<h1>Oh Hai</h1>')
imgkit.options.should be_empty
end
-
+
=begin
it "should default to 'UTF-8' encoding" do
imgkit = IMGKit.new('Captación')
end
=end
-
+
it "should not have any stylesheet by default" do
imgkit = IMGKit.new('<h1>Oh Hai</h1>')
imgkit.stylesheets.should be_empty
end
end
-
+
context "command" do
it "should contstruct the correct command" do
imgkit = IMGKit.new('html')
imgkit.command[0].should include('wkhtmltoimage')
imgkit.command.should include('-')
@@ -47,11 +47,11 @@
it "should parse the options into a cmd line friedly format" do
imgkit = IMGKit.new('html', :quality => 75)
imgkit.command.should include('--quality')
end
-
+
it "will not include default options it is told to omit" do
imgkit = IMGKit.new('html')
imgkit = IMGKit.new('html', :disable_smart_shrinking => false)
imgkit.command.should_not include('--disable-smart-shrinking')
end
@@ -63,21 +63,21 @@
it "should properly handle multi-part arguments" do
imgkit = IMGKit.new('html', :custom_header => ['User-Agent', 'some user agent'])
imgkit.command[imgkit.command.index('--custom-header') + 1].should == 'User-Agent'
imgkit.command[imgkit.command.index('--custom-header') + 2].should == 'some user agent'
end
-
+
it "read the source from stdin if it is html" do
imgkit = IMGKit.new('html')
imgkit.command[-2..-1].should == ['-', '-']
end
-
+
it "specify the URL to the source if it is a url" do
imgkit = IMGKit.new('http://google.com')
imgkit.command[-2..-1].should == ['http://google.com', '-']
end
-
+
it "should specify the path to the source if it is a file" do
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
imgkit = IMGKit.new(File.new(file_path))
imgkit.command[-2..-1].should == [file_path, '-']
end
@@ -86,23 +86,25 @@
body = %{
<html>
<head>
<meta name="imgkit-page_size" content="Legal"/>
<meta name="imgkit-orientation" content="Landscape"/>
+ <meta name="imgkit-crop-h" content="900"/>
</head>
</html>
}
imgkit = IMGKit.new(body)
imgkit.command[imgkit.command.index('--page-size') + 1].should == 'Legal'
imgkit.command[imgkit.command.index('--orientation') + 1].should == 'Landscape'
+ imgkit.command[imgkit.command.index('--crop-h') + 1].should == '900'
end
end
-
+
context "#to_img(format = nil)" do
- def filetype_of(img)
+ def filetype_of(img)
result = nil
- tmpfile = Tempfile.new('imgkit')
+ tmpfile = Tempfile.new('imgkit')
File.open(tmpfile.path, 'w') { |f| f << img }
result = `file #{tmpfile.path}`
tmpfile.unlink()
result
end
@@ -110,17 +112,17 @@
it "should generate a IMG of the HTML" do
imgkit = IMGKit.new('html')
img = imgkit.to_img
filetype_of(img).should include('JPEG')
end
-
+
it "should generate an Image with a numerical parameter" do
imgkit = IMGKit.new('html', :quality => 50)
img = imgkit.to_img
filetype_of(img).should include('JPEG')
end
-
+
it "should generate an Image with a symbol parameter" do
imgkit = IMGKit.new('html', :username => 'chris')
img = imgkit.to_img
filetype_of(img).should include('JPEG')
end
@@ -146,11 +148,11 @@
css = File.join(SPEC_ROOT,'fixtures','example.css')
imgkit.stylesheets << css
imgkit.to_img
imgkit.source.to_s.should include("<style>#{File.read(css)}</style><html>")
end
-
+
it "should throw an error if the source is not html and stylesheets have been added" do
imgkit = IMGKit.new('http://google.com')
css = File.join(SPEC_ROOT,'fixtures','example.css')
imgkit.stylesheets << css
lambda { imgkit.to_img }.should raise_error(IMGKit::ImproperSourceError)
@@ -231,21 +233,21 @@
context "#to_<unkown_format>" do
it "should raise and UnknownFormatError" do
lambda { IMGKit.new("Hello, world").to_blah }.should raise_error(IMGKit::UnknownFormatError)
end
end
-
+
context "#to_file" do
before do
@file_path = File.join(SPEC_ROOT,'fixtures','test.jpg')
File.delete(@file_path) if File.exist?(@file_path)
end
-
+
after do
File.delete(@file_path) if File.exist?(@file_path)
end
-
+
it "should create a file with the result of :to_img as content" do
imgkit = IMGKit.new('html', :quality => 50)
imgkit.expects(:to_img).returns('CONTENT')
file = imgkit.to_file(@file_path)
file.should be_instance_of(File)
@@ -262,31 +264,31 @@
end
end
it "should raise UnknownFormatError when format is unknown" do
kit = IMGKit.new("html")
- lambda {
- kit.to_file("file.bad_format")
+ lambda {
+ kit.to_file("file.bad_format")
}.should raise_error(IMGKit::UnknownFormatError)
end
it "should not create the file if format is unknown" do
kit = IMGKit.new("html")
- kit.to_file("file.bad_format") rescue nil
+ kit.to_file("file.bad_format") rescue nil
File.exist?("file.bad_format").should be_false
end
end
-
+
context "security" do
before do
@test_path = File.join(SPEC_ROOT,'fixtures','security-oops')
File.delete(@test_path) if File.exist?(@test_path)
end
-
+
after do
File.delete(@test_path) if File.exist?(@test_path)
end
-
+
it "should not allow shell injection in options" do
imgkit = IMGKit.new('html', :password => "blah\"; touch #{@test_path} #")
imgkit.to_img
File.exist?(@test_path).should be_false
end