From 45c433c6e7f6224f00bb3396b21e85f5bdbef674 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 24 Jun 2021 11:03:51 -0400 Subject: [PATCH] Test query params in internal links --- spec/html-proofer/cache_spec.rb | 85 +++++++++++++------ .../.new_internal_url_w_query_params.log | 1 + .../fixtures/links/internal_query_link.html | 3 + 3 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 spec/html-proofer/fixtures/cache/.new_internal_url_w_query_params.log create mode 100644 spec/html-proofer/fixtures/links/internal_query_link.html diff --git a/spec/html-proofer/cache_spec.rb b/spec/html-proofer/cache_spec.rb index a23d0378..e5ce9bce 100644 --- a/spec/html-proofer/cache_spec.rb +++ b/spec/html-proofer/cache_spec.rb @@ -246,58 +246,89 @@ def read_cache(cache_file) Timecop.return end - end - it 'does recheck failures, regardless of external-only cache' do - cache_file_name = '.external.log' - cache_location = File.join(storage_dir, cache_file_name) + it 'does recheck failures, regardless of external-only cache' do + cache_file_name = '.external.log' + cache_location = File.join(storage_dir, cache_file_name) - file = "#{FIXTURES_DIR}/cache/external_example.html" + file = "#{FIXTURES_DIR}/cache/external_example.html" - new_time = Time.local(2021, 0o1, 27, 12, 0, 0) - Timecop.freeze(new_time) + new_time = Time.local(2021, 0o1, 27, 12, 0, 0) + Timecop.freeze(new_time) - File.delete(cache_location) if File.exist?(cache_location) + File.delete(cache_location) if File.exist?(cache_location) - run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) - cache = JSON.parse(File.read(cache_location)) + cache = JSON.parse(File.read(cache_location)) - expect(cache.keys.count).to eq(1) - expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + expect(cache.keys.count).to eq(1) + expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') - run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) - cache = JSON.parse(File.read(cache_location)) - expect(cache.keys.count).to eq(1) - expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + cache = JSON.parse(File.read(cache_location)) + expect(cache.keys.count).to eq(1) + expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') - Timecop.return + Timecop.return + end + + it 'does recheck failures, regardless of external and internal cache' do + cache_file_name = '.internal_and_external.log' + cache_location = File.join(storage_dir, cache_file_name) + + file = "#{FIXTURES_DIR}/cache/internal_and_external_example.html" + + new_time = Time.local(2021, 0o1, 27, 12, 0, 0) + Timecop.freeze(new_time) + + File.delete(cache_location) if File.exist?(cache_location) + + run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + + cache = JSON.parse(File.read(cache_location)) + + expect(cache.keys.count).to eq(1) + expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + + run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + + cache = JSON.parse(File.read(cache_location)) + expect(cache.keys.count).to eq(2) + expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + + Timecop.return + end end - it 'does recheck failures, regardless of external and internal cache' do - cache_file_name = '.internal_and_external.log' + it 'does not blow up if internal link has query params' do + cache_file_name = '.new_internal_url_w_query_params.log' cache_location = File.join(storage_dir, cache_file_name) - file = "#{FIXTURES_DIR}/cache/internal_and_external_example.html" + file = "#{FIXTURES_DIR}/links/internal_query_link.html" - new_time = Time.local(2021, 0o1, 27, 12, 0, 0) + new_time = Time.local(2020, 0o1, 27, 12, 0, 0) Timecop.freeze(new_time) File.delete(cache_location) if File.exist?(cache_location) - run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) cache = JSON.parse(File.read(cache_location)) expect(cache.keys.count).to eq(1) - expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + expect(cache.keys[0]).to eq('./gpl.png?v=2020-12-10-1') - run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options)) + Timecop.return - cache = JSON.parse(File.read(cache_location)) - expect(cache.keys.count).to eq(2) - expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer') + new_time = Time.local(2021, 0o6, 20, 12, 0, 0) + Timecop.freeze(new_time) + + # expect_any_instance_of(HTMLProofer::Cache).to receive(:add) + + # we expect one new link to be added because it's outside the 30d time frame + run_proofer(file, :file, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options)) Timecop.return end diff --git a/spec/html-proofer/fixtures/cache/.new_internal_url_w_query_params.log b/spec/html-proofer/fixtures/cache/.new_internal_url_w_query_params.log new file mode 100644 index 00000000..2c53cc94 --- /dev/null +++ b/spec/html-proofer/fixtures/cache/.new_internal_url_w_query_params.log @@ -0,0 +1 @@ +{"./gpl.png?v=2020-12-10-1":{"time":"2021-06-20 12:00:00 -0400","filenames":["spec/html-proofer/fixtures/links/internal_query_link.html"],"status":200,"message":""}} \ No newline at end of file diff --git a/spec/html-proofer/fixtures/links/internal_query_link.html b/spec/html-proofer/fixtures/links/internal_query_link.html new file mode 100644 index 00000000..cb9692c9 --- /dev/null +++ b/spec/html-proofer/fixtures/links/internal_query_link.html @@ -0,0 +1,3 @@ +