ruby-link-checker 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/README.md +153 -19
  4. data/UPGRADING.md +46 -0
  5. data/lib/ruby-link-checker/async/http/checker.rb +19 -6
  6. data/lib/ruby-link-checker/async/http/result.rb +1 -1
  7. data/lib/ruby-link-checker/cache.rb +90 -21
  8. data/lib/ruby-link-checker/checker.rb +136 -13
  9. data/lib/ruby-link-checker/config.rb +79 -0
  10. data/lib/ruby-link-checker/net/http/checker.rb +3 -2
  11. data/lib/ruby-link-checker/net/http/result.rb +1 -1
  12. data/lib/ruby-link-checker/redirect.rb +11 -0
  13. data/lib/ruby-link-checker/result.rb +96 -1
  14. data/lib/ruby-link-checker/tasks.rb +24 -12
  15. data/lib/ruby-link-checker/typhoeus/hydra/checker.rb +59 -11
  16. data/lib/ruby-link-checker/typhoeus/hydra/result.rb +1 -1
  17. data/lib/ruby-link-checker/version.rb +1 -1
  18. data/lib/ruby-link-checker.rb +1 -0
  19. metadata +18 -51
  20. data/.github/workflows/coverage.yml +0 -24
  21. data/.github/workflows/danger-comment.yml +0 -17
  22. data/.github/workflows/danger.yml +0 -12
  23. data/.github/workflows/lint.yml +0 -20
  24. data/.github/workflows/release.yml +0 -14
  25. data/.github/workflows/test.yml +0 -25
  26. data/.gitignore +0 -6
  27. data/.rspec +0 -2
  28. data/.rubocop.yml +0 -26
  29. data/.rubocop_todo.yml +0 -332
  30. data/AGENTS.md +0 -23
  31. data/CONTRIBUTING.md +0 -129
  32. data/Dangerfile +0 -6
  33. data/Gemfile +0 -27
  34. data/RELEASING.md +0 -37
  35. data/Rakefile +0 -26
  36. data/benchmarks/check.rb +0 -38
  37. data/benchmarks/data/opensearch.org/links.txt +0 -4270
  38. data/benchmarks/data/opensearch.org/small.txt +0 -100
  39. data/ruby-link-checker.gemspec +0 -22
  40. data/spec/fixtures/200.yml +0 -65
  41. data/spec/fixtures/301_200.yml +0 -102
  42. data/spec/fixtures/301_301.yml +0 -114
  43. data/spec/fixtures/301_400_301_200.yml +0 -185
  44. data/spec/fixtures/301_403.yml +0 -78
  45. data/spec/fixtures/308_429.yml +0 -108
  46. data/spec/fixtures/404.yml +0 -57
  47. data/spec/fixtures/404_200.yml +0 -89
  48. data/spec/fixtures/404_404.yml +0 -81
  49. data/spec/fixtures/429_200.yml +0 -135
  50. data/spec/fixtures/429_429_200.yml +0 -201
  51. data/spec/ruby-link-checker/async/http/checker_spec.rb +0 -184
  52. data/spec/ruby-link-checker/callbacks_spec.rb +0 -76
  53. data/spec/ruby-link-checker/checker_spec.rb +0 -97
  54. data/spec/ruby-link-checker/config_spec.rb +0 -45
  55. data/spec/ruby-link-checker/errors/redirect_loop_error_spec.rb +0 -18
  56. data/spec/ruby-link-checker/net/http/checker_spec.rb +0 -93
  57. data/spec/ruby-link-checker/result_error_spec.rb +0 -34
  58. data/spec/ruby-link-checker/result_spec.rb +0 -41
  59. data/spec/ruby-link-checker/task_spec.rb +0 -20
  60. data/spec/ruby-link-checker/tasks_spec.rb +0 -91
  61. data/spec/ruby-link-checker/typhoeus/hydra/checker_spec.rb +0 -180
  62. data/spec/ruby-link-checker/version_spec.rb +0 -9
  63. data/spec/spec_helper.rb +0 -19
  64. data/spec/support/config.rb +0 -8
  65. data/spec/support/link_checker.rb +0 -609
  66. data/spec/support/vcr.rb +0 -13
  67. data/spec/support/with_result.rb +0 -15
  68. data/spec/support/with_url.rb +0 -14
@@ -1,184 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'async'
5
- require 'async/http'
6
-
7
- describe LinkChecker::Async::HTTP::Checker do
8
- module AsyncTestLinkChecker
9
- class Task < LinkChecker::Async::HTTP::Task; end
10
-
11
- class LinkChecker < LinkChecker::Async::HTTP::Checker
12
- def check(url, options = {})
13
- super
14
- run
15
- end
16
- end
17
- end
18
-
19
- before :all do
20
- VCR.configure do |config|
21
- config.hook_into :webmock
22
- # async-http's WebMock adapter replays responses through a real HTTP/1
23
- # parser, which rejects a Transfer-Encoding header alongside a
24
- # Content-Length or an empty body. Let the adapter set Content-Length.
25
- config.before_playback do |interaction|
26
- interaction.response.headers.delete('Transfer-Encoding')
27
- interaction.response.headers.delete_if { |key, _value| key.downcase == 'content-length' }
28
- end
29
- end
30
- end
31
-
32
- after do
33
- LinkChecker::Async::HTTP::Config.reset
34
- end
35
-
36
- describe 'concurrency' do
37
- let(:checker) { described_class.new(max_concurrency: 2) }
38
- let(:counts) { [] }
39
- let(:completed) { [] }
40
-
41
- before do
42
- active = 0
43
- 5.times do
44
- checker._queue do
45
- counts << (active += 1)
46
- sleep(0.005)
47
- active -= 1
48
- completed << active
49
- end
50
- end
51
- end
52
-
53
- it 'bounds active work and waits for queued tasks' do
54
- checker.run
55
- expect(counts.max).to eq(2)
56
- expect(counts.size).to eq(5)
57
- expect(completed.size).to eq(5)
58
- expect(completed.last).to eq(0)
59
- end
60
-
61
- it 'rejects invalid limits' do
62
- expect { described_class.new(max_concurrency: 0) }.to raise_error ArgumentError
63
- end
64
-
65
- it 'reuses clients for the same endpoint' do
66
- expect(checker._client_for(URI('https://www.example.org/one'))).to equal(
67
- checker._client_for(URI('https://www.example.org/two'))
68
- )
69
- checker.run
70
- end
71
- end
72
-
73
- describe AsyncTestLinkChecker::LinkChecker do
74
- it_behaves_like 'a link checker'
75
-
76
- describe '.config' do
77
- it 'returns the Config module' do
78
- expect(LinkChecker::Async::HTTP.config).to eq LinkChecker::Async::HTTP::Config
79
- end
80
- end
81
-
82
- context 'with timeout options', vcr: { cassette_name: '200' } do
83
- before do
84
- LinkChecker::Async::HTTP.configure do |config|
85
- config.read_timeout = 5
86
- config.open_timeout = 10
87
- end
88
- expect(Async::HTTP::Endpoint).to receive(:parse).with(
89
- URI(url).to_s,
90
- hash_including(timeout: 10)
91
- ).and_call_original
92
- end
93
-
94
- include_context 'with url'
95
-
96
- it 'creates requests with a default timeout' do
97
- expect(result.success?).to be true
98
- end
99
- end
100
-
101
- context 'with a protocol option', vcr: { cassette_name: '200' } do
102
- before do
103
- LinkChecker::Async::HTTP.configure do |config|
104
- config.protocol = protocol
105
- end
106
- expect(Async::HTTP::Endpoint).to receive(:parse).with(
107
- URI(url).to_s,
108
- hash_including(protocol: expected_protocol)
109
- ).and_call_original
110
- end
111
-
112
- include_context 'with url'
113
-
114
- context 'http1' do
115
- let(:protocol) { :http1 }
116
- let(:expected_protocol) { Async::HTTP::Protocol::HTTP1 }
117
-
118
- it 'creates requests using HTTP/1' do
119
- expect(result.success?).to be true
120
- end
121
- end
122
-
123
- context 'http2' do
124
- let(:protocol) { :http2 }
125
- let(:expected_protocol) { Async::HTTP::Protocol::HTTP2 }
126
-
127
- it 'creates requests using HTTP/2' do
128
- expect(result.success?).to be true
129
- end
130
- end
131
- end
132
-
133
- context 'with an unsupported protocol option' do
134
- before do
135
- LinkChecker::Async::HTTP.configure do |config|
136
- config.protocol = :http3
137
- end
138
- end
139
-
140
- include_context 'with url'
141
-
142
- it 'raises an ArgumentError' do
143
- expect(result.error?).to be true
144
- expect(result.error.message).to eq('Unsupported protocol: :http3, expected :http1 or :http2')
145
- end
146
- end
147
-
148
- context 'timeout' do
149
- before do
150
- stub_request(:get, 'https://www.example.org/').to_timeout
151
- end
152
-
153
- include_context 'with url'
154
-
155
- around do |example|
156
- VCR.turned_off { example.run }
157
- end
158
-
159
- it 'times out' do
160
- expect(result.success?).to be false
161
- expect(result.error?).to be true
162
- end
163
-
164
- context 'with metadata' do
165
- let(:options) { { foo: :bar } }
166
-
167
- it 'times out' do
168
- expect(result.error?).to be true
169
- expect(result.options).to eq(foo: :bar)
170
- end
171
- end
172
- end
173
- end
174
-
175
- describe LinkChecker::Async::HTTP::Result do
176
- describe '#code' do
177
- it 'returns -1 when there is no response' do
178
- request = instance_double(Protocol::HTTP::Request)
179
- result = described_class.new(URI('https://www.example.org'), 'GET', nil, request, nil, {})
180
- expect(result.code).to eq(-1)
181
- end
182
- end
183
- end
184
- end
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::Callbacks do
6
- subject do
7
- Class.new do
8
- include LinkChecker::Callbacks
9
-
10
- def logger
11
- @logger ||= Logger.new(nil)
12
- end
13
- end.new
14
- end
15
-
16
- context 'one callback' do
17
- before do
18
- allow(subject).to receive(:called!)
19
- subject.on :foo do |data|
20
- subject.called! data
21
- end
22
- end
23
-
24
- it 'invokes callback' do
25
- 2.times { subject.send(:callback, :foo, :foo) }
26
- 2.times { subject.send(:callback, :bar, :bar) }
27
- expect(subject).to have_received(:called!).with(:foo).twice
28
- expect(subject).not_to have_received(:called!).with(:bar)
29
- end
30
- end
31
-
32
- context 'multiple callbacks' do
33
- before do
34
- allow(subject).to receive(:called!)
35
- subject.on :foo, :bar do |data|
36
- subject.called! data
37
- end
38
- end
39
-
40
- it 'invokes both callbacks' do
41
- 2.times { subject.send(:callback, :foo, :foo) }
42
- subject.send(:callback, :bar, :bar)
43
- expect(subject).to have_received(:called!).with(:foo).twice
44
- expect(subject).to have_received(:called!).with(:bar)
45
- end
46
- end
47
-
48
- context 'method_missing' do
49
- it 'invokes callback for a bang method' do
50
- allow(subject).to receive(:called!)
51
- subject.on :foo do |data|
52
- subject.called! data
53
- end
54
- subject.foo! :foo
55
- expect(subject).to have_received(:called!).with(:foo)
56
- end
57
-
58
- it 'raises NoMethodError for a non-bang method' do
59
- expect { subject.foo }.to raise_error NoMethodError
60
- end
61
- end
62
-
63
- context 'when a callback raises an error' do
64
- before do
65
- subject.on :foo do
66
- raise StandardError, 'oops'
67
- end
68
- end
69
-
70
- it 'rescues and logs the error' do
71
- allow(subject.logger).to receive(:error)
72
- expect(subject.send(:callback, :foo)).to be false
73
- expect(subject.logger).to have_received(:error)
74
- end
75
- end
76
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'spec_helper'
3
-
4
- describe LinkChecker::Checker do
5
- describe LinkChecker::Cache do
6
- let(:directory) { Dir.mktmpdir('link-checker-spec-') }
7
- let(:file) { File.join(directory, 'cache.json') }
8
- let(:cache) { described_class.new({ file: file, ttl: 60 }, logger: LinkChecker::Logger.default) }
9
- let(:result) do
10
- LinkChecker::Net::HTTP::Result.new(
11
- URI('https://www.example.org/final'), 'GET', URI('https://www.example.org/'),
12
- {}, Net::HTTPOK.new('1.1', '200', 'OK'), { location: 'private.html' }
13
- )
14
- end
15
-
16
- after do
17
- FileUtils.remove_entry(directory)
18
- end
19
-
20
- it 'expires successful entries using the configured TTL' do
21
- cache.store('key', result)
22
- allow(Time).to receive(:now).and_return(Time.now + 61)
23
- expect(cache.fetch('key', 'https://www.example.org/')).to be_nil
24
- end
25
-
26
- it 'retains the final URL and status without persisting metadata or headers' do
27
- cache.store('key', result)
28
- restored = cache.fetch('key', 'https://www.example.org/')
29
- expect(restored.result_uri.to_s).to eq('https://www.example.org/final')
30
- expect(restored.code).to eq(200)
31
- data = JSON.parse(File.read(file)).fetch('entries').fetch('key')
32
- expect(data.keys).to contain_exactly('checked_at', 'method', 'code', 'result_uri')
33
- end
34
-
35
- it 'merges writes from separate cache instances' do
36
- other_cache = described_class.new({ file: file, ttl: 60 }, logger: LinkChecker::Logger.default)
37
- cache.store('one', result)
38
- other_cache.store('two', result)
39
- expect(cache.fetch('one', 'https://www.example.org/')).not_to be_nil
40
- expect(cache.fetch('two', 'https://www.example.org/')).not_to be_nil
41
- end
42
-
43
- it 'recovers from corrupted cache files' do
44
- File.write(file, 'invalid JSON')
45
- expect(cache.fetch('key', 'https://www.example.org/')).to be_nil
46
- cache.store('key', result)
47
- expect(cache.fetch('key', 'https://www.example.org/').code).to eq(200)
48
- end
49
-
50
- it 'ignores malformed cache entries' do
51
- File.write(file, JSON.generate('version' => 1, 'entries' => { 'key' => true }))
52
- expect(cache.fetch('key', 'https://www.example.org/')).to be_nil
53
- end
54
-
55
- it 'does not store credentials in redirect URLs' do
56
- result.result_uri = URI('https://user:password@www.example.org/')
57
- cache.store('key', result)
58
- expect(File).not_to exist(file)
59
- end
60
-
61
- it 'requires an explicit file and TTL' do
62
- expect { described_class.new({}, logger: LinkChecker::Logger.default) }.to raise_error ArgumentError
63
- expect { described_class.new({ file: file }, logger: LinkChecker::Logger.default) }.to raise_error ArgumentError
64
- end
65
-
66
- it 'does not turn a cache write failure into a failed link' do
67
- allow(FileUtils).to receive(:mkdir_p).and_raise(Errno::EACCES)
68
- expect { cache.store('key', result) }.not_to raise_error
69
- end
70
- end
71
-
72
- context 'config' do
73
- it 'requires at least one method' do
74
- expect { described_class.new(methods: []) }.to raise_error ArgumentError, 'Missing methods.'
75
- end
76
-
77
- it 'rejects an invalid redirect limit' do
78
- expect { described_class.new(max_redirects: -1) }.to raise_error ArgumentError, 'Invalid redirect limit: -1'
79
- end
80
-
81
- it 'validates retry delays and backoff' do
82
- expect { described_class.new(retry_after: -1) }.to raise_error ArgumentError
83
- expect { described_class.new(retry_backoff: 0.5) }.to raise_error ArgumentError
84
- expect { described_class.new(retry_jitter: 2) }.to raise_error ArgumentError
85
- expect { described_class.new(max_retry_delay: Float::INFINITY) }.to raise_error ArgumentError
86
- end
87
-
88
- it 'rejects invalid retry status codes' do
89
- expect { described_class.new(retry_status_codes: ['503']) }.to raise_error ArgumentError
90
- end
91
-
92
- it 'allows per-instance disabling of global Retry-After handling' do
93
- LinkChecker.config.respect_retry_after = true
94
- expect(described_class.new(respect_retry_after: false).respect_retry_after).to be false
95
- end
96
- end
97
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'spec_helper'
3
-
4
- describe LinkChecker::Config do
5
- describe '#configure' do
6
- context 'methods' do
7
- before do
8
- LinkChecker.configure do |config|
9
- config.methods = %w[GET]
10
- end
11
- end
12
-
13
- it 'sets methods' do
14
- expect(LinkChecker.config.methods).to eq %w[GET]
15
- end
16
- end
17
-
18
- context 'retries' do
19
- it 'requires a positive integer' do
20
- expect { LinkChecker.config.retries = -1 }.to raise_error ArgumentError, 'Invalid number of retries: -1'
21
- end
22
-
23
- context 'max_redirects' do
24
- it 'rejects negative limits' do
25
- expect { LinkChecker.config.max_redirects = -1 }.to raise_error ArgumentError, 'Invalid redirect limit: -1'
26
- end
27
- end
28
- end
29
- end
30
-
31
- describe 'defaults' do
32
- it 'sets methods' do
33
- expect(LinkChecker.config.methods).to eq %w[HEAD GET]
34
- end
35
- it 'sets user agent' do
36
- expect(LinkChecker.config.user_agent).to eq "Ruby Link Checker/#{LinkChecker::VERSION}"
37
- end
38
- it 'does not set logger' do
39
- expect(LinkChecker.config.logger).to be nil
40
- end
41
- it 'sets retries' do
42
- expect(LinkChecker.config.retries).to eq 0
43
- end
44
- end
45
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::Errors::RedirectLoopError do
6
- subject(:error) do
7
- described_class.new(['https://www.example.org', 'https://www.example.org/redirect'])
8
- end
9
-
10
- it 'returns the last url' do
11
- expect(error.url).to eq 'https://www.example.org/redirect'
12
- end
13
-
14
- it 'formats a message with all urls' do
15
- expect(error.message)
16
- .to eq 'Redirect loop: https://www.example.org -> https://www.example.org/redirect.'
17
- end
18
- end
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::Net::HTTP::Checker do
6
- before :all do
7
- VCR.configure do |config|
8
- config.hook_into :webmock
9
- end
10
- end
11
-
12
- after do
13
- LinkChecker::Net::HTTP::Config.reset
14
- end
15
-
16
- it_behaves_like 'a link checker'
17
-
18
- context 'with an HTTP URL' do
19
- let(:checker) { described_class.new(methods: ['GET']) }
20
-
21
- around do |example|
22
- VCR.turned_off { example.run }
23
- end
24
-
25
- it 'does not enable TLS' do
26
- stub_request(:get, 'http://www.example.org/').to_return(status: 200)
27
- checker.check('http://www.example.org/')
28
- expect(checker.results[:success].first.code).to eq(200)
29
- expect(a_request(:get, 'http://www.example.org/')).to have_been_made.once
30
- end
31
- end
32
-
33
- describe '.config' do
34
- it 'returns the Config module' do
35
- expect(LinkChecker::Net::HTTP.config).to eq LinkChecker::Net::HTTP::Config
36
- end
37
- end
38
-
39
- context 'with timeout options', vcr: { cassette_name: '200' } do
40
- before do
41
- LinkChecker::Net::HTTP.configure do |config|
42
- config.read_timeout = 5
43
- config.open_timeout = 10
44
- end
45
- expect_any_instance_of(Net::HTTP).to receive(:read_timeout=).with(5)
46
- expect_any_instance_of(Net::HTTP).to receive(:open_timeout=).with(10)
47
- end
48
-
49
- include_context 'with url'
50
-
51
- it 'creates requests with a default timeout' do
52
- expect(result.success?).to be true
53
- end
54
- end
55
-
56
- context 'timeout' do
57
- before do
58
- stub_request(:get, 'https://www.example.org/').to_timeout
59
- end
60
-
61
- include_context 'with url'
62
-
63
- around do |example|
64
- VCR.turned_off { example.run }
65
- end
66
-
67
- it 'times out' do
68
- expect(result.success?).to be false
69
- expect(result.error?).to be true
70
- expect(result.to_s).to eq 'GET https://www.example.org : ERROR (Net::OpenTimeout)'
71
- end
72
-
73
- context 'with metadata' do
74
- let(:options) { { foo: :bar } }
75
-
76
- it 'times out' do
77
- expect(result.error?).to be true
78
- expect(result.options).to eq(foo: :bar)
79
- end
80
- end
81
- end
82
-
83
- describe LinkChecker::Net::HTTP::Result do
84
- describe '#code' do
85
- it 'returns -1 when the response raises' do
86
- response = instance_double(Net::HTTPResponse)
87
- allow(response).to receive(:code).and_raise(StandardError)
88
- result = described_class.new(URI('https://www.example.org'), 'GET', nil, nil, response, {})
89
- expect(result.code).to eq(-1)
90
- end
91
- end
92
- end
93
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::ResultError do
6
- subject(:result) do
7
- described_class.new('https://www.example.org', 'GET', 'https://www.example.org', StandardError.new('oops'))
8
- end
9
-
10
- it 'is an error' do
11
- expect(result.error?).to be true
12
- end
13
-
14
- it 'returns the error class name as code' do
15
- expect(result.code).to eq 'StandardError'
16
- end
17
-
18
- it 'classifies native timeouts without replacing the exception' do
19
- error = Net::ReadTimeout.new('timeout')
20
- result.error = error
21
- expect(result.error_type).to eq(:timeout)
22
- expect(result.error).to equal(error)
23
- end
24
-
25
- it 'classifies native connection failures' do
26
- result.error = Errno::ECONNREFUSED.new
27
- expect(result.error_type).to eq(:connection)
28
- end
29
-
30
- it 'does not classify programming errors as transient' do
31
- result.error = ArgumentError.new
32
- expect(result.error_type).to eq(:other)
33
- end
34
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::Result do
6
- subject(:result) do
7
- described_class.new('https://www.example.org', 'GET', 'https://www.example.org')
8
- end
9
-
10
- it 'defaults to not successful' do
11
- expect(result.success?).to be false
12
- end
13
-
14
- it 'defaults to not a failure' do
15
- expect(result.failure?).to be false
16
- end
17
-
18
- it 'defaults to not an error' do
19
- expect(result.error?).to be false
20
- end
21
-
22
- it 'defaults to not a redirect' do
23
- expect(result.redirect?).to be false
24
- end
25
-
26
- it 'defaults redirect_to to nil' do
27
- expect(result.redirect_to).to be_nil
28
- end
29
-
30
- it 'defaults request_headers to an empty hash' do
31
- expect(result.request_headers).to eq({})
32
- end
33
-
34
- it 'defaults code to nil' do
35
- expect(result.code).to be_nil
36
- end
37
-
38
- it 'defaults error to nil' do
39
- expect(result.error).to be_nil
40
- end
41
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe LinkChecker::Task do
6
- subject(:task) do
7
- described_class.new(
8
- LinkChecker::Net::HTTP::Checker.new,
9
- 'https://www.example.org',
10
- 'GET',
11
- 'https://www.example.org'
12
- )
13
- end
14
-
15
- describe '#run!' do
16
- it 'raises NotImplementedError' do
17
- expect { task.run! }.to raise_error NotImplementedError
18
- end
19
- end
20
- end