diff --git a/Gemfile b/Gemfile index c920a2c..cb85077 100644 --- a/Gemfile +++ b/Gemfile @@ -7,15 +7,16 @@ gemspec gem 'childprocess', '~> 5.0' gem 'combine_pdf', '~> 1.0' -gem 'mini_magick', '~> 4.12' -gem 'pdf-reader', '~> 2.11' -gem 'puma', '~> 6.4' -gem 'rack-test', '~> 1.1' -gem 'rake', '~> 13.0' -gem 'rspec', '~> 3.12' -gem 'rubocop', '~> 1.43' +gem 'mini_magick', '~> 5.0' +gem 'pdf-reader', '~> 2.13' +gem 'puma', '~> 6.5' +gem 'rack-test', '~> 2.1' +gem 'rackup', '~> 2.2' +gem 'rake', '~> 13.2' +gem 'rspec', '~> 3.13' +gem 'rubocop', '~> 1.68' gem 'rubocop-rake', '~> 0.6' -gem 'rubocop-rspec', '~> 2.18' -gem 'sinatra', '~> 3.2' +gem 'rubocop-rspec', '~> 3.2' +gem 'sinatra', '~> 4.1' # Limit simplecov to 0.17.x due to https://github.com/codeclimate/test-reporter/issues/413 gem 'simplecov', '~> 0.17', '< 0.18' diff --git a/README.md b/README.md index fbdc90a..ca95317 100644 --- a/README.md +++ b/README.md @@ -288,7 +288,7 @@ In respective controller's action use: ```ruby respond_to do |format| format.html do - response.headers['Content-Disposition'] = %(attachment; filename="lorem_ipsum.pdf") + response.headers['content-disposition'] = %(attachment; filename="lorem_ipsum.pdf") render layout: 'pdf' end diff --git a/lib/grover/middleware.rb b/lib/grover/middleware.rb index eca22ee..612649c 100644 --- a/lib/grover/middleware.rb +++ b/lib/grover/middleware.rb @@ -85,7 +85,7 @@ def ignore_request? end def html_content?(headers) - headers['Content-Type'] =~ %r{text/html|application/xhtml\+xml} + headers['content-type'] =~ %r{text/html|application/xhtml\+xml} end def update_response(response, headers) @@ -155,11 +155,11 @@ def fetch_cover_pdf(path) def assign_headers(headers, body, content_type) # Do not cache results - headers.delete 'ETag' - headers.delete 'Cache-Control' + headers.delete 'etag' + headers.delete 'cache-control' - headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s - headers['Content-Type'] = content_type + headers['content-length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s + headers['content-type'] = content_type end def configure_env_for_grover_request(env) diff --git a/spec/grover/middleware_spec.rb b/spec/grover/middleware_spec.rb index 37e3043..0983d9d 100644 --- a/spec/grover/middleware_spec.rb +++ b/spec/grover/middleware_spec.rb @@ -2,6 +2,9 @@ require 'spec_helper' +require 'rack/lint' +require 'rack/builder' + describe Grover::Middleware do subject(:mock_app) do builder = Rack::Builder.new @@ -18,16 +21,16 @@ @request_env = env.deep_dup response_size = 0 response.each { |part| response_size += part.length } - [200, headers.merge('Content-Length' => response_size.to_s), response] + [200, headers.merge('content-length' => response_size.to_s), response] end end let(:app) { Rack::Lint.new(subject) } let(:headers) do { - 'Content-Type' => 'text/html', - 'ETag' => 'foo', - 'Cache-Control' => 'max-age=2592000, public' + 'content-type' => 'text/html', + 'etag' => 'foo', + 'cache-control' => 'max-age=2592000, public' } end let(:response) { ['Grover McGroveryface'] } @@ -39,18 +42,18 @@ context 'when requesting a PDF' do it 'returns PDF content type' do get 'http://www.example.org/test.pdf' - expect(last_response.headers['Content-Type']).to eq 'application/pdf' + expect(last_response.headers['content-type']).to eq 'application/pdf' response_size = Grover.new('Grover McGroveryface', display_url: 'http://www.example.org/test').to_pdf.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end it 'matches PDF case insensitive' do get 'http://www.example.org/test.PDF' - expect(last_response.headers['Content-Type']).to eq 'application/pdf' + expect(last_response.headers['content-type']).to eq 'application/pdf' response_size = Grover.new('Grover McGroveryface', display_url: 'http://www.example.org/test').to_pdf.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end context 'when `allow_file_uris` configuration option is set' do @@ -74,18 +77,18 @@ it 'returns PNG content type' do get 'http://www.example.org/test.png' - expect(last_response.headers['Content-Type']).to eq 'image/png' + expect(last_response.headers['content-type']).to eq 'image/png' response_size = Grover.new('Grover McGroveryface').to_png.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end it 'matches PNG case insensitive' do get 'http://www.example.org/test.PNG' - expect(last_response.headers['Content-Type']).to eq 'image/png' + expect(last_response.headers['content-type']).to eq 'image/png' response_size = Grover.new('Grover McGroveryface').to_png.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end context 'when `allow_file_uris` configuration option is set' do @@ -109,26 +112,26 @@ it 'returns JPEG content type' do get 'http://www.example.org/test.jpeg' - expect(last_response.headers['Content-Type']).to eq 'image/jpeg' + expect(last_response.headers['content-type']).to eq 'image/jpeg' response_size = Grover.new('Grover McGroveryface').to_jpeg.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end it 'matches JPEG case insensitive' do get 'http://www.example.org/test.JPEG' - expect(last_response.headers['Content-Type']).to eq 'image/jpeg' + expect(last_response.headers['content-type']).to eq 'image/jpeg' response_size = Grover.new('Grover McGroveryface').to_jpeg.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end it 'matches JPG case insensitive' do get 'http://www.example.org/test.JPG' - expect(last_response.headers['Content-Type']).to eq 'image/jpeg' + expect(last_response.headers['content-type']).to eq 'image/jpeg' response_size = Grover.new('Grover McGroveryface').to_jpeg.bytesize expect(last_response.body.bytesize).to eq response_size - expect(last_response.headers['Content-Length']).to eq response_size.to_s + expect(last_response.headers['content-length']).to eq response_size.to_s end context 'when `allow_file_uris` configuration option is set' do @@ -150,9 +153,9 @@ context 'when request doesnt have an extension' do it 'returns the downstream content type' do get 'http://www.example.org/test' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Grover McGroveryface' - expect(last_response.headers['Content-Length']).to eq '20' + expect(last_response.headers['content-length']).to eq '20' end context 'when `allow_file_uris` configuration option is set' do @@ -160,9 +163,9 @@ it 'returns the downstream content and content type' do get 'http://www.example.org/test' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Grover McGroveryface' - expect(last_response.headers['Content-Length']).to eq '20' + expect(last_response.headers['content-length']).to eq '20' end end end @@ -170,9 +173,9 @@ context 'when request has a non-PDF/PNG/JPEG extension' do it 'returns the downstream content type' do get 'http://www.example.org/test.html' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Grover McGroveryface' - expect(last_response.headers['Content-Length']).to eq '20' + expect(last_response.headers['content-length']).to eq '20' end end end @@ -335,8 +338,8 @@ context 'when requesting a PDF' do it 'deletes the cache headers' do get 'http://www.example.org/test.pdf' - expect(last_response.headers).not_to have_key 'ETag' - expect(last_response.headers).not_to have_key 'Cache-Control' + expect(last_response.headers).not_to have_key 'etag' + expect(last_response.headers).not_to have_key 'cache-control' end context 'when app configuration has PDF middleware disabled' do @@ -344,8 +347,8 @@ it 'returns the cache headers' do get 'http://www.example.org/test.pdf' - expect(last_response.headers['ETag']).to eq 'foo' - expect(last_response.headers['Cache-Control']).to eq 'max-age=2592000, public' + expect(last_response.headers['etag']).to eq 'foo' + expect(last_response.headers['cache-control']).to eq 'max-age=2592000, public' end end end @@ -353,8 +356,8 @@ context 'when requesting a PNG' do it 'returns the cache headers' do get 'http://www.example.org/test.png' - expect(last_response.headers['ETag']).to eq 'foo' - expect(last_response.headers['Cache-Control']).to eq 'max-age=2592000, public' + expect(last_response.headers['etag']).to eq 'foo' + expect(last_response.headers['cache-control']).to eq 'max-age=2592000, public' end context 'when app configuration has PNG middleware enabled' do @@ -362,8 +365,8 @@ it 'deletes the cache headers' do get 'http://www.example.org/test.png' - expect(last_response.headers).not_to have_key 'ETag' - expect(last_response.headers).not_to have_key 'Cache-Control' + expect(last_response.headers).not_to have_key 'etag' + expect(last_response.headers).not_to have_key 'cache-control' end end end @@ -371,8 +374,8 @@ context 'when requesting a JPEG' do it 'returns the cache headers' do get 'http://www.example.org/test.jpeg' - expect(last_response.headers['ETag']).to eq 'foo' - expect(last_response.headers['Cache-Control']).to eq 'max-age=2592000, public' + expect(last_response.headers['etag']).to eq 'foo' + expect(last_response.headers['cache-control']).to eq 'max-age=2592000, public' end context 'when app configuration has JPEG middleware enabled' do @@ -380,14 +383,14 @@ it 'deletes the cache headers for JPEG' do get 'http://www.example.org/test.jpeg' - expect(last_response.headers).not_to have_key 'ETag' - expect(last_response.headers).not_to have_key 'Cache-Control' + expect(last_response.headers).not_to have_key 'etag' + expect(last_response.headers).not_to have_key 'cache-control' end it 'deletes the cache headers for JPG' do get 'http://www.example.org/test.jpg' - expect(last_response.headers).not_to have_key 'ETag' - expect(last_response.headers).not_to have_key 'Cache-Control' + expect(last_response.headers).not_to have_key 'etag' + expect(last_response.headers).not_to have_key 'cache-control' end end end @@ -395,8 +398,8 @@ context 'when not requesting a PDF, PNG or JPEG' do it 'returns the cache headers' do get 'http://www.example.org/test' - expect(last_response.headers['ETag']).to eq 'foo' - expect(last_response.headers['Cache-Control']).to eq 'max-age=2592000, public' + expect(last_response.headers['etag']).to eq 'foo' + expect(last_response.headers['cache-control']).to eq 'max-age=2592000, public' end end end @@ -407,7 +410,7 @@ it 'returns response as PDF' do get 'http://www.example.org/test.pdf' - expect(last_response.headers['Content-Type']).to eq 'application/pdf' + expect(last_response.headers['content-type']).to eq 'application/pdf' expect(last_response.body.bytesize).to( eq(Grover.new('Rackalicious', display_url: 'http://www.example.org/test').to_pdf.bytesize) ) @@ -418,14 +421,14 @@ it 'returns response as text (original)' do get 'http://www.example.org/test.pdf' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Rackalicious' end end it 'returns PNG response as text (original)' do get 'http://www.example.org/test.png' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Rackalicious' end @@ -434,20 +437,20 @@ it 'returns response as PNG' do get 'http://www.example.org/test.png' - expect(last_response.headers['Content-Type']).to eq 'image/png' + expect(last_response.headers['content-type']).to eq 'image/png' expect(last_response.body.bytesize).to eq Grover.new('Rackalicious').to_png.bytesize end end it 'returns JPEG response as text (original)' do get 'http://www.example.org/test.jpeg' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Rackalicious' end it 'returns JPG response as text (original)' do get 'http://www.example.org/test.jpg' - expect(last_response.headers['Content-Type']).to eq 'text/html' + expect(last_response.headers['content-type']).to eq 'text/html' expect(last_response.body).to eq 'Rackalicious' end @@ -456,13 +459,13 @@ it 'returns response as JPEG' do get 'http://www.example.org/test.jpeg' - expect(last_response.headers['Content-Type']).to eq 'image/jpeg' + expect(last_response.headers['content-type']).to eq 'image/jpeg' expect(last_response.body.bytesize).to eq Grover.new('Rackalicious').to_jpeg.bytesize end it 'returns response as JPG' do get 'http://www.example.org/test.jpg' - expect(last_response.headers['Content-Type']).to eq 'image/jpeg' + expect(last_response.headers['content-type']).to eq 'image/jpeg' expect(last_response.body.bytesize).to eq Grover.new('Rackalicious').to_jpeg.bytesize end end @@ -473,7 +476,7 @@ it 'returns response as PDF' do get 'http://www.example.org/test.pdf' - expect(last_response.headers['Content-Type']).to eq 'application/pdf' + expect(last_response.headers['content-type']).to eq 'application/pdf' expect(last_response.body.bytesize).to( eq(Grover.new('Part 1Part 2', display_url: 'http://www.example.org/test').to_pdf.bytesize) ) @@ -733,7 +736,7 @@ HTML end - [200, headers.merge('Content-Length' => response.length.to_s), [response]] + [200, headers.merge('content-length' => response.length.to_s), [response]] end end @@ -772,7 +775,7 @@ HTML end - [200, headers.merge('Content-Length' => response.length.to_s), [response]] + [200, headers.merge('content-length' => response.length.to_s), [response]] end end @@ -1013,7 +1016,7 @@ else 'Default page contents' end - [200, headers.merge('Content-Length' => response.length.to_s), [response]] + [200, headers.merge('content-length' => response.length.to_s), [response]] end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11f9cc0..e7391ad 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,8 +27,6 @@ include Rack::Test::Methods end -MiniMagick.validate_on_create = false - def fixture_path(file) File.join(File.expand_path(__dir__), 'fixtures', file) end