FIX: Topic#featured_link_root_domain extracts URL before parsing.

This commit is contained in:
Guo Xiang Tan
2017-12-04 10:00:07 +08:00
parent 846b047847
commit e73fbfe265
3 changed files with 16 additions and 8 deletions

View File

@ -133,7 +133,6 @@ class PostsController < ApplicationController
end
def create
@manager_params = create_params
@manager_params[:first_post_checks] = !is_api?

View File

@ -1267,7 +1267,14 @@ SQL
end
def featured_link_root_domain
MiniSuffix.domain(URI.parse(self.featured_link).hostname)
url = URI.extract(self.featured_link).first
begin
MiniSuffix.domain(URI.parse(url).hostname)
rescue URI::InvalidURIError => e
Rails.logger.warn("#{e.message}: #{e.backtrace.join("\n")}")
nil
end
end
private

View File

@ -2099,12 +2099,14 @@ describe Topic do
describe '#featured_link_root_domain' do
let(:topic) { Fabricate.build(:topic) }
it 'should extract the root domain correctly' do
[
"https://meta.discourse.org",
"https://meta.discourse.org/",
"https://meta.discourse.org/?filter=test"
"https://meta.discourse.org/?filter=test",
"https://meta.discourse.org/ there might be text",
"http://meta.discourse.org/\u0441",
].each do |featured_link|
it "should extract the root domain from #{featured_link} correctly" do
topic.featured_link = featured_link
expect(topic.featured_link_root_domain).to eq("discourse.org")
end