pdfkit 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pdfkit might be problematic. Click here for more details.

@@ -2,52 +2,51 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe PDFKit do
5
-
6
5
  context "initialization" do
7
6
  it "should accept HTML as the source" do
8
7
  pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
9
- pdfkit.source.should be_html
10
- pdfkit.source.to_s.should == '<h1>Oh Hai</h1>'
8
+ expect(pdfkit.source).to be_html
9
+ expect(pdfkit.source.to_s).to eq('<h1>Oh Hai</h1>')
11
10
  end
12
11
 
13
12
  it "should accept a URL as the source" do
14
13
  pdfkit = PDFKit.new('http://google.com')
15
- pdfkit.source.should be_url
16
- pdfkit.source.to_s.should == 'http://google.com'
14
+ expect(pdfkit.source).to be_url
15
+ expect(pdfkit.source.to_s).to eq('http://google.com')
17
16
  end
18
17
 
19
18
  it "should accept a File as the source" do
20
19
  file_path = File.join(SPEC_ROOT,'fixtures','example.html')
21
20
  pdfkit = PDFKit.new(File.new(file_path))
22
- pdfkit.source.should be_file
23
- pdfkit.source.to_s.should == file_path
21
+ expect(pdfkit.source).to be_file
22
+ expect(pdfkit.source.to_s).to eq(file_path)
24
23
  end
25
24
 
26
25
  it "should parse the options into a cmd line friedly format" do
27
26
  pdfkit = PDFKit.new('html', :page_size => 'Letter')
28
- pdfkit.options.should have_key('--page-size')
27
+ expect(pdfkit.options).to have_key('--page-size')
29
28
  end
30
29
 
31
30
  it "should parse complex options into a cmd line friedly format" do
32
31
  pdfkit = PDFKit.new('html', :replace => {'value' => 'something else'} )
33
- pdfkit.options.should have_key('--replace')
32
+ expect(pdfkit.options).to have_key('--replace')
34
33
  end
35
34
 
36
35
  it "should provide default options" do
37
36
  pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
38
37
  ['--margin-top', '--margin-right', '--margin-bottom', '--margin-left'].each do |option|
39
- pdfkit.options.should have_key(option)
38
+ expect(pdfkit.options).to have_key(option)
40
39
  end
41
40
  end
42
41
 
43
42
  it "should default to 'UTF-8' encoding" do
44
43
  pdfkit = PDFKit.new('Captación')
45
- pdfkit.options['--encoding'].should == 'UTF-8'
44
+ expect(pdfkit.options['--encoding']).to eq('UTF-8')
46
45
  end
47
46
 
48
47
  it "should not have any stylesheedt by default" do
49
48
  pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
50
- pdfkit.stylesheets.should be_empty
49
+ expect(pdfkit.stylesheets).to be_empty
51
50
  end
52
51
  end
53
52
 
@@ -55,30 +54,36 @@ describe PDFKit do
55
54
  it "should contstruct the correct command" do
56
55
  pdfkit = PDFKit.new('html', :page_size => 'Letter', :toc_l1_font_size => 12, :replace => {'foo' => 'bar'})
57
56
  command = pdfkit.command
58
- command.should include "wkhtmltopdf"
59
- command.should include "--page-size Letter"
60
- command.should include "--toc-l1-font-size 12"
61
- command.should include "--replace foo bar"
57
+ expect(command).to include "wkhtmltopdf"
58
+ expect(command).to include "--page-size Letter"
59
+ expect(command).to include "--toc-l1-font-size 12"
60
+ expect(command).to include "--replace foo bar"
62
61
  end
63
62
 
64
63
  it "should setup one cookie only" do
65
64
  pdfkit = PDFKit.new('html', cookie: {cookie_name: :cookie_value})
66
65
  command = pdfkit.command
67
- command.should include "--cookie cookie_name cookie_value"
66
+ expect(command).to include "--cookie cookie_name cookie_value"
67
+ end
68
+
69
+ it "should not break Windows paths" do
70
+ pdfkit = PDFKit.new('html')
71
+ allow(PDFKit.configuration).to receive(:wkhtmltopdf).and_return 'c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe'
72
+ expect(pdfkit.command).not_to include('Program\ Files')
68
73
  end
69
74
 
70
75
  it "should setup multiple cookies when passed a hash" do
71
76
  pdfkit = PDFKit.new('html', :cookie => {:cookie_name1 => :cookie_val1, :cookie_name2 => :cookie_val2})
