2017-07-21 15:29:04 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
require_dependency 'retrieve_title'
|
|
|
|
|
|
|
|
describe RetrieveTitle do
|
|
|
|
|
|
|
|
context "extract_title" do
|
|
|
|
|
|
|
|
it "will extract the value from the title tag" do
|
|
|
|
title = RetrieveTitle.extract_title(
|
|
|
|
"<html><title>My Cool Title</title></html>"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(title).to eq("My Cool Title")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will strip whitespace" do
|
|
|
|
title = RetrieveTitle.extract_title(
|
|
|
|
"<html><title> Another Title\n\n </title></html>"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(title).to eq("Another Title")
|
|
|
|
end
|
|
|
|
|
2017-08-02 14:27:21 -04:00
|
|
|
it "will pick og:title if title is missing" do
|
|
|
|
title = RetrieveTitle.extract_title(<<~HTML
|
|
|
|
<html>
|
|
|
|
<meta property="og:title" content="Good Title"
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(title).to eq("Good Title")
|
|
|
|
end
|
|
|
|
|
2017-07-21 15:29:04 -04:00
|
|
|
it "will prefer the title from an opengraph tag" do
|
|
|
|
title = RetrieveTitle.extract_title(<<~HTML
|
|
|
|
<html>
|
2017-08-02 14:27:21 -04:00
|
|
|
<title>Good Title</title>
|
|
|
|
<meta property="og:title" content="Bad Title"
|
2017-07-21 15:29:04 -04:00
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(title).to eq("Good Title")
|
|
|
|
end
|
|
|
|
|
2017-09-28 09:29:50 -04:00
|
|
|
it "will parse a YouTube url from javascript" do
|
|
|
|
title = RetrieveTitle.extract_title(<<~HTML
|
|
|
|
<html>
|
|
|
|
<title>YouTube</title>
|
|
|
|
<script>document.title = "Video Title";</script>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
)
|
|
|
|
expect(title).to eq("Video Title")
|
|
|
|
end
|
|
|
|
|
2017-07-21 15:29:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|