diff --git a/lib/search/grouped_search_results.rb b/lib/search/grouped_search_results.rb index 88538f02ed2..f5239018877 100644 --- a/lib/search/grouped_search_results.rb +++ b/lib/search/grouped_search_results.rb @@ -78,13 +78,15 @@ class Search urls = Set.new cooked.scan(URI.regexp(%w{http https})) { urls << $& } - urls.each do |url| - case File.extname(URI(url).path || "") - when Oneboxer::VIDEO_REGEX - cooked.gsub!(url, I18n.t("search.video")) - when Oneboxer::AUDIO_REGEX - cooked.gsub!(url, I18n.t("search.audio")) + begin + case File.extname(URI(url).path || "") + when Oneboxer::VIDEO_REGEX + cooked.gsub!(url, I18n.t("search.video")) + when Oneboxer::AUDIO_REGEX + cooked.gsub!(url, I18n.t("search.audio")) + end + rescue URI::InvalidURIError end end diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb index c3aaa881300..7b81e27306d 100644 --- a/spec/lib/search_spec.rb +++ b/spec/lib/search_spec.rb @@ -28,7 +28,7 @@ describe Search do expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}") end - it "strips URLs correctly when blurb is" do + it "strips URLs correctly when blurb is longer than limit" do cooked = <<~RAW Here goes a test cooked with enough characters to hit the blurb limit. @@ -41,6 +41,13 @@ describe Search do expect(result).to eq("Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{I18n.t("search.audio")}") end + it "does not fail on bad URLs" do + cooked = <<~RAW + invalid URL: http:error] should not trip up blurb generation. + RAW + result = Search::GroupedSearchResults.blurb_for(cooked) + expect(result).to eq("invalid URL: http:error] should not trip up blurb generation.") + end end end