pdfkit 0.8.0 → 0.8.7.1
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.
- checksums.yaml +5 -5
- data/.github/workflows/release-drafter.yml +19 -0
- data/.github/workflows/stale.yml +19 -0
- data/.github/workflows/test.yml +59 -0
- data/.ruby-version +1 -1
- data/.travis.yml +42 -8
- data/CHANGELOG.md +71 -1
- data/Gemfile +1 -1
- data/README.md +36 -10
- data/lib/pdfkit/configuration.rb +38 -3
- data/lib/pdfkit/html_preprocessor.rb +25 -0
- data/lib/pdfkit/middleware.rb +53 -43
- data/lib/pdfkit/os.rb +21 -0
- data/lib/pdfkit/pdfkit.rb +55 -96
- data/lib/pdfkit/source.rb +36 -7
- data/lib/pdfkit/version.rb +3 -1
- data/lib/pdfkit/wkhtmltopdf.rb +82 -0
- data/lib/pdfkit.rb +6 -0
- data/pdfkit.gemspec +9 -7
- data/spec/configuration_spec.rb +85 -12
- data/spec/html_preprocessor_spec.rb +71 -0
- data/spec/middleware_spec.rb +240 -107
- data/spec/os_spec.rb +67 -0
- data/spec/pdfkit_spec.rb +132 -52
- data/spec/source_spec.rb +67 -13
- data/spec/spec_helper.rb +5 -1
- metadata +45 -48
data/spec/pdfkit_spec.rb
CHANGED
@@ -1,28 +1,37 @@
|
|
1
1
|
#encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require 'spec_helper'
|
3
5
|
|
4
6
|
describe PDFKit do
|
5
7
|
describe "initialization" do
|
6
8
|
# Source
|
7
|
-
it "
|
9
|
+
it "accepts HTML as the source" do
|
8
10
|
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
|
9
11
|
expect(pdfkit.source).to be_html
|
10
12
|
expect(pdfkit.source.to_s).to eq('<h1>Oh Hai</h1>')
|
11
13
|
end
|
12
14
|
|
13
|
-
it "
|
15
|
+
it "accepts a URL as the source" do
|
14
16
|
pdfkit = PDFKit.new('http://google.com')
|
15
17
|
expect(pdfkit.source).to be_url
|
16
18
|
expect(pdfkit.source.to_s).to eq('http://google.com')
|
17
19
|
end
|
18
20
|
|
19
|
-
it "
|
21
|
+
it "accepts a File as the source" do
|
20
22
|
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
21
23
|
pdfkit = PDFKit.new(File.new(file_path))
|
22
24
|
expect(pdfkit.source).to be_file
|
23
25
|
expect(pdfkit.source.to_s).to eq(file_path)
|
24
26
|
end
|
25
27
|
|
28
|
+
it "accepts a Tempfile as the source" do
|
29
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
30
|
+
pdfkit = PDFKit.new(Tempfile.new(file_path))
|
31
|
+
expect(pdfkit.source).to be_file
|
32
|
+
expect(pdfkit.source.to_s).to match(/^#{Dir.tmpdir}/)
|
33
|
+
end
|
34
|
+
|
26
35
|
# Options
|
27
36
|
## options keys
|
28
37
|
it "drops options without values" do
|
@@ -126,26 +135,26 @@ describe PDFKit do
|
|
126
135
|
expect(pdfkit.options[['--allow', 'http://google.com']]).to eql nil
|
127
136
|
end
|
128
137
|
|
129
|
-
|
130
|
-
it "has no stylesheet by default" do
|
131
|
-
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
|
132
|
-
expect(pdfkit.stylesheets).to be_empty
|
133
|
-
end
|
134
|
-
|
135
|
-
it "should not prepend cover with --" do
|
138
|
+
it "does not prepend cover option with --" do
|
136
139
|
pdfkit = PDFKit.new('html', "cover" => 'http://google.com')
|
137
140
|
expect(pdfkit.options).to have_key('cover')
|
138
141
|
end
|
139
142
|
|
140
|
-
it "
|
143
|
+
it "does not prepend the toc option with --" do
|
141
144
|
pdfkit = PDFKit.new('html', 'toc' => '')
|
142
145
|
expect(pdfkit.options).to have_key('toc')
|
143
146
|
end
|
144
147
|
|
145
|
-
it "
|
148
|
+
it "handles cover and toc params passed as symbols" do
|
146
149
|
pdfkit = PDFKit.new('html', {toc: true})
|
147
150
|
expect(pdfkit.options).to have_key('toc')
|
148
151
|
end
|
152
|
+
|
153
|
+
# Stylesheets
|
154
|
+
it "has no stylesheet by default" do
|
155
|
+
pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
|
156
|
+
expect(pdfkit.stylesheets).to be_empty
|
157
|
+
end
|
149
158
|
end
|
150
159
|
|
151
160
|
describe "#options" do
|
@@ -163,7 +172,7 @@ describe PDFKit do
|
|
163
172
|
end
|
164
173
|
|
165
174
|
describe "#command" do
|
166
|
-
it "
|
175
|
+
it "constructs the correct command" do
|
167
176
|
pdfkit = PDFKit.new('html', :page_size => 'Letter', :toc_l1_font_size => 12, :replace => {'foo' => 'bar'})
|
168
177
|
command = pdfkit.command
|
169
178
|
expect(command).to include "wkhtmltopdf"
|
@@ -172,26 +181,37 @@ describe PDFKit do
|
|
172
181
|
expect(command).to include "--replace foo bar"
|
173
182
|
end
|
174
183
|
|
175
|
-
it "
|
184
|
+
it "sets up one cookie when hash has only one cookie" do
|
176
185
|
pdfkit = PDFKit.new('html', cookie: {cookie_name: :cookie_value})
|
177
186
|
command = pdfkit.command
|
178
187
|
expect(command).to include "--cookie cookie_name cookie_value"
|
179
188
|
end
|
180
189
|
|
181
|
-
it "
|
190
|
+
it "does not break Windows paths" do
|
182
191
|
pdfkit = PDFKit.new('html')
|
183
192
|
allow(PDFKit.configuration).to receive(:wkhtmltopdf).and_return 'c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe'
|
184
193
|
expect(pdfkit.command).not_to include('Program\ Files')
|
185
194
|
end
|
186
195
|
|
187
|
-
it "
|
196
|
+
it "does not shell escape source URLs" do
|
197
|
+
pdfkit = PDFKit.new('https://www.google.com/search?q=pdfkit')
|
198
|
+
expect(pdfkit.command).to include "https://www.google.com/search?q=pdfkit"
|
199
|
+
end
|
200
|
+
|
201
|
+
it "formats source for the command" do
|
202
|
+
pdfkit = PDFKit.new('https://www.google.com/search?q=pdfkit')
|
203
|
+
expect(pdfkit.source).to receive(:to_input_for_command)
|
204
|
+
pdfkit.command
|
205
|
+
end
|
206
|
+
|
207
|
+
it "sets up multiple cookies when passed multiple cookies" do
|
188
208
|
pdfkit = PDFKit.new('html', :cookie => {:cookie_name1 => :cookie_val1, :cookie_name2 => :cookie_val2})
|
189
209
|
command = pdfkit.command
|
190
210
|
expect(command).to include "--cookie cookie_name1 cookie_val1"
|
191
211
|
expect(command).to include "--cookie cookie_name2 cookie_val2"
|
192
212
|
end
|
193
213
|
|
194
|
-
it "
|
214
|
+
it "sets up multiple cookies when passed an array of tuples" do
|
195
215
|
pdfkit = PDFKit.new('html', :cookie => [[:cookie_name1, :cookie_val1], [:cookie_name2, :cookie_val2]])
|
196
216
|
command = pdfkit.command
|
197
217
|
expect(command).to include "--cookie cookie_name1 cookie_val1"
|
@@ -209,39 +229,51 @@ describe PDFKit do
|
|
209
229
|
expect(pdfkit.command).not_to include('--disable-smart-shrinking')
|
210
230
|
end
|
211
231
|
|
212
|
-
it "
|
232
|
+
it "encapsulates string arguments in quotes" do
|
213
233
|
pdfkit = PDFKit.new('html', :header_center => "foo [page]")
|
214
234
|
expect(pdfkit.command).to include "--header-center foo\\ \\[page\\]"
|
215
235
|
end
|
216
236
|
|
217
|
-
it "
|
237
|
+
it "sanitizes string arguments" do
|
218
238
|
pdfkit = PDFKit.new('html', :header_center => "$(ls)")
|
219
239
|
expect(pdfkit.command).to include "--header-center \\$\\(ls\\)"
|
220
240
|
end
|
221
241
|
|
222
242
|
it "read the source from stdin if it is html" do
|
223
243
|
pdfkit = PDFKit.new('html')
|
224
|
-
expect(pdfkit.command).to match
|
244
|
+
expect(pdfkit.command).to match(/- -$/)
|
225
245
|
end
|
226
246
|
|
227
|
-
it "
|
247
|
+
it "specifies the URL to the source if it is a url" do
|
228
248
|
pdfkit = PDFKit.new('http://google.com')
|
229
|
-
expect(pdfkit.command).to match
|
249
|
+
expect(pdfkit.command).to match(/"http:\/\/google.com" -$/)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "does not break Windows paths" do
|
253
|
+
pdfkit = PDFKit.new('html')
|
254
|
+
allow(PDFKit.configuration).to receive(:wkhtmltopdf).and_return 'c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe'
|
255
|
+
expect(pdfkit.command).not_to include('Program\ Files')
|
230
256
|
end
|
231
257
|
|
232
|
-
it "
|
258
|
+
it "specifies the path to the source if it is a file" do
|
233
259
|
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
234
260
|
pdfkit = PDFKit.new(File.new(file_path))
|
235
|
-
expect(pdfkit.command).to match
|
261
|
+
expect(pdfkit.command).to match(/#{file_path} -$/)
|
262
|
+
end
|
263
|
+
|
264
|
+
it "specifies the path to the source if it is a tempfile" do
|
265
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
266
|
+
pdfkit = PDFKit.new(Tempfile.new(file_path))
|
267
|
+
expect(pdfkit.command).to match(/#{Dir.tmpdir}\S+ -$/)
|
236
268
|
end
|
237
269
|
|
238
|
-
it "
|
270
|
+
it "specifies the path for the ouput if a path is given" do
|
239
271
|
file_path = "/path/to/output.pdf"
|
240
272
|
pdfkit = PDFKit.new("html")
|
241
|
-
expect(pdfkit.command(file_path)).to match
|
273
|
+
expect(pdfkit.command(file_path)).to match(/#{file_path}$/)
|
242
274
|
end
|
243
275
|
|
244
|
-
it "
|
276
|
+
it "detects special pdfkit meta tags" do
|
245
277
|
body = %{
|
246
278
|
<html>
|
247
279
|
<head>
|
@@ -256,7 +288,7 @@ describe PDFKit do
|
|
256
288
|
expect(command).to include "--orientation Landscape"
|
257
289
|
end
|
258
290
|
|
259
|
-
it "
|
291
|
+
it "detects cookies meta tag" do
|
260
292
|
body = %{
|
261
293
|
<html>
|
262
294
|
<head>
|
@@ -270,7 +302,7 @@ describe PDFKit do
|
|
270
302
|
expect(command).to include "--cookie rails_session rails_session_value --cookie cookie_variable cookie_variable_value"
|
271
303
|
end
|
272
304
|
|
273
|
-
it "
|
305
|
+
it "detects disable_smart_shrinking meta tag" do
|
274
306
|
body = %{
|
275
307
|
<html>
|
276
308
|
<head>
|
@@ -284,7 +316,7 @@ describe PDFKit do
|
|
284
316
|
expect(command).not_to include "--disable-smart-shrinking true"
|
285
317
|
end
|
286
318
|
|
287
|
-
it "
|
319
|
+
it "detects names with hyphens instead of underscores" do
|
288
320
|
body = %{
|
289
321
|
<html>
|
290
322
|
<head>
|
@@ -298,7 +330,7 @@ describe PDFKit do
|
|
298
330
|
expect(pdfkit.command).not_to include 'name\='
|
299
331
|
end
|
300
332
|
|
301
|
-
it "
|
333
|
+
it "detects special pdfkit meta tags despite bad markup" do
|
302
334
|
body = %{
|
303
335
|
<html>
|
304
336
|
<head>
|
@@ -314,7 +346,7 @@ describe PDFKit do
|
|
314
346
|
expect(command).to include "--orientation Landscape"
|
315
347
|
end
|
316
348
|
|
317
|
-
it "
|
349
|
+
it "skips non-pdfkit meta tags" do
|
318
350
|
body = %{
|
319
351
|
<html>
|
320
352
|
<head>
|
@@ -330,17 +362,17 @@ describe PDFKit do
|
|
330
362
|
expect(command).to include "--orientation Landscape"
|
331
363
|
end
|
332
364
|
|
333
|
-
it "
|
365
|
+
it "does not use quiet when told to" do
|
334
366
|
pdfkit = PDFKit.new('html', quiet: false)
|
335
367
|
expect(pdfkit.command).not_to include '--quiet'
|
336
368
|
end
|
337
369
|
|
338
|
-
it "
|
370
|
+
it "uses quiet option by default" do
|
339
371
|
pdfkit = PDFKit.new('html')
|
340
372
|
expect(pdfkit.command).to include '--quiet'
|
341
373
|
end
|
342
374
|
|
343
|
-
it "
|
375
|
+
it "does not use quiet option in verbose mode" do
|
344
376
|
PDFKit.configure do |config|
|
345
377
|
config.verbose = true
|
346
378
|
end
|
@@ -353,7 +385,7 @@ describe PDFKit do
|
|
353
385
|
end
|
354
386
|
end
|
355
387
|
|
356
|
-
it "
|
388
|
+
it "does not use quiet option in verbose mode when option of quiet is configured" do
|
357
389
|
PDFKit.configure do |config|
|
358
390
|
config.verbose = true
|
359
391
|
config.default_options[:quiet] = true
|
@@ -366,10 +398,44 @@ describe PDFKit do
|
|
366
398
|
config.verbose = false
|
367
399
|
end
|
368
400
|
end
|
401
|
+
|
402
|
+
it "does not use xvfb-run wrapper by default" do
|
403
|
+
pdfkit = PDFKit.new('html')
|
404
|
+
expect(pdfkit.command).not_to include 'xvfb-run'
|
405
|
+
end
|
406
|
+
|
407
|
+
it "uses xvfb-run wrapper when option of using xvfb is configured" do
|
408
|
+
PDFKit.configure do |config|
|
409
|
+
config.use_xvfb = true
|
410
|
+
end
|
411
|
+
|
412
|
+
pdfkit = PDFKit.new('html')
|
413
|
+
expect(pdfkit.command).to include 'xvfb-run'
|
414
|
+
|
415
|
+
PDFKit.configure do |config|
|
416
|
+
config.use_xvfb = false
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
context "on windows" do
|
421
|
+
before do
|
422
|
+
allow(PDFKit::OS).to receive(:host_is_windows?).and_return(true)
|
423
|
+
end
|
424
|
+
|
425
|
+
it "escapes special windows characters" do
|
426
|
+
pdf = PDFKit.new('html', :title => 'hello(world)')
|
427
|
+
expect(pdf.command).to include 'hello^(world^)'
|
428
|
+
end
|
429
|
+
|
430
|
+
it "quotes spaces in options" do
|
431
|
+
pdf = PDFKit.new('html', :title => 'hello world')
|
432
|
+
expect(pdf.command).to include "--title 'hello world'"
|
433
|
+
end
|
434
|
+
end
|
369
435
|
end
|
370
436
|
|
371
437
|
describe "#to_pdf" do
|
372
|
-
it "
|
438
|
+
it "does not read the contents of the pdf when saving it as a file" do
|
373
439
|
file_path = "/my/file/path.pdf"
|
374
440
|
pdfkit = PDFKit.new('html', :page_size => 'Letter')
|
375
441
|
|
@@ -388,25 +454,25 @@ describe PDFKit do
|
|
388
454
|
pdfkit.to_pdf(file_path)
|
389
455
|
end
|
390
456
|
|
391
|
-
it "
|
457
|
+
it "generates a PDF of the HTML" do
|
392
458
|
pdfkit = PDFKit.new('html', :page_size => 'Letter')
|
393
459
|
pdf = pdfkit.to_pdf
|
394
460
|
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
395
461
|
end
|
396
462
|
|
397
|
-
it "
|
463
|
+
it "generates a PDF with a numerical parameter" do
|
398
464
|
pdfkit = PDFKit.new('html', :header_spacing => 1)
|
399
465
|
pdf = pdfkit.to_pdf
|
400
466
|
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
401
467
|
end
|
402
468
|
|
403
|
-
it "
|
469
|
+
it "generates a PDF with a symbol parameter" do
|
404
470
|
pdfkit = PDFKit.new('html', :page_size => :Letter)
|
405
471
|
pdf = pdfkit.to_pdf
|
406
472
|
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
407
473
|
end
|
408
474
|
|
409
|
-
it "
|
475
|
+
it "adds the stylesheet to the head tag if it has a head tag" do
|
410
476
|
pdfkit = PDFKit.new("<html><head></head><body>Hai!</body></html>")
|
411
477
|
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
412
478
|
pdfkit.stylesheets << css
|
@@ -414,7 +480,7 @@ describe PDFKit do
|
|
414
480
|
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
|
415
481
|
end
|
416
482
|
|
417
|
-
it "
|
483
|
+
it "prepends style tags if the HTML doesn't have a head tag" do
|
418
484
|
pdfkit = PDFKit.new("<html><body>Hai!</body></html>")
|
419
485
|
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
420
486
|
pdfkit.stylesheets << css
|
@@ -422,14 +488,14 @@ describe PDFKit do
|
|
422
488
|
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style><html>")
|
423
489
|
end
|
424
490
|
|
425
|
-
it "
|
491
|
+
it "throws an error if the source is not html and stylesheets have been added" do
|
426
492
|
pdfkit = PDFKit.new('http://google.com')
|
427
493
|
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
428
494
|
pdfkit.stylesheets << css
|
429
495
|
expect { pdfkit.to_pdf }.to raise_error(PDFKit::ImproperSourceError)
|
430
496
|
end
|
431
497
|
|
432
|
-
it "
|
498
|
+
it "can deal with ActiveSupport::SafeBuffer" do
|
433
499
|
pdfkit = PDFKit.new(ActiveSupport::SafeBuffer.new "<html><head></head><body>Hai!</body></html>")
|
434
500
|
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
435
501
|
pdfkit.stylesheets << css
|
@@ -437,7 +503,15 @@ describe PDFKit do
|
|
437
503
|
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
|
438
504
|
end
|
439
505
|
|
440
|
-
it "
|
506
|
+
it "can deal with ActiveSupport::SafeBuffer if the HTML doesn't have a head tag" do
|
507
|
+
pdfkit = PDFKit.new(ActiveSupport::SafeBuffer.new "<html><body>Hai!</body></html>")
|
508
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
509
|
+
pdfkit.stylesheets << css
|
510
|
+
pdfkit.to_pdf
|
511
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
|
512
|
+
end
|
513
|
+
|
514
|
+
it "escapes \\X in stylesheets" do
|
441
515
|
pdfkit = PDFKit.new("<html><head></head><body>Hai!</body></html>")
|
442
516
|
css = File.join(SPEC_ROOT,'fixtures','example_with_hex_symbol.css')
|
443
517
|
pdfkit.stylesheets << css
|
@@ -446,22 +520,28 @@ describe PDFKit do
|
|
446
520
|
end
|
447
521
|
|
448
522
|
#NOTICE: This test is failed if use wkhtmltopdf-binary (0.9.9.1)
|
449
|
-
it "
|
523
|
+
it "throws an error if it is unable to connect" do
|
450
524
|
pdfkit = PDFKit.new("http://google.com/this-should-not-be-found/404.html")
|
451
|
-
expect { pdfkit.to_pdf }.to raise_error /exitstatus=1/
|
525
|
+
expect { pdfkit.to_pdf }.to raise_error PDFKit::ImproperWkhtmltopdfExitStatus, /exitstatus=1/
|
452
526
|
end
|
453
527
|
|
454
|
-
it "
|
528
|
+
it "does not throw an error if it is unable to connect", pending: 'this test works for wkhtmltopdf-binary (0.9.9.1)' do
|
455
529
|
pdfkit = PDFKit.new("http://localhost/this-should-not-be-found/404.html")
|
456
530
|
pdf = pdfkit.to_pdf
|
457
531
|
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
458
532
|
end
|
459
533
|
|
460
|
-
it "
|
534
|
+
it "generates a PDF if there are missing assets" do
|
461
535
|
pdfkit = PDFKit.new("<html><body><img alt='' src='http://example.com/surely-it-doesnt-exist.gif' /></body></html>")
|
462
536
|
pdf = pdfkit.to_pdf
|
463
537
|
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
464
538
|
end
|
539
|
+
|
540
|
+
it "can handle ampersands in URLs" do
|
541
|
+
pdfkit = PDFKit.new('https://www.google.com/search?q=pdfkit&sort=ASC')
|
542
|
+
pdf = pdfkit.to_pdf
|
543
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
544
|
+
end
|
465
545
|
end
|
466
546
|
|
467
547
|
describe "#to_file" do
|
@@ -474,14 +554,14 @@ describe PDFKit do
|
|
474
554
|
File.delete(@file_path)
|
475
555
|
end
|
476
556
|
|
477
|
-
it "
|
557
|
+
it "creates a file with the PDF as content" do
|
478
558
|
pdfkit = PDFKit.new('html', :page_size => 'Letter')
|
479
559
|
file = pdfkit.to_file(@file_path)
|
480
560
|
expect(file).to be_instance_of(File)
|
481
561
|
expect(File.read(file.path)[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
482
562
|
end
|
483
563
|
|
484
|
-
it "
|
564
|
+
it "does not truncate data (in Ruby 1.8.6)" do
|
485
565
|
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
486
566
|
pdfkit = PDFKit.new(File.new(file_path))
|
487
567
|
pdf_data = pdfkit.to_pdf
|
@@ -501,7 +581,7 @@ describe PDFKit do
|
|
501
581
|
File.delete(@test_path) if File.exist?(@test_path)
|
502
582
|
end
|
503
583
|
|
504
|
-
it "
|
584
|
+
it "does not allow shell injection in options" do
|
505
585
|
pdfkit = PDFKit.new('html', :header_center => "a title\"; touch #{@test_path} #")
|
506
586
|
pdfkit.to_pdf
|
507
587
|
expect(File.exist?(@test_path)).to eq(false)
|
data/spec/source_spec.rb
CHANGED
@@ -1,74 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe PDFKit::Source do
|
4
6
|
describe "#url?" do
|
5
|
-
it "
|
7
|
+
it "returns true if passed a url like string" do
|
6
8
|
source = PDFKit::Source.new('http://google.com')
|
7
9
|
expect(source).to be_url
|
8
10
|
end
|
9
11
|
|
10
|
-
it "
|
12
|
+
it "returns false if passed a file" do
|
11
13
|
source = PDFKit::Source.new(File.new(__FILE__))
|
12
14
|
expect(source).not_to be_url
|
13
15
|
end
|
14
16
|
|
15
|
-
it "
|
17
|
+
it "returns false if passed a tempfile" do
|
18
|
+
source = PDFKit::Source.new(::Tempfile.new(__FILE__))
|
19
|
+
expect(source).not_to be_url
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns false if passed HTML" do
|
16
23
|
source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
|
17
24
|
expect(source).not_to be_url
|
18
25
|
end
|
19
26
|
|
20
|
-
it "
|
27
|
+
it "returns false if passed HTML with embedded urls at the beginning of a line" do
|
21
28
|
source = PDFKit::Source.new("<blink>Oh Hai!</blink>\nhttp://www.google.com")
|
22
29
|
expect(source).not_to be_url
|
23
30
|
end
|
24
31
|
end
|
25
32
|
|
26
33
|
describe "#file?" do
|
27
|
-
it "
|
34
|
+
it "returns true if passed a file" do
|
28
35
|
source = PDFKit::Source.new(::File.new(__FILE__))
|
29
36
|
expect(source).to be_file
|
30
37
|
end
|
31
38
|
|
32
|
-
it "
|
39
|
+
it "returns true if passed a tempfile" do
|
40
|
+
source = PDFKit::Source.new(::Tempfile.new(__FILE__))
|
41
|
+
expect(source).to be_file
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns false if passed a url like string" do
|
33
45
|
source = PDFKit::Source.new('http://google.com')
|
34
46
|
expect(source).not_to be_file
|
35
47
|
end
|
36
48
|
|
37
|
-
it "
|
49
|
+
it "returns false if passed HTML" do
|
38
50
|
source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
|
39
51
|
expect(source).not_to be_file
|
40
52
|
end
|
41
53
|
end
|
42
54
|
|
43
55
|
describe "#html?" do
|
44
|
-
it "
|
56
|
+
it "returns true if passed HTML" do
|
45
57
|
source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
|
46
58
|
expect(source).to be_html
|
47
59
|
end
|
48
60
|
|
49
|
-
it "
|
61
|
+
it "returns false if passed a file" do
|
50
62
|
source = PDFKit::Source.new(::File.new(__FILE__))
|
51
63
|
expect(source).not_to be_html
|
52
64
|
end
|
53
65
|
|
54
|
-
it "
|
66
|
+
it "returns false if passed a tempfile" do
|
67
|
+
source = PDFKit::Source.new(::Tempfile.new(__FILE__))
|
68
|
+
expect(source).not_to be_html
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns false if passed a url like string" do
|
55
72
|
source = PDFKit::Source.new('http://google.com')
|
56
73
|
expect(source).not_to be_html
|
57
74
|
end
|
58
75
|
end
|
59
76
|
|
77
|
+
describe "#to_input_for_command" do
|
78
|
+
it "URI escapes source URLs and encloses them in quotes to accomodate ampersands" do
|
79
|
+
source = PDFKit::Source.new("https://www.google.com/search?q='cat<dev/zero>/dev/null'")
|
80
|
+
expect(source.to_input_for_command).to eq "\"https://www.google.com/search?q='cat%3Cdev/zero%3E/dev/null'\""
|
81
|
+
end
|
82
|
+
|
83
|
+
it "URI escapes source URI only escape part of it" do
|
84
|
+
source = PDFKit::Source.new("https://www.google.com/search?q='%20 sleep 5'")
|
85
|
+
expect(source.to_input_for_command).to eq "\"https://www.google.com/search?q='%2520%20sleep%205'\""
|
86
|
+
end
|
87
|
+
|
88
|
+
it "does not URI escape previously escaped source URLs" do
|
89
|
+
source = PDFKit::Source.new("https://www.google.com/search?q='cat%3Cdev/zero%3E/dev/null'")
|
90
|
+
expect(source.to_input_for_command).to eq "\"https://www.google.com/search?q='cat%3Cdev/zero%3E/dev/null'\""
|
91
|
+
end
|
92
|
+
|
93
|
+
it "returns a '-' for HTML strings to indicate that we send that content through STDIN" do
|
94
|
+
source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
|
95
|
+
expect(source.to_input_for_command).to eq '-'
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns the file path for file sources" do
|
99
|
+
source = PDFKit::Source.new(::File.new(__FILE__))
|
100
|
+
expect(source.to_input_for_command).to match 'spec/source_spec.rb'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns the file path for tempfile sources" do
|
104
|
+
source = PDFKit::Source.new(file = ::Tempfile.new(__FILE__))
|
105
|
+
expect(source.to_input_for_command).to match file.path
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
60
109
|
describe "#to_s" do
|
61
|
-
it "
|
110
|
+
it "returns the HTML if passed HTML" do
|
62
111
|
source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
|
63
112
|
expect(source.to_s).to eq('<blink>Oh Hai!</blink>')
|
64
113
|
end
|
65
114
|
|
66
|
-
it "
|
115
|
+
it "returns a path if passed a file" do
|
67
116
|
source = PDFKit::Source.new(::File.new(__FILE__))
|
68
117
|
expect(source.to_s).to eq(__FILE__)
|
69
118
|
end
|
70
119
|
|
71
|
-
it "
|
120
|
+
it "returns a path if passed a tempfile" do
|
121
|
+
source = PDFKit::Source.new(file = ::Tempfile.new(__FILE__))
|
122
|
+
expect(source.to_s).to eq(file.path)
|
123
|
+
end
|
124
|
+
|
125
|
+
it "returns the url if passed a url like string" do
|
72
126
|
source = PDFKit::Source.new('http://google.com')
|
73
127
|
expect(source.to_s).to eq('http://google.com')
|
74
128
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
SPEC_ROOT = File.dirname(__FILE__)
|
2
4
|
$LOAD_PATH.unshift(SPEC_ROOT)
|
3
5
|
$LOAD_PATH.unshift(File.join(SPEC_ROOT, '..', 'lib'))
|
@@ -6,13 +8,15 @@ SimpleCov.start do
|
|
6
8
|
add_filter 'spec/'
|
7
9
|
end
|
8
10
|
|
11
|
+
Warning[:deprecated] = true if defined?(Warning.[]=)
|
12
|
+
|
9
13
|
require 'pdfkit'
|
10
14
|
require 'rspec'
|
11
15
|
require 'mocha'
|
12
16
|
require 'rack'
|
13
17
|
require 'rack/test'
|
14
18
|
require 'active_support'
|
15
|
-
require 'custom_wkhtmltopdf_path' if File.
|
19
|
+
require 'custom_wkhtmltopdf_path' if File.exist?(File.join(SPEC_ROOT, 'custom_wkhtmltopdf_path.rb'))
|
16
20
|
|
17
21
|
RSpec.configure do |config|
|
18
22
|
include Rack::Test::Methods
|