Skip to content

Commit

Permalink
Headers respond with the correct version
Browse files Browse the repository at this point in the history
In issue stitchfix#70 we see that response headers do not respond with the correct version.

This adds a rack middleware that overrides the Content-Type header to show the version.

Co-authored-by: Juan Carlos Ruiz<[email protected]>
  • Loading branch information
Aliciawyse committed Jun 10, 2019
1 parent a7a4ff6 commit f933bc9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
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"]
[status, headers, body]
end
end
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

0 comments on commit f933bc9

Please sign in to comment.