1+ require 'spec_helper'
2+
3+ describe Grape ::Middleware ::Versioner ::Param do
4+
5+ let ( :app ) { lambda { |env | [ 200 , env , env [ 'api.version' ] ] } }
6+ subject { Grape ::Middleware ::Versioner ::Param . new ( app , @options || { } ) }
7+
8+ it 'should set the API version based on the default param (apiver)' do
9+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "apiver" => "v1" } } )
10+ subject . call ( env ) [ 1 ] [ "api.version" ] . should == 'v1'
11+ end
12+
13+ it 'should cut (only) the version out of the params' , :focus => true do
14+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "apiver" => "v1" , "other_param" => "5" } } )
15+ subject . call ( env ) [ 1 ] [ 'rack.request.query_hash' ] [ "apiver" ] . should be_nil
16+ subject . call ( env ) [ 1 ] [ 'rack.request.query_hash' ] [ "other_param" ] . should == "5"
17+ end
18+
19+ it 'should provide a nil version if no version is given' do
20+ env = Rack ::MockRequest . env_for ( "/" )
21+ subject . call ( env ) . last . should be_nil
22+ end
23+
24+ context 'with specified parameter name' do
25+ before { @options = { :parameter => [ 'v' ] } }
26+ it 'should set the API version based on the custom parameter name' do
27+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "v" => "v1" } } )
28+ s = subject . call ( env ) [ 1 ] [ "api.version" ] == "v1"
29+ end
30+ it 'should not set the API version based on the default param' do
31+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "apiver" => "v1" } } )
32+ s = subject . call ( env ) [ 1 ] [ "api.version" ] == nil
33+ end
34+ end
35+
36+ context 'with specified versions' do
37+ before { @options = { :versions => [ 'v1' , 'v2' ] } }
38+ it 'should throw an error if a non-allowed version is specified' do
39+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "apiver" => "v3" } } )
40+ catch ( :error ) { subject . call ( env ) } [ :status ] . should == 404
41+ end
42+
43+ it 'should allow versions that have been specified' do
44+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { "apiver" => "v1" } } )
45+ subject . call ( env ) [ 1 ] [ "api.version" ] . should == 'v1'
46+ end
47+ end
48+
49+ it 'should return a 200 when no version is set (matches the first version found)' do
50+ @options = {
51+ :versions => [ 'v1' ] ,
52+ :version_options => { :using => :header }
53+ }
54+ env = Rack ::MockRequest . env_for ( "/awesome" , { :params => { } } )
55+ subject . call ( env ) . first . should == 200
56+ end
57+
58+ end
0 commit comments