FIX: Hide excerpt of binary files in GitHub onebox (#15639)

Oneboxer did not know if a file is binary or not and always tried to
show an excerpt of the file.
This commit is contained in:
Bianca Nenciu
2022-01-19 14:45:36 +02:00
committed by GitHub
parent ffd0f5b500
commit 376799b1a4
3 changed files with 69 additions and 44 deletions

View File

@ -5,6 +5,7 @@ require "rails_helper"
describe Onebox::Engine::GithubBlobOnebox do
before do
@link = "https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb"
@uri = URI.parse(@link)
stub_request(:get, "https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb")
.to_return(status: 200, body: onebox_response(described_class.onebox_name))
end
@ -20,5 +21,16 @@ describe Onebox::Engine::GithubBlobOnebox do
it "includes blob contents" do
expect(html).to include("module Oneboxer")
end
it "does not include blob contents if it is binary" do
# stub_request if the response must be binary (ASCII-8BIT)
uri = mock('object')
uri.stubs(:open).returns(File.open("#{Rails.root}/spec/fixtures/pdf/small.pdf", "rb"))
URI.stubs(:parse).with(@link).returns(@uri)
URI.stubs(:parse).with('https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb').returns(uri)
expect(html).not_to include('/Pages')
expect(html).to include('This file is binary.')
end
end
end