Commit 5e6acf96 authored by abcang's avatar abcang Committed by Eugen Rochko
Browse files

Fix Nokogiri::HTML at FetchLinkCardService (#4072)

parent b52a5e6b
# frozen_string_literal: true # frozen_string_literal: true
require 'nkf'
class FetchLinkCardService < BaseService class FetchLinkCardService < BaseService
include HttpHelper include HttpHelper
...@@ -84,7 +85,8 @@ class FetchLinkCardService < BaseService ...@@ -84,7 +85,8 @@ class FetchLinkCardService < BaseService
return if response.code != 200 || response.mime_type != 'text/html' return if response.code != 200 || response.mime_type != 'text/html'
page = Nokogiri::HTML(response.to_s) html = response.to_s
page = Nokogiri::HTML(html, nil, NKF.guess(html).to_s)
card.type = :link card.type = :link
card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content
......
HTTP/1.1 200 OK
Server: nginx/1.11.10
Date: Tue, 04 Jul 2017 16:43:39 GMT
Content-Type: text/html
Content-Length: 273
Connection: keep-alive
Last-Modified: Tue, 04 Jul 2017 16:41:34 GMT
Accept-Ranges: bytes
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0J Mac">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=x-sjis">
<TITLE>JSIS̃y[W</TITLE>
</HEAD>
<BODY>
<P><CENTER><B><FONT SIZE="+2">SJIS̃y[W</FONT></B><BR>
<HR><BR>
</BODY>
</HTML>
...@@ -6,6 +6,8 @@ RSpec.describe FetchLinkCardService do ...@@ -6,6 +6,8 @@ RSpec.describe FetchLinkCardService do
before do before do
stub_request(:head, 'http://example.xn--fiqs8s/').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) stub_request(:head, 'http://example.xn--fiqs8s/').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt')) stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt'))
stub_request(:head, 'http://example.com/sjis').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt'))
stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404) stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404)
subject.call(status) subject.call(status)
...@@ -19,6 +21,14 @@ RSpec.describe FetchLinkCardService do ...@@ -19,6 +21,14 @@ RSpec.describe FetchLinkCardService do
expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once
end end
end end
context do
let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') }
it 'works with SJIS' do
expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once
end
end
end end
context 'in a remote status' do context 'in a remote status' do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment