Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cookies from incoming request are passed to Grover via Middleware #63

Merged
merged 5 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 8 additions & 2 deletions lib/grover/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ def convert_to_pdf(grover)
def create_grover_for_response(response)
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)

body = HTMLPreprocessor.process body, root_url, protocol
Grover.new(body, display_url: request_url)

options = { display_url: request_url }
cookies = Rack::Utils.parse_cookies(env).map do |name, value|
{ name: name, value: value, domain: env['HTTP_HOST'] }
end
options[:cookies] = cookies if cookies.any?

Grover.new(body, options)
end

def add_cover_content(grover)
Expand Down
14 changes: 14 additions & 0 deletions spec/grover/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ def request_env
expect(last_response.body).to eq 'A converted PDF'
end

it 'passes through cookies to Grover' do
expect(Grover).to(
receive(:new).
with(
'Grover McGroveryface',
display_url: 'http://www.example.org/test',
cookies: [{ domain: 'www.example.org', name: 'key', value: 'value' }]
).and_return(grover)
)
expect(grover).to receive(:to_pdf).with(no_args).and_return 'A converted PDF'
get 'http://www.example.org/test.pdf', nil, { 'HTTP_COOKIE' => 'key=value' }
expect(last_response.body).to eq 'A converted PDF'
end

context 'when app configuration has PDF middleware disabled' do
before { allow(Grover.configuration).to receive(:use_pdf_middleware).and_return false }

Expand Down