wicked_pdf 1.0.3 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/.github/issue_template.md +15 -0
  3. data/.github/workflows/ci.yml +56 -0
  4. data/.rubocop.yml +60 -0
  5. data/.rubocop_todo.yml +84 -37
  6. data/CHANGELOG.md +230 -0
  7. data/README.md +188 -30
  8. data/Rakefile +13 -9
  9. data/gemfiles/5.0.gemfile +8 -0
  10. data/gemfiles/5.1.gemfile +8 -0
  11. data/gemfiles/5.2.gemfile +9 -0
  12. data/gemfiles/6.0.gemfile +10 -0
  13. data/gemfiles/6.1.gemfile +12 -0
  14. data/gemfiles/7.0.gemfile +12 -0
  15. data/generators/wicked_pdf/templates/wicked_pdf.rb +9 -0
  16. data/lib/generators/wicked_pdf_generator.rb +5 -9
  17. data/lib/wicked_pdf/binary.rb +65 -0
  18. data/lib/wicked_pdf/middleware.rb +3 -3
  19. data/lib/wicked_pdf/option_parser.rb +229 -0
  20. data/lib/wicked_pdf/pdf_helper.rb +101 -88
  21. data/lib/wicked_pdf/progress.rb +33 -0
  22. data/lib/wicked_pdf/railtie.rb +6 -33
  23. data/lib/wicked_pdf/tempfile.rb +38 -7
  24. data/lib/wicked_pdf/version.rb +1 -1
  25. data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +212 -88
  26. data/lib/wicked_pdf/wicked_pdf_helper.rb +29 -26
  27. data/lib/wicked_pdf.rb +37 -274
  28. data/test/fixtures/database.yml +4 -0
  29. data/test/fixtures/manifest.js +3 -0
  30. data/test/fixtures/wicked.js +1 -0
  31. data/test/functional/pdf_helper_test.rb +74 -5
  32. data/test/functional/wicked_pdf_helper_assets_test.rb +86 -25
  33. data/test/functional/wicked_pdf_helper_test.rb +15 -13
  34. data/test/test_helper.rb +22 -7
  35. data/test/unit/wicked_pdf_binary_test.rb +26 -0
  36. data/test/unit/wicked_pdf_option_parser_test.rb +128 -0
  37. data/test/unit/wicked_pdf_test.rb +14 -168
  38. data/test/unit/wkhtmltopdf_location_test.rb +48 -0
  39. data/wicked_pdf.gemspec +20 -14
  40. metadata +69 -38
  41. data/.travis.yml +0 -57
  42. data/CHANGLOG.md +0 -48
  43. data/gemfiles/2.3.gemfile +0 -10
  44. data/gemfiles/3.0.gemfile +0 -7
  45. data/gemfiles/3.1.gemfile +0 -8
  46. data/gemfiles/3.2.gemfile +0 -7
  47. data/gemfiles/4.0.gemfile +0 -6
  48. data/gemfiles/4.1.gemfile +0 -6
  49. data/gemfiles/4.2.gemfile +0 -6
  50. data/gemfiles/rails_edge.gemfile +0 -6
data/lib/wicked_pdf.rb CHANGED
@@ -1,47 +1,35 @@
1
1
  # wkhtml2pdf Ruby interface
2
- # http://code.google.com/p/wkhtmltopdf/
2
+ # http://wkhtmltopdf.org/
3
3
 
4
4
  require 'logger'
5
5
  require 'digest/md5'
6
6
  require 'rbconfig'
7
+ require 'open3'
7
8
 
8
- if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
9
- require 'win32/open3'
10
- else
11
- require 'open3'
12
- end
13
-
14
- begin
15
- require 'active_support/core_ext/module/attribute_accessors'
16
- rescue LoadError
17
- require 'active_support/core_ext/class/attribute_accessors'
18
- end
19
-
20
- begin
21
- require 'active_support/core_ext/object/blank'
22
- rescue LoadError
23
- require 'active_support/core_ext/blank'
24
- end
9
+ require 'active_support/core_ext/module/attribute_accessors'
10
+ require 'active_support/core_ext/object/blank'
25
11
 
