Use charlock_holmes instead of nkf at FetchLinkCardService (#4080)
* Specs for language detection * Use CharlockHolmes instead of NKF * Correct mistakes * Correct style * Set hint_enc instead of falling back and strip_tags * Improve specs * Add dependencies
This commit is contained in:
parent
794781d121
commit
007ab330e6
11 changed files with 78 additions and 4 deletions
20
spec/fixtures/requests/koi8-r.txt
vendored
Normal file
20
spec/fixtures/requests/koi8-r.txt
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
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=koi8-r">
|
||||
<TITLE>íÏÓËÏ×Ñ ÎÁÞÉÎÁÅÔß ÔÏÌØËÏ ×ß XVI ÓÔ. ÐÒÉ×ÌÅËÁÔØ ×ÎÉÍÁÎÅ ÉÎÏÓÔÒÁÎÃÅ×ß.</TITLE>
|
||||
</HEAD>
|
||||
<BODY>
|
||||
<P><CENTER><B><FONT SIZE="+2">íÏÓËÏ×Ñ ÎÁÞÉÎÁÅÔß ÔÏÌØËÏ ×ß XVI ÓÔ. ÐÒÉ×ÌÅËÁÔØ ×ÎÉÍÁÎÅ ÉÎÏÓÔÒÁÎÃÅ×ß.</FONT></B><BR>
|
||||
<HR><BR>
|
||||
</BODY>
|
||||
</HTML>
|
4
spec/fixtures/requests/sjis.txt
vendored
4
spec/fixtures/requests/sjis.txt
vendored
|
@ -11,10 +11,10 @@ Accept-Ranges: bytes
|
|||
<HEAD>
|
||||
<META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0J Mac">
|
||||
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=x-sjis">
|
||||
<TITLE>JSISのページ</TITLE>
|
||||
<TITLE>SJISのページ</TITLE>
|
||||
</HEAD>
|
||||
<BODY>
|
||||
<P><CENTER><B><FONT SIZE="+2">SJISのページ</FONT></B><BR>
|
||||
<P><CENTER><B><FONT SIZE="+2">私も同年ましていわゆる記念人ってものの時でしありです。もし時間に意味者は正しくどんな発会ませだまでが申し上げがいらっしゃるたには参考帰るたいだから、少しにもやっあっましなた。金からいうないのはどうも九月をできるだけたたくた。けっして岡田さんに反抗幸少し徴に云おでしょ金力こうした権力あなたか指図がというお出入りなくだろなありて、その昔は私か金力陰を怒らから、久原さんのものをがたのいつがしかるにご希望と向いばそれmanにご矛盾へ参りように同時にご演説がしでならので、多分もし表裏に変ったてくれです事で考えたた。しかも例えばごがたがとどまらものも実際むやみとありですて、この自分では申しんてとして世間に並べのに行かなかっな。</FONT></B><BR>
|
||||
<HR><BR>
|
||||
</BODY>
|
||||
</HTML>
|
||||
|
|
20
spec/fixtures/requests/sjis_with_wrong_charset.txt
vendored
Normal file
20
spec/fixtures/requests/sjis_with_wrong_charset.txt
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
HTTP/1.1 200 OK
|
||||
Server: nginx/1.11.10
|
||||
Date: Tue, 04 Jul 2017 16:43:39 GMT
|
||||
Content-Type: text/html; charset=utf-8
|
||||
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>SJISのページ</TITLE>
|
||||
</HEAD>
|
||||
<BODY>
|
||||
<P><CENTER><B><FONT SIZE="+2">私も同年ましていわゆる記念人ってものの時でしありです。もし時間に意味者は正しくどんな発会ませだまでが申し上げがいらっしゃるたには参考帰るたいだから、少しにもやっあっましなた。金からいうないのはどうも九月をできるだけたたくた。けっして岡田さんに反抗幸少し徴に云おでしょ金力こうした権力あなたか指図がというお出入りなくだろなありて、その昔は私か金力陰を怒らから、久原さんのものをがたのいつがしかるにご希望と向いばそれmanにご矛盾へ参りように同時にご演説がしでならので、多分もし表裏に変ったてくれです事で考えたた。しかも例えばごがたがとどまらものも実際むやみとありですて、この自分では申しんてとして世間に並べのに行かなかっな。</FONT></B><BR>
|
||||
<HR><BR>
|
||||
</BODY>
|
||||
</HTML>
|
|
@ -8,6 +8,10 @@ RSpec.describe FetchLinkCardService do
|
|||
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, 'http://example.com/sjis_with_wrong_charset').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
|
||||
stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt'))
|
||||
stub_request(:head, 'http://example.com/koi8-r').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
|
||||
stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
|
||||
stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404)
|
||||
|
||||
subject.call(status)
|
||||
|
@ -27,6 +31,25 @@ RSpec.describe FetchLinkCardService do
|
|||
|
||||
it 'works with SJIS' do
|
||||
expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once
|
||||
expect(status.preview_card.title).to eq("SJISのページ")
|
||||
end
|
||||
end
|
||||
|
||||
context do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') }
|
||||
|
||||
it 'works with SJIS even with wrong charset header' do
|
||||
expect(a_request(:get, 'http://example.com/sjis_with_wrong_charset')).to have_been_made.at_least_once
|
||||
expect(status.preview_card.title).to eq("SJISのページ")
|
||||
end
|
||||
end
|
||||
|
||||
context do
|
||||
let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') }
|
||||
|
||||
it 'works with koi8-r' do
|
||||
expect(a_request(:get, 'http://example.com/koi8-r')).to have_been_made.at_least_once
|
||||
expect(status.preview_card.title).to eq("Московя начинаетъ только въ XVI ст. привлекать внимане иностранцевъ.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue