From 5e6acf960183aea9440ce0d9e28c86f043e88c54 Mon Sep 17 00:00:00 2001 From: abcang Date: Wed, 5 Jul 2017 21:54:21 +0900 Subject: [PATCH] Fix Nokogiri::HTML at FetchLinkCardService (#4072) --- app/services/fetch_link_card_service.rb | 4 +++- spec/fixtures/requests/sjis.txt | 20 +++++++++++++++++++ spec/services/fetch_link_card_service_spec.rb | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/requests/sjis.txt diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 4ce221267d..8ddaa2bf40 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'nkf' class FetchLinkCardService < BaseService include HttpHelper @@ -84,7 +85,8 @@ class FetchLinkCardService < BaseService 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.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content diff --git a/spec/fixtures/requests/sjis.txt b/spec/fixtures/requests/sjis.txt new file mode 100644 index 0000000000..9041aa25d6 --- /dev/null +++ b/spec/fixtures/requests/sjis.txt @@ -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 + + + + + + JSISのページ + + +

SJISのページ
+

+ + diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 9df41cf559..7d7f8e7484 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -6,6 +6,8 @@ RSpec.describe FetchLinkCardService do before do 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(: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) subject.call(status) @@ -19,6 +21,14 @@ RSpec.describe FetchLinkCardService do expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once 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 context 'in a remote status' do