FIX: follow redirects for inline/mini onebox (#13512)

FIX: follow redirects for inline/mini onebox (#13512)

diff --git a/lib/retrieve_title.rb b/lib/retrieve_title.rb
index fd652b3..abab835 100644
--- a/lib/retrieve_title.rb
+++ b/lib/retrieve_title.rb
@@ -57,24 +57,26 @@ module RetrieveTitle
     encoding = nil
 
     fd.get do |_response, chunk, uri|
+      unless Net::HTTPRedirection === _response
+        if current
+          current << chunk
+        else
+          current = chunk
+        end
 
-      if current
-        current << chunk
-      else
-        current = chunk
-      end
-      if !encoding && content_type = _response['content-type']&.strip&.downcase
-        if content_type =~ /charset="?([a-z0-9_-]+)"?/
-          encoding = Regexp.last_match(1)
-          if !Encoding.list.map(&:name).map(&:downcase).include?(encoding)
-            encoding = nil
+        if !encoding && content_type = _response['content-type']&.strip&.downcase
+          if content_type =~ /charset="?([a-z0-9_-]+)"?/
+            encoding = Regexp.last_match(1)
+            if !Encoding.list.map(&:name).map(&:downcase).include?(encoding)
+              encoding = nil
+            end
           end
         end
-      end
 
-      max_size = max_chunk_size(uri) * 1024
-      title = extract_title(current, encoding)
-      throw :done if title || max_size < current.length
+        max_size = max_chunk_size(uri) * 1024
+        title = extract_title(current, encoding)
+        throw :done if title || max_size < current.length
+      end
     end
     title
   end
diff --git a/spec/components/retrieve_title_spec.rb b/spec/components/retrieve_title_spec.rb
index 56e80a4..a519e7e 100644
--- a/spec/components/retrieve_title_spec.rb
+++ b/spec/components/retrieve_title_spec.rb
@@ -89,5 +89,16 @@ describe RetrieveTitle do
       IPSocket.stubs(:getaddress).returns('100.2.3.4')
       expect(RetrieveTitle.crawl("https://brelksdjflaskfj.com/amazing")).to eq("japanese こんにちは website")
     end
+
+    it "can follow redirect" do
+      stub_request(:get, "http://foobar.com/amazing").
+        to_return(status: 301, body: "", headers: { "location" => "https://wikipedia.com/amazing" })
+
+      stub_request(:get, "https://wikipedia.com/amazing").
+        to_return(status: 200, body: "<html><title>very amazing</title>", headers: {})
+
+      IPSocket.stubs(:getaddress).returns('100.2.3.4')
+      expect(RetrieveTitle.crawl("http://foobar.com/amazing")).to eq("very amazing")
+    end
   end
 end

GitHub sha: 0adeddde610ad503de16541c90f6deb4df2f4245

This commit appears in #13512 which was approved by eviltrout. It was merged by techAPJ.

This commit has been mentioned on Discourse Meta. There might be relevant details there: