diff --git a/.rubocop.yml b/.rubocop.yml index 5486891..3e2ecb8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,4 @@ -require: +plugins: - rubocop-rake - rubocop-rspec diff --git a/lib/grover/js/processor.cjs b/lib/grover/js/processor.cjs index 358851a..cef82d2 100644 --- a/lib/grover/js/processor.cjs +++ b/lib/grover/js/processor.cjs @@ -104,6 +104,12 @@ const _processPage = (async (convertAction, uriOrHtml, options) => { page = await browser.newPage(); + // Apply global page timeout + const timeout = options.timeout; delete options.timeout + if (timeout !== undefined) { + page.setDefaultTimeout(timeout); + } + // Basic auth const username = options.username; delete options.username const password = options.password; delete options.password @@ -124,10 +130,8 @@ const _processPage = (async (convertAction, uriOrHtml, options) => { } // Setup timeout option (if provided) - if (options.timeout === null) delete options.timeout; let requestOptions = {}; let requestTimeout = options.requestTimeout; delete options.requestTimeout; - if (requestTimeout === undefined) requestTimeout = options.timeout; if (requestTimeout !== undefined) { requestOptions.timeout = requestTimeout; } diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index 124ec7f..8b43ebb 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -8,6 +8,13 @@ class Grover # @see https://github.com/pdfkit/pdfkit # class Middleware # rubocop:disable Metrics/ClassLength + PDF_REGEX = /\.pdf$/i + private_constant :PDF_REGEX + PNG_REGEX = /\.png$/i + private_constant :PNG_REGEX + JPEG_REGEX = /\.jpe?g$/i + private_constant :JPEG_REGEX + def initialize(app, *args) @app = app @pdf_request = false @@ -40,10 +47,6 @@ def _call(env) # rubocop:disable Metrics/MethodLength private - PDF_REGEX = /\.pdf$/i - PNG_REGEX = /\.png$/i - JPEG_REGEX = /\.jpe?g$/i - attr_reader :pdf_request, :png_request, :jpeg_request def check_file_uri_configuration diff --git a/spec/grover/processor_spec.rb b/spec/grover/processor_spec.rb index ba60a0f..693660a 100644 --- a/spec/grover/processor_spec.rb +++ b/spec/grover/processor_spec.rb @@ -903,7 +903,7 @@ <<-HTML - #{' '} +

Loading

@@ -963,16 +963,19 @@ end end - shared_examples 'raises navigation timeout error' do + shared_examples 'raises navigation timeout error' do |timeout: 1| if puppeteer_version_on_or_after? '2.0.0' it do - expect { convert }.to raise_error Grover::JavaScript::TimeoutError, 'Navigation timeout of 1 ms exceeded' + expect { convert }.to raise_error( + Grover::JavaScript::TimeoutError, + "Navigation timeout of #{timeout} ms exceeded" + ) end else it do expect { convert }.to raise_error( Grover::JavaScript::TimeoutError, - 'Navigation Timeout Exceeded: 1ms exceeded' + "Navigation Timeout Exceeded: #{timeout}ms exceeded" ) end end @@ -992,6 +995,36 @@ it { is_expected.to start_with "%PDF-1.4\n" } end + + context 'when the waitForSelector option is provided' do + let(:url_or_html) do + <<-HTML + + + + + + HTML + end + let(:options) { basic_header_footer_options.merge('timeout' => timeout, 'waitForSelector' => 'h1') } + + context 'when the timeout is less than the page content load' do + let(:timeout) { 100 } + + it_behaves_like 'raises navigation timeout error', timeout: 100 + end + + context 'when the timeout is greater than the page content load' do + let(:timeout) { 2000 } + + it { is_expected.to start_with "%PDF-1.4\n" } + it { expect(pdf_text_content).to eq "#{date} Hey there #{protocol}://www.example.net/foo/bar 1/1" } + end + end end context 'when requestTimeout option is specified' do