2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-15 16:56:29 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'file_helper'
|
|
|
|
|
|
|
|
describe FileHelper do
|
|
|
|
|
|
|
|
let(:url) { "https://eviltrout.com/trout.png" }
|
2018-08-07 00:01:58 -04:00
|
|
|
let(:png) { File.read("#{Rails.root}/spec/fixtures/images/cropped.png") }
|
2017-05-15 16:56:29 -04:00
|
|
|
|
|
|
|
before do
|
2017-05-26 03:19:09 -04:00
|
|
|
stub_request(:any, /https:\/\/eviltrout.com/)
|
2017-05-15 16:56:29 -04:00
|
|
|
stub_request(:get, url).to_return(body: png)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "download" do
|
2017-09-28 02:35:27 -04:00
|
|
|
it "correctly raises an OpenURI HTTP error if it gets a 404 even with redirect" do
|
|
|
|
url = "http://fourohfour.com/404"
|
|
|
|
stub_request(:get, url).to_return(status: 404, body: "404")
|
|
|
|
|
|
|
|
expect do
|
|
|
|
begin
|
|
|
|
FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp',
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
expect(e.io.status[0]).to eq("404")
|
|
|
|
raise
|
|
|
|
end
|
2018-09-13 03:43:58 -04:00
|
|
|
end.to raise_error(OpenURI::HTTPError, "404 Error: 404")
|
2017-09-28 02:35:27 -04:00
|
|
|
end
|
|
|
|
|
2019-05-27 20:28:57 -04:00
|
|
|
it "does not follow redirects if instructed not to" do
|
|
|
|
url2 = "https://test.com/image.png"
|
|
|
|
stub_request(:get, url).to_return(status: 302, body: "", headers: { location: url2 })
|
|
|
|
|
|
|
|
missing = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp',
|
|
|
|
follow_redirect: false
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(missing).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does follow redirects if instructed to" do
|
|
|
|
url2 = "https://test.com/image.png"
|
|
|
|
stub_request(:get, url).to_return(status: 302, body: "", headers: { location: url2 })
|
|
|
|
stub_request(:get, url2).to_return(status: 200, body: "i am the body")
|
|
|
|
|
2019-05-27 21:33:08 -04:00
|
|
|
begin
|
|
|
|
found = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp',
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2019-05-27 20:28:57 -04:00
|
|
|
|
2019-05-27 21:33:08 -04:00
|
|
|
expect(found.read).to eq("i am the body")
|
|
|
|
ensure
|
|
|
|
found&.close
|
|
|
|
found&.unlink
|
|
|
|
end
|
2019-05-27 20:28:57 -04:00
|
|
|
end
|
|
|
|
|
2017-09-28 02:35:27 -04:00
|
|
|
it "correctly raises an OpenURI HTTP error if it gets a 404" do
|
|
|
|
url = "http://fourohfour.com/404"
|
|
|
|
|
|
|
|
stub_request(:get, url).to_return(status: 404, body: "404")
|
|
|
|
|
|
|
|
expect do
|
|
|
|
begin
|
|
|
|
FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp',
|
|
|
|
follow_redirect: false
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
expect(e.io.status[0]).to eq("404")
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end.to raise_error(OpenURI::HTTPError)
|
|
|
|
end
|
|
|
|
|
2017-05-15 16:56:29 -04:00
|
|
|
it "returns a file with the image" do
|
2018-08-17 05:34:25 -04:00
|
|
|
begin
|
|
|
|
tmpfile = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png))
|
|
|
|
ensure
|
|
|
|
tmpfile&.close
|
2019-05-27 21:33:08 -04:00
|
|
|
tmpfile&.unlink
|
2018-08-17 05:34:25 -04:00
|
|
|
end
|
2017-05-15 16:56:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works with a protocol relative url" do
|
2018-08-17 05:34:25 -04:00
|
|
|
begin
|
|
|
|
tmpfile = FileHelper.download(
|
|
|
|
"//eviltrout.com/trout.png",
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png))
|
|
|
|
ensure
|
|
|
|
tmpfile&.close
|
2019-05-27 21:33:08 -04:00
|
|
|
tmpfile&.unlink
|
2018-08-17 05:34:25 -04:00
|
|
|
end
|
2017-05-15 16:56:29 -04:00
|
|
|
end
|
2018-07-29 22:48:44 -04:00
|
|
|
|
2018-08-17 04:17:58 -04:00
|
|
|
describe 'when max_file_size is exceeded' do
|
|
|
|
it 'should return nil' do
|
|
|
|
tmpfile = FileHelper.download(
|
|
|
|
"//eviltrout.com/trout.png",
|
|
|
|
max_file_size: 1,
|
|
|
|
tmp_file_name: 'trouttmp'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tmpfile).to eq(nil)
|
|
|
|
end
|
2018-08-17 04:52:55 -04:00
|
|
|
|
|
|
|
it 'is able to retain the tmpfile' do
|
2018-08-17 05:34:25 -04:00
|
|
|
begin
|
|
|
|
tmpfile = FileHelper.download(
|
|
|
|
"//eviltrout.com/trout.png",
|
|
|
|
max_file_size: 1,
|
|
|
|
tmp_file_name: 'trouttmp',
|
|
|
|
retain_on_max_file_size_exceeded: true
|
|
|
|
)
|
2018-08-17 04:52:55 -04:00
|
|
|
|
2018-08-17 05:34:25 -04:00
|
|
|
expect(tmpfile.closed?).to eq(false)
|
|
|
|
ensure
|
|
|
|
tmpfile&.close
|
2019-05-27 21:33:08 -04:00
|
|
|
tmpfile&.unlink
|
2018-08-17 05:34:25 -04:00
|
|
|
end
|
2018-08-17 04:52:55 -04:00
|
|
|
end
|
2018-08-17 04:17:58 -04:00
|
|
|
end
|
|
|
|
|
2018-07-29 22:48:44 -04:00
|
|
|
describe 'when url is a jpeg' do
|
|
|
|
let(:url) { "https://eviltrout.com/trout.jpg" }
|
|
|
|
|
|
|
|
it "should prioritize the content type returned by the response" do
|
2018-08-17 05:34:25 -04:00
|
|
|
begin
|
|
|
|
stub_request(:get, url).to_return(body: png, headers: {
|
|
|
|
"content-type": "image/png"
|
|
|
|
})
|
2018-07-29 22:48:44 -04:00
|
|
|
|
2018-08-17 05:34:25 -04:00
|
|
|
tmpfile = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: 10000,
|
|
|
|
tmp_file_name: 'trouttmp'
|
|
|
|
)
|
2018-07-29 22:48:44 -04:00
|
|
|
|
2018-08-17 05:34:25 -04:00
|
|
|
expect(File.extname(tmpfile)).to eq('.png')
|
|
|
|
ensure
|
|
|
|
tmpfile&.close
|
2019-05-27 21:33:08 -04:00
|
|
|
tmpfile&.unlink
|
2018-08-17 05:34:25 -04:00
|
|
|
end
|
2018-07-29 22:48:44 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-15 16:56:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|