Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/redirect_follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions spec/lib/open_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/view/opengraph_uft8.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title>OpenGraph Title Fallback 😰</title>
<meta property="og:title" content="OpenGraph Title 🎸" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://test.host" />
<meta property="og:description" content="Rspec 用の OpenGraph サンプル サイト" />
<meta property="og:image" content="http://test.host/images/rock1.jpg" />
<meta property="og:image:width" content="300" />
<meta property="og:image:height" content="300" />
<meta property="og:image" content="/images/rock2.jpg" />
<meta property="og:image:height" content="1000" />
<meta property="og:locale" content="en_GB" />
<meta property="og:locale:alternate" content="fr_FR" />
<meta property="og:locale:alternate" content="es_ES" />
<meta name="description" content="Short Description Fallback" />
</head>
<body>
<img src="http://test.host/images/wall1.jpg" />
<img src="http://test.host/images/wall2.jpg" />
</body>
</html>