1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# frozen_string_literal: true
require 'spec_helper'
describe Grape::Middleware::Versioner::Param do
subject { described_class.new(app, **options) }
let(:app) { ->(env) { [200, env, env['api.version']] } }
let(:options) { {} }
it 'sets the API version based on the default param (apiver)' do
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
expect(subject.call(env)[1]['api.version']).to eq('v1')
end
it 'cuts (only) the version out of the params' do
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1', 'other_param' => '5' })
env['rack.request.query_hash'] = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
expect(subject.call(env)[1]['rack.request.query_hash']['apiver']).to be_nil
expect(subject.call(env)[1]['rack.request.query_hash']['other_param']).to eq('5')
end
it 'provides a nil version if no version is given' do
env = Rack::MockRequest.env_for('/')
expect(subject.call(env).last).to be_nil
end
context 'with specified parameter name' do
let(: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
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
expect(subject.call(env)[1]['api.version']).to be_nil
end
end
context 'with specified versions' do
let(:options) { { versions: %w[v1 v2] } }
it 'throws an error if a non-allowed version is specified' do
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v3' })
expect(catch(:error) { subject.call(env) }[:status]).to eq(404)
end
it 'allows versions that have been specified' do
env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
expect(subject.call(env)[1]['api.version']).to eq('v1')
end
end
context 'when no version is set' do
let(:options) do
{
versions: ['v1'],
version_options: { using: :header }
}
end
it 'returns a 200 (matches the first version found)' do
env = Rack::MockRequest.env_for('/awesome', params: {})
expect(subject.call(env).first).to eq(200)
end
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
|