forked from stitchfix/stitches
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Headers respond with the correct version
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
1 parent
a7a4ff6
commit f933bc9
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |