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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +2 -1
- data/CHANGELOG.md +10 -0
- data/Gemfile +4 -0
- data/lib/pdfkit/configuration.rb +1 -1
- data/lib/pdfkit/middleware.rb +25 -20
- data/lib/pdfkit/pdfkit.rb +91 -90
- data/lib/pdfkit/version.rb +1 -1
- data/pdfkit.gemspec +2 -1
- data/spec/middleware_spec.rb +72 -64
- data/spec/pdfkit_spec.rb +81 -57
- data/spec/source_spec.rb +16 -17
- data/spec/spec_helper.rb +0 -1
- metadata +19 -6
- data/Gemfile.lock +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a7e2a27521d1d1a34a809f5b3d2ce8db2b893ca
|
4
|
+
data.tar.gz: e336025cf967c7a673e46c8b289aa5f80edc97ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74ffa36e6a43b1d9b0e1b45d821a5974e0c58e651cb5bf5e13666dae61cdd19bb0626ec4133186777931df25db5b63899adcf6cdcafb550d691a4ec55935c91c
|
7
|
+
data.tar.gz: 1aafe95aa04762e43484cb619c0572406156a309891a090e7021ad7fc27890a0d4f728d55e4f4efbeedfd42529ba051fc0ddfaa611cd7a7b47da9f63e523fed2
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
2015-05-06
|
2
|
+
=================
|
3
|
+
* Bump to 0.7.0
|
4
|
+
* Fix issue #230 where PDFKit called `bundle exec` without a Gemfile
|
5
|
+
* Fix issue #183 where PDFKit broke the path to wkhtmltopdf.exe by escaping
|
6
|
+
spaces in paths
|
7
|
+
* Improve performance by not storing the PDF in memory if a path is
|
8
|
+
provided. Thanks @mikefarah
|
9
|
+
* Middleware now infers HTTP or HTTPS from environment for relative URLs
|
10
|
+
|
1
11
|
2014-04-20
|
2
12
|
==================
|
3
13
|
* Bump to 0.6.2
|
data/Gemfile
CHANGED
data/lib/pdfkit/configuration.rb
CHANGED
@@ -19,7 +19,7 @@ class PDFKit
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def wkhtmltopdf
|
22
|
-
@wkhtmltopdf ||= (defined?(Bundler::GemfileError) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
22
|
+
@wkhtmltopdf ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
23
23
|
end
|
24
24
|
|
25
25
|
def quiet?
|
data/lib/pdfkit/middleware.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
class PDFKit
|
2
|
-
|
3
2
|
class Middleware
|
4
|
-
|
5
3
|
def initialize(app, options = {}, conditions = {})
|
6
4
|
@app = app
|
7
5
|
@options = options
|
8
6
|
@conditions = conditions
|
7
|
+
@render_pdf = false
|
9
8
|
end
|
10
9
|
|
11
10
|
def call(env)
|
@@ -42,12 +41,22 @@ class PDFKit
|
|
42
41
|
|
43
42
|
private
|
44
43
|
|
45
|
-
# Change relative paths to absolute
|
44
|
+
# Change relative paths to absolute, and relative protocols to absolute protocols
|
46
45
|
def translate_paths(body, env)
|
47
|
-
|
46
|
+
body = translate_relative_paths(body, env)
|
47
|
+
translate_relative_protocols(body, env)
|
48
|
+
end
|
49
|
+
|
50
|
+
def translate_relative_paths(body, env)
|
48
51
|
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
|
52
|
+
# Try out this regexp using rubular http://rubular.com/r/vmuGSkheuu
|
53
|
+
body.gsub(/(href|src)=(['"])\/([^\/]([^\"']*|[^"']*))['"]/, "\\1=\\2#{root}\\3\\2")
|
54
|
+
end
|
49
55
|
|
50
|
-
|
56
|
+
def translate_relative_protocols(body, env)
|
57
|
+
protocol = "#{env['rack.url_scheme']}://"
|
58
|
+
# Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
|
59
|
+
body.gsub(/(href|src)=(['"])\/\/([^\"']*|[^"']*)['"]/, "\\1=\\2#{protocol}\\3\\2")
|
51
60
|
end
|
52
61
|
|
53
62
|
def rendering_pdf?
|
@@ -55,25 +64,16 @@ class PDFKit
|
|
55
64
|
end
|
56
65
|
|
57
66
|
def render_as_pdf?
|
58
|
-
|
67
|
+
request_path = @request.path
|
68
|
+
request_path_is_pdf = request_path.match(%r{\.pdf$})
|
59
69
|
|
60
70
|
if request_path_is_pdf && @conditions[:only]
|
61
|
-
|
62
|
-
|
63
|
-
if pattern.is_a?(Regexp)
|
64
|
-
@request.path =~ pattern
|
65
|
-
else
|
66
|
-
@request.path[0, pattern.length] == pattern
|
67
|
-
end
|
71
|
+
conditions_as_regexp(@conditions[:only]).any? do |pattern|
|
72
|
+
request_path =~ pattern
|
68
73
|
end
|
69
74
|
elsif request_path_is_pdf && @conditions[:except]
|
70
|
-
|
71
|
-
|
72
|
-
if pattern.is_a?(Regexp)
|
73
|
-
return false if @request.path =~ pattern
|
74
|
-
else
|
75
|
-
return false if @request.path[0, pattern.length] == pattern
|
76
|
-
end
|
75
|
+
conditions_as_regexp(@conditions[:except]).each do |pattern|
|
76
|
+
return false if request_path =~ pattern
|
77
77
|
end
|
78
78
|
|
79
79
|
return true
|
@@ -98,5 +98,10 @@ class PDFKit
|
|
98
98
|
(accepts || '').split(',').unshift(type).compact.join(',')
|
99
99
|
end
|
100
100
|
|
101
|
+
def conditions_as_regexp(conditions)
|
102
|
+
[conditions].flatten.map do |pattern|
|
103
|
+
pattern.is_a?(Regexp) ? pattern : Regexp.new('^' + pattern)
|
104
|
+
end
|
105
|
+
end
|
101
106
|
end
|
102
107
|
end
|
data/lib/pdfkit/pdfkit.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'shellwords'
|
2
2
|
|
3
3
|
class PDFKit
|
4
|
-
|
5
4
|
class NoExecutableError < StandardError
|
6
5
|
def initialize
|
7
6
|
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
|
@@ -33,8 +32,7 @@ class PDFKit
|
|
33
32
|
end
|
34
33
|
|
35
34
|
def command(path = nil)
|
36
|
-
args =
|
37
|
-
args += @options.to_a.flatten.compact
|
35
|
+
args = @options.to_a.flatten.compact
|
38
36
|
|
39
37
|
if @source.html?
|
40
38
|
args << '-' # Get HTML from stdin
|
@@ -44,7 +42,7 @@ class PDFKit
|
|
44
42
|
|
45
43
|
args << (path || '-') # Write to file or stdout
|
46
44
|
|
47
|
-
args.shelljoin
|
45
|
+
[executable, args.shelljoin].join ' '
|
48
46
|
end
|
49
47
|
|
50
48
|
def executable
|
@@ -65,12 +63,12 @@ class PDFKit
|
|
65
63
|
result = IO.popen(invoke, "wb+") do |pdf|
|
66
64
|
pdf.puts(@source.to_s) if @source.html?
|
67
65
|
pdf.close_write
|
68
|
-
pdf.gets(nil)
|
66
|
+
pdf.gets(nil) if path.nil?
|
69
67
|
end
|
70
|
-
result = File.read(path) if path
|
71
68
|
|
72
|
-
# $? is thread safe per
|
73
|
-
|
69
|
+
# $? is thread safe per
|
70
|
+
# http://stackoverflow.com/questions/2164887/thread-safe-external-process-in-ruby-plus-checking-exitstatus
|
71
|
+
raise "command failed (exitstatus=#{$?.exitstatus}): #{invoke}" if empty_result?(path, result) or !successful?($?)
|
74
72
|
return result
|
75
73
|
end
|
76
74
|
|
@@ -81,114 +79,117 @@ class PDFKit
|
|
81
79
|
|
82
80
|
protected
|
83
81
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
def find_options_in_meta(content)
|
89
|
-
# Read file if content is a File
|
90
|
-
content = content.read if content.is_a?(File)
|
82
|
+
# Pulled from:
|
83
|
+
# https://github.com/wkhtmltopdf/wkhtmltopdf/blob/ebf9b6cfc4c58a31349fb94c568b254fac37b3d3/README_WKHTMLTOIMAGE#L27
|
84
|
+
REPEATABLE_OPTIONS = %w[--allow --cookie --custom-header --post --post-file --run-script]
|
91
85
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0].split
|
96
|
-
found[name] = meta.scan(/content=["']([^"'\\]+)["']/)[0][0]
|
97
|
-
end
|
98
|
-
end
|
86
|
+
def find_options_in_meta(content)
|
87
|
+
# Read file if content is a File
|
88
|
+
content = content.read if content.is_a?(File)
|
99
89
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
found[
|
105
|
-
found[new_key][key] = value
|
90
|
+
found = {}
|
91
|
+
content.scan(/<meta [^>]*>/) do |meta|
|
92
|
+
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
|
93
|
+
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0].split
|
94
|
+
found[name] = meta.scan(/content=["']([^"'\\]+)["']/)[0][0]
|
106
95
|
end
|
107
|
-
|
108
|
-
found
|
109
96
|
end
|
110
97
|
|
111
|
-
|
112
|
-
|
98
|
+
tuple_keys = found.keys.select { |k| k.is_a? Array }
|
99
|
+
tuple_keys.each do |key|
|
100
|
+
value = found.delete key
|
101
|
+
new_key = key.shift
|
102
|
+
found[new_key] ||= {}
|
103
|
+
found[new_key][key] = value
|
113
104
|
end
|
114
105
|
|
115
|
-
|
116
|
-
|
106
|
+
found
|
107
|
+
end
|
117
108
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
109
|
+
def style_tag_for(stylesheet)
|
110
|
+
"<style>#{File.read(stylesheet)}</style>"
|
111
|
+
end
|
112
|
+
|
113
|
+
def append_stylesheets
|
114
|
+
raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
|
115
|
+
|
116
|
+
stylesheets.each do |stylesheet|
|
117
|
+
if @source.to_s.match(/<\/head>/)
|
118
|
+
@source = Source.new(@source.to_s.gsub(/(<\/head>)/) {|s| style_tag_for(stylesheet) + s })
|
119
|
+
else
|
120
|
+
@source.to_s.insert(0, style_tag_for(stylesheet))
|
124
121
|
end
|
125
122
|
end
|
123
|
+
end
|
126
124
|
|
127
|
-
|
128
|
-
|
125
|
+
def normalize_options(options)
|
126
|
+
normalized_options = {}
|
129
127
|
|
130
|
-
|
131
|
-
|
128
|
+
options.each do |key, value|
|
129
|
+
next if !value
|
132
130
|
|
133
|
-
|
134
|
-
|
131
|
+
# The actual option for wkhtmltopdf
|
132
|
+
normalized_key = "--#{normalize_arg key}"
|
135
133
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
end
|
141
|
-
else # Otherwise, just normalize it like usual
|
142
|
-
normalized_options[normalized_key] = normalize_value(value)
|
134
|
+
# If the option is repeatable, attempt to normalize all values
|
135
|
+
if REPEATABLE_OPTIONS.include? normalized_key
|
136
|
+
normalize_repeatable_value(value) do |normalized_key_piece, normalized_value|
|
137
|
+
normalized_options[[normalized_key, normalized_key_piece]] = normalized_value
|
143
138
|
end
|
139
|
+
else # Otherwise, just normalize it like usual
|
140
|
+
normalized_options[normalized_key] = normalize_value(value)
|
144
141
|
end
|
145
|
-
|
146
|
-
normalized_options
|
147
142
|
end
|
148
143
|
|
149
|
-
|
150
|
-
|
151
|
-
end
|
144
|
+
normalized_options
|
145
|
+
end
|
152
146
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
147
|
+
def normalize_arg(arg)
|
148
|
+
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
149
|
+
end
|
150
|
+
|
151
|
+
def normalize_value(value)
|
152
|
+
case value
|
153
|
+
when TrueClass, 'true' #ie, ==true, see http://www.ruby-doc.org/core-1.9.3/TrueClass.html
|
154
|
+
nil
|
155
|
+
when Hash
|
156
|
+
value.to_a.flatten.collect{|x| normalize_value(x)}.compact
|
157
|
+
when Array
|
158
|
+
value.flatten.collect{|x| x.to_s}
|
159
|
+
else
|
160
|
+
value.to_s
|
164
161
|
end
|
162
|
+
end
|
165
163
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
end
|
172
|
-
else
|
173
|
-
[normalize_value(value), '']
|
164
|
+
def normalize_repeatable_value(value)
|
165
|
+
case value
|
166
|
+
when Hash, Array
|
167
|
+
value.each do |(key, val)|
|
168
|
+
yield [normalize_value(key), normalize_value(val)]
|
174
169
|
end
|
170
|
+
else
|
171
|
+
[normalize_value(value), '']
|
175
172
|
end
|
173
|
+
end
|
176
174
|
|
177
|
-
|
178
|
-
|
175
|
+
def successful?(status)
|
176
|
+
return true if status.success?
|
179
177
|
|
180
|
-
|
181
|
-
|
182
|
-
|
178
|
+
# Some of the codes: https://code.google.com/p/wkhtmltopdf/issues/detail?id=1088
|
179
|
+
# returned when assets are missing (404): https://code.google.com/p/wkhtmltopdf/issues/detail?id=548
|
180
|
+
return true if status.exitstatus == 2 && error_handling?
|
183
181
|
|
184
|
-
|
185
|
-
|
182
|
+
false
|
183
|
+
end
|
186
184
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
# https://code.google.com/p/wkhtmltopdf/issues/detail?id=55
|
191
|
-
%w(skip ignore).include?(@options['--load-error-handling'])
|
192
|
-
end
|
185
|
+
def empty_result?(path, result)
|
186
|
+
(path && File.size(path) == 0) || (path.nil? && result.to_s.strip.empty?)
|
187
|
+
end
|
193
188
|
|
189
|
+
def error_handling?
|
190
|
+
@options.key?('--ignore-load-errors') ||
|
191
|
+
# wkhtmltopdf v0.10.0 beta4 replaces ignore-load-errors with load-error-handling
|
192
|
+
# https://code.google.com/p/wkhtmltopdf/issues/detail?id=55
|
193
|
+
%w(skip ignore).include?(@options['--load-error-handling'])
|
194
|
+
end
|
194
195
|
end
|
data/lib/pdfkit/version.rb
CHANGED
data/pdfkit.gemspec
CHANGED
@@ -23,7 +23,8 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.add_development_dependency(%q<activesupport>, [">= 3.0.8"])
|
24
24
|
s.add_development_dependency(%q<mocha>, [">= 0.9.10"])
|
25
25
|
s.add_development_dependency(%q<rack-test>, [">= 0.5.6"])
|
26
|
+
s.add_development_dependency(%q<i18n>, ["~>0.6.11"]) # Ruby 1.9.2 compatibility
|
26
27
|
s.add_development_dependency(%q<rake>, ["~>0.9.2"])
|
27
28
|
s.add_development_dependency(%q<rdoc>, ["~> 4.0.1"])
|
28
|
-
s.add_development_dependency(%q<rspec>, ["~>
|
29
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.0"])
|
29
30
|
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -16,22 +16,30 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe PDFKit::Middleware do
|
19
|
-
let(:headers)
|
19
|
+
let(:headers) do
|
20
|
+
{'Content-Type' => "text/html"}
|
21
|
+
end
|
20
22
|
|
21
23
|
describe "#call" do
|
22
24
|
describe "caching" do
|
23
|
-
let(:headers)
|
25
|
+
let(:headers) do
|
26
|
+
{
|
27
|
+
'Content-Type' => "text/html",
|
28
|
+
'ETag' => 'foo',
|
29
|
+
'Cache-Control' => 'max-age=2592000, public'
|
30
|
+
}
|
31
|
+
end
|
24
32
|
|
25
33
|
context "by default" do
|
26
34
|
before { mock_app }
|
27
35
|
|
28
36
|
it "deletes ETag" do
|
29
37
|
get 'http://www.example.org/public/test.pdf'
|
30
|
-
last_response.headers["ETag"].
|
38
|
+
expect(last_response.headers["ETag"]).to be_nil
|
31
39
|
end
|
32
40
|
it "deletes Cache-Control" do
|
33
41
|
get 'http://www.example.org/public/test.pdf'
|
34
|
-
last_response.headers["Cache-Control"].
|
42
|
+
expect(last_response.headers["Cache-Control"]).to be_nil
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -40,11 +48,12 @@ describe PDFKit::Middleware do
|
|
40
48
|
|
41
49
|
it "preserves ETag" do
|
42
50
|
get 'http://www.example.org/public/test.pdf'
|
43
|
-
last_response.headers["ETag"].
|
51
|
+
expect(last_response.headers["ETag"]).not_to be_nil
|
44
52
|
end
|
53
|
+
|
45
54
|
it "preserves Cache-Control" do
|
46
55
|
get 'http://www.example.org/public/test.pdf'
|
47
|
-
last_response.headers["Cache-Control"].
|
56
|
+
expect(last_response.headers["Cache-Control"]).not_to be_nil
|
48
57
|
end
|
49
58
|
end
|
50
59
|
end
|
@@ -59,16 +68,16 @@ describe PDFKit::Middleware do
|
|
59
68
|
context "matching" do
|
60
69
|
specify do
|
61
70
|
get 'http://www.example.org/public/test.pdf'
|
62
|
-
last_response.headers["Content-Type"].
|
63
|
-
last_response.body.bytesize.
|
71
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
72
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
64
73
|
end
|
65
74
|
end
|
66
75
|
|
67
76
|
context "not matching" do
|
68
77
|
specify do
|
69
78
|
get 'http://www.example.org/secret/test.pdf'
|
70
|
-
last_response.headers["Content-Type"].
|
71
|
-
last_response.body.
|
79
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
80
|
+
expect(last_response.body).to eq("Hello world!")
|
72
81
|
end
|
73
82
|
end
|
74
83
|
end # one regex
|
@@ -79,16 +88,16 @@ describe PDFKit::Middleware do
|
|
79
88
|
context "matching" do
|
80
89
|
specify do
|
81
90
|
get 'http://www.example.org/public/test.pdf'
|
82
|
-
last_response.headers["Content-Type"].
|
83
|
-
last_response.body.bytesize.
|
91
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
92
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
84
93
|
end
|
85
94
|
end
|
86
95
|
|
87
96
|
context "not matching" do
|
88
97
|
specify do
|
89
98
|
get 'http://www.example.org/secret/test.pdf'
|
90
|
-
last_response.headers["Content-Type"].
|
91
|
-
last_response.body.
|
99
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
100
|
+
expect(last_response.body).to eq("Hello world!")
|
92
101
|
end
|
93
102
|
end
|
94
103
|
end # multiple regex
|
@@ -101,16 +110,16 @@ describe PDFKit::Middleware do
|
|
101
110
|
context "matching" do
|
102
111
|
specify do
|
103
112
|
get 'http://www.example.org/public/test.pdf'
|
104
|
-
last_response.headers["Content-Type"].
|
105
|
-
last_response.body.bytesize.
|
113
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
114
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
106
115
|
end
|
107
116
|
end
|
108
117
|
|
109
118
|
context "not matching" do
|
110
119
|
specify do
|
111
120
|
get 'http://www.example.org/secret/test.pdf'
|
112
|
-
last_response.headers["Content-Type"].
|
113
|
-
last_response.body.
|
121
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
122
|
+
expect(last_response.body).to eq("Hello world!")
|
114
123
|
end
|
115
124
|
end
|
116
125
|
end # one string
|
@@ -121,16 +130,16 @@ describe PDFKit::Middleware do
|
|
121
130
|
context "matching" do
|
122
131
|
specify do
|
123
132
|
get 'http://www.example.org/public/test.pdf'
|
124
|
-
last_response.headers["Content-Type"].
|
125
|
-
last_response.body.bytesize.
|
133
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
134
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
126
135
|
end
|
127
136
|
end
|
128
137
|
|
129
138
|
context "not matching" do
|
130
139
|
specify do
|
131
140
|
get 'http://www.example.org/secret/test.pdf'
|
132
|
-
last_response.headers["Content-Type"].
|
133
|
-
last_response.body.
|
141
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
142
|
+
expect(last_response.body).to eq("Hello world!")
|
134
143
|
end
|
135
144
|
end
|
136
145
|
end # multiple string
|
@@ -147,16 +156,16 @@ describe PDFKit::Middleware do
|
|
147
156
|
context "matching" do
|
148
157
|
specify do
|
149
158
|
get 'http://www.example.org/public/test.pdf'
|
150
|
-
last_response.headers["Content-Type"].
|
151
|
-
last_response.body.bytesize.
|
159
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
160
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
152
161
|
end
|
153
162
|
end
|
154
163
|
|
155
164
|
context "not matching" do
|
156
165
|
specify do
|
157
166
|
get 'http://www.example.org/secret/test.pdf'
|
158
|
-
last_response.headers["Content-Type"].
|
159
|
-
last_response.body.
|
167
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
168
|
+
expect(last_response.body).to eq("Hello world!")
|
160
169
|
end
|
161
170
|
end
|
162
171
|
end # one regex
|
@@ -167,16 +176,16 @@ describe PDFKit::Middleware do
|
|
167
176
|
context "matching" do
|
168
177
|
specify do
|
169
178
|
get 'http://www.example.org/public/test.pdf'
|
170
|
-
last_response.headers["Content-Type"].
|
171
|
-
last_response.body.bytesize.
|
179
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
180
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
172
181
|
end
|
173
182
|
end
|
174
183
|
|
175
184
|
context "not matching" do
|
176
185
|
specify do
|
177
186
|
get 'http://www.example.org/secret/test.pdf'
|
178
|
-
last_response.headers["Content-Type"].
|
179
|
-
last_response.body.
|
187
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
188
|
+
expect(last_response.body).to eq("Hello world!")
|
180
189
|
end
|
181
190
|
end
|
182
191
|
end # multiple regex
|
@@ -189,16 +198,16 @@ describe PDFKit::Middleware do
|
|
189
198
|
context "matching" do
|
190
199
|
specify do
|
191
200
|
get 'http://www.example.org/public/test.pdf'
|
192
|
-
last_response.headers["Content-Type"].
|
193
|
-
last_response.body.bytesize.
|
201
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
202
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
194
203
|
end
|
195
204
|
end
|
196
205
|
|
197
206
|
context "not matching" do
|
198
207
|
specify do
|
199
208
|
get 'http://www.example.org/secret/test.pdf'
|
200
|
-
last_response.headers["Content-Type"].
|
201
|
-
last_response.body.
|
209
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
210
|
+
expect(last_response.body).to eq("Hello world!")
|
202
211
|
end
|
203
212
|
end
|
204
213
|
end # one string
|
@@ -209,16 +218,16 @@ describe PDFKit::Middleware do
|
|
209
218
|
context "matching" do
|
210
219
|
specify do
|
211
220
|
get 'http://www.example.org/public/test.pdf'
|
212
|
-
last_response.headers["Content-Type"].
|
213
|
-
last_response.body.bytesize.
|
221
|
+
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
|
222
|
+
expect(last_response.body.bytesize).to eq(PDFKit.new("Hello world!").to_pdf.bytesize)
|
214
223
|
end
|
215
224
|
end
|
216
225
|
|
217
226
|
context "not matching" do
|
218
227
|
specify do
|
219
228
|
get 'http://www.example.org/secret/test.pdf'
|
220
|
-
last_response.headers["Content-Type"].
|
221
|
-
last_response.body.
|
229
|
+
expect(last_response.headers["Content-Type"]).to eq("text/html")
|
230
|
+
expect(last_response.body).to eq("Hello world!")
|
222
231
|
end
|
223
232
|
end
|
224
233
|
end # multiple string
|
@@ -230,7 +239,7 @@ describe PDFKit::Middleware do
|
|
230
239
|
before do
|
231
240
|
#make sure tests don't find an old test_save.pdf
|
232
241
|
File.delete('spec/test_save.pdf') if File.exists?('spec/test_save.pdf')
|
233
|
-
File.exists?('spec/test_save.pdf').
|
242
|
+
expect(File.exists?('spec/test_save.pdf')).to eq(false)
|
234
243
|
end
|
235
244
|
|
236
245
|
context "when header PDFKit-save-pdf is present" do
|
@@ -238,7 +247,7 @@ describe PDFKit::Middleware do
|
|
238
247
|
headers = { 'PDFKit-save-pdf' => 'spec/test_save.pdf' }
|
239
248
|
mock_app({}, {only: '/public'}, headers)
|
240
249
|
get 'http://www.example.org/public/test_save.pdf'
|
241
|
-
File.exists?('spec/test_save.pdf').
|
250
|
+
expect(File.exists?('spec/test_save.pdf')).to eq(true)
|
242
251
|
end
|
243
252
|
|
244
253
|
it "should not raise when target directory does not exist" do
|
@@ -246,7 +255,7 @@ describe PDFKit::Middleware do
|
|
246
255
|
mock_app({}, {only: '/public'}, headers)
|
247
256
|
expect {
|
248
257
|
get 'http://www.example.com/public/test_save.pdf'
|
249
|
-
}.not_to raise_error
|
258
|
+
}.not_to raise_error
|
250
259
|
end
|
251
260
|
end
|
252
261
|
|
@@ -254,7 +263,7 @@ describe PDFKit::Middleware do
|
|
254
263
|
it "should not saved the .pdf to disk" do
|
255
264
|
mock_app({}, {only: '/public'}, {} )
|
256
265
|
get 'http://www.example.org/public/test_save.pdf'
|
257
|
-
File.exists?('spec/test_save.pdf').
|
266
|
+
expect(File.exists?('spec/test_save.pdf')).to eq(false)
|
258
267
|
end
|
259
268
|
end
|
260
269
|
end
|
@@ -267,15 +276,15 @@ describe PDFKit::Middleware do
|
|
267
276
|
|
268
277
|
specify do
|
269
278
|
get 'http://www.example.org/public/file.pdf'
|
270
|
-
@env["PATH_INFO"].
|
271
|
-
@env["REQUEST_URI"].
|
272
|
-
@env["SCRIPT_NAME"].
|
279
|
+
expect(@env["PATH_INFO"]).to eq("/public/file")
|
280
|
+
expect(@env["REQUEST_URI"]).to eq("/public/file")
|
281
|
+
expect(@env["SCRIPT_NAME"]).to be_empty
|
273
282
|
end
|
274
283
|
specify do
|
275
284
|
get 'http://www.example.org/public/file.txt'
|
276
|
-
@env["PATH_INFO"].
|
277
|
-
@env["REQUEST_URI"].
|
278
|
-
@env["SCRIPT_NAME"].
|
285
|
+
expect(@env["PATH_INFO"]).to eq("/public/file.txt")
|
286
|
+
expect(@env["REQUEST_URI"]).to be_nil
|
287
|
+
expect(@env["SCRIPT_NAME"]).to be_empty
|
279
288
|
end
|
280
289
|
end
|
281
290
|
|
@@ -295,15 +304,15 @@ describe PDFKit::Middleware do
|
|
295
304
|
end
|
296
305
|
specify do
|
297
306
|
get 'http://example.org/sub/public/file.pdf'
|
298
|
-
@env["PATH_INFO"].
|
299
|
-
@env["REQUEST_URI"].
|
300
|
-
@env["SCRIPT_NAME"].
|
307
|
+
expect(@env["PATH_INFO"]).to eq("/sub/public/file")
|
308
|
+
expect(@env["REQUEST_URI"]).to eq("/sub/public/file")
|
309
|
+
expect(@env["SCRIPT_NAME"]).to eq("/example.org")
|
301
310
|
end
|
302
311
|
specify do
|
303
312
|
get 'http://example.org/sub/public/file.txt'
|
304
|
-
@env["PATH_INFO"].
|
305
|
-
@env["REQUEST_URI"].
|
306
|
-
@env["SCRIPT_NAME"].
|
313
|
+
expect(@env["PATH_INFO"]).to eq("/sub/public/file.txt")
|
314
|
+
expect(@env["REQUEST_URI"]).to be_nil
|
315
|
+
expect(@env["SCRIPT_NAME"]).to eq("/example.org")
|
307
316
|
end
|
308
317
|
end
|
309
318
|
|
@@ -319,25 +328,25 @@ describe PDFKit::Middleware do
|
|
319
328
|
it "should correctly parse relative url with single quotes" do
|
320
329
|
@body = %{<html><head><link href='/stylesheets/application.css' media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src="/test.png" /></body></html>}
|
321
330
|
body = @pdf.send :translate_paths, @body, @env
|
322
|
-
body.
|
331
|
+
expect(body).to eq("<html><head><link href='http://example.com/stylesheets/application.css' media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src=\"http://example.com/test.png\" /></body></html>")
|
323
332
|
end
|
324
333
|
|
325
334
|
it "should correctly parse relative url with double quotes" do
|
326
335
|
@body = %{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />}
|
327
336
|
body = @pdf.send :translate_paths, @body, @env
|
328
|
-
body.
|
337
|
+
expect(body).to eq("<link href=\"http://example.com/stylesheets/application.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />")
|
329
338
|
end
|
330
339
|
|
331
340
|
it "should correctly parse relative url with double quotes" do
|
332
341
|
@body = %{<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>}
|
333
342
|
body = @pdf.send :translate_paths, @body, @env
|
334
|
-
body.
|
343
|
+
expect(body).to eq("<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>")
|
335
344
|
end
|
336
345
|
|
337
346
|
it "should return the body even if there are no valid substitutions found" do
|
338
347
|
@body = "NO MATCH"
|
339
348
|
body = @pdf.send :translate_paths, @body, @env
|
340
|
-
body.
|
349
|
+
expect(body).to eq("NO MATCH")
|
341
350
|
end
|
342
351
|
end
|
343
352
|
|
@@ -353,7 +362,7 @@ describe PDFKit::Middleware do
|
|
353
362
|
it "should add the root_url" do
|
354
363
|
@body = %{<html><head><link href='/stylesheets/application.css' media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src="/test.png" /></body></html>}
|
355
364
|
body = @pdf.send :translate_paths, @body, @env
|
356
|
-
body.
|
365
|
+
expect(body).to eq("<html><head><link href='http://example.net/stylesheets/application.css' media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src=\"http://example.net/test.png\" /></body></html>")
|
357
366
|
end
|
358
367
|
|
359
368
|
after do
|
@@ -366,19 +375,18 @@ describe PDFKit::Middleware do
|
|
366
375
|
it "should not get stuck rendering each request as pdf" do
|
367
376
|
mock_app
|
368
377
|
# false by default. No requests.
|
369
|
-
@app.send(:rendering_pdf?).
|
378
|
+
expect(@app.send(:rendering_pdf?)).to eq(false)
|
370
379
|
|
371
380
|
# Remain false on a normal request
|
372
381
|
get 'http://www.example.org/public/file'
|
373
|
-
@app.send(:rendering_pdf?).
|
382
|
+
expect(@app.send(:rendering_pdf?)).to eq(false)
|
374
383
|
|
375
384
|
# Return true on a pdf request.
|
376
385
|
get 'http://www.example.org/public/file.pdf'
|
377
|
-
@app.send(:rendering_pdf?).
|
386
|
+
expect(@app.send(:rendering_pdf?)).to eq(true)
|
378
387
|
|
379
388
|
# Restore to false on any non-pdf request.
|
380
389
|
get 'http://www.example.org/public/file'
|
381
|
-
@app.send(:rendering_pdf?).
|
390
|
+
expect(@app.send(:rendering_pdf?)).to eq(false)
|
382
391
|
end
|
383
|
-
|
384
392
|
end
|