spec/grape/middleware/versioner/param_spec.rb in grape-0.14.0 vs spec/grape/middleware/versioner/param_spec.rb in grape-0.15.0
- old
+ new
@@ -20,11 +20,11 @@
env = Rack::MockRequest.env_for('/')
expect(subject.call(env).last).to be_nil
end
context 'with specified parameter name' do
- before { @options = { parameter: 'v' } }
+ before { @options = { version_options: { parameter: 'v' } } }
it 'sets the API version based on the custom parameter name' do
env = Rack::MockRequest.env_for('/awesome', params: { 'v' => 'v1' })
expect(subject.call(env)[1]['api.version']).to eq('v1')
end
it 'does not set the API version based on the default param' do
@@ -50,7 +50,111 @@
versions: ['v1'],
version_options: { using: :header }
}
env = Rack::MockRequest.env_for('/awesome', params: {})
expect(subject.call(env).first).to eq(200)
+ end
+
+ context 'when there are multiple versions without a custom param' do
+ subject { Class.new(Grape::API) }
+
+ let(:v1_app) do
+ Class.new(Grape::API) do
+ version 'v1', using: :param
+ content_type :v1_test, 'application/vnd.test.a-cool_resource-v1+json'
+ formatter :v1_test, ->(object, _) { object }
+ format :v1_test
+
+ resources :users do
+ get :hello do
+ 'one'
+ end
+ end
+ end
+ end
+
+ let(:v2_app) do
+ Class.new(Grape::API) do
+ version 'v2', using: :param
+ content_type :v2_test, 'application/vnd.test.a-cool_resource-v2+json'
+ formatter :v2_test, ->(object, _) { object }
+ format :v2_test
+
+ resources :users do
+ get :hello do
+ 'two'
+ end
+ end
+ end
+ end
+
+ def app
+ subject.mount v2_app
+ subject.mount v1_app
+ subject
+ end
+
+ it 'responds correctly to a v1 request' do
+ versioned_get '/users/hello', 'v1', using: :param, parameter: :apiver
+ expect(last_response.body).to eq('one')
+ expect(last_response.body).not_to include('API vendor or version not found')
+ end
+
+ it 'responds correctly to a v2 request' do
+ versioned_get '/users/hello', 'v2', using: :param, parameter: :apiver
+ expect(last_response.body).to eq('two')
+ expect(last_response.body).not_to include('API vendor or version not found')
+ end
+ end
+
+ context 'when there are multiple versions with a custom param' do
+ subject { Class.new(Grape::API) }
+
+ let(:v1_app) do
+ Class.new(Grape::API) do
+ version 'v1', using: :param, parameter: 'v'
+ content_type :v1_test, 'application/vnd.test.a-cool_resource-v1+json'
+ formatter :v1_test, ->(object, _) { object }
+ format :v1_test
+
+ resources :users do
+ get :hello do
+ 'one'
+ end
+ end
+ end
+ end
+
+ let(:v2_app) do
+ Class.new(Grape::API) do
+ version 'v2', using: :param, parameter: 'v'
+ content_type :v2_test, 'application/vnd.test.a-cool_resource-v2+json'
+ formatter :v2_test, ->(object, _) { object }
+ format :v2_test
+
+ resources :users do
+ get :hello do
+ 'two'
+ end
+ end
+ end
+ end
+
+ def app
+ subject.mount v2_app
+ subject.mount v1_app
+ subject
+ end
+
+ it 'responds correctly to a v1 request' do
+ versioned_get '/users/hello', 'v1', using: :param, parameter: 'v'
+ expect(last_response.body).to eq('one')
+ expect(last_response.body).not_to include('API vendor or version not found')
+ end
+
+ it 'responds correctly to a v2 request' do
+ versioned_get '/users/hello', 'v2', using: :param, parameter: 'v'
+ expect(last_response.body).to eq('two')
+ expect(last_response.body).not_to include('API vendor or version not found')
+ end
end
end