mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 09:08:10 +08:00
DEV: Add isValidUrl
helper function to theme migrations (#26817)
This commit adds a `isValidUrl` helper function to the context in which theme migrations are ran in. This helper function is to make it easier for theme developers to check if a string is a valid URL or path when writing theme migrations. This can be helpful in cases when migrating a string based setting to `type: objects` which contain `type: string` properties with URL validations enabled. This commit also introduces the `UrlHelper.is_valid_url?` method which actually checks that the URL string is of the valid format instead of only checking if the URL string is parseable which is what `UrlHelper.relaxed_parse` does and is not sufficient for our needs.
This commit is contained in:

committed by
GitHub

parent
bfc0f3f4cd
commit
a6624af66e
@ -271,4 +271,53 @@ RSpec.describe UrlHelper do
|
||||
expect(described_class.rails_route_from_url(url)).to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".is_valid_url?" do
|
||||
it "should return true for a valid HTTP URL" do
|
||||
expect(described_class.is_valid_url?("http://www.example.com")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for a valid HTTPS URL" do
|
||||
expect(described_class.is_valid_url?("https://www.example.com")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for a valid FTP URL" do
|
||||
expect(described_class.is_valid_url?("ftp://example.com")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for a valid mailto URL" do
|
||||
expect(described_class.is_valid_url?("mailto:someone@discourse.org")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for a valid LDAP URL" do
|
||||
expect(described_class.is_valid_url?("ldap://ldap.example.com/dc=example;dc=com?quer")).to eq(
|
||||
true,
|
||||
)
|
||||
end
|
||||
|
||||
it "should return true for a path" do
|
||||
expect(described_class.is_valid_url?("/some/path")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for a path with query params" do
|
||||
expect(described_class.is_valid_url?("/some/path?query=param")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return true for anchor links" do
|
||||
expect(described_class.is_valid_url?("#anchor")).to eq(true)
|
||||
expect(described_class.is_valid_url?("#")).to eq(true)
|
||||
end
|
||||
|
||||
it "should return false for invalid urls" do
|
||||
expect(described_class.is_valid_url?("")).to eq(false)
|
||||
expect(described_class.is_valid_url?("http//www.example.com")).to eq(false)
|
||||
expect(described_class.is_valid_url?("http:/www.example.com")).to eq(false)
|
||||
expect(described_class.is_valid_url?("https:///www.example.com")).to eq(false)
|
||||
expect(described_class.is_valid_url?("mailtoooo:someone@discourse.org")).to eq(false)
|
||||
expect(described_class.is_valid_url?("ftp://")).to eq(false)
|
||||
expect(described_class.is_valid_url?("http://")).to eq(false)
|
||||
expect(described_class.is_valid_url?("https://")).to eq(false)
|
||||
expect(described_class.is_valid_url?("ldap://")).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user