72
77
  command = pdfkit.command
73
- command.should include "--cookie cookie_name1 cookie_val1"
74
- command.should include "--cookie cookie_name2 cookie_val2"
78
+ expect(command).to include "--cookie cookie_name1 cookie_val1"
79
+ expect(command).to include "--cookie cookie_name2 cookie_val2"
75
80
  end
76
81
 
77
82
  it "should setup multiple cookies when passed an array of tuples" do
78
83
  pdfkit = PDFKit.new('html', :cookie => [[:cookie_name1, :cookie_val1], [:cookie_name2, :cookie_val2]])
79
84
  command = pdfkit.command
80
- command.should include "--cookie cookie_name1 cookie_val1"
81
- command.should include "--cookie cookie_name2 cookie_val2"
85
+ expect(command).to include "--cookie cookie_name1 cookie_val1"
86
+ expect(command).to include "--cookie cookie_name2 cookie_val2"
82
87
  end
83
88
 
84
89
  it "will not include default options it is told to omit" do
@@ -87,41 +92,41 @@ describe PDFKit do
87
92
  end
88
93
 
89
94
  pdfkit = PDFKit.new('html')
90
- pdfkit.command.should include('--disable-smart-shrinking')
95
+ expect(pdfkit.command).to include('--disable-smart-shrinking')
91
96
  pdfkit = PDFKit.new('html', :disable_smart_shrinking => false)
92
- pdfkit.command.should_not include('--disable-smart-shrinking')
97
+ expect(pdfkit.command).not_to include('--disable-smart-shrinking')
93
98
  end
94
99
 
95
100
  it "should encapsulate string arguments in quotes" do
96
101
  pdfkit = PDFKit.new('html', :header_center => "foo [page]")
97
- pdfkit.command.should include "--header-center foo\\ \\[page\\]"
102
+ expect(pdfkit.command).to include "--header-center foo\\ \\[page\\]"
98
103
  end
99
104
 
100
105
  it "should sanitize string arguments" do
101
106
  pdfkit = PDFKit.new('html', :header_center => "$(ls)")
102
- pdfkit.command.should include "--header-center \\$\\(ls\\)"
107
+ expect(pdfkit.command).to include "--header-center \\$\\(ls\\)"
103
108
  end
104
109
 
105
110
  it "read the source from stdin if it is html" do
106
111
  pdfkit = PDFKit.new('html')
107
- pdfkit.command.should match /- -$/
112
+ expect(pdfkit.command).to match /- -$/
108
113
  end
109
114
 
110
115
  it "specify the URL to the source if it is a url" do
111
116
  pdfkit = PDFKit.new('http://google.com')
112
- pdfkit.command.should match /http:\/\/google.com -$/
117
+ expect(pdfkit.command).to match /http:\/\/google.com -$/
113
118
  end
114
119
 
115
120
  it "should specify the path to the source if it is a file" do
116
121
  file_path = File.join(SPEC_ROOT,'fixtures','example.html')
117
122
  pdfkit = PDFKit.new(File.new(file_path))
118
- pdfkit.command.should match /#{file_path} -$/
123
+ expect(pdfkit.command).to match /#{file_path} -$/
119
124
  end
120
125
 
121
126
  it "should specify the path for the ouput if a apth is given" do
122
127
  file_path = "/path/to/output.pdf"
123
128
  pdfkit = PDFKit.new("html")
124
- pdfkit.command(file_path).should match /#{file_path}$/
129
+ expect(pdfkit.command(file_path)).to match /#{file_path}$/
125
130
  end
126
131
 
127
132
  it "should detect special pdfkit meta tags" do
@@ -135,8 +140,8 @@ describe PDFKit do
135
140
  }
136
141
  pdfkit = PDFKit.new(body)
137
142
  command = pdfkit.command
138
- command.should include "--page-size Legal"
139
- command.should include "--orientation Landscape"
143
+ expect(command).to include "--page-size Legal"
144
+ expect(command).to include "--orientation Landscape"
140
145
  end
141
146
 
142
147
  it "should detect cookies meta tag" do
@@ -150,7 +155,7 @@ describe PDFKit do
150
155
  }
151
156
  pdfkit = PDFKit.new(body)
152
157
  command = pdfkit.command
153
- command.should include "--cookie rails_session rails_session_value --cookie cookie_variable cookie_variable_value"
158
+ expect(command).to include "--cookie rails_session rails_session_value --cookie cookie_variable cookie_variable_value"
154
159
  end
155
160
 
156
161
  it "should detect disable_smart_shrinking meta tag" do
@@ -163,8 +168,8 @@ describe PDFKit do
163
168
  }
164
169
  pdfkit = PDFKit.new(body)
165
170
  command = pdfkit.command
166
- command.should include "--disable-smart-shrinking"
167
- command.should_not include "--disable-smart-shrinking true"
171
+ expect(command).to include "--disable-smart-shrinking"
172
+ expect(command).not_to include "--disable-smart-shrinking true"
168
173
  end
169
174
 
170
175
  it "should detect names with hyphens instead of underscores" do
@@ -178,7 +183,7 @@ describe PDFKit do
178
183
  </html>
179
184
  }
180
185
  pdfkit = PDFKit.new(body)
181
- pdfkit.command.should_not include 'name\='
186
+ expect(pdfkit.command).not_to include 'name\='
182
187
  end
183
188
 
184
189
  it "should detect special pdfkit meta tags despite bad markup" do
@@ -193,8 +198,8 @@ describe PDFKit do
193
198
  }
194
199
  pdfkit = PDFKit.new(body)
195
200
  command = pdfkit.command
196
- command.should include "--page-size Legal"
197
- command.should include "--orientation Landscape"
201
+ expect(command).to include "--page-size Legal"
202
+ expect(command).to include "--orientation Landscape"
198
203
  end
199
204
 
200
205
  it "should skip non-pdfkit meta tags" do
@@ -209,18 +214,18 @@ describe PDFKit do
209
214
  }
210
215
  pdfkit = PDFKit.new(body)
211
216
  command = pdfkit.command
212
- command.should_not include "--page-size Legal"
213
- command.should include "--orientation Landscape"
217
+ expect(command).not_to include "--page-size Legal"
218
+ expect(command).to include "--orientation Landscape"
214
219
  end
215
220
 
216
221
  it "should not use quiet" do
217
222
  pdfkit = PDFKit.new('html', quiet: false)
218
- pdfkit.command.should_not include '--quiet'
223
+ expect(pdfkit.command).not_to include '--quiet'
219
224
  end
220
225
 
221
226
  it "should use quiet option by defautl" do
222
227
  pdfkit = PDFKit.new('html')
223
- pdfkit.command.should include '--quiet'
228
+ expect(pdfkit.command).to include '--quiet'
224
229
  end
225
230
 
226
231
  it "should not use quiet option in verbose mode" do
@@ -229,7 +234,7 @@ describe PDFKit do
229
234
  end
230
235
 
231
236
  pdfkit = PDFKit.new('html')
232
- pdfkit.command.should_not include '--quiet'
237
+ expect(pdfkit.command).not_to include '--quiet'
233
238
 
234
239
  PDFKit.configure do |config|
235
240
  config.verbose = false
@@ -239,22 +244,41 @@ describe PDFKit do
239
244
  end
240
245
 
241
246
  context "#to_pdf" do
247
+ it "should not read the contents of the pdf when saving it as a file" do
248
+ file_path = "/my/file/path.pdf"
249
+ pdfkit = PDFKit.new('html', :page_size => 'Letter')
250
+
251
+ mock_pdf = double
252
+ expect(mock_pdf).to receive(:puts)
253
+ expect(mock_pdf).not_to receive(:gets) # do no read the contents when given a file path
254
+ expect(mock_pdf).to receive(:close_write)
255
+
256
+
257
+ expect(IO).to receive(:popen) do |args, mode, &block|
258
+ block.call(mock_pdf)
259
+ end
260
+
261
+ expect(::File).to receive(:size).with(file_path).and_return(50)
262
+
263
+ pdfkit.to_pdf(file_path)
264
+ end
265
+
242
266
  it "should generate a PDF of the HTML" do
243
267
  pdfkit = PDFKit.new('html', :page_size => 'Letter')
244
268
  pdf = pdfkit.to_pdf
245
- pdf[0...4].should == "%PDF" # PDF Signature at beginning of file
269
+ expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
246
270
  end
247
271
 
248
272
  it "should generate a PDF with a numerical parameter" do
249
273
  pdfkit = PDFKit.new('html', :header_spacing => 1)
250
274
  pdf = pdfkit.to_pdf
251
- pdf[0...4].should == "%PDF" # PDF Signature at beginning of file
275
+ expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
252
276
  end
253
277
 
