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

Headers respond with the correct version #80

Closed
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: 2 additions & 0 deletions lib/stitches/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'stitches/api_key'
require 'stitches/valid_mime_type'
require 'stitches/response_header'

module Stitches
class Railtie < Rails::Railtie
config.app_middleware.use Stitches::ApiKey
config.app_middleware.use Stitches::ValidMimeType
config.app_middleware.use Stitches::ResponseHeader
end
end
12 changes: 12 additions & 0 deletions lib/stitches/response_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative 'allowlist_middleware'

module Stitches
class ResponseHeader < Stitches::AllowlistMiddleware
protected
def do_call(env)
status, headers, body = @app.call(env)
headers["Content-Type"] = env["CONTENT_TYPE"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one of our main concerns with this solution. This overwrites the response header for Content-Type with a value that we believe is non-standard. For example, what was returned as:
Content-Type: application/json; charset=utf-8
would become
Content-Type: application/json; version=x, overwriting charset=utf-8. According to RFC 7231 Section 3.1.1.5, this appears to be a non-standard value for this header.

[status, headers, body]
end
end
end
2 changes: 1 addition & 1 deletion lib/stitches/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Stitches
VERSION = '3.7.3'
VERSION = '3.8.3'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our pull request template contained some outdated instructions for updating the VERSION, so doing this is unnecessary. If this PR is approved and merged, one of the maintainers would take care of this when releasing a new version of this gem.

On a side note, the version here should be 3.8.0 following SEMVER guidelines. The patch portion of the version (the last digit) gets reset to 0. If this had been a major version change, it would be 4.0.0.

end
35 changes: 35 additions & 0 deletions spec/response_header_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper.rb'

describe Stitches::ResponseHeader do
let(:app) { double("rack app") }
let(:headers) { {"Content-Type" => ""} }

before do
allow(app).to receive(:call).with(env).and_return([nil, headers, nil])
end

subject(:middleware) { described_class.new(app, namespace: "/api") }

describe "#call" do
context "valid header" do
let(:env) {
{
"PATH_INFO" => "/api/ping",
"HTTP_ACCEPT" => "application/json; version=99",
"CONTENT_TYPE" => "application/json; version=99"
}
}

before do
@response = middleware.call(env)
end
it "calls through to the rest of the chain" do
expect(app).to have_received(:call).with(env)
end

it "has the Content-Type version information" do
expect(@response[1]["Content-Type"]).to eq("application/json; version=99")
end
end
end
end