From 2ce75a852328fd1d416b559510931e8cb067f631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 17 Dec 2013 00:35:34 +0100 Subject: [PATCH] FIX: canonical url should not use the CDN --- app/controllers/topics_controller.rb | 2 +- lib/url_helper.rb | 8 ++++++-- spec/components/url_helper_spec.rb | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 54625b51d55..e4da54da04c 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -59,7 +59,7 @@ class TopicsController < ApplicationController perform_show_response - canonical_url absolute(@topic_view.canonical_path) + canonical_url absolute_without_cdn(@topic_view.canonical_path) end def wordpress diff --git a/lib/url_helper.rb b/lib/url_helper.rb index 33f1b6ed4f4..caf8e0b67db 100644 --- a/lib/url_helper.rb +++ b/lib/url_helper.rb @@ -6,8 +6,12 @@ module UrlHelper url.start_with?(Discourse.asset_host || Discourse.base_url_no_prefix) end - def absolute(url) - url =~ /^\/[^\/]/ ? (Discourse.asset_host || Discourse.base_url_no_prefix) + url : url + def absolute(url, cdn = Discourse.asset_host) + url =~ /^\/[^\/]/ ? (cdn || Discourse.base_url_no_prefix) + url : url + end + + def absolute_without_cdn(url) + absolute(url, nil) end def schemaless(url) diff --git a/spec/components/url_helper_spec.rb b/spec/components/url_helper_spec.rb index a010af8e3bb..bc631ab02a0 100644 --- a/spec/components/url_helper_spec.rb +++ b/spec/components/url_helper_spec.rb @@ -33,10 +33,24 @@ describe UrlHelper do helper.absolute("http://www.discourse.org").should == "http://www.discourse.org" end - it "changes a relative url to an absolute one" do + it "changes a relative url to an absolute one using base url by default" do helper.absolute("/path/to/file").should == "http://test.localhost/path/to/file" end + it "changes a relative url to an absolute one using the cdn when enabled" do + Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com") + helper.absolute("/path/to/file").should == "http://my.cdn.com/path/to/file" + end + + end + + describe "#absolute_without_cdn" do + + it "changes a relative url to an absolute one using base url even when cdn is enabled" do + Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com") + helper.absolute_without_cdn("/path/to/file").should == "http://test.localhost/path/to/file" + end + end describe "#schemaless" do