254
278
  it "should generate a PDF with a symbol parameter" do
255
279
  pdfkit = PDFKit.new('html', :page_size => :Letter)
256
280
  pdf = pdfkit.to_pdf
257
- pdf[0...4].should == "%PDF" # PDF Signature at beginning of file
281
+ expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
258
282
  end
259
283
 
260
284
  it "should have the stylesheet added to the head if it has one" do
@@ -262,7 +286,7 @@ describe PDFKit do
262
286
  css = File.join(SPEC_ROOT,'fixtures','example.css')
263
287
  pdfkit.stylesheets << css
264
288
  pdfkit.to_pdf
265
- pdfkit.source.to_s.should include("<style>#{File.read(css)}</style>")
289
+ expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
266
290
  end
267
291
 
268
292
  it "should prepend style tags if the HTML doesn't have a head tag" do
@@ -270,14 +294,14 @@ describe PDFKit do
270
294
  css = File.join(SPEC_ROOT,'fixtures','example.css')
271
295
  pdfkit.stylesheets << css
272
296
  pdfkit.to_pdf
273
- pdfkit.source.to_s.should include("<style>#{File.read(css)}</style><html>")
297
+ expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style><html>")
274
298
  end
275
299
 
276
300
  it "should throw an error if the source is not html and stylesheets have been added" do
277
301
  pdfkit = PDFKit.new('http://google.com')
278
302
  css = File.join(SPEC_ROOT,'fixtures','example.css')
279
303
  pdfkit.stylesheets << css
280
- lambda { pdfkit.to_pdf }.should raise_error(PDFKit::ImproperSourceError)
304
+ expect { pdfkit.to_pdf }.to raise_error(PDFKit::ImproperSourceError)
281
305
  end
282
306
 
283
307
  it "should be able to deal with ActiveSupport::SafeBuffer" do
@@ -285,7 +309,7 @@ describe PDFKit do
285
309
  css = File.join(SPEC_ROOT,'fixtures','example.css')
286
310
  pdfkit.stylesheets << css
287
311
  pdfkit.to_pdf
288
- pdfkit.source.to_s.should include("<style>#{File.read(css)}</style></head>")
312
+ expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
289
313
  end
290
314
 
291
315
  it "should escape \\X in stylesheets" do
@@ -293,25 +317,25 @@ describe PDFKit do
293
317
  css = File.join(SPEC_ROOT,'fixtures','example_with_hex_symbol.css')
294
318
  pdfkit.stylesheets << css
295
319
  pdfkit.to_pdf
296
- pdfkit.source.to_s.should include("<style>#{File.read(css)}</style></head>")
320
+ expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
297
321
  end
298
322
 
299
323
  #NOTICE: This test is failed if use wkhtmltopdf-binary (0.9.9.1)
300
324
  it "should throw an error if it is unable to connect" do
301
325
  pdfkit = PDFKit.new("http://google.com/this-should-not-be-found/404.html")
302
- lambda { pdfkit.to_pdf }.should raise_error /exitstatus=2/
326
+ expect { pdfkit.to_pdf }.to raise_error /exitstatus=2/
303
327
  end
304
328
 
305
329
  it "should not throw an error if it is unable to connect", pending: 'this test works for wkhtmltopdf-binary (0.9.9.1)' do
306
330
  pdfkit = PDFKit.new("http://localhost/this-should-not-be-found/404.html")
307
331
  pdf = pdfkit.to_pdf
308
- pdf[0...4].should == "%PDF" # PDF Signature at the beginning
332
+ expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
309
333
  end
310
334
 
311
335
  it "should generate PDF if there are missing assets" do
312
336
  pdfkit = PDFKit.new("<html><body><img alt='' src='http://example.com/surely-it-doesnt-exist.gif' /></body></html>", ignore_load_errors: true)
313
337
  pdf = pdfkit.to_pdf
314
- pdf[0...4].should == "%PDF" # PDF Signature at the beginning
338
+ expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
315
339
  end
316
340
  end
317
341
 
@@ -328,8 +352,8 @@ describe PDFKit do
328
352
  it "should create a file with the PDF as content" do
329
353
  pdfkit = PDFKit.new('html', :page_size => 'Letter')
330
354
  file = pdfkit.to_file(@file_path)
331
- file.should be_instance_of(File)
332
- File.read(file.path)[0...4].should == "%PDF" # PDF Signature at beginning of file
355
+ expect(file).to be_instance_of(File)
356
+ expect(File.read(file.path)[0...4]).to eq("%PDF") # PDF Signature at beginning of file
333
357
  end
334
358
 
335
359
  it "should not truncate data (in Ruby 1.8.6)" do
@@ -338,7 +362,7 @@ describe PDFKit do
338
362
  pdf_data = pdfkit.to_pdf
339
363
  file = pdfkit.to_file(@file_path)
340
364
  file_data = open(@file_path, 'rb') {|io| io.read }
341
- pdf_data.size.should == file_data.size
365
+ expect(pdf_data.size).to eq(file_data.size)
342
366
  end
343
367
  end
344
368
 
@@ -355,7 +379,7 @@ describe PDFKit do
355
379
  it "should not allow shell injection in options" do
356
380
  pdfkit = PDFKit.new('html', :header_center => "a title\"; touch #{@test_path} #")
357
381
  pdfkit.to_pdf
358
- File.exist?(@test_path).should be_false
382
+ expect(File.exist?(@test_path)).to eq(false)
359
383
  end
360
384
  end
361
385
  end
@@ -5,74 +5,73 @@ describe PDFKit::Source do
5
5
  describe "#url?" do
6
6
  it "should return true if passed a url like string" do
7
7
  source = PDFKit::Source.new('http://google.com')
8
- source.should be_url
8
+ expect(source).to be_url
9
9
  end
10
10
 
11
11
  it "should return false if passed a file" do
12
12
  source = PDFKit::Source.new(File.new(__FILE__))
13
- source.should_not be_url
13
+ expect(source).not_to be_url
14
14
  end
15
15
 
16
16
  it "should return false if passed HTML" do
17
17
  source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
18
- source.should_not be_url
18
+ expect(source).not_to be_url
19
19
  end
20
20
 
21
21
  it "should return false if passed HTML with embedded urls at the beginning of a line" do
22
22
  source = PDFKit::Source.new("<blink>Oh Hai!</blink>\nhttp://www.google.com")
23
- source.should_not be_url
23
+ expect(source).not_to be_url
24
24
  end
25
25
  end
26
26
 
27
27
  describe "#file?" do
28
28
  it "should return true if passed a file" do
29
- source = PDFKit::Source.new(File.new(__FILE__))
30
- source.should be_file
29
+ source = PDFKit::Source.new(::File.new(__FILE__))
30
+ expect(source).to be_file
31
31
  end
32
32
 
33
33
  it "should return false if passed a url like string" do
34
34
  source = PDFKit::Source.new('http://google.com')
35
- source.should_not be_file
35
+ expect(source).not_to be_file
36
36
  end
37
37
 
38
38
  it "should return false if passed HTML" do
39
39
  source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
40
- source.should_not be_file
40
+ expect(source).not_to be_file
41
41
  end
42
42
  end
43
43
 
44
44
  describe "#html?" do
45
45
  it "should return true if passed HTML" do
46
46
  source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
47
- source.should be_html
47
+ expect(source).to be_html
48
48
  end
49
49
 
50
50
  it "should return false if passed a file" do
51
- source = PDFKit::Source.new(File.new(__FILE__))
52
- source.should_not be_html
51
+ source = PDFKit::Source.new(::File.new(__FILE__))
52
+ expect(source).not_to be_html
53
53
  end
54
54
 
55
55
  it "should return false if passed a url like string" do
56
56
  source = PDFKit::Source.new('http://google.com')
57
- source.should_not be_html
57
+ expect(source).not_to be_html
58
58
  end
59
59
  end
60
60
 
61
61
  describe "#to_s" do
62
62
  it "should return the HTML if passed HTML" do
63
63
  source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
64
- source.to_s.should == '<blink>Oh Hai!</blink>'
64
+ expect(source.to_s).to eq('<blink>Oh Hai!</blink>')
65
65
  end
66
66
 
67
67
  it "should return a path if passed a file" do
68
- source = PDFKit::Source.new(File.new(__FILE__))
69
- source.to_s.should == __FILE__
68
+ source = PDFKit::Source.new(::File.new(__FILE__))
69
+ expect(source.to_s).to eq(__FILE__)
70
70
  end
71
71
 
72
72
  it "should return the url if passed a url like string" do
73
73
  source = PDFKit::Source.new('http://google.com')
74
- source.to_s.should == 'http://google.com'
74
+ expect(source.to_s).to eq('http://google.com')
75
75
  end
76
76
  end
77
-
78
77
  end