Skip to content

Commit 7f4a6bc

Browse files
authored
Merge pull request #259 from rubycdp/download-file
Send real file and check for absolute path
2 parents e2e6629 + 61dcc72 commit 7f4a6bc

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

lib/ferrum/page.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,16 @@ def prepare_page
262262
end
263263

264264
if @browser.options[:save_path]
265-
command("Browser.setDownloadBehavior", behavior: "allow", downloadPath: @browser.options[:save_path], eventsEnabled: true)
265+
unless Pathname.new(@browser.options[:save_path]).absolute?
266+
raise Error, "supply absolute path as `:save_path` option"
267+
end
268+
269+
@browser.command("Browser.setDownloadBehavior",
270+
downloadPath: browser.options[:save_path],
271+
behavior: "allow", eventsEnabled: true)
272+
command("Page.setDownloadBehavior",
273+
downloadPath: browser.options[:save_path],
274+
behavior: "allow")
266275
end
267276

268277
@browser.extensions.each do |extension|

spec/download_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@
33
module Ferrum
44
describe Browser do
55
context "download support" do
6+
let(:filename) { "attachment.pdf" }
67
let(:browser) do
78
Ferrum::Browser.new(
89
base_url: Ferrum::Server.server.base_url,
9-
save_path: "/tmp/ferrum"
10+
save_path: save_path
1011
)
1112
end
1213

13-
it "saves an attachment" do
14-
browser.go_to("/attachment.pdf")
14+
context "absolute path" do
15+
let(:save_path) { "/tmp/ferrum" }
1516

16-
expect(File.exist?("/tmp/ferrum/attachment.pdf")).to be true
17+
it "saves an attachment" do
18+
browser.go_to("/#{filename}")
19+
20+
expect(File.exist?("#{save_path}/#{filename}")).to be true
21+
ensure
22+
FileUtils.rm_rf(save_path)
23+
end
24+
end
25+
26+
context "local path" do
27+
let(:save_path) { "spec/tmp" }
28+
29+
it "raises an error" do
30+
expect do
31+
browser.go_to("/#{filename}")
32+
end.to raise_error(Ferrum::Error, "supply absolute path as `:save_path` option")
33+
end
1734
end
1835
end
1936
end

spec/support/application.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ def initialize(something, message)
183183
end
184184

185185
get "/attachment.pdf" do
186-
path = "/tmp/ferrum/attachment.pdf"
187-
FileUtils.touch(path)
188-
attachment(path, :attachment)
186+
attachment("attachment.pdf")
187+
send_file("attachment.pdf")
189188
end
190189

191190
get "/:view" do |view|

spec/support/public/attachment.pdf

4.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)