Skip to content

Commit cba257e

Browse files
committed
Added consideration of vendors when using header based version selection
class MyAPI < Grape::API vendor 'v1', :using => :header, :vendor => 'twitter', :format => :json get '/' do 'hello world' end end and let it still return the '/' even when the accept-header "application/vnd.some_other_vendor-v1+json" was given. This commit will let it return a 404, and sets the X-Cascade pass as well. I've added this because although the version is right, part of the Accept header (the vendor part) is not correct.
1 parent 4332cb0 commit cba257e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/grape/middleware/versioner/header.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def before
3535
env['api.subtype'] = subtype
3636

3737
subtype.scan(/vnd\.(.+)?-(.+)?\+(.*)?/) do |vendor, version, format|
38-
if options[:versions] && !options[:versions].include?(version)
38+
is_vendored = options[:version_options] && options[:version_options][:vendor]
39+
correctly_vendored = is_vendored ? options[:version_options][:vendor] == vendor : true
40+
41+
if (options[:versions] && !options[:versions].include?(version)) || !correctly_vendored
3942
throw :error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found"
4043
end
4144

spec/grape/middleware/versioner/header_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@
8080
end
8181
end
8282

83+
context 'vendors' do
84+
before do
85+
@options = {
86+
:version => ['v1'],
87+
:version_options => {:using => :header, :vendor => 'vendor'}
88+
}
89+
end
90+
91+
it 'should match with correct vendor' do
92+
status = subject.call('HTTP_ACCEPT' => accept).first
93+
status.should == 200
94+
end
95+
96+
it 'should not match with an incorrect vendor' do
97+
expect {
98+
env = subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor-v1+json').last
99+
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
100+
end
101+
end
102+
83103
context 'no matched version' do
84104
before do
85105
@options = {

0 commit comments

Comments
 (0)