From f011fbdca9a4882a2871e9a52ccc875d42c87c9f Mon Sep 17 00:00:00 2001
From: Aslak Knutsen <aslak@4fs.no>
Date: Thu, 27 Mar 2014 04:24:57 +0100
Subject: [PATCH] Normalize URL from Feed Entry after adding link to original
 in Topic

Since a URL might be case sensitive, adding a link to the original
Feed Entry with changed case to the Topic could end in 404.
---
 app/models/topic_embed.rb       |  4 ++--
 spec/models/topic_embed_spec.rb | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 552493ac620..8623571a4c8 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -14,13 +14,13 @@ class TopicEmbed < ActiveRecord::Base
   def self.import(user, url, title, contents)
     return unless url =~ /^https?\:\/\//
 
-    url = normalize_url(url)
-
     if SiteSetting.embed_truncate
       contents = first_paragraph_from(contents)
     end
     contents << "\n<hr>\n<small>#{I18n.t('embed.imported_from', link: "<a href='#{url}'>#{url}</a>")}</small>\n"
 
+    url = normalize_url(url)
+
     embed = TopicEmbed.where("lower(embed_url) = ?", url).first
     content_sha1 = Digest::SHA1.hexdigest(contents)
     post = nil
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 3db8aaf0738..da91e125739 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -41,6 +41,17 @@ describe TopicEmbed do
         post.cooked.should =~ /new contents/
       end
 
+      it "Should leave uppercase Feed Entry URL untouched in content" do
+        cased_url = 'http://eviltrout.com/ABCD'
+        post = TopicEmbed.import(user, cased_url, title, "some random content")
+        post.cooked.should =~ /#{cased_url}/
+      end
+
+      it "Should leave lowercase Feed Entry URL untouched in content" do
+        cased_url = 'http://eviltrout.com/abcd'
+        post = TopicEmbed.import(user, cased_url, title, "some random content")
+        post.cooked.should =~ /#{cased_url}/
+      end
     end
 
   end