-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'add-version-to-response'
- Loading branch information
Showing
17 changed files
with
266 additions
and
9 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
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
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
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
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
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
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,27 @@ | ||
module VersionCake | ||
module ResponseStrategy | ||
class Base | ||
def execute(_context, _status, _headers, _response) | ||
raise Exception, "ResponseStrategy requires execute to be implemented" | ||
end | ||
|
||
def version_key | ||
VersionCake.config.version_key | ||
end | ||
|
||
def self.lookup(strategy) | ||
case strategy | ||
when String, Symbol | ||
strategy_name = "response_strategy/#{strategy}_strategy".camelize | ||
begin | ||
VersionCake.const_get(strategy_name).new | ||
rescue | ||
raise Exception, "Unknown VersionCake response strategy #{strategy_name}" | ||
end | ||
else | ||
raise Exception, "Invalid response strategy" | ||
end | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/versioncake/response_strategy/http_content_type_strategy.rb
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 @@ | ||
module VersionCake | ||
module ResponseStrategy | ||
class HttpContentTypeStrategy < Base | ||
def execute(context, _status, headers, _response) | ||
return if headers['Content-Type'].nil? | ||
|
||
headers['Content-Type'] << ';' unless headers['Content-Type'].end_with? ';' | ||
headers['Content-Type'] << " #{version_key}=#{context.version.to_s}" | ||
end | ||
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,13 @@ | ||
module VersionCake | ||
module ResponseStrategy | ||
class HttpHeaderStrategy < Base | ||
def execute(context, _status, headers, _response) | ||
headers[header_key] = context.version.to_s | ||
end | ||
|
||
def header_key | ||
version_key.gsub('_', '-') | ||
end | ||
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,13 @@ | ||
module VersionCake | ||
class VersionedResponseService | ||
def initialize(config) | ||
@strategies = config.response_strategies | ||
end | ||
|
||
def inject_version(versioned_context, status, headers, response) | ||
@strategies.each do |strategy| | ||
strategy.execute(versioned_context, status, headers, response) | ||
end | ||
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
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
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,60 @@ | ||
require 'spec_helper' | ||
require 'rack' | ||
|
||
describe VersionCake::Rack::Middleware do | ||
let(:response_strategy) { nil } | ||
let(:config) do | ||
VersionCake::Configuration.new.tap do |config| | ||
config.extraction_strategy = [:http_header] | ||
config.response_strategy = response_strategy | ||
config.resources do |resource_config| | ||
resource_config.resource %r{.*}, [], [], (1..5) | ||
end | ||
end | ||
end | ||
let(:upstream_headers) { {} } | ||
let(:middleware) do | ||
VersionCake::Rack::Middleware.new( | ||
double(call: [nil, upstream_headers, nil] ), | ||
config | ||
) | ||
end | ||
|
||
context '#call' do | ||
let(:env) do | ||
{ | ||
'SCRIPT_NAME' => '', | ||
'PATH_INFO' => '', | ||
'HTTP_API_VERSION' => '1' | ||
} | ||
end | ||
|
||
subject { middleware.call(env) } | ||
let(:response_headers) { subject[1] } | ||
|
||
context 'when response_strategy is http_header' do | ||
let(:response_strategy) { [:http_header] } | ||
|
||
it 'sets the version in the response header' do | ||
expect(response_headers['api-version']).to eq '1' | ||
end | ||
end | ||
|
||
context 'when response_strategy is http_content_type' do | ||
let(:response_strategy) { [:http_content_type] } | ||
let(:upstream_headers) { { 'Content-Type' => 'application/vnd.api+json; charset=utf-8;' } } | ||
|
||
it 'sets the version in the content type' do | ||
expect(response_headers['Content-Type']).to match 'application/vnd.api+json; charset=utf-8; api_version=1' | ||
end | ||
|
||
context 'with a simpler content type' do | ||
let(:upstream_headers) { { 'Content-Type' => 'application/json' } } | ||
|
||
it 'sets the version in the content type' do | ||
expect(response_headers['Content-Type']).to match 'application/json; api_version=1' | ||
end | ||
end | ||
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,16 @@ | ||
require 'spec_helper' | ||
|
||
describe VersionCake::ResponseStrategy::Base do | ||
describe '.lookup' do | ||
let(:strategy) { :http_content_type } | ||
subject(:found_strategy) { VersionCake::ResponseStrategy::Base.lookup(strategy) } | ||
|
||
it { expect(found_strategy.class).to eq VersionCake::ResponseStrategy::HttpContentTypeStrategy } | ||
end | ||
|
||
describe '#execute' do | ||
subject(:execute) { VersionCake::ResponseStrategy::Base.new.execute(nil,nil,nil,nil) } | ||
|
||
it { expect { execute }.to raise_error Exception } | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/unit/response_strategy/http_content_type_strategy_spec.rb
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,27 @@ | ||
require 'spec_helper' | ||
|
||
describe VersionCake::ResponseStrategy::HttpContentTypeStrategy do | ||
describe '#execute' do | ||
let(:headers) { { 'Content-Type' => 'application/json' } } | ||
let(:context) { double('content', version: 4) } | ||
before do | ||
VersionCake::ResponseStrategy::HttpContentTypeStrategy.new.execute( | ||
context, nil, headers, nil | ||
) | ||
end | ||
|
||
it { expect(headers['Content-Type']).to eq 'application/json; api_version=4' } | ||
|
||
context 'for a header that ends in a semi colon' do | ||
let(:headers) { { 'Content-Type' => 'application/vnd.api+json; charset=utf-8;' } } | ||
|
||
it { expect(headers['Content-Type']).to eq 'application/vnd.api+json; charset=utf-8; api_version=4' } | ||
end | ||
|
||
context 'when there is no content type' do | ||
let(:headers) { {} } | ||
|
||
it { expect(headers.empty?).to eq true } | ||
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,18 @@ | ||
require 'spec_helper' | ||
|
||
describe VersionCake::ResponseStrategy::HttpHeaderStrategy do | ||
describe '#execute' do | ||
let(:headers) { { } } | ||
let(:context) { double('content', version: 8) } | ||
before do | ||
VersionCake::ResponseStrategy::HttpHeaderStrategy.new.execute( | ||
context, nil, headers, nil | ||
) | ||
end | ||
|
||
it do | ||
expect(headers.keys).to include 'api-version' | ||
expect(headers['api-version']).to eq '8' | ||
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,30 @@ | ||
require 'spec_helper' | ||
|
||
describe VersionCake::VersionedResponseService do | ||
|
||
let(:config) { double(response_strategies: [VersionCake::ResponseStrategy::HttpHeaderStrategy.new]) } | ||
let(:service) { described_class.new(config)} | ||
|
||
describe '#inject_version' do | ||
let(:context) { double('content', version: 2) } | ||
let(:headers) { { } } | ||
|
||
before { service.inject_version(context, nil, headers, nil) } | ||
|
||
it { expect(headers['api-version']).to eq '2' } | ||
|
||
context 'when configured with multiple response strategies' do | ||
let(:headers) { { 'Content-Type' => 'application/vnd.api+json; charset=utf-8;' } } | ||
|
||
let(:config) do | ||
double(response_strategies: [ | ||
VersionCake::ResponseStrategy::HttpHeaderStrategy.new, | ||
VersionCake::ResponseStrategy::HttpContentTypeStrategy.new | ||
]) | ||
end | ||
|
||
it { expect(headers['Content-Type']).to eq 'application/vnd.api+json; charset=utf-8; api_version=2' } | ||
it { expect(headers['api-version']).to eq '2' } | ||
end | ||
end | ||
end |