download file within transact

This commit is contained in:
Kanezoh 2021-08-24 08:09:43 +09:00
parent d344fadc44
commit cfa824c909
2 changed files with 15 additions and 13 deletions

View File

@ -57,13 +57,6 @@ describe "Mechanize Agent test" do
agent2.history.pop.uri.to_s.should eq "http://example.com/form" agent2.history.pop.uri.to_s.should eq "http://example.com/form"
end end
it "can download page" do
agent = Mechanize.new
agent.download("http://example.com", "mechanizecr_example.html")
File.exists?("mechanizecr_example.html").should eq true
File.delete("mechanizecr_example.html")
end
it "doesn't add page to history within transact block" do it "doesn't add page to history within transact block" do
agent = Mechanize.new agent = Mechanize.new
agent.get("http://example.com/") agent.get("http://example.com/")
@ -73,4 +66,13 @@ describe "Mechanize Agent test" do
end end
agent.history.size.should eq 1 agent.history.size.should eq 1
end end
it "can download page" do
agent = Mechanize.new
agent.history.size.should eq 0
agent.download("http://example.com", "mechanizecr_example.html")
agent.history.size.should eq 0
File.exists?("mechanizecr_example.html").should eq true
File.delete("mechanizecr_example.html")
end
end end

View File

@ -130,13 +130,13 @@ class Mechanize
filename, filename,
headers = HTTP::Headers.new, headers = HTTP::Headers.new,
params : Hash(String, String | Array(String)) = Hash(String, String).new) params : Hash(String, String | Array(String)) = Hash(String, String).new)
transact do
page = get(uri, headers, params) page = get(uri, headers, params)
case page case page
when MechanizeCr::File when MechanizeCr::File
File.write(filename, page.body) File.write(filename, page.body)
end end
page end
end end
# Runs given block, then resets the page history as it was before. # Runs given block, then resets the page history as it was before.