-
Notifications
You must be signed in to change notification settings - Fork 61
WIP: Fix and add tests for website Links #73
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,61 @@ | ||
| require 'spec_helper' | ||
| class Links | ||
| def initialize(all_links,urls) | ||
| @n = Array.new | ||
| all_links.each do |link| | ||
| if !link.text.nil? \ | ||
| && !link.text.empty? \ | ||
| && !link.path.nil? \ | ||
| && !urls.include?(link[:href]) | ||
| urls.push link[:href] | ||
| @n.push(link) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def each | ||
| @n.each do |l| | ||
| yield l | ||
| end | ||
| end | ||
|
|
||
| def self.url_exists?(url, status) | ||
| 10.times do | ||
| begin | ||
| res = Net::HTTP.get_response(URI(url)) | ||
| status.replace res.code | ||
| rescue OpenSSL::SSL::SSLError, SocketError | ||
| return false | ||
| end | ||
| case res | ||
| when Net::HTTPRedirection | ||
| url = res['location'] | ||
| when Net::HTTPSuccess | ||
| return true | ||
| else | ||
| break | ||
| end | ||
| end | ||
| false | ||
| end | ||
| end | ||
|
|
||
| def is_uri? url | ||
| if URI(url).scheme | ||
| true | ||
| else | ||
| false | ||
| end | ||
| end | ||
|
|
||
| # Array of all generated pages | ||
| site = File.join(File.dirname(__FILE__), '..', '_site', '**', '*.html') | ||
| PAGES = Dir.glob(site).map{ |p| p.gsub(/[^_]+\/_site(.*)/, '\\1') } | ||
|
|
||
| urls = ['http://localhost:4000', 'http://localhost'] | ||
|
|
||
| PAGES.each do |p| | ||
| status='' | ||
| describe p do | ||
| it_behaves_like 'Page' | ||
| it_behaves_like 'Page with search box' unless p == '/search.html' | ||
|
|
@@ -13,13 +64,28 @@ | |
| visit p | ||
| end | ||
|
|
||
| it 'has only valid internal hyperlinks' do | ||
| page.all(:css, 'a').each do |link| | ||
| next if link.text == '' || link[:href].match(/(http|\/\/).*/) | ||
| page.find(:xpath, link.path).click | ||
| expect(page.status_code).to be(200), "expected link '#{link.text}' to work" | ||
| visit p | ||
| it 'has valid internal hyperlinks' do | ||
| links = Links.new(page.all(:css, 'a'),urls) | ||
| links.each do |link| | ||
| url=link[:href] | ||
| if !is_uri? url | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
| page.find(:xpath, link.path).click | ||
| expect(page.status_code).to be(200), "expected link '#{link.text}' to work" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
| end | ||
| end | ||
| visit p | ||
| end | ||
|
|
||
| it 'has valid external hyperlinks' do | ||
| links = Links.new(page.all(:css, 'a'),urls) | ||
| links.each do |link| | ||
| url=link[:href] | ||
| if is_uri? url | ||
| result = Links.url_exists? url | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking of it a bit more the method could return two values instead (you're not passing it anyway and using a global variable). |
||
| expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')" | ||
| end | ||
| end | ||
| visit p | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be made a
Linksobject class's instance variable.