26
12
  require 'wicked_pdf/version'
27
13
  require 'wicked_pdf/railtie'
14
+ require 'wicked_pdf/option_parser'
28
15
  require 'wicked_pdf/tempfile'
16
+ require 'wicked_pdf/binary'
29
17
  require 'wicked_pdf/middleware'
18
+ require 'wicked_pdf/progress'
30
19
 
31
20
  class WickedPdf
32
21
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
33
- BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
34
- EXE_NAME = 'wkhtmltopdf'
35
22
  @@config = {}
36
23
  cattr_accessor :config
37
24
 
25
+ include Progress
26
+
38
27
  def initialize(wkhtmltopdf_binary_path = nil)
39
- @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
40
- fail "Location of #{EXE_NAME} unknown" if @exe_path.empty?
41
- fail "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
42
- fail "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
28
+ @binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
29
+ end
43
30
 
44
- retreive_binary_version
31
+ def binary_version
32
+ @binary.version
45
33
  end
46
34
 
47
35
  def pdf_from_html_file(filepath, options = {})
@@ -51,44 +39,44 @@ class WickedPdf
51
39
  def pdf_from_string(string, options = {})
52
40
  options = options.dup
53
41
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
54
- string_file = WickedPdfTempfile.new('wicked_pdf.html', options[:temp_path])
55
- string_file.binmode
56
- string_file.write(string)
57
- string_file.close
58
-
59
- pdf = pdf_from_html_file(string_file.path, options)
60
- pdf
61
- rescue => e
62
- raise "Error: #{e}"
42
+ string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
43
+ string_file.write_in_chunks(string)
44
+ pdf_from_html_file(string_file.path, options)
63
45
  ensure
64
- string_file.close! if string_file
46
+ string_file.close if string_file
65
47
  end
66
48
 
67
49
  def pdf_from_url(url, options = {})
68
50
  # merge in global config options
69
51
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
70
- generated_pdf_file = WickedPdfTempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
71
- command = [@exe_path]
72
- command << '-q' unless on_windows? # suppress errors on stdout
52
+ generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
53
+ command = [@binary.path]
54
+ command.unshift(@binary.xvfb_run_path) if options[:use_xvfb]
73
55
  command += parse_options(options)
74
56
  command << url
75
57
  command << generated_pdf_file.path.to_s
76
58
 
77
59
  print_command(command.inspect) if in_development_mode?
78
60
 
79
- err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
80
- stderr.read
61
+ if track_progress?(options)
62
+ invoke_with_progress(command, options)
63
+ else
64
+ err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
65
+ stderr.read
66
+ end
81
67
  end
82
68
  if options[:return_file]
83
69
  return_file = options.delete(:return_file)
84
70
  return generated_pdf_file
85
71
  end
86
- generated_pdf_file.rewind
87
- generated_pdf_file.binmode
88
- pdf = generated_pdf_file.read
89
- fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.length == 0
72
+
73
+ pdf = generated_pdf_file.read_in_chunks
74
+
75
+ raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
76
+ raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
77
+
90
78
  pdf
91
- rescue => e
79
+ rescue StandardError => e
92
80
  raise "Failed to execute:\n#{command}\nError: #{e}"
93
81
  ensure
94
82
  generated_pdf_file.close! if generated_pdf_file && !return_file
@@ -97,12 +85,9 @@ class WickedPdf
97
85
  private
98
86
 
99
87
  def in_development_mode?
100
- return Rails.env == 'development' if defined?(Rails)
101
- RAILS_ENV == 'development' if defined?(RAILS_ENV)
102
- end
88
+ return Rails.env == 'development' if defined?(Rails.env)
103
89
 
104
- def get_binary_version
105
- @binary_version
90
+ RAILS_ENV == 'development' if defined?(RAILS_ENV)
106
91
  end
107
92
 
