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

Take query into account when validating external links #297

Merged
merged 1 commit into from
Apr 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/nanoc/extra/checking/checks/external_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,22 @@ def validate(href)
raise 'should not have gotten here'
end

def path_for_url(url)
if url.path.nil? || url.path.empty?
path = '/'
else
path = url.path
end

if url.query
path << '?' << url.query
end

path
end

def request_url_once(url, req_method = Net::HTTP::Head)
path = (url.path.nil? || url.path.empty? ? '/' : url.path)
req = req_method.new(path)
req = req_method.new(self.path_for_url(url))
http = Net::HTTP.new(url.host, url.port)
if url.instance_of? URI::HTTPS
http.use_ssl = true
Expand Down
28 changes: 27 additions & 1 deletion test/extra/checking/checks/test_external_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def check.request_url_once(url)
end
end

def test_valid?
def test_valid_by_path
with_site do |site|
# Create check
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)
Expand All @@ -36,6 +36,20 @@ def check.request_url_once(url)
end
end

def test_valid_by_query
with_site do |site|
# Create check
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)
def check.request_url_once(url)
Net::HTTPResponse.new('1.1', url.query == 'status=200' ? '200' : '404', 'okay')
end

# Test
assert_nil check.validate('http://example.com/?status=200')
refute_nil check.validate('http://example.com/?status=404')
end
end

def test_fallback_to_get_when_head_is_not_allowed
with_site do |site|
#Create check
Expand All @@ -50,4 +64,16 @@ def check.request_url_once(url, req_method = Net::HTTP::Head)
end
end

def test_path_for_url
with_site do |site|
check = Nanoc::Extra::Checking::Checks::ExternalLinks.new(site)

assert_equal '/', check.send(:path_for_url, URI.parse('http://example.com'))
assert_equal '/', check.send(:path_for_url, URI.parse('http://example.com/'))
assert_equal '/?foo=bar', check.send(:path_for_url, URI.parse('http://example.com?foo=bar'))
assert_equal '/?foo=bar', check.send(:path_for_url, URI.parse('http://example.com/?foo=bar'))
assert_equal '/meow?foo=bar', check.send(:path_for_url, URI.parse('http://example.com/meow?foo=bar'))
end
end

end