Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require:
plugins:
- rubocop-rake
- rubocop-rspec

Expand Down
8 changes: 6 additions & 2 deletions lib/grover/js/processor.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions lib/grover/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 37 additions & 4 deletions spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@
<<-HTML
<html>
<head><link rel='icon' href='data:;base64,iVBORw0KGgo='></head>
#{' '}

<body>
<p id="loading">Loading</p>
<p id="content" style="display: none">Loaded</p>
Expand Down Expand Up @@ -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
Expand All @@ -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>
<body></body>

<script>
setTimeout(function() {
document.body.innerHTML = '<h1>Hey there</h1>';
}, 200);
</script>
</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
Expand Down
Loading