Skip to content

Commit

Permalink
Fix #328: Version can now be specified as both String and Symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Feb 9, 2013
1 parent f6f585e commit 354439a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release
* A formatter that raises an exception will cause the API to return a 500 error - [@dblock](http://github.com/dblock).
* [#131](https://github.com/intridea/grape/issues/131): Added instructions for Grape API reloading in Rails - [@jyn](http://github.com/jyn), [@dblock](http://github.com/dblock).
* [#317](https://github.com/intridea/grape/issues/317): Added `headers` that returns a hash of parsed HTTP request headers - [@dblock](http://github.com/dblock).
* [#328](https://github.com/intridea/grape/issues/328): Version can now be specified as both String and Symbol - [@dblock](http://github.com/dblock).
* Your contribution here.

0.2.6 (01/11/2013)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/versioner/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def before
potential_version = request.params[paramkey]

unless potential_version.nil?
if options[:versions] && !options[:versions].include?(potential_version)
if options[:versions] && ! options[:versions].find { |v| v.to_s == potential_version }
throw :error, :status => 404, :message => "404 API Version Not Found", :headers => {'X-Cascade' => 'pass'}
end
env['api.version'] = potential_version
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/versioner/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def before
pieces = path.split('/')
potential_version = pieces[1]
if potential_version =~ options[:pattern]
if options[:versions] && !options[:versions].include?(potential_version)
if options[:versions] && ! options[:versions].find { |v| v.to_s == potential_version }
throw :error, :status => 404, :message => "404 API Version Not Found"
end

Expand Down
14 changes: 8 additions & 6 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,15 @@ def memoized
get '/url'
last_response.body.should == "http://example.org/url"
end
it 'should include version' do
subject.version 'v1', :using => :path
subject.get('/url') do
request.url
[ 'v1', :v1 ].each do |version|
it 'should include version #{version}' do
subject.version version, :using => :path
subject.get('/url') do
request.url
end
get "/#{version}/url"
last_response.body.should == "http://example.org/#{version}/url"
end
get '/v1/url'
last_response.body.should == "http://example.org/v1/url"
end
it 'should include prefix' do
subject.version 'v1', :using => :path
Expand Down
32 changes: 17 additions & 15 deletions spec/grape/middleware/versioner/header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@
status.should == 200
end

context 'when version is set' do
before do
@options[:versions] = ['v1']
end

it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
env['api.format'].should eql 'json'
status.should == 200
end

it 'is nil if not provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
env['api.format'].should eql nil
status.should == 200
[ 'v1', :v1 ].each do |version|
context 'when version is set to #{version{' do
before do
@options[:versions] = [ version ]
end

it 'is set' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
env['api.format'].should eql 'json'
status.should == 200
end

it 'is nil if not provided' do
status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
env['api.format'].should eql nil
status.should == 200
end
end
end
end
Expand Down
20 changes: 12 additions & 8 deletions spec/grape/middleware/versioner/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

context 'with a pattern' do
before{ @options = {:pattern => /v./i} }
before { @options = {:pattern => /v./i} }
it 'sets the version if it matches' do
subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
end
Expand All @@ -27,14 +27,18 @@
end
end

context 'with specified versions' do
before{ @options = {:versions => ['v1', 'v2']}}
it 'throws an error if a non-allowed version is specified' do
catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404
end
[ [ 'v1', 'v2'], [ :v1, :v2 ], [ :v1, 'v2' ], [ 'v1', :v2 ] ].each do |versions|
context 'with specified versions as #{versions}' do
before { @options = { :versions => versions } }

it 'throws an error if a non-allowed version is specified' do
catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404
end

it 'allows versions that have been specified' do
subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1'
it 'allows versions that have been specified' do
subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1'
end
end
end

end

0 comments on commit 354439a

Please sign in to comment.