Skip to content

Commit 03d1df2

Browse files
committed
Merge pull request nanoc#297 from ddfreyne/bug-external-links-checker-ignores-query
Take query into account when validating external links
2 parents 3ca149d + e0eebd4 commit 03d1df2

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/nanoc/extra/checking/checks/external_links.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,22 @@ def validate(href)
136136
raise 'should not have gotten here'
137137
end
138138

139+
def path_for_url(url)
140+
if url.path.nil? || url.path.empty?
141+
path = '/'
142+
else
143+
path = url.path
144+
end
145+
146+
if url.query
147+
path << '?' << url.query
148+
end
149+
150+
path
151+
end
152+
139153
def request_url_once(url, req_method = Net::HTTP::Head)
140-
path = (url.path.nil? || url.path.empty? ? '/' : url.path)
141-
req = req_method.new(path)
154+
req = req_method.new(self.path_for_url(url))
142155
http = Net::HTTP.new(url.host, url.port)
143156
if url.instance_of? URI::HTTPS
144157
http.use_ssl = true

test/extra/checking/checks/test_external_links.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def check.request_url_once(url)
2121
end
2222
end
2323

24-
def test_valid?
24+
def test_valid_by_path
2525
with_site do |site|
2626
# Create check
2727
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)
@@ -36,6 +36,20 @@ def check.request_url_once(url)
3636
end
3737
end
3838

39+
def test_valid_by_query
40+
with_site do |site|
41+
# Create check
42+
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)
43+
def check.request_url_once(url)
44+
Net::HTTPResponse.new('1.1', url.query == 'status=200' ? '200' : '404', 'okay')
45+
end
46+
47+
# Test
48+
assert_nil check.validate('http://example.com/?status=200')
49+
refute_nil check.validate('http://example.com/?status=404')
50+
end
51+
end
52+
3953
def test_fallback_to_get_when_head_is_not_allowed
4054
with_site do |site|
4155
#Create check
@@ -50,4 +64,16 @@ def check.request_url_once(url, req_method = Net::HTTP::Head)
5064
end
5165
end
5266

67+
def test_path_for_url
68+
with_site do |site|
69+
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)
70+
71+
assert_equal '/', check.send(:path_for_url, URI.parse('http://example.com'))
72+
assert_equal '/', check.send(:path_for_url, URI.parse('http://example.com/'))
73+
assert_equal '/?foo=bar', check.send(:path_for_url, URI.parse('http://example.com?foo=bar'))
74+
assert_equal '/?foo=bar', check.send(:path_for_url, URI.parse('http://example.com/?foo=bar'))
75+
assert_equal '/meow?foo=bar', check.send(:path_for_url, URI.parse('http://example.com/meow?foo=bar'))
76+
end
77+
end
78+
5379
end

0 commit comments

Comments
 (0)