|
1 | 1 | require 'spec_helper' |
| 2 | +require 'ap' |
| 3 | +require 'uri' |
| 4 | +class Links |
| 5 | + def initialize(all_links,urls) |
| 6 | + @n = Array.new |
| 7 | + all_links.each do |link| |
| 8 | + if !link.text.nil? \ |
| 9 | + && !link.text.empty? \ |
| 10 | + && !link.path.nil? \ |
| 11 | + && !urls.include?(link[:href]) |
| 12 | + urls.push link[:href] |
| 13 | + @n.push(link) |
| 14 | + end |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + def each |
| 19 | + @n.each do |l| |
| 20 | + yield l |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def self.url_exists?(url, status) |
| 25 | + 10.times do |
| 26 | + begin |
| 27 | + res = Net::HTTP.get_response(URI(url)) |
| 28 | + status.replace res.code |
| 29 | + rescue OpenSSL::SSL::SSLError, SocketError |
| 30 | + return false |
| 31 | + end |
| 32 | + case res |
| 33 | + when Net::HTTPRedirection |
| 34 | + url = res['location'] |
| 35 | + when Net::HTTPSuccess |
| 36 | + return true |
| 37 | + else |
| 38 | + break |
| 39 | + end |
| 40 | + end |
| 41 | + false |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +def is_uri? url |
| 46 | + if URI(url).scheme |
| 47 | + true |
| 48 | + else |
| 49 | + false |
| 50 | + end |
| 51 | +end |
2 | 52 |
|
3 | 53 | # Array of all generated pages |
4 | 54 | site = File.join(File.dirname(__FILE__), '..', '_site', '**', '*.html') |
5 | 55 | PAGES = Dir.glob(site).map{ |p| p.gsub(/[^_]+\/_site(.*)/, '\\1') } |
6 | 56 |
|
7 | 57 | urls = ['http://localhost:4000', 'http://localhost'] |
8 | | -status='' |
| 58 | + |
9 | 59 | PAGES.each do |p| |
| 60 | + status='' |
10 | 61 | describe p do |
11 | 62 | it_behaves_like 'Page' |
12 | 63 | it_behaves_like 'Page with search box' unless p == '/search.html' |
|
16 | 67 | end |
17 | 68 |
|
18 | 69 | it 'has valid internal hyperlinks' do |
19 | | - page.all(:css, 'a').each do |link| |
20 | | - #do not check link, which isn't clickable or hasn't got any path |
21 | | - next if link.text.nil?\ |
22 | | - || link.text.empty?\ |
23 | | - || link[:href].match(/^http.*/)\ |
24 | | - || link.path.nil?\ |
25 | | - || urls.include?(link[:href]) |
26 | | - page.find(:xpath, link.path).click |
27 | | - urls.push link.path |
28 | | - expect(page.status_code).to be(200), "expected link '#{link.text}' to work" |
29 | | - visit p |
| 70 | + links = Links.new(page.all(:css, 'a'),urls) |
| 71 | + links.each do |link| |
| 72 | + url=link[:href] |
| 73 | + if !is_uri? url |
| 74 | + #ap [link.path, link.text, link[:href]] |
| 75 | + page.find(:xpath, link.path).click |
| 76 | + expect(page.status_code).to be(200), "expected link '#{link.text}' to work" |
| 77 | + end |
30 | 78 | end |
| 79 | + visit p |
31 | 80 | end |
32 | 81 |
|
33 | 82 | it 'has valid external hyperlinks' do |
34 | | - page.all(:css, 'a').each do |link| |
35 | | - next if link.text.nil?\ |
36 | | - || link.text.empty?\ |
37 | | - || !link[:href].match(/^http.*/)\ |
38 | | - || urls.include?(link[:href]) |
39 | | - urls.push(link[:href]) |
40 | | - result = url_exists? link[:href], status |
41 | | - expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error '#{status}')" |
42 | | - visit p |
| 83 | + links = Links.new(page.all(:css, 'a'),urls) |
| 84 | + links.each do |link| |
| 85 | + url=link[:href] |
| 86 | + if is_uri? url |
| 87 | + #ap [link.path, link.text, link[:href]] |
| 88 | + result = Links.url_exists? url |
| 89 | + expect(result).to be_truthy, "expected link '#{link.text}' => '#{link[:href]}' to work (Error code '#{status}')" |
| 90 | + end |
43 | 91 | end |
| 92 | + visit p |
44 | 93 | end |
45 | 94 | end |
46 | 95 | end |
0 commit comments