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 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
Expand Up @@ -23,7 +23,7 @@ Metrics/BlockLength:
- "**/*_spec.rb"

Metrics/ClassLength:
Max: 135
Max: 140

RSpec/ExampleLength:
Max: 12
Expand Down
27 changes: 15 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Changelog

## Unreleased
- None

### Added
- (https://github.com/Studiosity/grover/pull/63) Ensure cookies from incoming request are passed to Grover via Middleware


## [0.12.1](releases/tag/v0.12.1) - 2020-05-12
### Fixed
Expand Down Expand Up @@ -31,7 +34,7 @@

## [0.10.1](releases/tag/v0.10.1) - 2020-01-13
### Fixed
- [#39](https://github.com/Studiosity/grover/pull/39) Fix middleware thread safety issue ([@jnimety][])
- [#39](https://github.com/Studiosity/grover/pull/39) Fix middleware thread safety issue ([@jnimety][])

## [0.9.2](releases/tag/v0.9.2) - 2019-12-27
### Added
Expand Down Expand Up @@ -65,10 +68,10 @@
## [0.7.4](releases/tag/v0.7.4) - 2019-07-09
### Breaking change
- [#18](https://github.com/Studiosity/grover/pull/18) Use `GROVER_NO_SANDBOX` for disabling sandbox ([@koenhandekyn][])

## [0.7.3](releases/tag/v0.7.3) - 2019-05-23
### Fixed
- [#14](https://github.com/Studiosity/grover/pull/14) Metadata options not included if source contained *any* line starting with `http`
- [#14](https://github.com/Studiosity/grover/pull/14) Metadata options not included if source contained *any* line starting with `http`
- [#15](https://github.com/Studiosity/grover/pull/15) Add magic comment for freezing string literals

## [0.7.2](releases/tag/v0.7.2) - 2019-01-22
Expand All @@ -89,7 +92,7 @@

### Breaking change
- The `{{display_url}}` header/footer hack was removed in favour of passing the URL via `display_url` option
(for middleware/raw HTML only)
(for middleware/raw HTML only)

## [0.5.5](releases/tag/v0.5.5) - 2018-09-20
### Fixed
Expand Down Expand Up @@ -117,7 +120,7 @@

## [0.4.3](releases/tag/v0.4.3) - 2018-09-10
### Added
- Pass through flag to indicate to upstream middleware/app that Grover has interacted with the environment
- Pass through flag to indicate to upstream middleware/app that Grover has interacted with the environment

## [0.4.2](releases/tag/v0.4.2) - 2018-09-09
### Fixed
Expand Down Expand Up @@ -165,11 +168,11 @@
- Processor support for inline HTML (render via the URI rather than trying to `setContent`)

### Changed
- Minor refactor of middleware for readability
- Minor refactor of middleware for readability

## [0.2.0](releases/tag/v0.2.0) - 2018-08-23
### Added
- Rack middleware for rendering upstream HTML as PDF (based heavily on PDFKit middleware)
- Rack middleware for rendering upstream HTML as PDF (based heavily on PDFKit middleware)
- Allow PDF processor to handle inline HTML

### Fixed
Expand All @@ -189,17 +192,17 @@
## [0.1.0](releases/tag/v0.1.0) - 2018-08-22
### Added
- First pass at PDF processor
- Console script for expediting development
- Console script for expediting development

## [0.0.1](releases/tag/v0.0.1) - 2018-08-22
### Added
- Initial gem framework
- Initial gem framework

[@koenhandekyn]: https://github.com/koenhandekyn
[@joergschiller]: https://github.com/joergschiller
[@ryansmith23]: https://github.com/ryansmith23
[@rwtaylor]: https://github.com/rwtaylor
[@rwtaylor]: https://github.com/rwtaylor
[@jnimety]: https://github.com/jnimety
[@willkoehler]: https://github.com/willkoehler
[@Richacinas]: https://github.com/Richacinas
[@inspiredstuffs]: https://github.com/inspiredstuffs
[@inspiredstuffs]: https://github.com/inspiredstuffs
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