From 936aa38ff8dd3542de4ab00bfda7da5e508d6444 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 29 Jun 2018 10:39:58 -0700 Subject: [PATCH] Handle missing manifest link --- lib/manifique/web_client.rb | 29 ++++++++----------- spec/fixtures/mastodon-no-manifest.html | 37 +++++++++++++++++++++++++ spec/manifique/web_client_spec.rb | 26 ++++++++++++++++- 3 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 spec/fixtures/mastodon-no-manifest.html diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb index b039c4f..bad2004 100644 --- a/lib/manifique/web_client.rb +++ b/lib/manifique/web_client.rb @@ -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 diff --git a/spec/fixtures/mastodon-no-manifest.html b/spec/fixtures/mastodon-no-manifest.html new file mode 100644 index 0000000..3c576d3 --- /dev/null +++ b/spec/fixtures/mastodon-no-manifest.html @@ -0,0 +1,37 @@ + + + + + + + + + + + + kosmos.social + + + + + + + + + + + + + + + +
+ +
+ + diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index 33d6461..ffbbc49 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -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