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

fix: support non-lower-case Content-Type header provided by app #516

Merged
merged 3 commits into from
Oct 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/pdfkit/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def _call(env)
status, headers, response = @app.call(env)

begin
if rendering_pdf? && headers['content-type'] =~ /text\/html|application\/xhtml\+xml/
content_type_header = headers.has_key?('Content-Type') ? 'Content-Type' : 'content-type'
if rendering_pdf? && headers[content_type_header] =~ /text\/html|application\/xhtml\+xml/
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)

Expand All @@ -49,7 +50,7 @@ def _call(env)
end

headers['content-length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
headers['content-type'] = 'application/pdf'
headers[content_type_header] = 'application/pdf'
headers['content-disposition'] ||= @conditions[:disposition] || 'inline'
end
rescue StandardError => e
Expand Down
23 changes: 23 additions & 0 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,29 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
end
end

describe "content type header" do
before { mock_app }

context "lower case" do
specify "header gets correctly updated" do
get 'http://www.example.org/public/test.pdf'
expect(last_response.headers["content-type"]).to eq("application/pdf")
end
end

context "mixed case" do
let(:headers) do
{'Content-Type' => "text/html"}
end

specify "header gets correctly updated" do
pending("this test only applies to rack 2.x and is rejected by rack 3.x") if Rack.release >= "3.0.0"
get 'http://www.example.org/public/test.pdf'
expect(last_response.headers["Content-Type"]).to eq("application/pdf")
end
end
end

describe "remove .pdf from PATH_INFO and REQUEST_URI" do
before { mock_app }

Expand Down