108
93
  def on_windows?
@@ -110,232 +95,10 @@ class WickedPdf
110
95
  end
111
96
 
112
97
  def print_command(cmd)
113
- p '*' * 15 + cmd + '*' * 15
114
- end
115
-
116
- def retreive_binary_version
117
- _stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
118
- @binary_version = parse_version(stdout.gets(nil))
119
- rescue StandardError
120
- end
121
-
122
- def parse_version(version_info)
123
- match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
124
- if match_data && (2 == match_data.length)
125
- Gem::Version.new(match_data[1])
126
- else
127
- DEFAULT_BINARY_VERSION
128
- end
98
+ Rails.logger.debug '[wicked_pdf]: ' + cmd
129
99
  end
130
100
 
131
101
  def parse_options(options)
132
- [
133
- parse_extra(options),
134
- parse_others(options),
135
- parse_global(options),
136
- parse_outline(options.delete(:outline)),
137
- parse_header_footer(:header => options.delete(:header),
138
- :footer => options.delete(:footer),
139
- :layout => options[:layout]),
140
- parse_cover(options.delete(:cover)),
141
- parse_toc(options.delete(:toc)),
142
- parse_basic_auth(options)
143
- ].flatten
144
- end
145
-
146
- def parse_extra(options)
147
- return [] if options[:extra].nil?
148
- return options[:extra].split if options[:extra].respond_to?(:split)
149
- options[:extra]
150
- end
151
-
152
- def parse_basic_auth(options)
153
- if options[:basic_auth]
154
- user, passwd = Base64.decode64(options[:basic_auth]).split(':')
155
- ['--username', user, '--password', passwd]
156
- else
157
- []
158
- end
159
- end
160
-
161
- def make_option(name, value, type = :string)
162
- if value.is_a?(Array)
163
- return value.collect { |v| make_option(name, v, type) }
164
- end
165
- if type == :name_value
166
- parts = value.to_s.split(' ')
167
- ["--#{name.tr('_', '-')}", *parts]
168
- elsif type == :boolean
169
- ["--#{name.tr('_', '-')}"]
170
- else
171
- ["--#{name.tr('_', '-')}", value.to_s]
172
- end
173
- end
174
-
175
- def valid_option(name)
176
- if get_binary_version < BINARY_VERSION_WITHOUT_DASHES
177
- "--#{name}"
178
- else
179
- name
180
- end
181
- end
182
-
183
- def make_options(options, names, prefix = '', type = :string)
184
- return [] if options.nil?
185
- names.collect do |o|
186
- if options[o].blank?
187
- []
188
- else
189
- make_option("#{prefix.blank? ? '' : prefix + '-'}#{o}",
190
- options[o],
191
- type)
192
- end
193
- end
194
- end
195
-
196
- def parse_header_footer(options)
197
- r = []
198
- [:header, :footer].collect do |hf|
199
- next if options[hf].blank?
200
- opt_hf = options[hf]
201
- r += make_options(opt_hf, [:center, :font_name, :left, :right], "#{hf}")
202
- r += make_options(opt_hf, [:font_size, :spacing], "#{hf}", :numeric)
203
- r += make_options(opt_hf, [:line], "#{hf}", :boolean)
204
- if options[hf] && options[hf][:content]
205
- @hf_tempfiles = [] unless defined?(@hf_tempfiles)
206
- @hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
207
- tf.write options[hf][:content]
208
- tf.flush
209
- options[hf][:html] = {}
210
- options[hf][:html][:url] = "file:///#{tf.path}"
211
- end
212
- unless opt_hf[:html].blank?
213
- r += make_option("#{hf}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
214
- end
215
- end unless options.blank?
216
- r
217
- end
218
-
219
- def parse_cover(argument)
220
- arg = argument.to_s
221
- return [] if arg.blank?
222
- # Filesystem path or URL - hand off to wkhtmltopdf
223
- if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
224
- [valid_option('cover'), arg]
225
- else # HTML content
226
- @hf_tempfiles ||= []
227
- @hf_tempfiles << tf = WickedPdfTempfile.new('wicked_cover_pdf.html')
228
- tf.write arg
229
- tf.flush
230
- [valid_option('cover'), tf.path]
231
- end
232
- end
233
-
234
- def parse_toc(options)
235
- return [] if options.nil?
236
- r = [valid_option('toc')]
237
- unless options.blank?
238
- r += make_options(options, [:font_name, :header_text], 'toc')
239
- r += make_options(options, [:xsl_style_sheet])
240
- r += make_options(options, [:depth,
241
- :header_fs,
242
- :text_size_shrink,
243
- :l1_font_size,
244
- :l2_font_size,
245
- :l3_font_size,
246
- :l4_font_size,
247
- :l5_font_size,
248
- :l6_font_size,
249
- :l7_font_size,
250
- :level_indentation,
251
- :l1_indentation,
252
- :l2_indentation,
253
- :l3_indentation,
254
- :l4_indentation,
255
- :l5_indentation,
256
- :l6_indentation,
257
- :l7_indentation], 'toc', :numeric)
258
- r += make_options(options, [:no_dots,
259
- :disable_links,
260
- :disable_back_links], 'toc', :boolean)
261
- r += make_options(options, [:disable_dotted_lines,
262
- :disable_toc_links], nil, :boolean)
263
- end
264
- r
265
- end
266
-
267
- def parse_outline(options)
268
- r = []
269
- unless options.blank?
270
- r = make_options(options, [:outline], '', :boolean)
271
- r += make_options(options, [:outline_depth], '', :numeric)
272
- end
273
- r
274
- end
275
-
276
- def parse_margins(options)
277
- make_options(options, [:top, :bottom, :left, :right], 'margin', :numeric)
278
- end
279
-
280
- def parse_global(options)
281
- r = []
282
- unless options.blank?
283
- r += make_options(options, [:orientation,
284
- :dpi,
285
- :page_size,
286
- :page_width,
287
- :title])
288
- r += make_options(options, [:lowquality,
289
- :grayscale,
290
- :no_pdf_compression], '', :boolean)
291
- r += make_options(options, [:image_dpi,
292
- :image_quality,
293
- :page_height], '', :numeric)
294
- r += parse_margins(options.delete(:margin))
295
- end
296
- r
297
- end
298
-
299
- def parse_others(options)
300
- r = []
301
- unless options.blank?
302
- r += make_options(options, [:proxy,
303
- :username,
304
- :password,
305
- :encoding,
306
- :user_style_sheet,
307
- :viewport_size])
308
- r += make_options(options, [:cookie,
309
- :post], '', :name_value)
310
- r += make_options(options, [:redirect_delay,
311
- :zoom,
312
- :page_offset,
313
- :javascript_delay], '', :numeric)
314
- r += make_options(options, [:book,
315
- :default_header,
316
- :disable_javascript,
317
- :enable_plugins,
318
- :disable_internal_links,
319
- :disable_external_links,
320
- :print_media_type,
321
- :disable_smart_shrinking,
322
- :use_xserver,
323
- :no_background], '', :boolean)
324
- r += make_options(options, [:no_stop_slow_scripts], '', nil)
325
- end
326
- r
327
- end
328
-
329
- def find_wkhtmltopdf_binary_path
330
- possible_locations = (ENV['PATH'].split(':') + %w(/usr/bin /usr/local/bin ~/bin)).uniq
331
- exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
332
- exe_path ||= begin
333
- detected_path = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
334
- detected_path.present? && detected_path
335
- rescue
336
- nil
337
- end
338
- exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
339
- exe_path || ''
102
+ OptionParser.new(binary_version).parse(options)
340
103
  end
341
104
  end
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ timeout: 500
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link wicked.js
3
+ //= link wicked.css
@@ -0,0 +1 @@
1
+ // Wicked js
@@ -5,10 +5,32 @@ module ActionController
5
5
  def render_to_string(opts = {})
6
6
  opts.to_s
7
7
  end
8
+
9
+ def self.alias_method_chain(_target, _feature); end
10
+ end
11
+ end
12
+
13
+ module ActionControllerMock
14
+ class Base
15
+ def render(_)
16
+ [:base]
17
+ end
18
+
19
+ def render_to_string; end
20
+
21
+ def self.after_action(_); end
8
22
  end
9
23
  end
10
24
 
11
25
  class PdfHelperTest < ActionController::TestCase
26
+ module SomePatch
27
+ def render(_)
28
+ super.tap do |s|
29
+ s << :patched
30
+ end
31
+ end
32
+ end
33
+
12
34
  def setup
13
35
  @ac = ActionController::Base.new
14
36
  end
@@ -17,11 +39,58 @@ class PdfHelperTest < ActionController::TestCase
17
39
  @ac = nil
18
40
  end
19
41
 
20
- if Rails::VERSION::MAJOR == 2
21
- test 'should prerender header and footer :template options' do
22
- options = @ac.send(:prerender_header_and_footer,
23
- :header => { :html => { :template => 'hf.html.erb' } })
24
- assert_match /^file:\/\/\/.*wicked_header_pdf.*\.html/, options[:header][:html][:url]
42
+ test 'should prerender header and footer :template options' do
43
+ options = @ac.send(:prerender_header_and_footer,
44
+ :header => { :html => { :template => 'hf.html.erb' } })
45
+ assert_match %r{^file:\/\/\/.*wicked_header_pdf.*\.html}, options[:header][:html][:url]
46
+ end
47
+
48
+ test 'should not interfere with already prepended patches' do
49
+ # Emulate railtie
50
+ if Rails::VERSION::MAJOR >= 5
51
+ # this spec tests the following:
52
+ # if another gem prepends a render method to ActionController::Base
53
+ # before wicked_pdf does, does calling render trigger an infinite loop?
54
+ # this spec fails with 6392bea1fe3a41682dfd7c20fd9c179b5a758f59 because PdfHelper
55
+ # aliases the render method prepended by the other gem to render_without_pdf, then
56
+ # base_evals its own definition of render, which calls render_with_pdf -> render_without_pdf.
57
+ # If the other gem uses the prepend inhertinance pattern (calling super instead of aliasing),
58
+ # when it calls super it calls the base_eval'd version of render instead of going up the
59
+ # inheritance chain, causing an infinite loop.
60
+
61
+ # This fiddling with consts is required to get around the fact that PdfHelper checks
62
+ # that it is being prepended to ActionController::Base
63
+ OriginalBase = ActionController::Base
64
+ ActionController.send(:remove_const, :Base)
65
+ ActionController.const_set(:Base, ActionControllerMock::Base)
66
+
67
+ # Emulate another gem being loaded before wicked
68
+ ActionController::Base.prepend(SomePatch)
69
+ ActionController::Base.prepend(::WickedPdf::PdfHelper)
70
+
71
+ begin
72
+ # test that wicked's render method is actually called
73
+ ac = ActionController::Base.new
74
+ ac.expects(:render_with_wicked_pdf)
75
+ ac.render(:cats)
76
+
77
+ # test that calling render does not trigger infinite loop
78
+ ac = ActionController::Base.new
79
+ assert_equal %i[base patched], ac.render(:cats)
80
+ rescue SystemStackError
81
+ assert_equal true, false # force spec failure
82
+ ensure
83
+ ActionController.send(:remove_const, :Base)
84
+ ActionController.const_set(:Base, OriginalBase)
85
+ end
86
+ end
87
+ end
88
+
89
+ test 'should call after_action instead of after_filter when able' do
90
+ ActionController::Base.expects(:after_filter).with(:clean_temp_files).never
91
+ ActionController::Base.expects(:after_action).with(:clean_temp_files).once
92
+ ActionController::Base.class_eval do
93
+ include ::WickedPdf::PdfHelper
25
94
  end
26
95
  end
27
96
  end
@@ -2,14 +2,29 @@ require 'test_helper'
2
2
  require 'action_view/test_case'
3
3
 
4
4
  class WickedPdfHelperAssetsTest < ActionView::TestCase
5
- include WickedPdfHelper::Assets
5
+ include WickedPdf::WickedPdfHelper::Assets
6
6
 
7
7
  if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
8
8
  test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
9
- source = File.new('test/fixtures/wicked.css', 'r')
10
- destination = Rails.root.join('app', 'assets', 'stylesheets', 'wicked.css')
11
- File.open(destination, 'w') { |f| f.write(source) }
12
- assert_match /data:text\/css;base64,.+/, wicked_pdf_asset_base64('wicked.css')
9
+ assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
10
+ end
11
+
12
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
13
+ Rails.configuration.assets.expects(:compile => true)
14
+ assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
15
+ wicked_pdf_stylesheet_link_tag('wicked')
16
+ end
17
+
18
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
19
+ Rails.configuration.assets.expects(:compile => true)
20
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"),
21
+ wicked_pdf_image_tag('pdf')
22
+ end
23
+
24
+ test 'wicked_pdf_javascript_include_tag should inline the javascripts passed in' do
25
+ Rails.configuration.assets.expects(:compile => true)
26
+ assert_equal "<script type='text/javascript'>// Wicked js\n;\n</script>",
27
+ wicked_pdf_javascript_include_tag('wicked')
13
28
  end
14
29
 
15
30
  test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
@@ -18,45 +33,91 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
18
33
  end
19
34
 
20
35
  test 'wicked_pdf_asset_path should return a url when assets are served by an asset server using HTTPS' do
21
- expects(:asset_path => 'https://assets.domain.com/dummy.png', 'precompiled_asset?' => true)
36
+ Rails.configuration.assets.expects(:compile => false)
37
+ expects(:asset_path => 'https://assets.domain.com/dummy.png')
22
38
  assert_equal 'https://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
23
39
  end
24
40
 
25
41
  test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with relative urls' do
42
+ Rails.configuration.assets.expects(:compile => false)
26
43
  expects(:asset_path => '//assets.domain.com/dummy.png')
27
- expects('precompiled_asset?' => true)
28
44
  assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
29
45
  end
30
46
 
31
47
  test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with no protocol set' do
48
+ Rails.configuration.assets.expects(:compile => false)
32
49
  expects(:asset_path => 'assets.domain.com/dummy.png')
33
- expects('precompiled_asset?' => true)
34
50
  assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
35
51
  end
36
52
 
37
- test 'wicked_pdf_asset_path should return a path when assets are precompiled' do
38
- expects('precompiled_asset?' => false)
53
+ test 'wicked_pdf_asset_path should return a path' do
54
+ Rails.configuration.assets.expects(:compile => true)
39
55
  path = wicked_pdf_asset_path('application.css')
40
56
 
41
- assert path.include?('/assets/stylesheets/application.css')
57
+ assert path.include?('/app/assets/stylesheets/application.css')
58
+ assert path.include?('file:///')
59
+
60
+ Rails.configuration.assets.expects(:compile => false)
61
+ expects(:asset_path => '/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
62
+ path = wicked_pdf_asset_path('application.css')
63
+
64
+ assert path.include?('/public/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
42
65
  assert path.include?('file:///')
43
66
  end
44
67
 
68
+ # This assets does not exists so probably it doesn't matter what is
69
+ # returned, but lets ensure that returned value is the same when assets
70
+ # are precompiled and when they are not
71
+ test 'wicked_pdf_asset_path should return a path when asset does not exist' do
72
+ Rails.configuration.assets.expects(:compile => true)
73
+ path = wicked_pdf_asset_path('missing.png')
74
+
75
+ assert path.include?('/public/missing.png')
76
+ assert path.include?('file:///')
77
+
78
+ Rails.configuration.assets.expects(:compile => false)
79
+ expects(:asset_path => '/missing.png')
80
+ path = wicked_pdf_asset_path('missing.png')
81
+
82
+ assert path.include?('/public/missing.png')
83
+ assert path.include?('file:///')
84
+ end
85
+
86
+ test 'wicked_pdf_asset_path should return a url when asset is url' do
87
+ Rails.configuration.assets.expects(:compile => true)
88
+ expects(:asset_path => 'http://example.com/rails.png')
89
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')
90
+
91
+ Rails.configuration.assets.expects(:compile => false)
92
+ expects(:asset_path => 'http://example.com/rails.png')
93
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')
94
+ end
95
+
96
+ test 'wicked_pdf_asset_path should return a url when asset is url without protocol' do
97
+ Rails.configuration.assets.expects(:compile => true)
98
+ expects(:asset_path => '//example.com/rails.png')
99
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')
100
+
101
+ Rails.configuration.assets.expects(:compile => false)
102
+ expects(:asset_path => '//example.com/rails.png')
103
+ assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')
104
+ end
105
+
45
106
  test 'WickedPdfHelper::Assets::ASSET_URL_REGEX should match various URL data type formats' do
46
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
47
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
48
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
49
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
50
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
51
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
52
- assert_no_match WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
53
- end
54
-
55
- test 'set_protocol should properly set the protocol when the asset is precompiled' do
56
- assert_equal 'http://assets.domain.com/dummy.png', set_protocol('//assets.domain.com/dummy.png')
57
- assert_equal '/assets.domain.com/dummy.png', set_protocol('/assets.domain.com/dummy.png')
58
- assert_equal 'http://assets.domain.com/dummy.png', set_protocol('http://assets.domain.com/dummy.png')
59
- assert_equal 'https://assets.domain.com/dummy.png', set_protocol('https://assets.domain.com/dummy.png')
107
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
108
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
109
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
110
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
111
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
112
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
113
+ assert_no_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
114
+ end
115
+
116
+ test 'prepend_protocol should properly set the protocol when the asset is precompiled' do
117
+ assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('//assets.domain.com/dummy.png')
118
+ assert_equal '/assets.domain.com/dummy.png', prepend_protocol('/assets.domain.com/dummy.png')
119
+ assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('http://assets.domain.com/dummy.png')
120
+ assert_equal 'https://assets.domain.com/dummy.png', prepend_protocol('https://assets.domain.com/dummy.png')
60
121
  end
61
122
  end
62
123
  end
@@ -2,25 +2,27 @@ require 'test_helper'
2
2
  require 'action_view/test_case'
3
3
 
4
4
  class WickedPdfHelperTest < ActionView::TestCase
5
- if Rails::VERSION::MAJOR == 2
6
- test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
7
- assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
8
- wicked_pdf_stylesheet_link_tag('../../../fixtures/wicked')
9
- end
5
+ include WickedPdf::WickedPdfHelper
10
6
 
11
- test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
12
- assert_equal image_tag("file:///#{Rails.root.join('public', 'images', 'pdf')}"),
13
- wicked_pdf_image_tag('pdf')
14
- end
7
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
8
+ assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
9
+ wicked_pdf_stylesheet_link_tag('../../../fixtures/wicked')
10
+ end
15
11
 
12
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
13
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'images', 'pdf')}"),
14
+ wicked_pdf_image_tag('pdf')
15
+ end
16
+
17
+ if Rails::VERSION::MAJOR == 2
16
18
  test 'wicked_pdf_javascript_src_tag should return the same as javascript_src_tag when passed a full path' do
17
19
  assert_equal javascript_src_tag("file:///#{Rails.root.join('public', 'javascripts', 'pdf')}", {}),
18
20
  wicked_pdf_javascript_src_tag('pdf')
19
21
  end
22
+ end
20
23
 
21
- test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
22
- assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
23
- wicked_pdf_javascript_include_tag('foo', 'bar')
24
- end
24
+ test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
25
+ assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
26
+ wicked_pdf_javascript_include_tag('foo', 'bar')
25
27
  end
26
28
  end