Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure host ends in a slash #245

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/sitemap_generator/sitemap_location.rb
Expand Up @@ -9,16 +9,27 @@ class SitemapLocation < Hash

PATH_OUTPUT_WIDTH = 47 # Character width of the path in the summary lines

[:host, :adapter].each do |method|
define_method(method) do
raise SitemapGenerator::SitemapError, "No value set for #{method}" unless self[method]
self[method]
end
def assert_value(key)
raise SitemapGenerator::SitemapError, "No value set for #{key}" unless self[key]
end

def assert_slash(key)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename append_slash

SitemapGenerator::Utilities.append_slash(self[key])
end

def adapter
assert_value(:adapter)
self[:adapter]
end

def host
assert_value(:host)
assert_slash(:host)
end

[:public_path, :sitemaps_path].each do |method|
define_method(method) do
Pathname.new(SitemapGenerator::Utilities.append_slash(self[method]))
Pathname.new(assert_slash(method))
end
end

Expand Down
24 changes: 12 additions & 12 deletions spec/sitemap_generator/link_set_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

describe SitemapGenerator::LinkSet do
let(:default_host) { 'http://example.com' }
let(:default_host) { 'http://example.com/' }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert all the changes to this file. The hostname must be able to be entered with or without a trailing slash, so having it omit the slash tests the code. And I'd rather not use URI.join in case that were to mask an issue with the joining of the path fragments.

let(:ls) { SitemapGenerator::LinkSet.new(:default_host => default_host) }

describe "initializer options" do
Expand Down Expand Up @@ -97,8 +97,8 @@

describe "sitemaps url" do
it "should change when the default_host is changed" do
ls.default_host = 'http://one.com'
ls.default_host.should == 'http://one.com'
ls.default_host = 'http://one.com/'
ls.default_host.should == 'http://one.com/'
ls.default_host.should == ls.sitemap.location.host
ls.default_host.should == ls.sitemap_index.location.host
end
Expand All @@ -121,7 +121,7 @@
describe "sitemap_index_url" do
it "should return the url to the index file" do
ls.default_host = default_host
ls.sitemap_index.location.url.should == "#{default_host}/sitemap.xml.gz"
ls.sitemap_index.location.url.should == URI.join(default_host, "/sitemap.xml.gz").to_s
ls.sitemap_index_url.should == ls.sitemap_index.location.url
end
end
Expand Down Expand Up @@ -215,7 +215,7 @@
end

describe "sitemaps host" do
let(:new_host) { 'http://wowza.com' }
let(:new_host) { 'http://wowza.com/' }

it "should have a host" do
ls.default_host = default_host
Expand Down Expand Up @@ -253,7 +253,7 @@

it "should not modify the index" do
@ls.sitemaps_host = 'http://newhost.com'
@ls.sitemap.location.host.should == 'http://newhost.com'
@ls.sitemap.location.host.should == 'http://newhost.com/'
@ls.sitemap_index.location.host.should == default_host
end

Expand Down Expand Up @@ -345,7 +345,7 @@
end

it "should set the default_host" do
host = 'http://defaulthost.com'
host = 'http://defaulthost.com/'
group = ls.group(:default_host => host)
group.default_host.should == host
group.sitemap.location.host.should == host
Expand All @@ -354,7 +354,7 @@

describe "sitemaps_host" do
it "should set the sitemaps host" do
@host = 'http://sitemaphost.com'
@host = 'http://sitemaphost.com/'
@group = ls.group(:sitemaps_host => @host)
@group.sitemaps_host.should == @host
@group.sitemap.location.host.should == @host
Expand Down Expand Up @@ -403,7 +403,7 @@
it "if only default_host is passed" do
group = ls.group(:default_host => 'http://newhost.com')
group.sitemap.should == ls.sitemap
group.sitemap.location.host.should == 'http://newhost.com'
group.sitemap.location.host.should == 'http://newhost.com/'
end
end

Expand Down Expand Up @@ -512,14 +512,14 @@
end

it "should set the default_host" do
host = 'http://defaulthost.com'
host = 'http://defaulthost.com/'
ls.create(:default_host => host)
ls.default_host.should == host
ls.sitemap.location.host.should == host
end

it "should set the sitemaps host" do
host = 'http://sitemaphost.com'
host = 'http://sitemaphost.com/'
ls.create(:sitemaps_host => host)
ls.sitemaps_host.should == host
ls.sitemap.location.host.should == host
Expand Down Expand Up @@ -590,7 +590,7 @@
end

describe "include_index?" do
let(:sitemaps_host) { 'http://amazon.com' }
let(:sitemaps_host) { 'http://amazon.com/' }

it "should be true if no sitemaps_host set, or it is the same" do
ls.include_index = true
Expand Down
11 changes: 8 additions & 3 deletions spec/sitemap_generator/sitemap_location_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

describe SitemapGenerator::SitemapLocation do
let(:default_host) { 'http://example.com' }
let(:default_host) { 'http://example.com/' }
let(:location) { SitemapGenerator::SitemapLocation.new }

it "public_path should default to the public directory in the application root" do
Expand Down Expand Up @@ -35,6 +35,11 @@
}.should raise_error
end

it "should append slash to host" do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just keep this change and revert the others.

location = SitemapGenerator::SitemapLocation.new(:filename => nil, :namer => nil, :host => "http://www.slash.com")
location.host.should == "http://www.slash.com/"
end

it "should accept a Namer option" do
@namer = SitemapGenerator::SimpleNamer.new(:xxx)
location = SitemapGenerator::SitemapLocation.new(:namer => @namer)
Expand Down Expand Up @@ -95,7 +100,7 @@
describe "when duplicated" do
it "should not inherit some objects" do
location = SitemapGenerator::SitemapLocation.new(:filename => 'xxx', :host => default_host, :public_path => 'public/')
location.url.should == default_host+'/xxx'
location.url.should == URI.join(default_host, '/xxx').to_s
location.public_path.to_s.should == 'public/'
dup = location.dup
dup.url.should == location.url
Expand Down Expand Up @@ -140,7 +145,7 @@
location = SitemapGenerator::SitemapLocation.new(
:public_path => 'public/google', :filename => 'xxx',
:host => default_host, :sitemaps_path => 'sub/dir')
location.url.should == default_host + '/sub/dir/xxx'
location.url.should == URI.join(default_host, '/sub/dir/xxx').to_s
end
end

Expand Down