FIX: FileHelper should prioritize response content-type.

Request to a URL with `.png` extension may return a jpg
instead causing us to attach the wrong extension to an
upload.
This commit is contained in:
Guo Xiang Tan
2018-07-30 10:48:44 +08:00
parent fc3b904e1f
commit b94633e844
3 changed files with 29 additions and 6 deletions

View File

@ -12,7 +12,6 @@ describe FileHelper do
end
describe "download" do
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")
@ -69,6 +68,24 @@ describe FileHelper do
)
expect(tmpfile.read[0..5]).to eq("GIF89a")
end
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
stub_request(:get, url).to_return(body: png, headers: {
"content-type": "image/png"
})
tmpfile = FileHelper.download(
url,
max_file_size: 10000,
tmp_file_name: 'trouttmp'
)
expect(File.extname(tmpfile)).to eq('.png')
end
end
end
end