2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-11-20 20:10:08 +08:00
|
|
|
|
|
|
|
describe UrlHelper do
|
|
|
|
|
2018-12-11 15:03:13 +08:00
|
|
|
describe "#relaxed parse" do
|
|
|
|
|
|
|
|
it "can handle double #" do
|
|
|
|
url = UrlHelper.relaxed_parse("https://test.com#test#test")
|
|
|
|
expect(url.to_s).to eq("https://test.com#test%23test")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-20 20:10:08 +08:00
|
|
|
describe "#is_local" do
|
|
|
|
|
|
|
|
it "is true when the file has been uploaded" do
|
|
|
|
store = stub
|
|
|
|
store.expects(:has_been_uploaded?).returns(true)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.is_local("http://discuss.site.com/path/to/file.png")).to eq(true)
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is true for relative assets" do
|
|
|
|
store = stub
|
|
|
|
store.expects(:has_been_uploaded?).returns(false)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.is_local("/assets/javascripts/all.js")).to eq(true)
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
2019-04-14 01:35:23 +08:00
|
|
|
it "is true for relative assets for subfolders" do
|
|
|
|
store = stub
|
|
|
|
store.expects(:has_been_uploaded?).returns(false)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
2019-11-15 13:48:24 +08:00
|
|
|
|
|
|
|
set_subfolder "/subpath"
|
2019-04-14 01:35:23 +08:00
|
|
|
expect(UrlHelper.is_local("/subpath/assets/javascripts/all.js")).to eq(true)
|
|
|
|
end
|
|
|
|
|
2014-01-15 06:51:36 +08:00
|
|
|
it "is true for plugin assets" do
|
|
|
|
store = stub
|
|
|
|
store.expects(:has_been_uploaded?).returns(false)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.is_local("/plugins/all.js")).to eq(true)
|
2014-01-15 06:51:36 +08:00
|
|
|
end
|
|
|
|
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#absolute" do
|
|
|
|
|
2016-09-05 13:57:46 +08:00
|
|
|
it "returns an absolute URL for CDN" do
|
|
|
|
begin
|
|
|
|
Rails.configuration.action_controller.asset_host = "//cdn.awesome.com"
|
|
|
|
expect(UrlHelper.absolute("/test.jpg")).to eq("https://cdn.awesome.com/test.jpg")
|
|
|
|
|
|
|
|
Rails.configuration.action_controller.asset_host = "https://cdn.awesome.com"
|
|
|
|
expect(UrlHelper.absolute("/test.jpg")).to eq("https://cdn.awesome.com/test.jpg")
|
|
|
|
|
|
|
|
Rails.configuration.action_controller.asset_host = "http://cdn.awesome.com"
|
|
|
|
expect(UrlHelper.absolute("/test.jpg")).to eq("http://cdn.awesome.com/test.jpg")
|
|
|
|
ensure
|
|
|
|
Rails.configuration.action_controller.asset_host = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-20 20:10:08 +08:00
|
|
|
it "does not change non-relative url" do
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.absolute("http://www.discourse.org")).to eq("http://www.discourse.org")
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
2013-12-17 07:35:34 +08:00
|
|
|
it "changes a relative url to an absolute one using base url by default" do
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.absolute("/path/to/file")).to eq("http://test.localhost/path/to/file")
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
2013-12-17 07:35:34 +08:00
|
|
|
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")
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.absolute("/path/to/file")).to eq("http://my.cdn.com/path/to/file")
|
2013-12-17 07:35:34 +08:00
|
|
|
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")
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.absolute_without_cdn("/path/to/file")).to eq("http://test.localhost/path/to/file")
|
2013-12-17 07:35:34 +08:00
|
|
|
end
|
|
|
|
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#schemaless" do
|
|
|
|
|
2015-07-24 12:08:32 +08:00
|
|
|
it "removes http schemas only" do
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.schemaless("http://www.discourse.org")).to eq("//www.discourse.org")
|
2015-07-24 12:08:32 +08:00
|
|
|
expect(UrlHelper.schemaless("https://secure.discourse.org")).to eq("https://secure.discourse.org")
|
2015-06-12 18:02:36 +08:00
|
|
|
expect(UrlHelper.schemaless("ftp://ftp.discourse.org")).to eq("ftp://ftp.discourse.org")
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-12-13 00:50:39 +08:00
|
|
|
describe "#escape_uri" do
|
|
|
|
it "doesn't escape simple URL" do
|
|
|
|
url = UrlHelper.escape_uri('http://example.com/foo/bar')
|
|
|
|
expect(url).to eq('http://example.com/foo/bar')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "escapes unsafe chars" do
|
|
|
|
url = UrlHelper.escape_uri("http://example.com/?a=\11\15")
|
|
|
|
expect(url).to eq('http://example.com/?a=%09%0D')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "escapes non-ascii chars" do
|
|
|
|
url = UrlHelper.escape_uri('http://example.com/ماهی')
|
|
|
|
expect(url).to eq('http://example.com/%D9%85%D8%A7%D9%87%DB%8C')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't escape already escaped chars" do
|
|
|
|
url = UrlHelper.escape_uri('http://example.com/foo%20bar/foo bar/')
|
|
|
|
expect(url).to eq('http://example.com/foo%20bar/foo%20bar/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-14 14:09:16 +08:00
|
|
|
describe "#local_cdn_url" do
|
|
|
|
let(:url) { "/uploads/default/1X/575bcc2886bf7a39684b57ca90be85f7d399bbc7.png" }
|
|
|
|
let(:asset_host) { "//my.awesome.cdn" }
|
|
|
|
|
|
|
|
it "should return correct cdn url for local relative urls" do
|
2019-10-14 15:19:11 +08:00
|
|
|
set_cdn_url asset_host
|
2019-10-14 14:09:16 +08:00
|
|
|
cdn_url = UrlHelper.local_cdn_url(url)
|
|
|
|
expect(cdn_url).to eq("#{asset_host}#{url}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-20 20:10:08 +08:00
|
|
|
end
|