Handle missing manifest link

This commit is contained in:
Basti 2018-06-29 10:39:58 -07:00
parent 26836d68d0
commit 936aa38ff8
3 changed files with 74 additions and 18 deletions

View File

@ -12,11 +12,14 @@ module Manifique
@url = options[:url]
end
def fetch_web_manifest
def fetch_website
res = do_get_request @url
links = parse_http_link_header(res)
doc = Nokogiri::HTML(res.body)
manifest_url = discover_web_manifest_url(links, doc)
@links = parse_http_link_header(res)
@html = Nokogiri::HTML(res.body)
end
def fetch_web_manifest
return false unless manifest_url = discover_web_manifest_url(@html)
unless manifest_url.match(/^https?\:\/\//)
# Link is just the manifest path, not an absolute URL
@ -25,13 +28,7 @@ module Manifique
res = do_get_request manifest_url
begin
manifest_data = JSON.parse(res.body)
rescue
manifest_data = false
end
manifest_data
JSON.parse(res.body) rescue false
end
private
@ -54,12 +51,10 @@ module Manifique
link_parser.parse(response)
end
def discover_web_manifest_url(links, doc)
if url = doc.at_css("link[rel=manifest]").attributes["href"].value
return url
else
raise "No Web App Manifest found on #{@url}"
end
def discover_web_manifest_url(html)
html.at_css("link[rel=manifest]").attributes["href"].value
rescue
false
end
end
end

37
spec/fixtures/mastodon-no-manifest.html vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -66,12 +66,36 @@ RSpec.describe Manifique::WebClient do
let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social") }
subject { web_client.fetch_web_manifest }
subject do
web_client.fetch_website
web_client.fetch_web_manifest
end
it "fetches and returns the manifest" do
expect(subject).to be_kind_of(Hash)
expect(subject["name"]).to eq("kosmos.social")
end
end
context "no link[rel=manifest] element found" do
before do
index_html = File.read(File.join(__dir__, "..", "fixtures", "mastodon-no-manifest.html"));
stub_request(:get, "https://kosmos.social/").
to_return(body: index_html, status: 200, headers: {
"Content-Type": "text/html; charset=utf-8"
})
end
let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social") }
subject do
web_client.fetch_website
web_client.fetch_web_manifest
end
it "returns false" do
expect(subject).to be(false)
end
end
end
end