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,609 +0,0 @@
1
- shared_context 'a link checker' do
2
- context 'with redirects', :aggregate_failures do
3
- let(:checker) { described_class.new(methods: ['GET'], max_redirects: 2) }
4
- let(:url) { 'https://www.example.org/start' }
5
- let(:result) { checker.results.values.flatten.first }
6
-
7
- around do |example|
8
- VCR.turned_off { example.run }
9
- end
10
-
11
- it 'resolves each relative redirect against the current URL' do
12
- stub_request(:any, url).to_return(status: 302, headers: { 'Location' => '/nested/next' })
13
- stub_request(:any, 'https://www.example.org/nested/next').to_return(status: 302, headers: { 'Location' => 'final' })
14
- stub_request(:any, 'https://www.example.org/nested/final').to_return(status: 200)
15
- checker.check(url, location: 'page.html')
16
- checker.run if checker.respond_to?(:run)
17
- expect(result.uri.to_s).to eq(url)
18
- expect(result.result_uri.to_s).to eq('https://www.example.org/nested/final')
19
- expect(result.options).to eq(location: 'page.html')
20
- end
21
-
22
- it 'stops redirect chains at the configured limit' do
23
- stub_request(:any, url).to_return(status: 302, headers: { 'Location' => '/one' })
24
- stub_request(:any, 'https://www.example.org/one').to_return(status: 302, headers: { 'Location' => '/two' })
25
- stub_request(:any, 'https://www.example.org/two').to_return(status: 302, headers: { 'Location' => '/three' })
26
- checker.check(url)
27
- checker.run if checker.respond_to?(:run)
28
- expect(result.error).to be_a(LinkChecker::Errors::RedirectLimitError)
29
- expect(result.uri.to_s).to eq(url)
30
- expect(result.result_uri.to_s).to eq('https://www.example.org/two')
31
- expect(a_request(:any, 'https://www.example.org/three')).not_to have_been_made
32
- end
33
-
34
- it 'detects a redirect back to the original URL without requesting it again' do
35
- stub_request(:any, url).to_return(status: 302, headers: { 'Location' => '/one' })
36
- stub_request(:any, 'https://www.example.org/one').to_return(status: 302, headers: { 'Location' => url })
37
- checker.check(url)
38
- checker.run if checker.respond_to?(:run)
39
- expect(checker.results[:error].first.error).to be_a(LinkChecker::Errors::RedirectLoopError)
40
- expect(a_request(:any, url)).to have_been_made.once
41
- end
42
-
43
- it 'allows redirects to be disabled' do
44
- checker.max_redirects = 0
45
- stub_request(:any, url).to_return(status: 302, headers: { 'Location' => '/one' })
46
- checker.check(url)
47
- checker.run if checker.respond_to?(:run)
48
- expect(checker.results[:error].first.error).to be_a(LinkChecker::Errors::RedirectLimitError)
49
- expect(a_request(:any, 'https://www.example.org/one')).not_to have_been_made
50
- end
51
- end
52
-
53
- context 'with transient retries' do
54
- let(:checker_options) { {} }
55
- let(:checker) { described_class.new({ methods: ['GET'], retries: 2 }.merge(checker_options)) }
56
- let(:url) { 'https://www.example.org/retry' }
57
-
58
- around do |example|
59
- VCR.turned_off { example.run }
60
- end
61
-
62
- it 'retries service errors and preserves metadata' do
63
- stub_request(:get, url).to_return({ status: 503 }, { status: 200 })
64
- checker.check(url, location: 'page.html')
65
- checker.run if checker.respond_to?(:run)
66
- expect(checker.results[:success].first.options).to eq(location: 'page.html')
67
- expect(a_request(:get, url)).to have_been_made.twice
68
- end
69
-
70
- it 'does not retry permanent HTTP failures' do
71
- stub_request(:get, url).to_return(status: 404)
72
- checker.check(url)
73
- checker.run if checker.respond_to?(:run)
74
- expect(checker.results[:failure].first.code).to eq(404)
75
- expect(a_request(:get, url)).to have_been_made.once
76
- end
77
-
78
- it 'retries timeouts and reports a timeout category' do
79
- stub_request(:get, url).to_timeout
80
- checker.check(url)
81
- checker.run if checker.respond_to?(:run)
82
- expect(checker.results[:error].first.error_type).to eq(:timeout)
83
- expect(a_request(:get, url)).to have_been_made.times(3)
84
- end
85
-
86
- it 'does not retry malformed URLs' do
87
- checker.check('\/invalid-url')
88
- checker.run if checker.respond_to?(:run)
89
- expect(checker.results[:error].first.error_type).to eq(:other)
90
- expect(checker.results[:error].size).to eq(1)
91
- end
92
-
93
- context 'with a configured delay and backoff' do
94
- let(:checker_options) { { retry_after: 0.005, retry_backoff: 2 } }
95
-
96
- it 'waits before each retry without changing the retry budget' do
97
- stub_request(:get, url).to_return({ status: 503 }, { status: 503 }, { status: 200 })
98
- started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
99
- checker.check(url)
100
- checker.run if checker.respond_to?(:run)
101
- elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
102
- expect(elapsed).to be >= 0.015
103
- expect(a_request(:get, url)).to have_been_made.times(3)
104
- expect(checker.results[:success].size).to eq(1)
105
- end
106
- end
107
-
108
- context 'with Retry-After enabled' do
109
- let(:checker_options) { { respect_retry_after: true, max_retry_delay: 0.005 } }
110
-
111
- it 'honors a bounded server delay' do
112
- stub_request(:get, url).to_return({ status: 429, headers: { 'Retry-After' => '5' } }, { status: 200 })
113
- started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
114
- checker.check(url)
115
- checker.run if checker.respond_to?(:run)
116
- elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
117
- expect(elapsed).to be >= 0.005
118
- expect(elapsed).to be < 1
119
- expect(checker.results[:success].size).to eq(1)
120
- end
121
-
122
- it 'accepts HTTP dates' do
123
- stub_request(:get, url).to_return(
124
- { status: 503, headers: { 'Retry-After' => (Time.now + 60).httpdate } }, { status: 200 }
125
- )
126
- checker.check(url)
127
- checker.run if checker.respond_to?(:run)
128
- expect(checker.results[:success].size).to eq(1)
129
- end
130
-
131
- it 'ignores invalid server delays' do
132
- stub_request(:get, url).to_return({ status: 503, headers: { 'Retry-After' => 'invalid' } }, { status: 200 })
133
- checker.check(url)
134
- checker.run if checker.respond_to?(:run)
135
- expect(checker.results[:success].size).to eq(1)
136
- end
137
- end
138
- end
139
-
140
- context 'with deduplication' do
141
- let(:checker) { described_class.new(methods: ['GET'], deduplicate: true) }
142
- let(:url) { 'https://www.example.org/' }
143
-
144
- around do |example|
145
- VCR.turned_off { example.run }
146
- end
147
-
148
- it 'checks once and preserves metadata for every submission' do
149
- stub_request(:any, url).to_return(status: 200)
150
- checker.check(url, location: 'one.html')
151
- checker.check(url, location: 'two.html')
152
- checker.run if checker.respond_to?(:run)
153
- expect(a_request(:any, url)).to have_been_made.once
154
- expect(checker.results[:success].map(&:options)).to eq([{ location: 'one.html' }, { location: 'two.html' }])
155
- expect(checker.results[:success].map(&:uri)).to eq([URI(url), URI(url)])
156
- end
157
-
158
- it 'rechecks failures instead of retaining them' do
159
- stub_request(:any, url).to_return({ status: 404 }, { status: 200 })
160
- checker.check(url)
161
- checker.run if checker.respond_to?(:run)
162
- checker.check(url)
163
- checker.run if checker.respond_to?(:run)
164
- expect(a_request(:any, url)).to have_been_made.twice
165
- expect(checker.results[:failure].size).to eq(1)
166
- expect(checker.results[:success].size).to eq(1)
167
- end
168
-
169
- it 'keeps query values distinct' do
170
- stub_request(:any, "#{url}?id=1").to_return(status: 200)
171
- stub_request(:any, "#{url}?id=2").to_return(status: 404)
172
- checker.check("#{url}?id=1")
173
- checker.check("#{url}?id=2")
174
- checker.run if checker.respond_to?(:run)
175
- expect(checker.results[:success].first.uri.query).to eq('id=1')
176
- expect(checker.results[:failure].first.uri.query).to eq('id=2')
177
- end
178
-
179
- it 'includes headers in request identity' do
180
- stub_request(:any, url).to_return(status: 200)
181
- checker.check(url)
182
- checker.run if checker.respond_to?(:run)
183
- checker.headers = { 'Accept' => 'text/html' }
184
- checker.check(url)
185
- checker.run if checker.respond_to?(:run)
186
- expect(a_request(:any, url)).to have_been_made.twice
187
- end
188
-
189
- context 'without result collection' do
190
- let(:checker) { described_class.new(methods: ['GET'], deduplicate: true, results: false) }
191
-
192
- it 'still delivers results to every subscriber' do
193
- completed = []
194
- stub_request(:any, url).to_return(status: 200)
195
- checker.on(:success) { |result| completed << result.options }
196
- checker.check(url, location: 'one.html')
197
- checker.check(url, location: 'two.html')
198
- checker.run if checker.respond_to?(:run)
199
- expect(completed).to eq([{ location: 'one.html' }, { location: 'two.html' }])
200
- expect(checker.results).to be_nil
201
- end
202
- end
203
- end
204
-
205
- context 'with persistent caching' do
206
- let(:directory) { Dir.mktmpdir('link-checker-spec-') }
207
- let(:cache_options) { { file: File.join(directory, 'cache.json'), ttl: 60 } }
208
- let(:checker) { described_class.new(methods: ['GET'], cache: cache_options) }
209
- let(:next_checker) { described_class.new(methods: ['GET'], cache: cache_options) }
210
- let(:url) { 'https://www.example.org/' }
211
-
212
- around do |example|
213
- VCR.turned_off { example.run }
214
- end
215
-
216
- after do
217
- FileUtils.remove_entry(directory)
218
- end
219
-
220
- it 'reuses a success across checker instances with fresh metadata' do
221
- stub_request(:any, url).to_return(status: 200)
222
- checker.check(url, location: 'one.html')
223
- checker.run if checker.respond_to?(:run)
224
- next_checker.check(url, location: 'two.html')
225
- next_checker.run if next_checker.respond_to?(:run)
226
- expect(next_checker.results[:success].first.cached?).to be true
227
- expect(next_checker.results[:success].first.options).to eq(location: 'two.html')
228
- expect(a_request(:any, url)).to have_been_made.once
229
- end
230
-
231
- it 'does not persist failed checks' do
232
- stub_request(:any, url).to_return({ status: 404 }, { status: 200 })
233
- checker.check(url)
234
- checker.run if checker.respond_to?(:run)
235
- next_checker.check(url)
236
- next_checker.run if next_checker.respond_to?(:run)
237
- expect(next_checker.results[:success].first.cached?).to be false
238
- expect(a_request(:any, url)).to have_been_made.twice
239
- end
240
- end
241
-
242
- context 'default' do
243
- subject do
244
- described_class.new
245
- end
246
-
247
- it 'has a default user-agent' do
248
- expect(subject.user_agent).to eq "Ruby Link Checker/#{LinkChecker::VERSION}"
249
- end
250
-
251
- it 'has no default headers' do
252
- expect(subject.headers).to eq({})
253
- end
254
- end
255
-
256
- context 'user-agent' do
257
- subject do
258
- described_class.new(user_agent: 'user/agent')
259
- end
260
-
261
- it 'updates user-agent' do
262
- expect(subject.user_agent).to eq 'user/agent'
263
- end
264
- end
265
-
266
- context 'headers' do
267
- subject do
268
- described_class.new(headers: { 'DNT' => '1' })
269
- end
270
-
271
- it 'updates headers' do
272
- expect(subject.headers).to eq('DNT' => '1')
273
- end
274
- end
275
-
276
- context 'check' do
277
- let(:url) { 'https://www.example.org' }
278
-
279
- include_context 'with result'
280
-
281
- context 'with metadata' do
282
- before do
283
- subject.check(url, foo: 'bar')
284
- end
285
-
286
- context 'GET' do
287
- subject do
288
- described_class.new(methods: ['GET'])
289
- end
290
-
291
- context 'check' do
292
- context '200', vcr: { cassette_name: '200' } do
293
- it 'passes metadata' do
294
- expect(result.options).to eq(foo: 'bar')
295
- end
296
- end
297
-
298
- context '404', vcr: { cassette_name: '404' } do
299
- it 'passes metadata' do
300
- expect(result.options).to eq(foo: 'bar')
301
- end
302
- end
303
-
304
- context 'error', vcr: { cassette_name: '404' } do
305
- let(:url) { '\/invalid-url' }
306
-
307
- it 'passes metadata' do
308
- expect(result.options).to eq(foo: 'bar')
309
- end
310
- end
311
-
312
- context 'a redirect loop', vcr: { cassette_name: '301+301' } do
313
- it 'passes metadata' do
314
- expect(result.options).to eq(foo: 'bar')
315
- end
316
- end
317
- end
318
- end
319
- end
320
-
321
- context 'without results' do
322
- before do
323
- subject.check(url, foo: 'bar')
324
- end
325
-
326
- context 'GET' do
327
- subject do
328
- described_class.new(results: false, methods: ['GET'])
329
- end
330
-
331
- context 'check' do
332
- context 'a valid URI that returns a 200', vcr: { cassette_name: '200' } do
333
- it 'passes metadata' do
334
- expect(subject.results).to be_nil
335
- end
336
- end
337
- end
338
- end
339
- end
340
-
341
- context 'without metadata' do
342
- before do
343
- subject.check(url)
344
- end
345
-
346
- context 'GET' do
347
- subject do
348
- described_class.new(methods: ['GET'])
349
- end
350
-
351
- context 'check' do
352
- context 'a valid URI that returns a 200', vcr: { cassette_name: '200' } do
353
- it 'sets user agent' do
354
- expect(result.request_headers['User-Agent']).to eq "Ruby Link Checker/#{LinkChecker::VERSION}"
355
- end
356
-
357
- it 'returns all metadata' do
358
- expect(result.options).to eq({})
359
- end
360
-
361
- context 'with custom headers' do
362
- subject do
363
- described_class.new(methods: ['GET'], headers: { 'DNT' => '1' })
364
- end
365
-
366
- it 'sends custom headers' do
367
- expect(result.request_headers['DNT']).to eq '1'
368
- end
369
- end
370
-
371
- it 'returns results' do
372
- expect(subject.results).to eq(
373
- error: [],
374
- failure: [],
375
- success: [
376
- result
377
- ]
378
- )
379
- end
380
-
381
- it 'succeeds' do
382
- expect(result.success?).to be true
383
- expect(result.error?).to be false
384
- expect(result.failure?).to be false
385
- expect(result.uri).to eq URI(url)
386
- expect(subject).to have_received(:called!).with(:result, result)
387
- expect(subject).to have_received(:called!).with(:success, result)
388
- end
389
- end
390
-
391
- context 'a 404', vcr: { cassette_name: '404', allow_playback_repeats: true } do
392
- it 'fails' do
393
- expect(result.success?).to be false
394
- expect(result.error?).to be false
395
- expect(result.failure?).to be true
396
- expect(result.uri).to eq URI(url)
397
- expect(result.response.code.to_i).to eq 404
398
- expect(subject).to have_received(:called!).with(:failure, result)
399
- end
400
-
401
- context 'with 0 retries' do
402
- subject do
403
- described_class.new(methods: ['GET'], retries: 0)
404
- end
405
-
406
- it 'fails' do
407
- expect(result.success?).to be false
408
- expect(result.error?).to be false
409
- expect(result.failure?).to be true
410
- expect(result.uri).to eq URI(url)
411
- expect(result.response.code.to_i).to eq 404
412
- expect(subject).to have_received(:called!).with(:failure, result).once
413
- expect(subject).not_to have_received(:called!).with(:retry, anything)
414
- end
415
- end
416
-
417
- context 'with 1 retry' do
418
- subject do
419
- described_class.new(methods: ['GET'], retries: 1, retry_status_codes: nil)
420
- end
421
-
422
- it 'fails' do
423
- expect(result.success?).to be false
424
- expect(result.error?).to be false
425
- expect(result.failure?).to be true
426
- expect(result.uri).to eq URI(url)
427
- expect(result.response.code.to_i).to eq 404
428
- expect(subject).to have_received(:called!).with(:failure, result).once
429
- expect(subject).to have_received(:called!).with(:retry, anything).once
430
- end
431
- end
432
-
433
- context 'with 2 retries' do
434
- subject do
435
- described_class.new(methods: ['GET'], retries: 2, retry_status_codes: nil)
436
- end
437
-
438
- it 'fails' do
439
- expect(result.success?).to be false
440
- expect(result.error?).to be false
441
- expect(result.failure?).to be true
442
- expect(result.uri).to eq URI(url)
443
- expect(result.response.code.to_i).to eq 404
444
- expect(subject).to have_received(:called!).with(:failure, result).once
445
- expect(subject).to have_received(:called!).with(:retry, anything).twice
446
- end
447
- end
448
- end
449
-
450
- context 'a redirect on HEAD followed by a 403', vcr: { cassette_name: '301+403' } do
451
- it 'calls redirect callback' do
452
- expect(result.success?).to be false
453
- expect(result.failure?).to be true
454
- expect(subject).to have_received(:called!).with(:redirect, anything)
455
- expect(subject).to have_received(:called!).with(:failure, result).once
456
- end
457
-
458
- it 'reports its original and result urls' do
459
- expect(result.uri.to_s).to eq url
460
- expect(result.result_uri.to_s).not_to eq url
461
- expect(result.result_uri.to_s).to eq 'https://www.dblock.org/'
462
- end
463
- end
464
-
465
- context 'a redirect on HEAD followed by a 200', vcr: { cassette_name: '301+200' } do
466
- it 'calls redirect callback' do
467
- expect(result.success?).to be true
468
- expect(result.failure?).to be false
469
- expect(result.redirect?).to be false
470
- expect(subject).to have_received(:called!).with(:redirect, anything)
471
- expect(subject).to have_received(:called!).with(:success, result)
472
- expect(subject).not_to have_received(:called!).with(:failure, anything)
473
- end
474
- end
475
-
476
- context 'a redirect loop', vcr: { cassette_name: '301+301' } do
477
- it 'calls redirect callback' do
478
- expect(result.success?).to be false
479
- expect(result.failure?).to be false
480
- expect(result.error?).to be true
481
- expect(result.error).to be_a LinkChecker::Errors::RedirectLoopError
482
- expect(result.redirect?).to be false
483
- expect(subject).to have_received(:called!).with(:redirect, anything).once
484
- expect(subject).to have_received(:called!).with(:error, result)
485
- expect(subject).not_to have_received(:called!).with(:failure, result)
486
- expect(subject).not_to have_received(:called!).with(:success, result)
487
- end
488
- end
489
-
490
- context 'a retry on 429', vcr: {
491
- cassette_name: '429+200',
492
- match_requests_on: [lambda { |_request, recorded_request|
493
- @matched ||= []
494
- if @matched.size + 1 === recorded_request.headers['Index'].first
495
- @matched << recorded_request
496
- true
497
- else
498
- false
499
- end
500
- }]
501
- } do
502
- subject do
503
- described_class.new(methods: ['GET'], retries: 1)
504
- end
505
-
506
- it 'calls a retry callback' do
507
- expect(result.success?).to be true
508
- expect(result.failure?).to be false
509
- expect(result.redirect?).to be false
510
- expect(subject).to have_received(:called!).with(:retry, anything)
511
- expect(subject).to have_received(:called!).with(:success, result)
512
- expect(subject).not_to have_received(:called!).with(:failure, anything)
513
- expect(subject).not_to have_received(:called!).with(:error, anything)
514
- end
515
- end
516
-
517
- context 'a failed retry on a redirect', vcr: { cassette_name: '308+429', allow_playback_repeats: true } do
518
- subject do
519
- described_class.new(methods: %w[HEAD], retries: 2)
520
- end
521
-
522
- it 'calls redirect callback' do
523
- expect(result.success?).to be false
524
- expect(result.failure?).to be true
525
- expect(result.error?).to be false
526
- expect(result.error).not_to be_a LinkChecker::Errors::RedirectLoopError
527
- end
528
- end
529
-
530
- context 'an invalid URI' do
531
- let(:url) { '\/invalid-url' }
532
-
533
- it 'fails' do
534
- expect(result.success?).to be false
535
- expect(result.failure?).to be false
536
- expect(result.error?).to be true
537
- expect(result.uri).to eq url
538
- expect(subject).to have_received(:called!).with(:result, result)
539
- expect(subject).to have_received(:called!).with(:error, result)
540
- expect(subject).not_to have_received(:called!).with(:failure)
541
- expect(subject).not_to have_received(:called!).with(:success)
542
- end
543
- end
544
- end
545
-
546
- context 'HEAD,GET' do
547
- subject do
548
- described_class.new(methods: %w[HEAD GET])
549
- end
550
-
551
- context 'a valid URI that fails on HEAD and succeeds on GET', vcr: { cassette_name: '404+200' } do
552
- it 'succeeds' do
553
- expect(result.success?).to be true
554
- expect(result.error?).to be false
555
- expect(result.failure?).to be false
556
- expect(result.uri).to eq URI(url)
557
- expect(subject).to have_received(:called!).with(:success, result)
558
- expect(subject).not_to have_received(:called!).with(:failure, result)
559
- end
560
- end
561
-
562
- context 'a valid URI that fails both on HEAD and GET', vcr: { cassette_name: '404+404' } do
563
- it 'fails' do
564
- expect(result.success?).to be false
565
- expect(result.error?).to be false
566
- expect(result.failure?).to be true
567
- expect(result.uri).to eq URI(url)
568
- expect(result.response.code.to_i).to eq 404
569
- expect(subject).to have_received(:called!).with(:failure, result).once
570
- end
571
- end
572
-
573
- context 'a retry on 429', vcr: {
574
- cassette_name: '429+429+200',
575
- match_requests_on: [lambda { |request, recorded_request|
576
- @matched ||= []
577
- if recorded_request.method == request.method && @matched.size + 1 === recorded_request.headers['Index'].first
578
- @matched << recorded_request
579
- true
580
- else
581
- false
582
- end
583
- }]
584
- } do
585
- subject do
586
- described_class.new(methods: %w[HEAD GET], retries: 1)
587
- end
588
-
589
- it 'executes HEAD twice, then falls back to GET' do
590
- expect(result.success?).to be true
591
- end
592
- end
593
-
594
- context 'a redirect on HEAD followed by a 400 error succeeds on GET',
595
- vcr: { cassette_name: '301+400+301+200' } do
596
- it 'calls redirect callback' do
597
- expect(result.success?).to be true
598
- expect(result.failure?).to be false
599
- expect(result.redirect?).to be false
600
- expect(subject).to have_received(:called!).with(:redirect, anything).twice
601
- expect(subject).to have_received(:called!).with(:success, result).once
602
- expect(subject).not_to have_received(:called!).with(:failure, anything)
603
- end
604
- end
605
- end
606
- end
607
- end
608
- end
609
- end
data/spec/support/vcr.rb DELETED
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'vcr'
4
- require 'webmock/rspec'
5
-
6
- VCR.configure do |config|
7
- config.cassette_library_dir = 'spec/fixtures'
8
- # config.default_cassette_options = { record: :new_episodes }
9
- config.configure_rspec_metadata!
10
- config.before_record do |i|
11
- i.response.body.force_encoding('UTF-8')
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- shared_context 'with result' do
2
- before do
3
- allow(subject).to receive(:called!)
4
- subject.on do |event, *data|
5
- subject.called! event, *data
6
- end
7
- subject.on :result do |result|
8
- @result = result
9
- end
10
- end
11
-
12
- let(:result) do
13
- @result
14
- end
15
- end
@@ -1,14 +0,0 @@
1
- shared_context 'with url' do
2
- subject do
3
- described_class.new(methods: ['GET'])
4
- end
5
-
6
- let(:options) { {} }
7
- let(:url) { 'https://www.example.org' }
8
-
9
- include_context 'with result'
10
-
11
- before do
12
- subject.check(url, options)
13
- end
14
- end