From 6024e085a289b0aea7443e24156149410245f8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Akkaoui?= Date: Mon, 26 May 2025 17:36:57 +0200 Subject: [PATCH] Check encoding when parsing request content, to support emojis and non ASCII alphabets --- lib/redirect_follower.rb | 4 ++++ spec/lib/open_graph_spec.rb | 42 +++++++++++++++++++++++++++++++++++ spec/view/opengraph_uft8.html | 22 ++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 spec/view/opengraph_uft8.html diff --git a/lib/redirect_follower.rb b/lib/redirect_follower.rb index 2547995..1855e41 100644 --- a/lib/redirect_follower.rb +++ b/lib/redirect_follower.rb @@ -23,6 +23,10 @@ def resolve http.verify_mode = OpenSSL::SSL::VERIFY_PEER end + # Force encoding detection, particularly usefull for non ASCII webpages + # like in Japanese or containing emojis... + http.response_body_encoding = true + self.response = http.request_get(uri.request_uri, @headers) if response.kind_of?(Net::HTTPRedirection) diff --git a/spec/lib/open_graph_spec.rb b/spec/lib/open_graph_spec.rb index f3a70db..5c0e037 100644 --- a/spec/lib/open_graph_spec.rb +++ b/spec/lib/open_graph_spec.rb @@ -51,6 +51,48 @@ ] } end + + context "with utf-8 content" do + it "should get values from opengraph metadata" do + response = double(body: File.open("#{File.dirname(__FILE__)}/../view/opengraph_uft8.html", 'r') { |f| f.read }) + RedirectFollower.stub(:new) { double(resolve: response) } + + og = OpenGraph.new("http://test.host", false) + og.src.should == "http://test.host" + og.title.should == "OpenGraph Title 🎸" + og.type.should == "article" + og.url.should == "http://test.host" + og.description.should == "Rspec 用の OpenGraph サンプル サイト" + og.images.should == ["http://test.host/images/rock1.jpg", "http://test.host/images/rock2.jpg"] + og.original_images.should == ["http://test.host/images/rock1.jpg", "/images/rock2.jpg"] + og.metadata.should == { + title: [{_value: "OpenGraph Title 🎸"}], + type: [{_value: "article"}], + url: [{_value: "http://test.host"}], + description: [{_value: "Rspec 用の OpenGraph サンプル サイト"}], + image: [ + { + _value: "http://test.host/images/rock1.jpg", + width: [{ _value: "300" }], + height: [{ _value: "300" }] + }, + { + _value: "/images/rock2.jpg", + height: [{ _value: "1000" }] + } + ], + locale: [ + { + _value: "en_GB", + alternate: [ + { _value: "fr_FR" }, + { _value: "es_ES" } + ] + } + ] + } + end + end end context "with fallback" do diff --git a/spec/view/opengraph_uft8.html b/spec/view/opengraph_uft8.html new file mode 100644 index 0000000..7462e5f --- /dev/null +++ b/spec/view/opengraph_uft8.html @@ -0,0 +1,22 @@ + + + OpenGraph Title Fallback 😰 + + + + + + + + + + + + + + + + + + +