FIX: Don’t raise on some search terms

Currently, when certain search terms are provided, this can lead to
`Search.need_segmenting?` raising an error because it makes `URI#path`
to return `nil` instead of a string.

This patch forces a cast to string so it won’t raise anymore.
This commit is contained in:
Loïc Guitaut 2024-09-19 10:42:48 +02:00 committed by Loïc Guitaut
parent e68748318e
commit fe1098ebac
2 changed files with 9 additions and 1 deletions

View File

@ -228,7 +228,7 @@ class Search
def self.need_segmenting?(data)
return false if data.match?(/\A\d+\z/)
!URI.parse(data).path.start_with?("/")
!URI.parse(data).path.to_s.start_with?("/")
rescue URI::InvalidURIError
true
end

View File

@ -31,6 +31,14 @@ RSpec.describe Search do
it { is_expected.not_to be_need_segmenting(data) }
end
context "when data makes `URI#path` return `nil`" do
let(:data) { "in:solved%20category:50%20order:likes" }
it "doesnt raise an error" do
expect { search.need_segmenting?(data) }.not_to raise_error
end
end
context "when data is something else" do
let(:data) { "text" }