mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-15 16:59:41 +00:00
Improve handling of encoding problems when creating link previews (#30929)
This commit is contained in:
17
spec/fixtures/requests/latin1_posing_as_utf8_broken.txt
vendored
Normal file
17
spec/fixtures/requests/latin1_posing_as_utf8_broken.txt
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
HTTP/1.1 200 OK
|
||||
server: nginx
|
||||
date: Thu, 13 Jun 2024 14:33:13 GMT
|
||||
content-type: text/html; charset=utf-8
|
||||
content-length: 158
|
||||
accept-ranges: bytes
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Tofu <20> l'orange</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Tofu <20> l'orange</h2>
|
||||
</body>
|
||||
</html>
|
||||
17
spec/fixtures/requests/latin1_posing_as_utf8_recoverable.txt
vendored
Normal file
17
spec/fixtures/requests/latin1_posing_as_utf8_recoverable.txt
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
HTTP/1.1 200 OK
|
||||
server: nginx
|
||||
date: Thu, 13 Jun 2024 14:33:13 GMT
|
||||
content-type: text/html; charset=utf-8
|
||||
content-length: 158
|
||||
accept-ranges: bytes
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Tofu with orange sauce</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Tofu <20> l'orange</h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -27,6 +27,8 @@ RSpec.describe FetchLinkCardService do
|
||||
stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
|
||||
stub_request(:get, 'http://example.com/windows-1251').to_return(request_fixture('windows-1251.txt'))
|
||||
stub_request(:get, 'http://example.com/low_confidence_latin1').to_return(request_fixture('low_confidence_latin1.txt'))
|
||||
stub_request(:get, 'http://example.com/latin1_posing_as_utf8_broken').to_return(request_fixture('latin1_posing_as_utf8_broken.txt'))
|
||||
stub_request(:get, 'http://example.com/latin1_posing_as_utf8_recoverable').to_return(request_fixture('latin1_posing_as_utf8_recoverable.txt'))
|
||||
stub_request(:get, 'http://example.com/aergerliche-umlaute').to_return(request_fixture('redirect_with_utf8_url.txt'))
|
||||
|
||||
Rails.cache.write('oembed_endpoint:example.com', oembed_cache) if oembed_cache
|
||||
@@ -159,10 +161,30 @@ RSpec.describe FetchLinkCardService do
|
||||
end
|
||||
|
||||
context 'with a URL of a page in ISO-8859-1 encoding, that charlock_holmes cannot detect' do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/low_confidence_latin1') }
|
||||
context 'when encoding in http header is correct' do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/low_confidence_latin1') }
|
||||
|
||||
it 'decodes the HTML' do
|
||||
expect(status.preview_card.title).to eq("Tofu á l'orange")
|
||||
it 'decodes the HTML' do
|
||||
expect(status.preview_card.title).to eq("Tofu á l'orange")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when encoding in http header is incorrect' do
|
||||
context 'when encoding problems appear in unrelated tags' do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/latin1_posing_as_utf8_recoverable') }
|
||||
|
||||
it 'decodes the HTML' do
|
||||
expect(status.preview_card.title).to eq('Tofu with orange sauce')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when encoding problems appear in title tag' do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/latin1_posing_as_utf8_broken') }
|
||||
|
||||
it 'does not create a preview card' do
|
||||
expect(status.preview_card).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user