diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rb index e3ef92ee849..477cbd16ba0 100644 --- a/lib/inline_oneboxer.rb +++ b/lib/inline_oneboxer.rb @@ -77,6 +77,9 @@ class InlineOneboxer url, max_redirects: max_redirects, initial_https_redirect_ignore_limit: SiteSetting.block_onebox_on_redirect, + headers: { + "Accept-Language" => Oneboxer.accept_language, + }, ) title = nil if title && title.length < MIN_TITLE_LENGTH return onebox_for(url, title, opts) @@ -115,7 +118,7 @@ class InlineOneboxer end def self.cache_key(url) - "inline_onebox:#{url}" + "inline_onebox:#{Oneboxer.onebox_locale}:#{url}" end def self.post_author_for_title(topic, post_number) diff --git a/lib/retrieve_title.rb b/lib/retrieve_title.rb index 512c3b271ca..e550df7c166 100644 --- a/lib/retrieve_title.rb +++ b/lib/retrieve_title.rb @@ -8,11 +8,12 @@ module RetrieveTitle FinalDestination::UrlEncodingError, ] - def self.crawl(url, max_redirects: nil, initial_https_redirect_ignore_limit: false) + def self.crawl(url, max_redirects: nil, initial_https_redirect_ignore_limit: false, headers: {}) fetch_title( url, max_redirects: max_redirects, initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit, + headers: headers, ) rescue *UNRECOVERABLE_ERRORS # ¯\_(ツ)_/¯ @@ -70,7 +71,12 @@ module RetrieveTitle end # Fetch the beginning of a HTML document at a url - def self.fetch_title(url, max_redirects: nil, initial_https_redirect_ignore_limit: false) + def self.fetch_title( + url, + max_redirects: nil, + initial_https_redirect_ignore_limit: false, + headers: {} + ) fd = FinalDestination.new( url, @@ -78,9 +84,7 @@ module RetrieveTitle stop_at_blocked_pages: true, max_redirects: max_redirects, initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit, - headers: { - Accept: "text/html,*/*", - }, + headers: headers.merge({ Accept: "text/html,*/*" }), ) current = nil diff --git a/spec/lib/inline_oneboxer_spec.rb b/spec/lib/inline_oneboxer_spec.rb index ad32b243fc0..3ef2d8d6012 100644 --- a/spec/lib/inline_oneboxer_spec.rb +++ b/spec/lib/inline_oneboxer_spec.rb @@ -20,19 +20,19 @@ RSpec.describe InlineOneboxer do end describe "caching" do - fab!(:topic) + url = "https://example.com/good-url" - before { InlineOneboxer.invalidate(topic.url) } - - it "puts an entry in the cache" do + before do SiteSetting.enable_inline_onebox_on_all_domains = true - url = "https://example.com/good-url" stub_request(:get, url).to_return( status: 200, body: "a blog", ) InlineOneboxer.invalidate(url) + end + + it "puts an entry in the cache" do expect(InlineOneboxer.cache_lookup(url)).to be_blank result = InlineOneboxer.lookup(url) @@ -42,6 +42,34 @@ RSpec.describe InlineOneboxer do expect(cached[:url]).to eq(url) expect(cached[:title]).to eq("a blog") end + + it "separates cache by default_locale" do + expect(InlineOneboxer.cache_lookup(url)).to be_blank + + result = InlineOneboxer.lookup(url) + expect(result[:title]).to be_present + + cached = InlineOneboxer.cache_lookup(url) + expect(cached[:title]).to eq("a blog") + + SiteSetting.default_locale = "fr" + + expect(InlineOneboxer.cache_lookup(url)).to be_blank + end + + it "separates cache by onebox_locale, when set" do + expect(InlineOneboxer.cache_lookup(url)).to be_blank + + result = InlineOneboxer.lookup(url) + expect(result[:title]).to be_present + + cached = InlineOneboxer.cache_lookup(url) + expect(cached[:title]).to eq("a blog") + + SiteSetting.onebox_locale = "fr" + + expect(InlineOneboxer.cache_lookup(url)).to be_blank + end end describe ".lookup" do